pyGame full core usage in simple loop

江枫思渺然 提交于 2020-01-01 08:02:33

问题


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?


回答1:


We fixed this in pygame 2.

pip install pygame==2.0.0.dev6

cheers,



来源:https://stackoverflow.com/questions/46160971/pygame-full-core-usage-in-simple-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!