Pygame 在 mac 上不断崩溃 [英] Pygame keeps crashing on mac

查看:75
本文介绍了Pygame 在 mac 上不断崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习 pygame,写了一个简单的程序来在屏幕上显示一些文本.

导入pygame,时间pygame.init()窗口 = pygame.display.set_mode((600,300))myfont = pygame.font.SysFont("Arial", 60)label = myfont.render("Hello Pygame!", 1, (255, 255, 0))window.blit(标签, (100, 100))pygame.display.update()时间.睡眠(15)pygame.quit()

但它一直崩溃.我使用的是python2.7

解决方案

问题是你只运行了一次代码,而不是重复每一帧都需要重复的代码行.

然后你调用 pygame.quit() 而不用 quit() 退出 Python 线程,这导致窗口只是崩溃"或没有响应.>

要解决此问题:

  • 在 while 循环中包含一些代码,这些代码将在每一帧上运行,从而保持程序运行和响应.

  • 确保初始化代码只运行一次.

  • 添加一些事件处理,让用户在点击X"按钮时退出程序.

一些有用的补充:

  • 包含一个允许 FPS 上限的 Clock.

  • 每帧都用黑色填充屏幕

  • 使用 pygame.quit() 正确退出游戏以退出 pygame 窗口和 sys.exit() 以退出 Python 线程.

Pygame 游戏中的 Clock 允许您指定 FPS.在每个主游戏循环迭代(帧)结束时,您调用 clock.tick(FPS) 以等待确保游戏以指定帧速率运行的时间量.

这是修改后的代码示例:

导入pygame导入系统# 这段代码只需要运行一次pygame.init()窗口 = pygame.display.set_mode((600,300))myfont = pygame.font.SysFont("Arial", 60)label = myfont.render("Hello Pygame!", 1, (255, 255, 0))时钟 = pygame.time.Clock()每秒帧数 = 30为真:#允许用户退出屏幕对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()系统退出()# 这段代码应该每帧运行一次window.fill((0, 0, 0))window.blit(标签, (100, 100))pygame.display.update()时钟滴答(FPS)

希望这个回答对您有所帮助,如果您还有其他问题,请随时在下面发表评论!

I started learning pygame, wrote a simple program to display some text on the screen.

import pygame, time

pygame.init() 
window = pygame.display.set_mode((600,300))
myfont = pygame.font.SysFont("Arial", 60)
label = myfont.render("Hello Pygame!", 1, (255, 255, 0))
window.blit(label, (100, 100))
pygame.display.update()
time.sleep(15)
pygame.quit()

But it keeps crashing. I am using python2.7

解决方案

The issue is that you are running the code only once and not repeating the lines of code that need to be repeated for every frame.

Then you are calling pygame.quit() without exiting the Python thread with quit() which results in the windows just "crashing" or not responding.

To fix this problem:

  • Include some code inside a while loop that will run on every frame and thus keep the program running and responding.

  • Make sure that initialization code is only ran once.

  • Add in some event handling to let the user exit the program when the "X" button is clicked.

Some useful additions:

  • Included a Clock which allows for an FPS-cap.

  • Filled the screen with black every frame

  • Exited the game properly with pygame.quit() to exit the pygame window and sys.exit() to exit the Python thread.

A Clock in pygame game allows you to specify an FPS. At the end of every main game loop iteration (frame) you call clock.tick(FPS) to wait the amount of time that will ensure the game is running at the specified framerate.

Here is the revised code example:

import pygame
import sys

# this code only needs to be ran once
pygame.init() 
window = pygame.display.set_mode((600,300))
myfont = pygame.font.SysFont("Arial", 60)
label = myfont.render("Hello Pygame!", 1, (255, 255, 0))
clock = pygame.time.Clock()
FPS = 30

while True:
    #allows user to exit the screen
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
           pygame.quit()
           sys.exit()

    # this code should be ran every frame
    window.fill((0, 0, 0))
    window.blit(label, (100, 100))
    pygame.display.update()
    clock.tick(FPS)

I hope that this answer has helped you and if you have any further questions please feel free to post a comment below!

这篇关于Pygame 在 mac 上不断崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆