gpt4 book ai didi

python - Pygame 需要 "for event in pygame.event.get()"才能不崩溃

转载 作者:太空狗 更新时间:2023-10-30 02:26:39 25 4
gpt4 key购买 nike

程序像这样工作正常,但是,我不明白为什么它需要无用的 for event in pygame.event.get(): None in the gameOver game_loop 中的 while 语句。如果您能找到一种方法来删除它或解释为什么没有它它就不能运行,那就太好了!

import pygame, time, random

pygame.init()

# SOUND/TEXTURES
icon = pygame.image.load("textures\snakeicon.png")
pygame.display.set_icon(icon)

# VARIABLES
white = (255, 255, 255)
black = (0, 0, 0)
red = (200, 0, 0)
green = (0, 155, 0)
bright_green = (0, 250, 0)
bright_red = (255, 0, 0)

font_size = 50
font = pygame.font.SysFont(None, font_size)

# FUNCTIONS

def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()


def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameWindow, ac, (x, y, w, h))
if click[0] == 1 and action != None:
if action == "play":
game_loop()

elif action == "quit":
gameRun = False
gameWindow.fill(white)
message_to_screen("Closing Game...", black, 280, 280)
pygame.display.update()
time.sleep(1)
pygame.quit()
quit()

else:
pygame.draw.rect(gameWindow, ic, (x, y, w, h))

smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
gameWindow.blit(textSurf, textRect)

def snake(rect_x, rect_y, block_size):
pygame.draw.rect(gameWindow, green, [rect_x, rect_y, block_size, block_size])

def message_to_screen(msg, color, x, y):
screen_text = font.render(msg, True, color)
gameWindow.blit(screen_text, [x, y])


# WINDOW/SURFACE
display_w = 800
display_h = 600
window_title = "Window"

gameWindow = pygame.display.set_mode((display_w, display_h))
pygame.display.set_caption(window_title)

# FPS/Clock
clock = pygame.time.Clock()


# Game Loop


def game_loop():
# RECT OPTIONS
moveSpeed = 10
block_size = 10

rect_x = display_w / 2
rect_y = display_h / 2

change_x = 0
change_y = 0

randApplex = round(random.randrange(0, display_w - block_size) / 10.0) * 10.0
randAppley = round(random.randrange(0, display_h - block_size) / 10.0) * 10.0

global gameRun, gameOver
gameRun = True
gameOver = False

while gameRun:

while gameOver:
gameRun = False
gameWindow.fill(white)
# button(msg, x, y, w, h, ic, ac, action=None)
message_to_screen("Game Over!", red, 300, 300)
button("Restart", 150, 450, 100, 50, green, bright_green, "play")
button("Quit", 550, 450, 100, 50, red, bright_red, "quit")
pygame.display.update()

# RIGHT HERE!

for event in pygame.event.get():
None

# RIGHT THERE!

for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRun = False
gameOver = False
gameWindow.fill(white)
message_to_screen("Closing Game...", black, 280, 280)
pygame.display.update()
time.sleep(1)
pygame.quit()
quit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
change_y = -moveSpeed
change_x = 0
elif event.key == pygame.K_s:
change_y = moveSpeed
change_x = 0
elif event.key == pygame.K_a:
change_x = -moveSpeed
change_y = 0
elif event.key == pygame.K_d:
change_x = moveSpeed
change_y = 0

# BOARDER CRASH
if rect_x >= display_w or rect_x < 0 or rect_y >= display_h or rect_y < 0:
gameOver = True
# LOGIC
rect_x += change_x
rect_y += change_y

if rect_x == randApplex and rect_y == randAppley:
randApplex = round(random.randrange(0, display_w - block_size) / 10.0) * 10.0
randAppley = round(random.randrange(0, display_h - block_size) / 10.0) * 10.0

# RENDER
gameWindow.fill(white)

pygame.draw.rect(gameWindow, red, [randApplex, randAppley, block_size, block_size])
snake(rect_x, rect_y, block_size)
pygame.display.update()

clock.tick(15)

message_to_screen("You Lose!", red, 325, 300)
pygame.display.update()
time.sleep(1)
message_to_screen("Closing Game!", black, 280, 350)
pygame.display.update()
time.sleep(1)

# QUIT
pygame.quit()
quit()


game_loop()

最佳答案

基本上,操作系统希望 pygame 在您的程序中处理事件。如果操作系统注意到事件未被处理,它会提醒用户。该程序实际上并没有崩溃或死机,操作系统只是说您的程序已变得无响应(这是因为您没有响应任何用户事件),但它仍然有效。

当您的游戏进入小场景时,您可能认为您不需要处理事件,但有一个事件您应该始终检查:pygame.QUIT 事件(当用户按下顶角的关闭按钮)。在您的示例中,您不允许用户在游戏结束序列期间退出(您为玩家提供了一个按钮来点击,但用户也希望点击关闭按钮也会关闭游戏)。

另一个原因是事件队列不断填​​满。因此,如果用户使用鼠标输入多个键并按下多个区域,他/她再次进入游戏(您有一个事件循环)之前什么都不会发生。然后将执行每个 事件。因此定期清空队列很重要。每次调用 pygame.event.get()pygame.event.clear() 时都会清空队列。

函数pygame.event.pump()是将所有事件放入事件队列的函数(它不会清除之前的事件,只是添加)。如果未调用该函数,事件队列将不会被任何事件填充/更新。但是,该函数在函数 pygame.event.get()pygame.event.clear()pygame.event.poll() 中被隐式调用pygame.event.wait()pygame.event.peek(),因此很少有理由显式调用它。如果您确定不想在某个时间处理事件,您可以使用 pygame.event.clear() 这样当您再次开始处理事件时事件队列为空。如果您根本不想处理事件,请使用 pygame.event.pump()

关于python - Pygame 需要 "for event in pygame.event.get()"才能不崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com