Pygame的墙 [英] Walls in Pygame

查看:74
本文介绍了Pygame的墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Pygame中制作一个简单的吃豆人游戏,但我不知道如何制作围墙.如何检查玩家移动时是否撞到墙壁?这是main.py的代码:

I am trying to make a simple Pac-Man game in Pygame, but I don't know how to make walls. How I can make check if player hit a wall when it moves? This is the code of 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文件中的播放器类:

This is the player class in player.py file:

#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类:

And here is the room.py file, this contain the Wall class:

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)

推荐答案

在将播放器移动到新位置之前,您需要遍历 WALLS 中的所有元素,并检查是否将玩家将与墙相交. pygame.Rect 为此提供了许多帮助方法( Rect.collide * )

Before you move the player to a new position, you need to iterate over all elements in WALLS and check whether the bounding box of the player would intersect with the wall. pygame.Rect has a number of helper methods for this (Rect.collide*)

如果这样做,则拒绝此举.

If it does, then reject the move.

这篇关于Pygame的墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆