康威的 PyGame 生活

Conway's PyGame 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?

我以前从未玩过生命游戏,所以也欢迎任何有关机制的意见。

EDIT/SOLUTION: 所有的舍入都把所有东西都扔掉了。

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

正如@user3757614 已经发布的那样,问题是 你使用整数除法 来计算每个 Cellwh实例:

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

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

在Python2中有a number of ways强制除法为浮点数。一个是以浮点格式制作除法的参数之一,因为这也会产生浮点输出:

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

注意 Python 3 / 是一个浮点除法,所以你不需要改变你对 w 的计算和 h。但是你需要把range((l*l)/2)改成range((l*l)//2),因为传递给range构造函数的参数必须是整数。

希望对您有所帮助:)