【发布时间】:2020-08-25 09:47:40
【问题描述】:
我正在编写一个炸弹人游戏,现在我没有任何精灵,但我使用矩形。 这是我的代码:
import pygame
pygame.init()
WinWidth = 800
WinHeight = 608
p1x = 32
p1y = 32
vel = 1
clock = pygame.time.Clock()
win = pygame.display.set_mode((WinWidth, WinHeight))
def player():
pygame.draw.rect(win, (0, 200, 200), (p1x, p1y, 32, 32))
player_rect = pygame.Rect(p1x, p1y, tlw, tlh)
class Wall:
def __init__(self, x, y):
self.x = x
self.y = y
pygame.draw.rect(win, (50, 50, 50), (x, y, 32, 32))
class Breakable:
def __init__(self, x, y):
self.x = x
self.y = y
pygame.draw.rect(win, (200, 150, 100), (x, y, 32, 32))
game_map1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 10, 10, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 10, 10, 0],
[0, 10, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2, 0, 10, 0],
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 2, 2, 2, 2, 10, 10, 2, 2, 10, 2, 2, 10, 10, 2, 2, 2, 2, 0],
[0, 2, 0, 2, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 2, 0, 2, 0],
[0, 2, 2, 2, 2, 2, 0, 2, 2, 10, 2, 2, 0, 2, 2, 2, 2, 2, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 10, 10, 10, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 0, 0, 2, 2, 10, 10, 10, 10, 10, 10, 10, 10, 10, 2, 2, 0, 0, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 10, 10, 10, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 2, 2, 2, 2, 2, 0, 2, 2, 10, 2, 2, 0, 2, 2, 2, 2, 2, 0],
[0, 2, 0, 2, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 2, 0, 2, 0],
[0, 2, 2, 2, 2, 10, 10, 2, 2, 10, 2, 2, 10, 10, 2, 2, 2, 2, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0],
[0, 10, 0, 2, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2, 0, 10, 0],
[0, 10, 10, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 10, 10, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
run = True
while run:
pygame.init()
clock.tick(100)
win.fill((0, 255, 200))
y = 0
for layer in game_map1:
x = 0
for tile in layer:
if tile == 0:
Wall(x * 32, y * 32)
wall_rect = pygame.Rect(x * 32, y * 32, tlw, tlh)
if tile == 10:
pygame.Rect(x * 32, y * 32, 32, 32)
if tile == 2:
Breakable(x * 32, y * 32)
x += 1
y += 1
player()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
if player_rect.colliderect(wall_rect):
p1x *= -1
p1y *= -1
else:
p1x += vel
elif event.key == pygame.K_a:
if player_rect.colliderect(wall_rect):
p1x *= -1
p1y *= -1
else:
p1x -= vel
if event.key == pygame.K_s:
if player_rect.colliderect(wall_rect):
p1x *= -1
p1y *= -1
else:
p1y += vel
elif event.key == pygame.K_w:
if player_rect.colliderect(wall_rect):
p1x *= -1
p1y *= -1
else:
p1y -= vel
pygame.display.update()
x 和 y 位于播放器矩形的左上角 - 它们是 p1x 和 p1y。
宽度和高度分别为tlw 和tlh。
The Wall 和 Breakable 也在使用 tlw 和 tlh,而且它们没有玩家拥有的特定坐标。
sooo... 我看了一个教程 (https://www.youtube.com/watch?v=HCWI2f7tQnY&t=2s),甚至尝试自己进行碰撞检测。但我就是做不到。我没有瓷砖地图中矩形的坐标,瓷砖地图中也有很多矩形。你怎么能用一个没有坐标和很多矩形的tilemap进行碰撞检测?是的,游戏目前没有太多内容......我用pygame.Rect的player_rect和wall_rect尝试了它,然后使用(colliderect),但它没有用,你怎么能做如果 player_rect 与所有 wall_rect 碰撞,而不仅仅是一个。
我的问题是:如何使用 colliderect 对 tilemaps 进行碰撞检测?
【问题讨论】:
标签: python pygame coordinates collision-detection