猫史档案馆


一个想法(凑字数)

用户:勤劳的大蜜蜂勤劳的大蜜蜂查看:4 回复:3 评论:4 创建时间:2020-04-18T18:20:58


我想用Python编一个生命游戏程序,但是没有时间,有人能帮我试试吗?emotion_挤眼


回复

上一页1 页 / 共 1下一页
大神George大神George

I来

点赞0


评论


尼安菌尼安菌

啥?

点赞0


评论


少幽科技少幽科技

import pygame
from pygame.locals import *
from sys import exit
import copy

SIZE = 20
pygame.init()
screen = pygame.display.set_mode((SIZE*32,SIZE*32), 0, 32)
game = [[0 for i in range(SIZE)] for j in range(SIZE)]
game2 = [[0 for i in range(SIZE)] for j in range(SIZE)]


def show():
    for i in range(SIZE):
        for j in range(SIZE):
            if not game[i][j]:
                pygame.draw.rect(screen,(200,200,200),Rect(j*32,i*32,32,32))
            else:
                pygame.draw.rect(screen, (0,0,255), Rect(j * 32, i * 32, 32, 32))


def count(x,y):
    cnt=0
    for i in range(-1,2):
        for j in range(-1,2):
            if not i and not j:
                continue
            if is_legit(x+i,y+j):
                if game[x+i][y+j]:cnt+=1;
    return cnt


def is_legit(x,y):
    return 0<=x<SIZE and 0<=y<SIZE


def cal():
    global game,game2
    for i in range(SIZE):
        for j in range(SIZE):
            if count(i,j)==2:
                game2[i][j]=game[i][j]
            elif count(i,j)==3:
                game2[i][j] = 1
            else:
                game2[i][j] = 0
            # if(count(i,j)):
                # print("count:{},{}={}   game:{}".format(i,j,count(i,j),game2[i][j]))
    game=copy.deepcopy(game2)
    # print("==========")


setting = True
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == MOUSEBUTTONDOWN:
            a,b,c=pygame.mouse.get_pressed()
            if c:
                setting=False
                continue
            if setting:
                ex, ey = event.pos
                game[ey//32][ex//32] = not game[ey//32][ex//32]
            else:
                cal()
    show()
    pygame.display.update()

点赞0


评论