[英]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.