我正在尝试在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()
答案 0 :(得分:1)
代码内的所有评论
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:simple template,您可以在开始新项目时使用它。
答案 1 :(得分:1)
我有类似的问题,修复并不是直截了当的。以下是我对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中的说明进行安装。有人可能需要为它安装自制软件,这是macOS的另一个软件包管理器。可以在https://brew.sh/
找到相关说明4)使用github链接上的说明安装后,你需要更改所有导入pygame以导入pygame_sdl2为pygame
5)瞧!固定......
答案 2 :(得分:0)
您必须在循环外加载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()
您只是忘了更新 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()