Python Pygame,玩家与房间中的不同墙壁碰撞
我正在用python编写一个迷宫游戏。我想做的是,当玩家碰到房间里的墙时,它会改变玩家的颜色。我的碰撞部分有问题 这只是代码的一部分,大部分只是迷宫部分 我是一个新的编码和一直试图学习pygame通过在线帮助。我知道的不多,所以什么都会有帮助Python Pygame,玩家与房间中的不同墙壁碰撞,python,pygame,collision,Python,Pygame,Collision,我正在用python编写一个迷宫游戏。我想做的是,当玩家碰到房间里的墙时,它会改变玩家的颜色。我的碰撞部分有问题 这只是代码的一部分,大部分只是迷宫部分 我是一个新的编码和一直试图学习pygame通过在线帮助。我知道的不多,所以什么都会有帮助 import pygame BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) BLUE = ( 0, 0, 255) RED = ( 255, 0, 0) YELLOW =
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)
使用您提供的代码,您只需更改
player
类构造函数中的player
颜色,即:
def __init__(self, x, y):
""" Constructor function """
...
self.image.fill(COLOR)
要更改player
颜色,您必须在碰撞检测部分内执行此操作。i、 e.如果您点击了任何内容,请更改颜色,代码中应显示以下内容:
# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
如果block\u hit\u list
中有东西,我们已经击中了一些东西,我们应该更新player
颜色,就像在构造函数中一样:
self.image.fill(DESIRED_COLOR)