因此,我最近开始使用python,现在正在开发一个简单的类似于2D-minecraft的游戏。我试图使用Pygame在Surface上绘制一些图像,但是存储世界上所有块的(2D)阵列总是发生奇怪的事情。我还没有做过任何随机地形生成的工作,因此我正在使用它来制作一个完全平坦的世界:
class World:
def __init__(self, width, height, blockSprites):
self.width = width
self.height = height
self.blockSprites = blockSprites
def generate(self):
temp = [self.air] * self.width
self.map = [temp] * self.height
for y in range(self.height):
for x in range(self.width):
if(y < 50):
self.map[y][x] = self.dirt
这是Block类和Camera类:
class Block:
def __init__(self, blockid, textureX, textureY):
self.textureX = textureX
self.textureY = textureY
self.solid = True
self.id = blockid
from math import floor
class Camera:
def __init__(self, worldWidth, worldHeight):
self.x = 0
self.xB = floor(self.x/64)
self.y = 55*64 #test value for initial camera position
self.yB = floor(self.y/64)
self.zoom = 1
self.baseWidth = 960
self.baseWidthB = 15 #in blocks (1block = 64px)
self.baseHeight = 640
self.baseHeightB = 10
这是World.draw函数,每帧都会被调用:
def draw(self, canvas, camera):
#canvas is the pygame surface which I draw on
canvas.fill(pygame.Color(143, 218, 239))
for y in range(camera.baseHeightB):
for x in range(camera.baseWidthB):
#id=0 means"air"
if(not self.map[camera.yB + y][camera.xB + x].id == 0):
canvas.blit(self.blockSprites, (x*64, camera.baseHeight - (y+1)*64), (self.map[camera.yB + y][camera.xB + x].textureX, self.map[camera. yB + y][camera.xB + x].textureY, 64, 64))
return canvas
问题是当我调用world.draw()时world.map不是一半的灰尘块和一半的空气块...这都是灰尘块...我尝试添加
else:
self.map[y][x] = self.air
到世界的尽头。生成,一切都变成了空气障碍...
有人可以告诉我为什么吗?
最佳答案
假设您有一个Item
类和o
的2个实例Item
:
class Item:
def __init__(self, c):
self.c = c
o = Item('o')
w, h = 3, 3
输出
for r in map: print([c.c for c in r])
是
['o', 'o', 'o']
['o', 'o', 'o']
['o', 'o', 'o']
语句
temp = [o] * w
创建对相同项目o
的引用的列表,语句map = [temp] * h
创建对相同列表对象temp
的引用的列表。temp = [o] * w
map = [temp] * h
最后,
map
的每个元素都引用一个唯一的列表temp
,而temp
的每个元素都引用一个唯一的对象o
。如果地图的一个内部元素的内容发生了变化,那么所有对象似乎都会发生变化。原因是只有一个对象
o
并且map中的所有元素都指向同一个对象。
因此输出:
map[1][1].c = '_'
for r in map: print([c.c for c in r])
是
['_', '_', '_']
['_', '_', '_']
['_', '_', '_']
如果创建了
x
的Item
的新实例,并且更改了map
的单个元素x = Item('x')
map[1][1] = x
for r in map: print([c.c for c in r])
然后,所有行上的项目似乎都发生了变化。注意,每个元素都引用相同的列表
temp
。没有多行,只有一个行对象temp
。如果
temp
的元素被更改,则这似乎是每一行的更改:['_', 'x', '_']
['_', 'x', '_']
['_', 'x', '_']
您必须创建一个地图,其中地图的每个元素都是
Item
的单独实例:map = [[Item('o') for i in range(w)] for j in range(h)]
注意,
Item('o')
创建一个Item
的新实例。现在,可以更改元素的内容,或者可以将新对象分配给内部元素,因为每一行中的每个对象都有一个单独的对象:
map[1][1].c = 'X'
map[1][0] = Item('x')
for r in map: print([c.c for c in r])
['o', 'o', 'o']
['x', 'X', 'o']
['o', 'o', 'o']