繁体   English   中英

连续运行python代码后,我的计算机最终崩溃

[英]Upon successive runs of my python code eventually crashes my computer

看起来,当我在头几次运行我的代码时,它运行得很漂亮,但是在大约第三到第四次运行后,它开始变慢了。 那是因为我的计算机似乎出现一些滞后,因此变得无响应。 最终,这使我的计算机崩溃,并且使在IDLE中进行编码变得困难。 我正在具有双核1.8 GHz处理器和2GB内存的笔记本电脑上的ubuntu 12.04上运行。 我不认为我要用这个推动硬件

#Beating Heart Game

#imports
import pygame
import random
import math
from pygame.locals import *

#Intialize Modules
pygame.init()
pygame.display.init()

#Globals
WIDTH = 600
HEIGHT = 600
FRAME = pygame.display.set_mode((WIDTH,HEIGHT))

#Colors
GRAY = (100, 100, 100)
NAVYBLUE = ( 60, 60, 100)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
CYAN = ( 0, 255, 255)
BLACK = ( 0, 0, 0)
NEARBLACK = ( 19, 15, 48)
COMBLUE = (233, 232, 255)

#Helper Functions

#Class Cell
class Cell:

    def __init__(self, radius, color, pos):
        self.radius = radius
        self.color = color
        self.pos = pos

    def get_position(self):
        return self.pos

    def move(self):
        pass

    def draw(self):
        pygame.draw.circle(surface, self.color, self.pos,self.radius, width=0)


#Class Heart
class Heart:

    def __init__(self,radius,color,pos):
        self.radius = radius
        self.color = color
        self.pos = pos

#Objects
cell = Cell(5, PURPLE, [WIDTH/2,HEIGHT/2])
pygame.draw.circle(FRAME,PURPLE,[WIDTH/2,HEIGHT/2],10,0)


#Game loop
class loop(object):
   gameloop = True
    while gameloop:
        pygame.display.set_caption("Beating Heart")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameloop = False
                pygame.display.quit()              
            pygame.display.update()
pygame.quit()

我刚刚启动了该程序,所以代码到目前为止是一个框架

由OP解决。

原来pygame的运行速度与CPU允许的速度一样快,因此在运行程序时,CPU使用率会高达100%。 要限制此插入对象

clock = pygame.timer.Clock()

然后在游戏循环中

clock.tick(60)

其中“ 60”是允许的fps。

在编写游戏循环时,我没有清楚地向我解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM