如何检测基于图块的运动中的碰撞?

时间:2018-11-18 04:11:15

标签: python pygame

我最近开始使用Python和Pygame模块进行编程。我正在尝试制作一个让玩家逐块移动的游戏,但是我不知道从哪里开始进行碰撞检测。

例如,玩家应该能够在草丛中移动,但不能入水。这是我到目前为止的内容:

import pygame as pg, sys

# Frames
clock = pg.time.Clock()

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Tile declarations
GRASS = 0
WATER = 1

# Tile colors
colors = {
    GRASS: GREEN,
    WATER: BLUE
}

# Tile dimensions
tileWidth = 50
tileHeight = 50

# Map
tileSet = [
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, WATER, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, WATER, WATER, WATER, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, WATER, WATER, WATER, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, WATER, WATER, WATER, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, WATER, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS]
]

# Map dimensions
mapWidth = 12
mapHeight = 9

# Screen dimensions
screenWidth = tileWidth * mapWidth
screenHeight = tileHeight * mapHeight

# Set up display
pg.init()
win = pg.display.set_mode((screenWidth, screenHeight))


class Player(pg.sprite.Sprite):

    def __init__(self, x, y, color):
        super().__init__()
        self.image = pg.Surface([tileWidth, tileHeight])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y


def map_gen():
    for row in range(mapHeight):
        for elementInRow in range(mapWidth):
            pg.draw.rect(win, colors[tileSet[row][elementInRow]], [elementInRow * tileWidth, row * tileHeight, tileWidth, tileHeight])


def main():
    sprite_list = pg.sprite.Group()
    player = Player(0, 0, RED)
    sprite_list.add(player)
    # Game loop variable
    running = True
    while running:
        # Inputs
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_w:
                    player.rect.y -= tileHeight
                if event.key == pg.K_a:
                    player.rect.x -= tileWidth
                if event.key == pg.K_s:
                    player.rect.y += tileHeight
                if event.key == pg.K_d:
                    player.rect.x += tileWidth
        # Map
        map_gen()
        sprite_list.update()
        sprite_list.draw(win)
        pg.display.flip()
        clock.tick(60)


if __name__ == "__main__":
    main()

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您有地图,可以将字符位置存储为x, y坐标,并检查要移动到的图块是否具有指定的tiype:

例如:

# Map
tileSet = [
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, WATER, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, WATER, WATER, WATER, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, WATER, WATER, WATER, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, WATER, WATER, WATER, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, WATER, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS],
    [GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS]]

player_pos = [5, 5]

...
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_w:
                    if tileSet[player_pos[1]+1][player_pos[0]] != WATER:
                        player_pos[1] += 1
                        player.rect.y -= tileHeight
...

注意coordinates system