Pygame的墙壁

时间:2014-06-19 08:09:13

标签: python pygame collision-detection

我想在Pygame中制作一个简单的吃豆人游戏,但我不知道如何制作墙壁。 我怎样才能检查玩家移动时是否撞墙? 这是main.py的代码:

# Imports
import pygame
import sys
import player
import room

# Colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 255, 0)
green = (0, 0, 255)

# Global variables
S_WIDTH = 800
S_HEIGHT = 800
FPS = 60
# player speed
SPEED = 2
WALLS = pygame.sprite.Group()

# Initialization 
pygame.init()
screen = pygame.display.set_mode([S_WIDTH, S_HEIGHT])
pygame.display.set_caption("Py-Man Alpha")
clock = pygame.time.Clock()

# Add walls to WALLS var.
wall = room.Wall(0, 600, 1000, 30, black, screen)
WALLS.add(wall)

# Main class
class Main(object):
    """
    This is main class of 
    the program. Here, the menu 
    will be showed and player 
    can start game, load, access 
    options or quit.
    """
    def __init__(self, screen):
        self.screen = screen

        self.menu()

    def menu(self):
        # Show menu
        pass

    def load(self):
        # Load game
        pass

    def options(self):
        # Show options
        pass

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

# Game class
class Game(object):
    """
    This is main class of game.
    All game events will happen here.
    """
    Player = player.Player(blue, 50, 50, 0, 0) 
    def __init__(self, screen, running):
        self.screen = screen
        self.running = running

        self.run()

    def run(self):
        while self.running:
            # Events
            for event in pygame.event.get():
                # If user hits 'x'
                if event.type == pygame.QUIT:
                    self.running = False
                # Keyborad events
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        self.Player.direction = 'up'
                    elif event.key == pygame.K_DOWN:
                        self.Player.direction = 'down'
                    elif event.key == pygame.K_LEFT:
                        self.Player.direction = 'left'
                    elif event.key == pygame.K_RIGHT:
                        self.Player.direction = 'right'
                    elif event.key == pygame.K_p:
                        self.Player.direction = 'pause'
            # Clear screen
            self.screen.fill(white)

            #Draw
            self.Player.move(SPEED, WALLS)
            self.Player.draw(self.screen)
            for wall in WALLS:
                wall.draw()

            # Set clock
            clock.tick(60)

            # Update screen
            pygame.display.flip()

        # End of game
        pygame.quit()
        sys.exit()

# Tests
game = Game(screen, True)

这是player.py文件中的播放器类:

#imports
import pygame

class Player(pygame.sprite.Sprite):
    """
    This class represent the player image
    and has the player actions.
    """
    direction = 'right'
    def __init__(self, color, width, height, x, y):
        # Pygame constructor
        pygame.sprite.Sprite.__init__(self)

        # Init. variables
        self.color = color
        self.width = width
        self.height = height
        self.x = x
        self.y = y 

        # Create sprite
        self.image = pygame.Surface([width, height])
        self.image.fill(self.color)
        self.rect = self.image.get_rect()

    def draw(self, screen):
        # Draw player on the screen
        pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height], 0)

    def move(self, speed, walls):
        # Move the player
        if self.direction == 'up':
            self.y -= speed
        elif self.direction == 'down':
            self.y += speed
        elif self.direction == 'left':
            self.x -= speed
        elif self.direction == 'right':  
            self.x += speed

这是room.py文件,它包含Wall类:

import pygame

class Wall(pygame.sprite.Sprite):
    """
    Walls can't be passed by player.
    """
    def __init__(self, x, y, width, height, color, screen):
        # Init.
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.screen = screen
        # Create
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()

    def draw(self):
        pygame.draw.rect(self.screen, self.color, [self.x, self.y, self.width, self.height], 0)

2 个答案:

答案 0 :(得分:2)

在将玩家移动到新位置之前,您需要遍历WALLS中的所有元素并检查玩家的边界框是否与墙相交。 pygame.Rect有许多辅助方法(Rect.collide*

如果是,请拒绝移动。

答案 1 :(得分:1)

您可以使用a.colliderect(b)检查move()中墙壁的碰撞

#Example
def move(self, speed, walls):
    # Move the player
    if playerRect.colliderect(wallRect) == False:
        if self.direction == 'up':
            self.y -= speed
    if playerRect.colliderect(wallRect) == True:
        #Deny the movement

沿着这些方向的东西