好的,所以我正在用 pygame 制作一个小游戏,并通过将图块生成到多维数组来构建地图。为了做到这一点,我使用了两个 for 循环。
def create(this, t):
if t == "grasslands":
for j in range(0, this.numRows):
for i in range(0, this.numColumns):
this.column.append(this.Tile("grass", j * this.tileWidth, i * this.tileHeight))
this.row.append(this.column)
j * this.tileWidth的值被正确地传递到 Tile 初始化中。尽管column[whatever].x值仍然为0。y值设置得很好,如果我使用i或任何其他值而不是j ,事情就很好了。这是我做错了什么还是 Python 有问题?
地图生成文件
import pygame
from sprite import *
from assets import *
class mapG:
def __init__(this, resw, resh):
this.numRows = 3
this.numcolumns = 3
this.tileWidth = 128
this.tileHeight = 128
this.row = []
this.column = []
this.width = this.numRows * this.tileWidth
this.height = this.numcolumns * this.tileHeight
def create(this, t):
if t == "grasslands":
for j in range(0, this.numRows):
for i in range(0, this.numcolumns):
this.column.append(this.Tile("grass", j * this.tileWidth, i * this.tileHeight))
this.row.append(this.column)
def tileAt(this, x, y):
pass
def moveRight(this):
for j in range(0,this.numRows):
for i in range(0, this.numcolumns):
this.row[j][i].incX(1)
def Update(this, src):
for j in range(0,this.numRows):
for i in range(0, this.numcolumns):
this.row[j][i].Update(src)
print(this.row[j][i].y, this.row[j][i].x)
class Tile:
def __init__(this, name, xpos, ypos):
this.y = ypos
this.x = xpos
this.image = assets.tile[name + ".png"]
this.sprite = sprite(this.image, this.x, this.y, 100, 100)
def incX(this, amount):
this.sprite.IncX(amount)
def decX(this, amount):
this.sprite.DecX(amount)
def incY(this, amount):
this.sprite.IncY(amount)
def decY(this, amount):
this.sprite.DecY(amount)
def Update(this, src = None):
if src != None:
this.sprite.Update(src)
精灵.py
import pygame
import assets
class sprite:
def __init__(this, image, xpos, ypos, width = None, height = None):
this.image = image
this.x = xpos
this.y = ypos
this.width = width
this.height = height
if this.width != None and this.height != None:
this.image = pygame.transform.scale(image, (this.width,this.height))
def GetPos(this):
return (this.x, this.y)
def IncX(this, amount):
this.x += amount
def IncY(this, amount):
this.y += amount
def DecX(this, amount):
this.x -= amount
def DecY(this, amount):
this.y -= amount
def Update(this, src = None):
if src != None:
src.blit(this.image, this.GetPos())