康威生命游戏克隆中的奇怪行为[重复]

问题描述 投票:0回答:0

我尝试使用 pygame 重新创建 Conways 生活游戏,但是在创建新方块或计算活着的邻居时有些东西不起作用。

import pygame, sys

screen_size = (800,800)
pygame.init()
screen = pygame.display.set_mode(screen_size)
clock = pygame.time.Clock()


counter=0

mouse_pos = []
zoom = 1
max_zoom = 10
cam_pos = [0,0]

world_size = 10

checking = False

rects = []
occupied = []

rects_to_create=[]
rects_to_kill=[]


def create_rect(mouse_pos):
    global rects
    if mouse_pos not in occupied:
        rects.append(pygame.Rect(mouse_pos[0],mouse_pos[1],1,1))
        occupied.append(mouse_pos)
        print("rect created at: "+str(mouse_pos))


def check_rects():
    global counter
    for x in range(0,world_size):
        for y in range(0,world_size):
            for a in range(x-1,x+1):
                for b in range(y-1,y+1):
                    if (a,b) in occupied:
                        counter+=1
                        print(counter)
                        

            if (x,y) in occupied:
                if counter == 3 or counter == 2:
                    rects_to_create.append((x,y))
            if (x,y) not in occupied:
                if counter == 3:
                    rects_to_create.append((x,y))
            if (x,y) in occupied:
                if counter < 2:
                    rects_to_kill.append((x,y))
                if counter > 3:
                    rects_to_kill.append((x,y))
            counter=0
    for i in rects_to_create:
        create_rect(i)
    for i in rects_to_kill:
        kill_rect(i)



def kill_rect(pos):
    global rects
    print(occupied)
    new_list = []
    for i in rects:
        if (i.x,i.y) != pos:
            new_list.append(i)
    print("rect killed at: "+str(pos))      
    rects = new_list
    if pos in occupied:
        occupied.remove(pos)
    print(occupied)
    


def draw_rects():
    global rects
    global cam_pos
    for i in rects:
        pygame.draw.rect(screen, (0,0,0), (i.left*zoom-cam_pos[0],i.top*zoom-cam_pos[1],i.width*zoom,i.height*zoom))


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit

        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mouse_pos=(int(round(pygame.mouse.get_pos()[0]/zoom)),int(round(pygame.mouse.get_pos()[1]/zoom)))
            if mouse_pos not in occupied:
                print(mouse_pos)
                create_rect(mouse_pos)
                occupied.append(mouse_pos)
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
            mouse_pos=(int(round(pygame.mouse.get_pos()[0]/zoom)),int(round(pygame.mouse.get_pos()[1]/zoom)))
            if mouse_pos in occupied:
                occupied.remove(mouse_pos)
                print(occupied)
                new_list = []
                for i in rects:
                    if (i.x,i.y) != mouse_pos:
                        new_list.append(i)
                rects = new_list
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 4:
            if zoom < max_zoom:
                zoom += 1
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 5:
            if zoom > 1:
                zoom -= 1
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                cam_pos[0]-=10
            if event.key == pygame.K_d:
                cam_pos[0]+=10
            if event.key == pygame.K_w:
                cam_pos[1]-=10
            if event.key == pygame.K_s:
                cam_pos[1]+=10

            if event.key == pygame.K_SPACE:
                checking= not checking
                print(checking)

    
    draw_rects()
    if checking:
        clock_rate=1
        check_rects()
    else:
        clock_rate=60

    pygame.display.update()

    screen.fill((187,187,187))
    clock.tick(clock_rate)

我已经尝试过编辑规则并更改方块被杀死/创建的时间点,但我似乎无法做到正确。你可以用滚轮放大,顺便用 a 和 d 移动

python pygame conways-game-of-life
© www.soinside.com 2019 - 2024. All rights reserved.