Pygame,玩家与房间中不同墙壁的碰撞

时间:2018-07-20 04:12:28

标签: python pygame collision

我正在用python开发迷宫游戏。我想做的是,只要播放器碰到房间内的墙壁,它就会改变播放器的颜色。我在碰撞部分遇到麻烦。

这只是代码的一部分,大部分只是迷宫的一部分。

我是编码的新手,一直在尝试通过在线帮助学习pygame。我不太了解,所以有什么用。

import pygame

BLACK  = (   0,   0,   0)
WHITE  = ( 255, 255, 255)
BLUE   = (   0,   0, 255)
RED    = ( 255,   0,   0)
YELLOW = ( 255, 255,   0)
GREEN  = (   0, 255,   0)
ORANGE = ( 255, 165,   0)
PURPLE = ( 255,   0, 255)
SOMETHING = (200, 100, 50)

player_color = 0

if player_color == 0:
    COLOR = WHITE
elif player_color == 1:
    COLOR = BLUE
elif player_color == 2:
    COLOR = RED
elif player_color == 3:
    COLOR = YELLOW
elif player_color == 4:
    COLOR = PURPLE
elif player_color == 5:
    COLOR = GREEN
elif player_color == 6:
    COLOR = ORANGE
elif player_color == 7:
    COLOR = BLACK

class Wall(pygame.sprite.Sprite):
    """This class represents the bar at the bottom that the player controls """

    def __init__(self, x, y, width, height, color):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()

        # Make a BLUE wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x


class Player(pygame.sprite.Sprite):
    """ This class represents the bar at the bottom that the player controls """

    # Set speed vector
    change_x = 0
    change_y = 0

# Set player color


    def __init__(self, x, y):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()



        # Set height, width
        self.image = pygame.Surface([15, 15])
        self.image.fill(COLOR)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

    def changespeed(self, x, y):
        """ Change the speed of the player. Called with a keypress. """
        self.change_x += x
        self.change_y += y

    def move(self, walls):
        """ Find a new position for the player """

        # Move left/right
        self.rect.x += self.change_x

        # Did this update cause us to hit a wall?
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            # If we are moving right, set our right side to the left side of
            # the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move up/down
        self.rect.y += self.change_y

        # Check and see if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:

            # Reset our position based on the top/bottom of the object.
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

class Room(object):
    """ Base class for all rooms. """

    """ Each room has a list of walls, and of enemy sprites. """
    wall_list = None
    enemy_sprites = None

    def __init__(self):
        """ Constructor, create our lists. """
        self.wall_list = pygame.sprite.Group()
        self.enemy_sprites = pygame.sprite.Group()

class Room1(Room):
    """This creates all the walls in room 1"""
    def __init__(self):
        Room.__init__(self)
        # Make the walls. (x_pos, y_pos, width, height)

        # This is a list of walls. Each is in the form [x, y, width, height]
        walls = [# Border
                 [0, 0, 20, 250, BLACK],
                 [0, 350, 20, 250, BLACK],
                 [780, 0, 20, 250, BLACK],
                 [780, 350, 20, 250, BLACK],
                 [20, 0, 760, 20, BLACK],
                 [20, 580, 760, 20, BLACK],

                 # Wall
                 [390, 50, 20, 500, WHITE],

                 # Room Number
                 [734, 40, 8, 4, BLACK],
                 [730, 44, 12, 4, BLACK],
                 [726, 48, 16, 4, BLACK],
                 [734, 52, 8, 28, BLACK],
                 [726, 80, 24, 8, BLACK]
                ]

        # Loop through the list. Create the wall, add it to the list
        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)

1 个答案:

答案 0 :(得分:1)

使用您提供的代码,您只需在player类构造函数中更改Player的颜色,即:

def __init__(self, x, y):
    """ Constructor function """
    ...
    self.image.fill(COLOR)

要更改player的颜色,您必须在碰撞检测部分内执行此操作。也就是说,如果您碰到任何东西,请更改颜色,在代码中应该在此处:

# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, walls, False)

如果block_hit_list里面有东西,我们已经碰到了东西,我们应该更新player颜色,就像在构造函数中一样:

 self.image.fill(DESIRED_COLOR)