我目前正在使用 Pygame 在 Python 3.6 中编写游戏。我的游戏有一个开始菜单,将鼠标悬停在上面时会突出显示一个按钮。单击按钮时,开始菜单消失,游戏开始。唯一的问题是,开始按钮仍然存在...我尝试使用boolean
值和if
语句使其在菜单消失时消失,但无济于事。
更糟糕的是,允许关闭窗口的pygame
代码不起作用(通过注释突出显示(。谁能帮我解决这些小问题?它们看起来微不足道,但我是pygame
新手,似乎在任何地方都找不到解决方法。
您将需要以下图片来运行该程序:
这是女孩的照片。
这是背景照片。
代码:
#Imports
import sys
import pygame
pygame.init() #Initialise Pygame
#RGB colours
GREY = (128,128,128)
WHITE = (255,255,255)
BLACK = (0,0,0)
#fonts
title_font = pygame.font.Font('freesansbold.ttf',72)
menu_font = pygame.font.Font('freesansbold.ttf',32)
#images
girl_img = pygame.image.load('emily.png')
background_img = pygame.image.load('tech_lab.png')
class Game: #Game Class
def __init__(self): #Constructor
self.size = width, height = (1000,563)
self.screen = pygame.display.set_mode(self.size)
def menu_screen(self): #Menu Screen Function
intro = True
while intro:
## for event in pygame.event.get(): #To close window
## print(event)
## if event.type == pygame.QUIT:
## pygame.quit()
## sys.exit()
self.screen.fill(GREY) #Set background colour
self.screen.blit(title_font.render('EXAMPLE', True, BLACK), (330,100)) #Display game title
start_button = pygame.draw.rect(self.screen, (BLACK), pygame.Rect(410,310,180,55)) #Display start button rect
self.screen.blit(menu_font.render('Play', True, GREY), (425,310)) #Display start button text
pygame.display.flip() #Update display
while True:
pygame.event.get() #Get pygame events
click_pos = pygame.mouse.get_pos() #Get mouse position
if 410+180 > click_pos[0] > 410 and 310+55 > click_pos[1] > 310: #If mouse position within start button rect...
pygame.draw.rect(self.screen, (WHITE), pygame.Rect(410,310,180,55)) #then change colour of start button rect
if (pygame.mouse.get_pressed())[0] == 1: #If start button rect clicked...
start_game(self) #Start main game
else:
pygame.draw.rect(self.screen, (BLACK), pygame.Rect(410,310,180,55)) #Otherwise start button rect colour is black
self.screen.blit(menu_font.render('Play', True, GREY), (425,310))#Display start button text
pygame.display.flip() #Update display
def start_game(game): #Main game function
game.screen.blit(background_img, [0,0]) #Display background image
game.screen.blit(girl_img, [80,25]) #Display image
pygame.display.flip() #Update display
def main():
game = Game() #Instantiate class 'game'
game.menu_screen() #Call menu screen function
main()
循环中的这个循环只会导致问题,并且必须去:
while intro:
....
while True
....
下面是一些伪代码:
Class Game:
def menu_screen():
displaying = True
while displaying:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# evaluate mouse click
if clicked button
displaying = False
self.game()
# drawing code for your images
pygame.display.flip()
def game():
game_running = True
while game_running:
# game running logic and drawing
def main():
# get the Game setup and call main menu
基本上,让你的游戏类控制一切。 它将具有显示菜单的菜单功能。然后他们单击该按钮,它将转到实际游戏的游戏类中的不同函数。
开始按钮仍然存在,因为您在进入 start_game(( 函数时从未清除屏幕。至于关闭窗口按钮不起作用的原因,这是因为我上面提到的循环中的循环。你被困在内心
while True:
循环,您永远无法从上面返回事件检查。(quit 代码本身很好,尽管你不需要 pygame.quit((,因为你完全使用 sys.ext(( 退出程序(
除了完全重新设计代码之外,这可以让您开始走上正确的轨道。