Python PyGame康威';生命的游戏,重画精灵
当我更新程序应该为每个位置使用的图像数组时,我可以将活细胞放置在死细胞上,但原始细胞不会消失,我不能将死细胞添加到活细胞上。有人有办法吗 原始文件Python PyGame康威';生命的游戏,重画精灵,python,pygame,Python,Pygame,当我更新程序应该为每个位置使用的图像数组时,我可以将活细胞放置在死细胞上,但原始细胞不会消失,我不能将死细胞添加到活细胞上。有人有办法吗 原始文件 import pygame, pygamehandle, standard, sys from pygame.locals import * loader = pygamehandle.load() pygame.mixer.music.load('music1.ogg') pygame.mixer.music.play(-1, 0.0) SCR
import pygame, pygamehandle, standard, sys
from pygame.locals import *
loader = pygamehandle.load()
pygame.mixer.music.load('music1.ogg')
pygame.mixer.music.play(-1, 0.0)
SCREEN_SIZE = (600, 400)
fps = 24
fpsClock = pygame.time.Clock()
imgs = ["live.png", "dead.png", "background.png"]
icon = "icon.png"
screen = loader.loadScreen(SCREEN_SIZE, "Game of Life", icon)
lImgs = loader.listImgLoad(imgs)
objects, grid = loader.grid(SCREEN_SIZE, lImgs[1])
loader.blit(objects, grid)
pygame.display.update()
while True:
mouseClicked = False
fpsClock.tick(fps)
for i in pygame.event.get():
if i.type == MOUSEBUTTONDOWN:
if i.button == 1:
mouseposx, mouseposy = pygame.mouse.get_pos()
mouseposx = (mouseposx // 20) * 20
mouseposy = (mouseposy // 20) * 20
mousepos = (mouseposx, mouseposy)
index = grid.index(mousepos)
objects[index] = lImgs[0]
if i.button == 2:
mouseposx, mouseposy = pygame.mouse.get_pos()
mouseposx = (mouseposx // 20) * 20
mouseposy = (mouseposy // 20) * 20
mousepos = (mouseposx, mouseposy)
index = grid.index(mousepos)
objects[index] = lImgs[1]
if i.type == QUIT:
pygame.quit()
sys.exit()
pygame.Surface.fill(screen, [0, 0, 0])
loader.blit(objects, grid)
pygame.display.flip()
我还使用了pygamehandle文件中的这些函数
import pygame, standard
class load(object):
pygame.init()
def loadScreen(self, size, text, icon):
pygame.display.set_caption(text, icon)
pygame.display.set_icon(pygame.image.load(icon))
screen = pygame.display.set_mode(size)
self.screen = screen
return screen
def listImgLoad(self, list):
img = []
for i in range (0, len(list)):
img.append(pygame.image.load(list[i]).convert())
return img
def blit(self, items, locations):
for i in range (0, len(items)):
self.screen.blit(items[i], locations[i])
def grid(self, size, object):
objects =[]
locations = []
x, y = size
for xT in range (0, int(x / 20)):
for yT in range(0, int(y / 20)):
objects.append(object)
locations.append((xT * 20, yT * 20))
return objects, locations
更好的方法是为每个单元格创建一个精灵类,如果单元格是死的或活的,则在deteermine中添加一个bool,并相应地进行blit 如果你熟悉这里的精灵,一开始可能会让人困惑,但它们将有助于制作更复杂的游戏,这里还有一个指向我的版本的链接
Goodluck如果代码很难阅读,很抱歉。谢谢。你的版本非常有用。当我完成一个链接时,我会对它进行注释。