pyGame 在简单循环中的完整核心使用 [英] pyGame full core usage in simple loop

查看:65
本文介绍了pyGame 在简单循环中的完整核心使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

这是一个未决问题,似乎与 alsa 音频有关.>

解决方法是关闭混音器,或从源代码安装 pyGame.

pygame.init()pygame.mixer.quit()


我刚刚开始使用 pyGame 进行开发,并发现我应该使用以下内容来控制 CPU 时间:

fps = 30时钟 = pygame.time.Clock()为真:#逻辑...时钟滴答(fps)

我似乎遇到的问题是,最基本的绘制一个以 1 FPS 运行的方形程序会消耗一个完整的 CPU 内核.

导入pygamepygame.init()大小 = ( 16, 16 )屏幕 = pygame.display.set_mode(size)pygame.display.set_caption("高CPU")时钟 = pygame.time.Clock()运行=真运行时:打印(渲染...")对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行=假screen.fill((255,255,255))# 使成为pygame.display.flip()时钟滴答(1)# 完成后pygame.quit()

一旦我调用这个脚本,我的机器的一个内核就会飙升至 100%,尽管消息仅以预期的 1 FPS 更新率打印到控制台.

$ ps aux |蟒蛇mclark 25867 97.4 0.1 652232 29088 pts/0 Sl+ 13:10 0:06 python highCPU.py

并运行最新的 pyGame

$ pip show pygame名称:pygame版本:1.9.3总结:Python 游戏开发主页:http://www.pygame.org作者:Pete Shinners、Rene Dudfield、Marcus von Appen、Bob Pendleton 等……作者邮箱:pygame@seul.org许可证:LGPL位置:/home/mclark/.local/lib/python2.7/site-packages要求:

这是最新版本的 pyGame 中可能存在的错误吗?还是我的时间管理不正确?

解决方案

我们在 pygame 2 中修复了这个问题.在某些 linux 系统上它不再需要 100% 的 CPU 内核.

python3 -m pip install pygame --upgrade

从 pygame 2.0.0 开始,在某些 Linux 系统上从源代码安装 pygame 可能会更好.

干杯,

Edit

This is an open issue and appears to be related to alsa audio.

A workaround is to shutdown the audio mixer, or install pyGame from source.

pygame.init()
pygame.mixer.quit()


I am just beginning development with pyGame and have found that I should use the following to gate CPU time:

fps = 30
clock = pygame.time.Clock()

while True:
    # Logic...
    clock.tick(fps)

The issue that I seem to be having, is that the most basic draw a square program running at 1 FPS consumes a full CPU core.

import pygame

pygame.init()

size = ( 16, 16 )
screen = pygame.display.set_mode(size)
pygame.display.set_caption("High CPU")

clock = pygame.time.Clock()

run=True
while run:

    print("Rendering...")

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             run=False

    screen.fill((255,255,255))

    # Render
    pygame.display.flip()
    clock.tick(1)

# When done
pygame.quit()

As soon as I invoke this script, a single core of my machine spikes to 100%, although the messages are only being printed to the console at the expected 1 FPS update rate.

$ ps aux | grep python
mclark   25867 97.4  0.1 652232 29088 pts/0    Sl+  13:10   0:06 python highCPU.py

And running latest pyGame

$ pip show pygame
Name: pygame
Version: 1.9.3
Summary: Python Game Development
Home-page: http://www.pygame.org
Author: Pete Shinners, Rene Dudfield, Marcus von Appen, Bob Pendleton, others...
Author-email: pygame@seul.org
License: LGPL
Location: /home/mclark/.local/lib/python2.7/site-packages
Requires: 

Is this a possible bug in the latest version of pyGame? Or am I managing time incorrectly?

解决方案

We fixed this in pygame 2. It does not take 100% of a CPU core on some linux systems anymore.

python3 -m pip install pygame --upgrade

As of pygame 2.0.0, it may be better to install pygame from source on some linux systems.

cheers,

这篇关于pyGame 在简单循环中的完整核心使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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