如何解决python的CPU使用率在Windows 10中变为0
我正在编写一个关于飞扬的鸟类整洁 AI 游戏的教程,我尝试运行到目前为止我所拥有的,但是当我的 pygame 窗口启动时,它只能工作一秒钟,然后它冻结。我检查了任务管理器,看起来在循环开始时它获得了 36% 的 cpu,但之后它下降到 0%。有没有办法对 python 任务进行优先级排序,使其获得连续的 cpu?
这是我试图运行的代码。
import pygame
import neat
import time
import os
import random
WIN_WIDTH = 500
WIN_HEIGHT = 800
BIRD_IMGS = [
pygame.transform.scale2x(pygame.image.load(os.path.join("imgs","bird1.png"))),pygame.transform.scale2x(pygame.image.load(os.path.join("imgs","bird2.png"))),"bird3.png")))
]
PIPE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs","pipe.png")))
BG_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs","bg.png")))
BASE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join("imgs","base.png")))
class Bird:
IMGS = BIRD_IMGS
MAX_ROTATION = 25
ROT_VEL = 20
ANIMATION_TIME = 5
def __init__(self,x,y):
self.x = x
self.y = y
self.tilt = 0
self.tick_count = 0
self.vel = 0
self.height = self.y
self.img_count = 0
self.img = self.IMGS[0]
def jump(self):
self.vel = -10.5
self.tick_count = 0
self.height = self.y
def move(self):
self.tick_count += 1
d = self.vel*self.tick_count + 1.5*self.tick_count**2
if d >= 16:
d = 16
if d < 0:
d -= 2
self.y = self.y + d
if d < 0 or self.y < self.height + 50:
if self.tilt < self.MAX_ROTATION:
self.tilt = self.MAX_ROTATION
else:
if self.tilt > -90:
self.tilt -= self.ROT_VEL
def draw(self,win):
self.img_count += 1
if self.img_count < self.ANIMATION_TIME:
self.img = self.IMGS[0]
elif self.img_count < self.ANIMATION_TIME*2:
self.img = self.IMGS[1]
elif self.img_count < self.ANIMATION_TIME*3:
self.img = self.IMGS[2]
elif self.img_count < self.ANIMATION_TIME*4:
self.img = self.IMGS[1]
elif self.img_count < self.ANIMATION_TIME*4+1:
self.img = self.IMGS[0]
self.img_count = 0
if self.tilt <= -80:
self.img = self.IMGS[1]
self.img_count = self.ANIMATION_TIME*2
rotated_image = pygame.transform.rotate(self.img,self.tilt)
new_rect = rotated_image.get_rect(center=self.img.get_rect(topleft=(self.x,self.y)).center)
win.blit(rotated_image,new_rect.topleft)
def get_mask(self):
return pygame.mask.from_surface(self.img)
def draw_window(win,bird):
win.blit(BG_IMG,(0,0))
bird.draw(win)
pygame.display.update()
def main():
bird = Bird(200,200)
win = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT))
run = True
clock = pygame.time.Clock()
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
bird.move()
draw_window(win,bird)
pygame.quit()
quit()
if __name__ == "__main__":
main()
我使用 python main.py
解决方法
这是{{3}}的问题。您必须在应用程序循环而不是事件循环中移动小鸟并更新游戏。事件循环仅在事件发生时执行,但应用循环在每一帧中执行。
# With offset as `0`,behaves as normal list
>>> offset_list = OffsetList(0,[10,20,30,40,50,60])
>>> offset_list[0]
10
# With offset as `1`,returns index+1
>>> offset_list = OffsetList(1,60])
>>> offset_list[0]
20
# With offset as `2`,returns index+2
>>> offset_list = OffsetList(2,60])
>>> offset_list[0]
30
# Slicing support,with `start` as start+offset and `end` as end+offset
>>> offset_list[1:]
[40,60]
# Assigning new value,based on index+offset
>>> offset_list[0] = 123
>>> offset_list
[10,123,60]
# Deleting value based on index+offset
>>> del offset_list[0]
>>> offset_list
[10,60]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。