用户:
胡*瑞查看:0 回复:0 评论:0 创建时间:2024-06-01T21:31:36
【作品展示】

【作品介绍】
wasd操作,空格交互
【作品源代码】
import pgzrun
import random
WIDTH, HEIGHT = 1100, 750
# ---------------初始化设置区----------------
# 背景图片列表,图片格式gif、png均支持,建议尺寸至少是 1100x750
my_bgs = ['my_bg01.gif','my_bg02.gif','my_bg03.gif',]
# 玩家图片列表,图片格式gif、png均支持,建议尺寸不超过 100x100
players= ['主角2.2.png']
# 游戏角色小图片列表,主界面展示,图片格式gif、png均支持,建议尺寸不超过 100x100
npcs = ['npc.png']
# 游戏角色大图片列表,对话框时展示,图片格式gif、png均支持,建议尺寸不超过 200x200
npcs_foc = ['npc_foc.png']
# 背景音乐列表,自己的音乐要放在music文件夹中,格式为mp3
#musics = ['音乐01.mp3','音乐02.mp3','音乐03.mp3','音乐04.mp3','音乐05.mp3',]
# 想自定义更改成自己的图片,则更改对应的上述列表的索引值,取到对应的图片
#music.play(musics[0])
bg = Actor(my_bgs[0], (WIDTH / 2, HEIGHT / 2))
player = Actor(players[0], (WIDTH / 2, HEIGHT / 2))
npc = Actor(npcs[0], (WIDTH * 3 / 7, HEIGHT * 3 / 4))
npc_foc = Actor(npcs_foc[0], (100, HEIGHT* 9 / 10 ))
# -------------------下面代码不建议更改------------------------
cave01 = Actor('地点01.png', (750, 600))
cave02 = Actor('地点02.png', (250, 650))
# 创建菜单栏相关元素
menu_rect = Rect((WIDTH / 2, HEIGHT / 2), (100, 200)) # 菜单位置和大小
menu_item1_rect = Rect((420, HEIGHT / 2 ), (360, 40)) # 选项1位置和大小
menu_item2_rect = Rect((420, HEIGHT / 2 + 60), (360, 40)) # 选项2位置和大小
menu_item3_rect = Rect((420, HEIGHT / 2 + 120), (360, 40)) # 选项2位置和大小
focus = '' # 焦点
# 创建对话栏相关元素
chat_box = Actor('对话框.png', (WIDTH / 2, HEIGHT* 9 / 10 ))
npc.text = ''
# 创建烤火相关元素
fire01 = Actor('地点03.png', (50, 450))
fire02 = Actor('地点04.png', (1000, 500))
blocks = [cave01,cave02, npc,fire01,fire02]
# 计数
score = 0
index01 = 0
# ----------------动作函数区--------------------
# 处理自定义的NPC动作
def chat():
npc.text01 = '你好,欢迎来到火星,请探索每一个拓荒点,一起把火星打造成宜居的星球吧!!!'
def make_fire01():
npc.text02 = '我们缺乏空气和水源,请你种上树木,寻找湖泊吧......'
def chat_done():
npc.text03 = '恭喜你,火星已探索完毕,现在已适合人类居住!' if score == 4 else '火星拓荒未完成,请继续探索剩余地点!'
# ----------------事件函数区--------------------
# 处理键盘、鼠标等事件
def on_key_down(key):
global focus,score
if keyboard.space and player.distance_to(npc) < 100:
if focus == '':
focus = 'menu'
elif focus == 'menu':
focus = ''
if keyboard.space and player.distance_to(cave01)< 100 and cave01.image != '树木01.png':
cave01.image = '树木01.png'
score +=1
if keyboard.space and player.distance_to(cave02)< 100 and cave02.image != '树木01.png':
cave02.image = '树木01.png'
score +=1
if keyboard.space and player.distance_to(fire01)< 100 and fire01.image != '湖泊.png':
fire01.image = '湖泊.png'
score +=1
if keyboard.space and player.distance_to(fire02) < 100 and fire02.image != '山川.png':
fire02.image = '山川.png'
score +=1
def on_mouse_down(pos):
global focus
if focus == 'menu':
if menu_item1_rect.collidepoint(pos):
focus = 'chat01'
chat()
elif menu_item2_rect.collidepoint(pos):
focus = 'chat02'
make_fire01()
elif menu_item3_rect.collidepoint(pos):
focus = 'chat_done'
chat_done()
elif focus == 'chat01':
if chat_box.collidepoint(pos):
focus = ''
elif focus == 'chat02':
if chat_box.collidepoint(pos):
focus = ''
elif focus == 'chat_done':
if chat_box.collidepoint(pos):
focus = ''
# -----------------主循环区--------------------
# 处理游戏运行过程中绘制和更新等
def display_menu():
# 绘制一个矩形作为菜单背景
screen.draw.filled_rect(menu_rect, 'cyan') # 菜单栏可替换成图片
screen.draw.text('闲聊', midbottom=menu_item1_rect.midbottom, fontname='simhei.ttf', color='white')
screen.draw.text('拓荒', midbottom=menu_item2_rect.midbottom, fontname='simhei.ttf', color='white')
screen.draw.text('定居', midbottom=menu_item3_rect.midbottom, fontname='simhei.ttf', color='white')
def display_chat():
# 显示对话栏相关元素
global index01
chat_box.draw()
npc_foc.draw()
if focus == 'chat01':
screen.draw.text(npc.text01 , (180, HEIGHT* 9 / 10), fontname='simhei.ttf', color='white')
index01 = 1
elif focus == 'chat02':
screen.draw.text(npc.text02 , (180, HEIGHT* 9 / 10), fontname='simhei.ttf', color='white')
elif focus == 'chat_done':
screen.draw.text(npc.text03 , (180, HEIGHT* 9 / 10), fontname='simhei.ttf', color='white')
def draw():
screen.clear()
if score != 4 :
bg.draw()
if index01 != 0 :
screen.draw.text('拓荒点:' + str(score), color="yellow", center=(WIDTH/2, 50), fontsize=70, fontname='simhei.ttf',
shadow=(0.5, 0.5), scolor='pink')
else:
screen.draw.text('探索者,欢迎你来到火星......', color="red", center=(WIDTH/2, 50), fontsize=70, fontname='simhei.ttf',
喵=(0.5, 0.5), scolor='yellow')
if score == 4:
bg.image = '宜居02.png'
bg.draw()
screen.draw.text('火星已探索完毕!!', color="green", center=(WIDTH/2, 50), fontsize=70, fontname='simhei.ttf',
喵=(0.6, 0.6), scolor='cyan')
screen.draw.text('你成功为人类寻找到了新家园', color="green", center=(WIDTH/2, 150), fontsize=70, fontname='simhei.ttf',
喵=(0.6, 0.6), scolor='yellow')
if index01 != 0 :
cave01.draw()
cave02.draw()
fire01.draw()
fire02.draw()
player.draw()
npc.draw()
if focus == 'menu':
display_menu()
elif focus == 'chat01' or focus == 'chat02' or focus == 'chat_done' :
display_chat()
def update():
# 在这里添加角色的移动代码
prev_player_pos = player.pos
if keyboard.a:
player.x -= 4
if keyboard.d:
player.x += 4
if keyboard.w:
player.y -= 4
if keyboard.s:
player.y += 4
# 碰撞检测
for block in blocks:
if player.colliderect(block):
player.pos = prev_player_pos
pgzrun.go()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn