[英]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的问题?
我以前从未做过《人生游戏》,因此也欢迎您提供任何有关机制的信息。
编辑/解决方案:所有舍入都将所有内容丢掉。
size = ((WIDTH/l) * l, (HEIGHT/l) * l)
screen = pygame.display.set_mode(size)
由于@ user3757614已经发布,问题是您使用整数除法来计算每个Cell
实例的w
和h
:
self.w = WIDTH / len(SEED[0])
self.h = HEIGHT / len(SEED)
在Python 2.7中,如果输入也为整数 , 则/
运算符为整数除法 ,因此w
和h
将始终四舍五入 (即, 舍去小数点),并且根据l
的值,右侧将有一个边框和屏幕的底部。
在Python 2中, 有多种方法可以将除法强制为浮点数 。 一种是使用浮点格式进行除法的参数之一,因为这也会产生浮点输出:
self.w = float(WIDTH) / len(SEED[0])
self.h = float(HEIGHT) / len(SEED)
需要注意的是在Python 3 /
是float师 ,这样您便不会需要改变你的计算w
和h
。 但是您需要将range((l*l)/2)
更改为range((l*l)//2)
因为传递给range构造函数的参数必须是整数。
我希望这有帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.