【问题标题】:Pygame Window Not RespondingPygame 窗口没有响应
【发布时间】:2017-01-16 18:27:48
【问题描述】:

我正在尝试在 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()

【问题讨论】:

  • 请提问。此外,与其一起使用的工作代码会更好。
  • 您有一个未正确完成的 if 语句。此外,你是无限循环,说while True 没有等待或停止它只会让游戏崩溃。
  • 找一些教程,你会比在 SO 上提问学得更快

标签: python random pygame


【解决方案1】:

代码内的所有cmets

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()

顺便说一句:simple template 可以在您开始新项目时使用。

PEP 8 -- Style Guide for Python Code

【讨论】:

  • 这很好,很详细,但他只要求修复他的代码,而不是让你为他写一个新程序。我同意,它非常好和慷慨,但你无法知道是否所有东西都为你服务(即使所有东西都得到了很好的评论,就像你所做的那样)。很抱歉批评。
  • @C._ OP 可以学习如何创建好的代码——因为 OP 代码很难看。参见 PEP8。
【解决方案2】:

我遇到了类似的问题,解决方法不是直截了当的。以下是我对 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 更改为 import pygame_sdl2 as pygame

5) 瞧!固定...

【讨论】:

    【解决方案3】:

    您必须在循环外加载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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2022-12-12
      相关资源
      最近更新 更多