首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏按钮卡在屏幕上,关闭窗口代码不工作。

游戏按钮卡在屏幕上,关闭窗口代码不工作。
EN

Stack Overflow用户
提问于 2017-03-22 22:40:07
回答 1查看 750关注 0票数 0

我目前正在用Python3.6编写一个游戏,使用Pygame编写。我的游戏有一个开始菜单,有一个按钮,当悬停时突出显示。单击按钮后,“开始”菜单消失,游戏开始。唯一的问题是,开始按钮仍然.我尝试使用boolean值和if语句使其在菜单出现时消失,但没有效果。

更糟糕的是,允许关闭窗口的pygame代码不起作用(注释掉突出显示)。有人能帮我解决这些小问题吗?它们似乎微不足道,但我对pygame并不熟悉,似乎在任何地方都找不到解决办法。

您需要下面的图片来运行该程序:

这是那个女孩的照片。

这是背景照片。

守则:

代码语言:javascript
复制
#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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 01:14:41

循环中的这个循环只会导致问题,必须这样做:

代码语言:javascript
复制
while intro:
    ....
    while True
    ....

下面是一些伪代码:

代码语言:javascript
复制
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()函数时从未清除屏幕,所以仍然保留开始按钮。至于为什么关闭窗口按钮不能工作,这是因为循环中的循环,正如我前面提到的。你被困在内心深处

代码语言:javascript
复制
while True:

循环,您将永远无法从上面返回事件检查。(退出代码本身很好,尽管您不需要pygame.quit(),因为您将完全使用sys.ext()退出程序。)

除非完全重新编写代码,否则这可以使您在正确的轨道上开始工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42963978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档