如何使对象在按键上移动对象数组?

时间:2016-08-18 15:50:59

标签: python arrays python-3.x oop pygame

以下是我的所有代码:

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

我如何才能达到预期的行为?

1 个答案:

答案 0 :(得分:1)

感谢您取消删除您的问题,以便发布回答。对不起,我花了这么长时间才回复你 - 希望现在还不晚,无法提供帮助。

以下是您问题中代码的重新修订(但正在工作)版本。让我失望的一个原因是你没有遵循PEP 8 - Style Guide for Python Code建议,这使得你的代码版本难以让我阅读和理解。一般缺乏解释发生了什么的评论,特别是在某些部分,并没有使它变得更容易。

我在Map.update()方法中实现的内容与我在评论中描述的有关如何修复ValueError问题的内容有些不同。

import pygame as pygame
import random as random
import sys

GRASS_NAME = "Grass"
HERO_NAME = "Hero"
MOVETILE_NAME = "MoveTile"
ROCK_NAME = "Rock"
TREE_NAME = "Tree"
MAP_SIZE = 25
TILE_WIDTH = 20
TILE_HEIGHT = 20
TILE_MARGIN = 4
TILE_SIZE = TILE_WIDTH+TILE_MARGIN
BLACK = 0, 0, 0
WHITE = 255, 255, 255
GREEN = 0, 255, 0
RED = 255, 0, 0
BLUE = 0, 0, 255
YELLOW = 255, 255, 0
BROWN = 128, 128, 0
UP, DOWN, LEFT, RIGHT = 0, 1, 2, 3
MOVEMENTS = {UP: (-1, 0), DOWN: (1, 0), LEFT: (0, -1,), RIGHT: (0, 1)}
MOVEMENT_KEYS = {pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN}

def quit_game():
    pygame.quit()
    sys.exit()

class MapTile(object):
    def __init__(self, name, xloc, yloc):
        self.name = name
        self.xloc = xloc
        self.yloc = yloc

class Character(object):
    def __init__(self, name, hp, allegiance, xloc, yloc):
        self.name = name
        self.hp = hp
        self.allegiance = allegiance
        self.xloc = xloc
        self.yloc = yloc

    def move(self, direction):
        dy, dx = MOVEMENTS[direction]
        self.yloc += dy
        self.xloc += dx

        # Enforce boundaries
        if self.xloc < 0:
            self.xloc = 0
        elif self.xloc > MAP_SIZE-1:
            self.xloc = MAP_SIZE-1

        if self.yloc < 0:
            self.yloc = 0
        elif self.yloc > MAP_SIZE-1:
            self.yloc = MAP_SIZE-1

        self.location()

    def location(self):
        print("{} coordinates: ({}, {})".format(self.name, self.xloc, self.yloc))

class Map(object):
    # Initialize grid
    grid = [[[] for column in range(MAP_SIZE)] for row in range(MAP_SIZE)]

    for row in range(MAP_SIZE):     # Completely fill grid with grass
        for column in range(MAP_SIZE):
            grid[row][column].append(MapTile(GRASS_NAME, column, row))

    # Add row of rocks near top
    row = 1
    for column in range(MAP_SIZE):
        grid[row][column].append(MapTile(ROCK_NAME, column, row))

    for _ in range(10):  # Add some random trees
        row, column = random.randint(0, MAP_SIZE-1), random.randint(0, MAP_SIZE-1)
        grid[row][column].append(MapTile(TREE_NAME, column, row))

    # Put hero character in random location
    row, column = random.randint(0, MAP_SIZE-1), random.randint(0, MAP_SIZE-1)
    hero = Character(HERO_NAME, 10, "Friendly", column, row)
    grid[row][column].append(hero)

    del row, column  # clean up class context

    def update(self):
        """Check every object in the grid and see if it has moved (if there is a
        discrepancy between the object's internal coordinates and its current
        position on the grid), and if so put it in its proper place by deleting
        it from its old list and appending it to the one for the proper location.
        """
        for row in range(MAP_SIZE):
            for column in range(MAP_SIZE):
                for i, object in enumerate(map.grid[row][column]):
                    if object.xloc != column or object.yloc != row: # wrong spot?
                        #print("BOOP")
                        map.grid[row][column].pop(i)
                        map.grid[object.yloc][object.xloc].append(object)

pygame.init()
clock = pygame.time.Clock()  # to limit framerate
screen = pygame.display.set_mode([1000, 1000])
map = Map()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit_game()
        elif(event.type == pygame.MOUSEBUTTONDOWN
             and pygame.mouse.get_pressed()[0]):  # button1 pressed?
            xpos, ypos = pygame.mouse.get_pos()
            column, row = xpos // TILE_SIZE, ypos // TILE_SIZE
            if 0 <= column < MAP_SIZE and 0 <= row < MAP_SIZE:  # on map?
                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 in MOVEMENT_KEYS:
                if event.key == pygame.K_LEFT:
                    map.hero.move(LEFT)
                elif event.key == pygame.K_RIGHT:
                    map.hero.move(RIGHT)
                elif event.key == pygame.K_UP:
                    map.hero.move(UP)
                else:
                    map.hero.move(DOWN)
                map.update()
            elif event.key == pygame.K_ESCAPE:
                quit_game()

    screen.fill(BLACK)

    # Draw grid
    for row in range(MAP_SIZE):
        for column in range(MAP_SIZE):
            # Determine color based on objects in this spot
            names = {object.name for object in map.grid[row][column]}
            color = WHITE  # default color (Grass)
            if HERO_NAME in names:
                color = YELLOW
            elif MOVETILE_NAME in names:
                color = BLUE
            elif TREE_NAME in names:
                color = GREEN
            elif ROCK_NAME in names:
                color = RED

            pygame.draw.rect(screen, color,
                             [TILE_SIZE*column + TILE_MARGIN,
                              TILE_SIZE*row + TILE_MARGIN,
                              TILE_WIDTH,
                              TILE_HEIGHT])

    clock.tick(60)
    pygame.display.flip()

pygame.quit()