我运行了你的代码但是所有的图片同时出现了。我希望在运行后首先只出现一个背景并且在背景上有三个按钮——‘开始游戏’、‘继续游戏’、‘CG画廊’。当我点击开始游戏后进入一个新画面并显示角色和对话框点击对话框可以显示下一句话并且在右上角有保存和回到主界面的按钮当点击保存后游戏所处进度应该被保存并且在主界面点解继续游戏可以回到被保存的进度继续。当点击回到主界面后应当回到最开始的背景和三个按钮的界面
好的,这个需要进行一些修改和添加,以下是修改后的代码:
import pygame
import json
pygame.init()
# 设置窗口
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('My Game')
# 加载图片
background_img = pygame.image.load('background.jpg').convert()
character_img = pygame.image.load('character.png').convert_alpha()
dialogue_box_img = pygame.image.load('dialogue_box.png').convert_alpha()
save_button_img = pygame.image.load('save_button.png').convert_alpha()
load_button_img = pygame.image.load('load_button.png').convert_alpha()
# 设置字体
pygame.font.init()
font = pygame.font.SysFont('SimHei', 24)
# 设置状态
game_state = 'menu'
# 设置对话
dialogues = [
{'speaker': '小明', 'content': '欢迎来到游戏世界!'},
{'speaker': '小红', 'content': '这个游戏很好玩!'},
{'speaker': '小刚', 'content': '来试试吧!'}
]
dialogue_index = 0
# 设置进度
progress = {
'dialogue_index': 0
}
# 加载进度
try:
with open('progress.json', 'r') as f:
progress = json.load(f)
except FileNotFoundError:
pass
# 保存进度
def save_progress():
with open('progress.json', 'w') as f:
json.dump(progress, f)
# 绘制菜单界面
def draw_menu():
screen.blit(background_img, (0, 0))
start_game_button_rect = pygame.Rect(300, 200, 200, 50)
pygame.draw.rect(screen, (255, 255, 255), start_game_button_rect)
start_game_button_text = font.render('开始游戏', True, (0, 0, 0))
screen.blit(start_game_button_text, (320, 210))
continue_game_button_rect = pygame.Rect(300, 270, 200, 50)
pygame.draw.rect(screen, (255, 255, 255), continue_game_button_rect)
continue_game_button_text = font.render('继续游戏', True, (0, 0, 0))
screen.blit(continue_game_button_text, (320, 280))
cg_gallery_button_rect = pygame.Rect(300, 340, 200, 50)
pygame.draw.rect(screen, (255, 255, 255), cg_gallery_button_rect)
cg_gallery_button_text = font.render('CG画廊', True, (0, 0, 0))
screen.blit(cg_gallery_button_text, (330, 350))
# 绘制对话框
def draw_dialogue():
screen.blit(background_img, (0, 0))
screen.blit(character_img, (50, 50))
screen.blit(dialogue_box_img, (150, 350))
speaker_text = font.render(dialogues[dialogue_index]['speaker'], True, (255, 255, 255))
screen.blit(speaker_text, (180, 370))
content_text = font.render(dialogues[dialogue_index]['content'], True, (255, 255, 255))
screen.blit(content_text, (180, 400))
save_button_rect = pygame.Rect(700, 50, 50, 50)
pygame.draw.rect(screen, (255, 255, 255), save_button_rect)
screen.blit(save_button_img, (700, 50))
load_button_rect = pygame.Rect(700, 120, 50, 50)
pygame.draw.rect(screen, (255, 255, 255), load_button_rect)
screen.blit(load_button_img, (700, 120))
# 处理事件
def handle_event():
global game_state, dialogue_index
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONUP:
if game_state == 'menu':
if start_game_button_rect.collidepoint(event.pos):
game_state = 'game'
dialogue_index = progress['dialogue_index']
elif continue_game_button_rect.collidepoint(event.pos):
game_state = 'game'
dialogue_index = progress['dialogue_index']
elif cg_gallery_button_rect.collidepoint(event.pos):
pass
elif game_state == 'game':
if save_button_rect.collidepoint(event.pos):
progress['dialogue_index'] = dialogue_index
save_progress()
elif load_button_rect.collidepoint(event.pos):
game_state = 'menu'
# 游戏循环
while True:
handle_event()
if game_state == 'menu':
draw_menu()
elif game_state == 'game':
draw_dialogue()
pygame.display.update()
主要的修改和添加如下:
- 添加了
game_state变量来表示游戏所处的状态,初始值为menu。在菜单状态下,显示背景和三个按钮;在游戏状态下,显示角色和对话框。 - 添加了
dialogues列表来表示对话内容。每个对话都是一个字典,包含speaker和content两个字段。 - 添加了
dialogue_index变量来表示当前对话的索引,初始值为 0。 - 添加了
progress字典来保存游戏进度。包含一个dialogue_index字段,表示当前对话的索引。 - 添加了
save_progress函数来保存进度。保存的数据为progress字典,保存在名为progress.json的文件中。 - 添加了
draw_menu函数来绘制菜单界面。绘制的内容包括背景、三个按钮。 - 添加了
draw_dialogue函数来绘制对话框。绘制的内容包括背景、角色、对话框、当前对话的内容、保存和回到主界面的按钮。 - 在处理事件的循环中,根据不同的状态来处理不同的事件。在菜单状态下,处理三个按钮的点击事件;在游戏状态下,处理保存和回到主界面的按钮的点击事件,并更新当前对话的索引。
- 在游戏循环中,根据不同的状态来绘制不同的界面。在菜单状态下,调用
draw_menu函数;在游戏状态下,调用draw_dialogue函数
原文地址: https://www.cveoy.top/t/topic/hpv9 著作权归作者所有。请勿转载和采集!