【问题标题】:How would I detect collision in tile-based movement?我将如何检测基于瓷砖的运动中的碰撞?
【发布时间】:2018-11-18 04:11:14
【问题描述】:

我最近开始使用 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()

有什么想法吗?

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    你有地图,你可以将角色位置存储为x, y坐标,并检查你要移动到的图块是否是指定的类型:

    例如:

    # 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

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 2013-01-26
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多