Python 康威';人生的游戏
我制作了一个生活游戏来尝试PyGame,但它停止渲染底部和右侧边缘 我不知道该去哪里找,即使我真的在网上找不到任何东西 完整的代码张贴,让你可以看到我的意思Python 康威';人生的游戏,python,pygame,conways-game-of-life,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
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()
正如@user3757614已经发布的那样,问题在于您使用整数除法来计算每个
单元格的w
和h
self.w = WIDTH / len(SEED[0])
self.h = HEIGHT / len(SEED)
在Python 2.7中,如果输入也是整数,则/
运算符是整数除法,因此w
和h
将始终向下取整(即小数点截断),并且根据l
的值,屏幕的右侧和底部将有一个边框
在Python2中,强制除法是浮点。一种是以浮点格式制作除法参数之一,因为这也会生成浮点输出:
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构造函数的参数必须是整数
我希望这有帮助:)我知道这并不能真正解决您的问题,但是如果您使用Python 3,您需要将range((l*l)/2)
更改为range((l*l)//2)
,因为传递给range
构造函数的参数必须是整数。:)思想:在“self.w=WIDTH/len(SEED[0])”行中,这是850/200,等于4.25。但是,这将是整数除法,因此self.w将等于4。换句话说,您可能要绘制所有200行/列,只是在右/下角将有一个50像素的边框。与要点相切:使用名为l
的变量是个坏主意。对于某些文本编辑器,l与1无法区分。即使它们是可以区分的,也很难区分。谢谢大家,这一切都在四舍五入中。使用{size=((宽度/l)*l,(高度/l)*l);screen=pygame.display.set_mode(大小);}轻松修复。从uu未来u导入分区添加是另一种选择
self.w = float(WIDTH) / len(SEED[0])
self.h = float(HEIGHT) / len(SEED)