Make a simple python game with pygame --- life game

Posted by jaylee on Mon, 11 Oct 2021 21:23:16 +0200

Make a simple python game with pygame - life game

Game of Life

Game of life is a computer program designed by Professor John Horton Conway of Cambridge University. This computer program is named "life game" because the images it simulates and displays look like the birth and reproduction process of life. At the same time, life game is also a two-dimensional cellular automata.

Life game is a two-dimensional grid game. Each square in this grid lives a living or dead cell. The life and death of a cell at the next moment depends on the number of living or dead cells in the adjacent 8 squares. If there are too many living cells in adjacent squares, the cell will die at the next moment due to lack of resources; On the contrary, if there are too few living cells around, the cell will die because of loneliness. At the beginning of the game, the system will randomly generate some cells. From these initial cells.

Rules:

  • If the cell is in a dead state, if there are three living cells around it, the cell will become a living state after reproduction
  • If the cell is in a living state and there are less than 2 living cells around it, the cell will become dead after reproduction
  • If the cell is in a living state, if there are 2 or 3 living cells around it, the state of the cell remains unchanged after reproduction
  • If the cell is in a living state and there are more than three living cells around it, the cell will become dead after reproduction

Rendering display:

Attach Code:

import pygame, sys, time, random
width=102   #Width of panel (with a layer of wall on the periphery)
high=102  #Height of panel (with a layer of wall on the periphery)
size=6   #Sets the size of the drawn single square

def initialization(arr):   #initialization
    for i in range(high):
        for j in range(width):
            ran=random.random()
            if ran>0.9:
                arr[i][j]=1
            else:
                pass
    return arr

def nextmultiply(arr):    #Next generation reproduction
    newarr = [([0] * width) for n in range(high)]
    for i in range(high):
        for j in range(width):
            num=0
            if (i==0 or i==high-1) or (j==0 or j==width-1):
                newarr[i][j]=0
            else:
                num=arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1]+arr[i][j-1]+arr[i][j+1]+arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1]
                if arr[i][j]==0:  #If the protocell is dead
                    if num==3:
                        newarr[i][j]=1
                else:    #If the protocells are alive
                    if num==2 or num==3:
                        newarr[i][j]=1
                    else:
                        newarr[i][j]=0
    return newarr



if __name__ == '__main__':
    color_white = pygame.Color(255, 255, 255)
    color_LightSkyBlue = pygame.Color(135,206,250)
    color_black = pygame.Color(0, 0, 0)
    pygame.init()
    screen = pygame.display.set_mode((width*size, high*size))
    screen.fill(color_white)
    pygame.display.set_caption("Life game Game of Life")
    arr = [([0] * width) for i in range(high)]  # Create a 2D array
    arr=initialization(arr)
    while(True):
        screen.fill(color_white)
        time.sleep(0.5)
        for i in range(high):
            for j in range(width):
                if arr[i][j]==1:
                    pygame.draw.rect(screen, color_black, (i * size, j * size, size, size))
                elif (i==0 or i==high-1) or (j==0 or j==width-1):
                    pygame.draw.rect(screen, color_LightSkyBlue, (i * size, j * size, size, size))
                else:
                    pass
        for event in pygame.event.get():  # monitor
            if event.type == pygame.QUIT:
                sys.exit()
        arr = nextmultiply(arr)
        pygame.display.update()

Topics: Python Game Development pygame