【问题标题】:Cannot start game in pygame in into screen无法在pygame中启动游戏进入屏幕
【发布时间】:2017-07-23 00:10:39
【问题描述】:

在我的简单进入程序中,我有一个开始按钮并检查事件,如果单击开始,我希望开始文本更改颜色,然后我希望将其更改为 false 以便游戏开始。这永远不会发生,并且 pygame 一直卡在屏幕上而没有停止。我希望 while 循环开始,然后它会转到另一个 while 循环。如何检查是否点击了开始,更改文本颜色,然后在以下代码中结束while循环?

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 60  # frames per second setting
clock = pygame.time.Clock()

screen = pygame.display.set_mode((700, 800))

window_width = 540
window_height = 800

#Title screen fonts
fontObj2 = pygame.font.Font('freesansbold.ttf', 60)
start = fontObj2.render("Start", True,(255,255,255))

into=True

while into==True:
    screen.blit(start, (window_width / 2, 600))

    for event in pygame.event.get(): #Closes game
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif start.get_rect().collidepoint(pygame.mouse.get_pos()):
            x, y = event.pos
            if start.get_rect().collidepoint(x, y):
                start = fontObj2.render("Start", True, (192,192,192))
                into=False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            if start.get_rect().collidepoint(x, y):
                start = fontObj2.render("Start", True, (192,192,192))
                into = False
    pygame.display.flip()
    pygame.display.update()
    clock.tick(FPS)

【问题讨论】:

    标签: pygame python-3.5


    【解决方案1】:

    首先你可以去掉start.get_rect().collidepoint(pygame.mouse.get_pos()),因为你已经在event.type == pygame.MOUSEBUTTONDOWN中定义了它。

    您的代码无法按您的意愿运行,因为start.get_rect() 的位置不在屏幕上的位置。如果你打印它,你会看到<rect(0, 0, 140, 61)>,它是左上角的一个矩形。

    要解决此问题,您只需将其更改为start.get_rect(x=window_width / 2, y=600) #where you blit the font 即可设置矩形的位置。

    所以要避免:

    import pygame, sys
    from pygame.locals import *
    
    pygame.init()
    
    FPS = 60 
    clock = pygame.time.Clock()
    
    screen = pygame.display.set_mode((700, 800))
    
    window_width = 540
    window_height = 800
    
    fontObj2 = pygame.font.Font('freesansbold.ttf', 60)
    start = fontObj2.render("Start", True,(255,255,255))
    
    into=True
    
    while into: #Equivalent to into == True
        screen.blit(start, (window_width / 2, 600))
    
        for event in pygame.event.get(): 
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            #got rid of another if statement
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if start.get_rect(x=window_width / 2, y=600).collidepoint(x, y): #changed start.get_rect
                    start = fontObj2.render("Start", True, (192,192,192))
                    into = False
    
        #same as before
        pygame.display.flip()
        pygame.display.update()
        clock.tick(FPS)
    

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多