康威的生命PyGame

时间:2015-04-10 16:57:11

标签: python pygame conways-game-of-life

我做了一个生命游戏来试试PyGame,但它停止渲染底部和右边缘。

不确定在哪里看,我甚至无法在网上找到任何东西。

发布完整代码,以便您了解我的意思。

import pygame
import random
pygame.init()

HEIGHT = 850 # Window Size
WIDTH = 850

l = 200 # cells per row
s = []

for i in range(l):
    s.append([])
    for j in range(l):
        if i % 2 == 0:
            s[i].append(1)
        else:
            s[i].append(0)

for i in range((l*l)/2):
    i = random.randrange(l)
    j = random.randrange(l)
    s[i][j] = not s[i][j]

SEED = s

BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
BLUE     = (   0,   0, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)

size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Conway's Game of Liff")

'''
CLASSES
'''

class Cell():
    def __init__(self, x, y, alive):
        self.x = x
        self.y = y
        self.w = WIDTH / len(SEED[0])
        self.h = HEIGHT / len(SEED)
        self.alive = alive

    def draw(self):
        x = self.x
        y = self.y
        h = self.h
        w = self.w
        pygame.draw.rect(screen, WHITE, pygame.Rect(x, y, h, w))

'''
Field Class - 2-d array of cells, updates each cell according to Evolve()
'''
class Field():
    def __init__(self, blank):
        temp = Cell(0, 0, False)
        w = temp.w
        h = temp.h
        del temp

        #sets all cells to false
        self.cells = [[Cell(i*w, j*h, False) for i in range(len(SEED))] for j in range(len(SEED[0]))]

        if not blank:
            for i in range(l):
                for j in range(l):
                    if SEED[i][j] == 1:
                        self.cells[i][j].alive = True

    def Evolve(self, i, j, temp):
        living = 0
        lim = l-1

        if i < lim:
            if self.cells[i+1][j].alive:
                living += 1
        if i < lim and j < lim:
            if self.cells[i+1][j+1].alive:
                living += 1
        if i > 0:
            if self.cells[i-1][j].alive:
                living += 1
        if j < lim:
            if self.cells[i][j+1].alive:
                living += 1
        if j > 0:
            if self.cells[i][j-1].alive:
                living += 1
        if i < lim and j > 0:
            if self.cells[i+1][j-1].alive:
                living += 1
        if i > 0 and j < lim:
            if self.cells[i-1][j+1].alive:
                living += 1
        if i > 0 and j > 0:
            if self.cells[i-1][j-1].alive:
                living += 1

        if self.cells[i][j].alive and (living < 2 or living > 3):
            temp.cells[i][j].alive = False
        elif (not self.cells[i][j].alive) and (living == 3):
            temp.cells[i][j].alive = True
        else:
            temp.cells[i][j].alive = self.cells[i][j].alive

    def drawLines(self):
        temp = Cell(0, 0, False)
        w = temp.w
        h = temp.h
        del temp

        for i in range(l):
            pygame.draw.line(screen, BLACK, (0, i*h), (WIDTH, i*h))
            pygame.draw.line(screen, BLACK, (i*w, 0), (i*w, HEIGHT))

    def update(self):
        temp_field = Field(True)
        imax = l
        jmax = l

        # populate buffer field
        for i in range(imax):
            for j in range(jmax):
                self.Evolve(i, j, temp_field)

        # copy buffer
        self.cells = temp_field.cells
        del temp_field

        for i in range(imax):
            for j in range(jmax):
                if self.cells[i][j].alive:
                    self.cells[i][j].draw()

        self.drawLines()



'''
MAIN STUFF
'''
#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
f = Field(False)

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(BLUE) # wipe buffer

    f.update()
    pygame.display.flip() # flip buffer to screen
    clock.tick(5)

pygame.quit()

奇怪的是,无论l(第7行)的值如何,边缘都不会绘制细胞或线条。这是我的代码还是python的问题?

我以前从未做过生命游戏,所以对机制的任何意见也都欢迎。

编辑/解决方案:所有四舍五入都将一切都抛弃了。

size = ((WIDTH/l) * l, (HEIGHT/l) * l)
screen = pygame.display.set_mode(size)

1 个答案:

答案 0 :(得分:2)

正如@ user3757614已发布的问题是您使用整数除法来计算每个w实例的hCell

self.w = WIDTH / len(SEED[0])
self.h = HEIGHT / len(SEED)

Python 2.7中/运算符是整数除法,如果输入也是整数,因此wh将始终为舍入向下(即小数点被截止),并且,根据l的值,屏幕的右侧和底部会有一个边框。

a number of ways强制除法在Python 2中是浮点。一种是以浮点格式进行除法的一个参数,因为这也产生一个浮点输出:

self.w = float(WIDTH) / len(SEED[0])
self.h = float(HEIGHT) / len(SEED)

请注意,在 Python 3 /是浮动分区,因此您无需更改wh的计算。但是您需要将range((l*l)/2)更改为range((l*l)//2),因为传递给范围构造函数的参数必须是整数。

我希望这会有所帮助:)