Python程序游戏的生活

时间:2018-05-04 08:35:02

标签: python

我目前正在尝试在python中制作conways游戏,我正在试图弄清楚如何让程序读取黑色正方形,但是我没有运气,任何人都可以提供帮助。 onoff print目前只是将所有内容打印为关闭(0)并且我希望它按顺序打印1,其中有黑色方块。

import pygame
import random
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GREY = (0, 210, 230)

WIDTH = 20
HEIGHT = 20
MARGIN = 1
gridsize = 20

onoff = []
grid = []
for row in range(gridsize):
    grid.append([])
    for column in range(gridsize):    # grid
        grid[row].append([])

    grid[row][column] = 0 # setting value of pixel to off

    for x in range(gridsize*gridsize):    #onoff update
        onoff.append(grid[row][column])

print(onoff)


pygame.init()
WINDOW_SIZE = [21*gridsize, 21* gridsize]
screen = pygame.display.set_mode(WINDOW_SIZE) # screen size


done = False
clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            done = True 
        elif event.type == pygame.MOUSEBUTTONDOWN:# mosue click detection
            pos = pygame.mouse.get_pos()
            column = pos[0] // (WIDTH + MARGIN)
            row = pos[1] // (HEIGHT + MARGIN)
            grid[row][column] = 1          

            print(onoff)
            print("Grid coordinates: ", row, column)

    for row in range(gridsize):
        for column in range(gridsize):
            color = WHITE  #grid creation

            if grid[row][column] == 1:
                color = BLACK
            pygame.draw.rect(screen,
                             color,
                             [(MARGIN + WIDTH) * column + MARGIN, # black rect
                              (MARGIN + HEIGHT) * row + MARGIN,
                              WIDTH,
                              HEIGHT])


    clock.tick(60)

    pygame.display.flip()

pygame.quit()

1 个答案:

答案 0 :(得分:0)

onoff = []
grid = []
for row in range(gridsize):
    grid.append([])
    for column in range(gridsize):    # grid
        grid[row].append([])

        grid[row][column] = 0 # setting value of pixel to off
        onoff.append(grid[row][column])

for x in range(len(grid)):
    print(grid[x])

好吧我解决了,谢谢你