2021-08-21

python数组实现生命游戏并用pygame显

首先是生成一个多维数组,文件名为life.py

import random
import copy

n=100
a=[[0 for i in range(n)] for j in range(n)]
#生成随机life
def pro_arr():
    for i in range(n):
        for j in range(n):
            k=random.randint(0,1)
            a[i][j]=k
    return a

def life():
    c=copy.deepcopy(a)
    for i in range(n):
        for j in range(n):
            if i==0:
                #左上角
                if j==0:
                    b=c[i][j+1]+c[i+1][j]+c[i+1][j+1]
                #右上角
                elif j==n-1:
                    b=c[i+1][j-1]+c[i+1][j]+c[i][j-1]
                #上边
                else:
                    b=c[i][j-1]+c[i][j+1]+c[i+1][j-1]+c[i+1][j]+c[i+1][j+1]
            elif i==n-1:
                #左下角
                if j==0:
                    b=c[i][j+1]+c[i-1][j+1]+c[i-1][j]
                #右下角
                elif j==n-1:
                    b=c[i][j-1]+c[i-1][j]+c[i-1][j-1]
                #下边
                else:
                    b=c[i-1][j-1]+c[i-1][j]+c[i-1][j+1]+c[i][j-1]+c[i][j+1]
            else:
                #左边
                if j==0:
                    b=c[i-1][j]+c[i-1][j+1]+c[i][j+1]+c[i+1][j]+c[i+1][j+1]
                #右边
                elif j==n-1:
                    b=c[i-1][j-1]+c[i+1][j-1]+c[i+1][j]+c[i][j-1]+c[i-1][j]
                #中间
                else:
                    b=c[i-1][j-1]+c[i-1][j]+c[i-1][j+1]+c[i][j-1]+c[i][j+1]+c[i+1][j-1]+c[i+1][j]+c[i+1][j+1]
            if b==3:
                a[i][j]=1
            elif b!=2:
                a[i][j]=0
    return a

然后就是用pygame实现了

import random
import pygame
import sys
from pygame.locals import *
import time
import life

# 屏幕大小
Window_Width = 500
Window_Height = 500
# 方块大小
Cell_Size = 20
assert Window_Width % Cell_Size == 0
assert Window_Height % Cell_Size == 0
Cell_W = int(Window_Width/Cell_Size)
Cell_H = int(Window_Height/Cell_Size)
# 背景颜色
Background_Color = (0, 0, 0)
Main_Display = pygame.display.set_mode((Window_Width, Window_Height))
color=(50,50,50)
n=100
# 画网格
def draw_Grid():
    # 垂直方向
    for x in range(0, Window_Width, Cell_Size):
        pygame.draw.line(Main_Display, color, (x, 0), (x, Window_Height))
    # 水平方向
    for y in range(0, Window_Height, Cell_Size):
        pygame.draw.line(Main_Display, color, (0, y), (Window_Width, y))
#画life
def draw_tux(a):
    Main_Display.fill(Background_Color)
    for i in range(n):
        for j in range(n):
            if a[i][j]==1:
                x=j
                y=i
                x1 = x* Cell_Size
                y1 = y* Cell_Size
                apple_Rect = pygame.Rect(x1, y1, Cell_Size, Cell_Size)
                pygame.draw.rect(Main_Display, (255, 0, 0), apple_Rect)
    draw_Grid()
    pygame.display.update()
                

if __name__ == '__main__':
    a=life.pro_arr()
    draw_tux(a)
    for i in range(100):
        time.sleep(0.15)
        a=life.life()
        draw_tux(a)

最后附上效果图
2021-08-21

上一篇:Python游戏开发,pygame模块,Python实现FlappyBird的小游戏


下一篇:Python游戏开发,pygame模块,Python实现24点小游戏