Python Pygame rect按钮卡在屏幕上,关闭窗口代码不工作
我目前正在使用Pygame在Python 3.6中编写一个游戏。 我的游戏有一个开始菜单,上面有一个按钮,当鼠标悬停在上面时会突出显示。 点击按钮后,“开始”菜单消失,游戏开始。 唯一的问题是,启动按钮仍然存在。。。我曾尝试使用Python Pygame rect按钮卡在屏幕上,关闭窗口代码不工作,python,pygame,window,rect,python-3.6,Python,Pygame,Window,Rect,Python 3.6,我目前正在使用Pygame在Python 3.6中编写一个游戏。 我的游戏有一个开始菜单,上面有一个按钮,当鼠标悬停在上面时会突出显示。 点击按钮后,“开始”菜单消失,游戏开始。 唯一的问题是,启动按钮仍然存在。。。我曾尝试使用boolean值和if语句使其在菜单执行时消失,但没有效果 更糟糕的是,允许关闭窗口的pygame代码不起作用(通过注释退出突出显示)。有人能帮我解决这些小问题吗?它们看起来很琐碎,但我对pygame还不熟悉,我似乎在任何地方都找不到修复方法 您需要以下图片才能运行程序:
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
基本上,让游戏类控制一切。它将具有显示菜单的菜单功能。然后他们点击按钮,它会转到实际游戏的game类中的另一个函数
之所以保留“开始”按钮,是因为您在进入“开始游戏”函数时从未清除过屏幕。至于为什么关闭窗口按钮不起作用,这是因为我上面提到的循环中的循环。你被困在内心深处
while True:
循环,您将永远无法从上面返回到事件检查。(虽然您不需要pygame.quit(),但退出代码本身很好,因为您完全使用sys.ext()退出程序)
除非完全重新编写代码,否则这可以让您走上正轨。谢谢您的回复。我已经将您的伪代码编写成Python代码并进行了尝试,一切都正常;游戏开始,菜单消失,单击时菜单关闭。然而,当我将鼠标悬停在开始按钮上时,它的颜色并没有改变。这是因为移除了while循环吗?当鼠标再次悬停在按钮上时,如何使按钮改变颜色而不破坏我的代码?别担心!我修好了。谢谢你的回答!