【发布时间】:2016-08-18 15:50:59
【问题描述】:
这是我所有的代码:
import random as random
import pygame as pygame
pygame.init() # initialize
clock = pygame.time.Clock() # framerate limit
Screen = pygame.display.set_mode([1000, 1000]) # Create screen object and Window Size
Done = False
MapSize = 25
TileWidth = 20
TileHeight = 20
TileMargin = 4
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
class MapTile(object):
def __init__(self, Name, xlocation, ylocation):
self.Name = Name
self.xlocation = xlocation
self.ylocation = ylocation
class Character(object):
def __init__(self, Name, HP, Allegiance, xlocation, ylocation):
self.Name = Name
self.HP = HP
self.Allegiance = Allegiance
self.xlocation = xlocation
self.ylocation = ylocation
def Move(self, Direction):
if Direction == "UP":
self.ylocation += 1
elif Direction == "LEFT":
self.xlocation -= 1
elif Direction == "RIGHT":
self.xlocation += 1
elif Direction == "DOWN":
self.ylocation -= 1
self.Location()
def Location(self):
print("Coordinates: " + str(self.xlocation) + ", " + str(self.ylocation))
class Map(object):
Grid = []
global MapSize
for Row in range(MapSize): # Creating grid
Grid.append([])
for Column in range(MapSize):
Grid[Row].append([])
for Row in range(MapSize): #Filling grid with grass
for Column in range(MapSize):
TempTile = MapTile("Grass", Row, Column)
Grid[Row][Column].append(TempTile)
for Row in range(MapSize): #Rocks
for Column in range(MapSize):
TempTile = MapTile("Rock", Row, Column)
if Row == 1:
Grid[Row][Column].append(TempTile)
for i in range(10): #Random trees
RandomRow = random.randint(0, MapSize - 1)
RandomColumn = random.randint(0, MapSize - 1)
TempTile = MapTile("Tree", Row, Column)
Grid[RandomRow][RandomColumn].append(TempTile)
def update(self):
for Row in range(MapSize):
for Column in range(MapSize):
for i in range(len(Map.Grid[Row][Column])):
if Map.Grid[Row][Column][i].xlocation != Column:
print("BOOP")
Map.Grid[Row][Map.Grid[Row][Column][i].xlocation].append(Map.Grid[Row][Column][i])
Map.Grid.remove(Map.Grid[Row][Column][i])
if Map.Grid[Row][Column][i].ylocation != Row:
print("BOOP")
Map.Grid[Row][Map.Grid[Row][Column][i].xlocation].append(Map.Grid[Row][Column][i])
else:
break
RandomRow = random.randint(0, MapSize - 1)
RandomColumn = random.randint(0, MapSize - 1)
Hero = Character("boop", 10, "Friendly", RandomRow, RandomColumn)
Grid[RandomRow][RandomColumn].append(Hero)
Map = Map()
while not Done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
Pos = pygame.mouse.get_pos()
Column = Pos[0] // (TileWidth + TileMargin)
Row = Pos[1] // (TileHeight + TileMargin)
print(str(Row) + ", " + str(Column))
for i in range(len(Map.Grid[Row][Column])):
print(str(Map.Grid[Row][Column][i].Name))
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Map.Hero.Move("LEFT")
if event.key == pygame.K_RIGHT:
Map.Hero.Move("RIGHT")
if event.key == pygame.K_UP:
Map.Hero.Move("UP")
if event.key == pygame.K_DOWN:
Map.Hero.Move("DOWN")
Map.update()
Screen.fill(BLACK)
for Row in range(MapSize): # Drawing grid
for Column in range(MapSize):
Color = WHITE
if len(Map.Grid[Row][Column]) == 2:
Color = RED
for i in range(0, len(Map.Grid[Row][Column])):
if Map.Grid[Row][Column][i].Name == "boop":
Color = GREEN
if Map.Grid[Row][Column][i].Name == "MoveTile":
Color = BLUE
pygame.draw.rect(Screen, Color, [(TileMargin + TileWidth) * Column + TileMargin,
(TileMargin + TileHeight) * Row + TileMargin,
TileWidth,
TileHeight])
clock.tick(60)
pygame.display.flip()
pygame.quit()
以及相关位:
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Map.Hero.Move("LEFT")
if event.key == pygame.K_RIGHT:
Map.Hero.Move("RIGHT")
if event.key == pygame.K_UP:
Map.Hero.Move("UP")
if event.key == pygame.K_DOWN:
Map.Hero.Move("DOWN")
Map.update()
移动功能:
def Move(self, Direction):
if Direction == "UP":
self.ylocation += 1
elif Direction == "LEFT":
self.xlocation -= 1
elif Direction == "RIGHT":
self.xlocation += 1
elif Direction == "DOWN":
self.ylocation -= 1
还有更新函数,问题出在哪里:
def update(self):
for Row in range(MapSize):
for Column in range(MapSize):
for i in range(len(Map.Grid[Row][Column])):
if Map.Grid[Row][Column][i].xlocation != Column:
print("BOOP")
Map.Grid[Row][Map.Grid[Row][Column][i].xlocation].append(Map.Grid[Row][Column][i])
Map.Grid.remove(Map.Grid[Row][Column][i])
if Map.Grid[Row][Column][i].ylocation != Row:
print("BOOP")
Map.Grid[Row][Map.Grid[Row][Column][i].xlocation].append(Map.Grid[Row][Column][i])
else:
break
更新函数的预期行为是检查网格中的每个对象并查看它是否已移动(如果对象的内部坐标与其在网格上的当前位置之间存在差异),并在网格中替换它在适当的位置。它通过在新位置附加对象的新版本并删除旧版本来做到这一点。
我得到的错误是:
Traceback (most recent call last):
File "/Users/kosay.jabre/Desktop/Monster.py", line 136, in <module>
Map.update()
File "/Users/kosay.jabre/Desktop/Monster.py", line 93, in update
Map.Grid.remove(Map.Grid[Row][Column][i])
ValueError: list.remove(x): x not in list
我怎样才能最好地实现预期的行为?
【问题讨论】:
-
您得到的错误是因为
Map.Grid.remove()仅在列表的第一个维度中查找值,该列表仅包含表示第二个维度的其他列表 - 所以它永远找不到对应的 @987654327 @实例引用。您需要遍历每个子列表和try以删除目标(并在/如果这样做时退出循环)。 -
你能帮我看看这样做的语法吗?另外,我不是要删除 MapTile,而是要删除 Hero。我想通过将英雄附加到新位置并删除旧实例来移动英雄。
标签: python arrays python-3.x oop pygame