[英]Pygame Window Not Responding
我正在尝试在Pygame中制作游戏,他的目标是开始游戏,但是只要您触摸它,开始按钮就会一直移动,但是窗口不会响应。 我还没有完成代码,因为我已经对其进行了测试并且无法正常工作。 到目前为止,这是我的代码:
import pygame
import random
import time
pygame.init()
display = pygame.display.set_mode((800,600))
pygame.display.set_caption('BEST 3D PLATFORMER FPS GAME!')
clock = pygame.time.Clock()
pygame.display.update()
clock.tick(60)
display.fill((255,255,255))
def newposition()
randx = random.randrange(100, 700)
randy = random.randrange(100,500)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pass
if event.ty
button = pygame.image.load('start.png')
display.blit(button,(randx,randy))
pygame.quit()
quit()
代码内的所有注释
import pygame
import random
# --- constants --- (UPPER_CASE names)
WHITE = (255, 255, 255) # space after every `,`
FPS = 30
# --- classes --- (CamelCase names)
#empty
# --- functions --- (lower_case names)
def new_position():
x = random.randrange(100, 700) # space after every `,`
y = random.randrange(100, 500) # space after every `,`
return x, y # you have to return value
# --- main --- (lower_case names)
# - init -
pygame.init()
display = pygame.display.set_mode((800, 600)) # space after every `,`
pygame.display.set_caption('BEST 3D PLATFORMER FPS GAME!')
# - objects -
# load only once - don't waste time to load million times in loop
button = pygame.image.load('start.png').convert_alpha()
button_rect = button.get_rect() # button size and position
button_rect.topleft = new_position() # set start position
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
#pass # it does nothing so you can't exit
running = False # to exit `while running:`
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False # to exit `while running:`
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left button
button_rect.topleft = new_position() # set new position
# if event.ty # Syntax Error
# - updates (without draws) -
#empty
# - draws (without updates) -
# you have to use it inside loop
display.fill(WHITE) # clear screeen before you draw elements in new place
display.blit(button, button_rect)
# you have to use it inside loop
pygame.display.update() # you have to send buffer to monitor
# - FPS -
# you have to use it inside loop
clock.tick(FPS)
# - end -
pygame.quit()
BTW:启动新项目时可以使用的简单模板 。
我有一个类似的问题,解决方法不直接。 这是我对python 3.6.1和Pygame 1.9.3的注释:
1)事件没有响应,因为没有为pygame生成任何显示,请在pygame初始化后添加一个窗口显示:
pygame.init() # Initializes pygame
pygame.display.set_mode((500, 500)) # <- add this line. It generates a window of 500 width and 500 height
2) pygame.event.get()
是生成事件列表的工具,但并非所有事件类都具有.key方法,例如鼠标移动。 因此,更改所有.key事件处理代码,例如
if event.key == pygame.K_q:
stop = True
至
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
stop = True
3)在某些mac / python组合中,即使在生成窗口后也不会集中/记录键盘事件,这是pygame的问题,并已在使用sdl2的pygame重新实现中得到解决(SDL是跨平台开发库,它提供了低级访问键盘,音频,鼠标等)。 可以按照https://github.com/renpy/pygame_sdl2上的说明进行安装。 可能需要为此安装homebrew,这是macOS的另一个软件包管理器。 可以在这里找到说明https://brew.sh/
4)使用github链接上的说明进行安装后,您需要将所有import pygame更改为将pygame_sdl2导入为pygame
5)瞧! 固定...
您必须在循环外加载button
(只需完成一次。)
button = pygame.image.load('start.png')
另外,您已经定义了 newposition()
,但尚未调用它。 同样,randx和randy将无法从函数外部访问,因为它们是本地的。
因此,将功能更改为:
def newposition()
randx = random.randrange(100, 700)
randy = random.randrange(100,500)
return randx, randy # Output the generated values
然后,在循环之前:
rand_coords = newposition()
the pygame display with your modifications to it. 您只是忘了用您的修改来 pygame显示。
在循环的最后,添加pygame.display.update()
,如下所示:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pass
# if event.ty why is that here?
display.blit(button,(rand_coords[0],rand_coords[1])) # Take our generated values
pygame.display.update()
最终代码:
import pygame
import random
import time
pygame.init()
display = pygame.display.set_mode((800,600))
pygame.display.set_caption('BEST 3D PLATFORMER FPS GAME!') # Yeah, exactly :)
clock = pygame.time.Clock()
display.fill((255,255,255))
def newposition()
randx = random.randrange(100, 700)
randy = random.randrange(100,500)
return randx, randy # Output the generated values
rand_coords = newposition() # Get rand coords
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Quit pygame if window in closed
# if event.ty why is that here?
clock.tick(60) # This needs to be in the loop
display.fill((255,255,255)) # You need to refill the screen with white every frame.
display.blit(button,(rand_coords[0],rand_coords[1])) # Take our generated values
pygame.display.update()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.