Python Pygame窗口没有响应
我试图在Pygame中制作一个游戏,他的目标是开始游戏,但无论何时触摸开始按钮,它都会一直移动,但窗口不会响应。我还没有完成代码,因为我测试了它,但它不工作。以下是我目前的代码:Python Pygame窗口没有响应,python,random,pygame,Python,Random,Pygame,我试图在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!') cl
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()
顺便说一句:你可以在开始新项目时使用它
代码中的所有注释
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()
顺便说一句:你可以在开始新项目时使用它
您必须将
按钮加载到循环外部(只需加载一次)
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()
你只是忘记了用你的修改来更新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()
您必须在循环外部加载按钮
(只需执行一次)
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()
你只是忘记了用你的修改来更新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()
我有一个类似的问题,解决方法并不直接。以下是我对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
到
3) 在某些mac/python组合中,即使在生成窗口后,它也不会聚焦/记录键盘事件,这是pygame的问题,并在使用sdl2重新实现pygame时得到修复(SDL是一个跨平台开发库,提供对键盘、音频、鼠标等的低级访问)。可以按照此处的说明进行安装。您可能需要为它安装homebrew,这是macOS的另一个软件包管理器。说明可以在这里找到
4) 使用github链接上的说明安装后,需要将所有import pygame更改为将pygame_sdl2作为pygame导入
5) 瞧!固定的 我也遇到过类似的问题,解决方法并不简单。以下是我对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
到
3) 在某些mac/python组合中,即使在生成窗口后,它也不会聚焦/记录键盘事件,这是pygame的问题,并在使用sdl2重新实现pygame时得到修复(SDL是一个跨平台开发库,提供对键盘、音频、鼠标等的低级访问)。可以按照此处的说明进行安装。您可能需要为它安装homebrew,这是macOS的另一个软件包管理器。说明可以在这里找到
4) 使用github链接上的说明安装后,需要将所有import pygame更改为将pygame_sdl2作为pygame导入
5) 瞧!固定的 请问一个问题。另外,最好使用与之配套的工作代码。您有一个未正确完成的if语句。另外,你是无限循环,说,而不等待或不停地说,这只会使游戏崩溃。找到一些教程,你会比在sop上提问更快。请问一个问题。另外,最好使用与之配套的工作代码。您有一个未正确完成的if语句。此外,你是无限循环,说,而真正的,没有等待或什么东西来阻止它只是要崩溃的游戏。找到一些教程,你会学到比问的更快,所以这是非常好的和精心设计的,所有的,但他只要求修复他的代码,而不是你为他写一个新的程序。这是非常好和慷慨的,我同意,但你不能学习,如果一切都是为你服务的(即使一切都是很好的评论,就像你所做的)。很抱歉批评。@C.\uOP可以学习如何创建好的代码-因为OP代码很难看。参见PEP8。这是非常好的、精心设计的,但他只是要求修改他的代码,而不是让你为他编写一个新程序。这是非常好和慷慨的,我同意,但你不能学习,如果一切都是为你服务的(即使一切都是很好的评论,就像你所做的)。很抱歉批评。@C.\uOP可以学习如何创建好的代码-因为OP代码很难看。见第8页。