[Python] Game of life in pygame running extremely slow [duplicate]

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    I am writing a simple version of Conways Game of Life in Python with pygame. It only runs at about 1 fps or even less and I can´t find out why. I´ve read similar questions and found out that nested loops and things like updating the screen to often can cause this but I don´t know how I could change my code. I What is making this code so slow?

    The draw_cells function in the main File must be the cause, because its the only one thats called every game tick.

    There may be a few unnecessary variables but I don´t think they make a difference.

    I am just a beginner, sorry if I am overlooking something completely obvious.

    Main File:

    import pygame
    import sys
    import random
    from life_cell import LifeCell

    class GameOfLife:
    """a class to manage the game mechanics"""
    def __init__(self):
    pygame.init()
    pygame.display.set_caption("Conways Game of life")
    self.clock=pygame.time.Clock()

    self.screen=pygame.display.set_mode((0,0), pygame.FULLSCREEN)
    self.screen_rect=self.screen.get_rect()

    self.width=self.screen.get_width()
    self.height=self.screen.get_height()
    self.hu=self.height/1000
    self.wu=self.width/1000
    self.sim_running=True

    self.size_unit=(self.width+self.height)/3000

    self.game_active = True

    self.screen=pygame.display.set_mode((0,0), pygame.FULLSCREEN)
    self.screen_rect=self.screen.get_rect()


    self.grid=[]
    self.row=[]
    self.draw_grid()
    self.draw_cells()
    #self.draw_cells()


    def run_game(self):
    """main loop of the game"""
    while True:
    self._check_events()
    if self.game_active:
    self._update_screen()
    self.clock.tick(10)

    def _check_events(self):
    """tracks key and mouse actions"""
    for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
    if event.key==pygame.K_SPACE:
    pass
    elif event.key == pygame.K_s:
    self.sim_running=False
    #self.guessed_time,self.time_difference=self.Timer.stop_timer(self.time_input)
    elif event.key == pygame.K_q:
    sys.exit()
    if event.type==pygame.MOUSEBUTTONDOWN:
    #mouse_pos=pygame.mouse.get_pos()
    #self._check_stop_button(mouse_pos)
    #self._check_time_selected(mouse_pos)
    pass

    def draw_grid(self):
    self.x_cell_count=self.width/(self.size_unit*10)
    self.y_cell_count=self.height/(self.size_unit*10)
    print(self.x_cell_count)
    print(self.y_cell_count)
    self.x_counter=0
    self.y_counter=0
    while self.y_counter <= self.y_cell_count:
    while self.x_counter <= self.x_cell_count:
    self.row.append(random.randint(0,1))
    self.x_counter=self.x_counter+1
    self.x_counter=0
    self.grid.append(self.row)
    self.y_counter=self.y_counter+1
    self.row=[]
    self.y_counter=0
    self.x_counter=0
    print(self.grid)
    self.x_cell_count=0
    self.y_cell_count=0


    def draw_cells(self):

    counter=0
    grid_lenght=len(self.grid)
    self.row=self.grid[0]
    row_lenght=len(self.row)
    grid=self.grid
    X=grid_lenght
    Y=row_lenght
    while self.y_counter < grid_lenght:
    self.row=self.grid[counter]
    for cell in self.row:
    print(cell,"cell")

    i=self.y_counter
    j=self.x_counter

    total = int((
    grid[(j-1)%X] + grid[(j+1)%Y] +
    grid[(i-1)%X][j] + grid[(i+1)%X][j] +
    grid[(i-1)%X][(j-1)%Y] + grid[(i-1)%X][(j+1)%Y] +
    grid[(i+1)%X][(j-1)%Y] + grid[(i+1)%X][(j+1)%Y]))


    if total <2 or total>3:
    if grid[self.y_counter][self.x_counter]==1:
    self.cell=LifeCell(self,self.size_unit*100*self.x_counter,self.size_unit*100*self.y_counter,0)
    self.cell.draw_cell()
    grid[self.y_counter][self.x_counter]=0
    elif self.grid[self.y_counter][self.x_counter]==0 and total ==3:
    grid[self.y_counter][self.x_counter]=1
    self.cell=LifeCell(self,self.size_unit*100*self.x_counter,self.size_unit*100*self.y_counter,1)
    self.cell.draw_cell()

    self.x_counter=self.x_counter+1
    self.x_counter=0
    self.y_counter=self.y_counter+1
    self.y_counter=0


    def _update_screen(self):
    self.draw_cells()
    pygame.display.update()

    if __name__=="__main__":
    LifeGame=GameOfLife()
    LifeGame.run_game()


    LifeCell File:

    import pygame


    class LifeCell:
    def __init__(self,LifeGame,x_pos,y_pos,color):
    self.screen=LifeGame.screen

    if color==1:
    self.color=(255,255,255)
    else:
    self.color=(0,0,0)
    self.hu=LifeGame.hu
    self.wu=LifeGame.wu
    self.size_unit=LifeGame.size_unit
    self.width=self.size_unit*100
    self.height=self.size_unit*100
    #self.color=(255,255,255)

    self.rect=pygame.Rect(x_pos,y_pos,self.width, self.height)
    #self.rect.right=self.screen_rect.right

    def draw_cell(self):
    self.screen.fill(self.color,self.rect)

    Continue reading...

Compartilhe esta Página