Pygame 的运行方式与不同的 Python 解释器不同 [英] Pygame running differently from different python interpreters

查看:45
本文介绍了Pygame 的运行方式与不同的 Python 解释器不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近编写了我的第一个 pygame 程序,正在编写第二个程序,发现运行该程序时出现问题.我使用 Python 3.8.6 的编辑器 MuEditor,当我使用这个编辑器运行游戏时,它完全按照我想要的方式工作.虽然,如果我从任何其他编辑器(如 IDLE 或 PyCharm)运行该程序,它会提高游戏速度并错误地显示字体,从而使游戏更难玩.我的朋友希望能够玩我写的游戏,所以我通过电子邮件向他发送了代码,他们通过 MuEditor 运行了它,但它仍然无法正常工作.当我在 MuEditor 中从我的计算机上运行该游戏时,该游戏只能按应有的方式运行,我想知道是否还有其他人遇到过这个问题,或者是否有解决方法?我非常怀疑这里的错误是在我的代码中,但如有必要,我可以提供.

解决方案

使用 pygame.time.Clock 控制每秒帧数,从而控制游戏速度.参见 pygame.time.Clock.tick():

<块引用>

这个方法应该每帧调用一次.它将计算自上次调用以来经过了多少毫秒.

方法tick()pygame.time.Clock 对象,以这种方式延迟游戏,循环的每次迭代都消耗相同的时间.

这意味着循环:

<块引用>

clock = pygame.time.Clock()运行 = 真运行时:时钟滴答(60)

每秒运行 60 次.

I recently wrote my first pygame program and am working on a second, and discovered an issue with running the program. I use the editor MuEditor for Python 3.8.6, and when I run the game using this editor it works exactly how I want it to. Although, if I run the program from ANY other editor such as IDLE or PyCharm, it ramps up the game speed and displays the fonts incorrectly, making the game much more difficult to play. My friend wants to be able to play the games I write, so I sent him the code through an email and they ran it through MuEditor, and it still worked incorrectly. The game only runs how it should when I run it from my computer in MuEditor, and I was wondering if anyone else has had this problem or if there's a fix for it? I highly doubt the error here is in my code, but I can provide it if necessary.

解决方案

Use pygame.time.Clock to control the frames per second and thus the game speed. See pygame.time.Clock.tick():

This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(60)

runs 60 times per second.

这篇关于Pygame 的运行方式与不同的 Python 解释器不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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