如何解决Pygame 游戏地图的碰撞
我正在尝试在 Pygame 中制作迷宫游戏,但无法实现阵列中 1(迷宫墙)的碰撞。我试图将碰撞检测放在创建地图的循环中,但它不起作用。我还将碰撞检测放在主循环中,但只有左上角的矩形检测到碰撞,而不是所有 1 个矩形。我将如何解决这个问题?谢谢!
import pygame
pygame.init()
screen = pygame.display.set_mode((700,700))
pygame.display.set_caption("Game")
speed = 20
x = 200
y = 600
def game_map():
global rect_one
surface = pygame.Surface((100,100),pygame.SRCALPHA)
rect_one = pygame.draw.rect(surface,(0,255),50,50))
global rect_two
surface_one = pygame.Surface((80,80),pygame.SRCALPHA)
rect_two = pygame.draw.rect(surface_one,(255,255,50))
tileX = 0
tileY = 0
global tile_list
tile_list = []
map = [
[1,1,1],[1,1]
]
for y,row in enumerate(map):
tileX = 0
for x,cell in enumerate(row):
image = surface if cell == 1 else surface_one
screen.blit(image,[x*50,y*50])
tile_list.append(rect_one)
pygame.display.update()
def player():
player = pygame.draw.rect(screen,0),(x,y,20,20))
for i in tile_list:
if player.colliderect(i):
print("hello")
loop = True
while loop:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
#player controls
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_RIGHT]:
x += speed
if keys[pygame.K_UP]:
y -= speed
if keys[pygame.K_DOWN]:
y += speed
screen.fill((255,255))
game_map()
player()
pygame.display.update()
pygame.quit()
解决方法
您的 tile_list
多次只包含一个 Rect
。
我稍微简化了您的代码,并为地图中的每个 Rect
使用了带有正确坐标的 1
。另请注意评论:
import pygame
pygame.init()
screen = pygame.display.set_mode((700,700))
pygame.display.set_caption("Game")
speed = 10
player_x = 200
player_y = 600
# Use a constant. There's not need to make big Surfaces and then draw a smaller rect on them to create the map.
TILESIZE = 50
tile_list = []
map = [
[1,1,1],[1,1]
]
# let's create a single Surface for the map and reuse that
grid = pygame.Surface((len(map[0]) * TILESIZE,len(map) * TILESIZE),pygame.SRCALPHA)
for y,row in enumerate(map):
for x,cell in enumerate(row):
# if we want a wall,we draw it on the new Surface
# also,we store the Rect in the tile_list so collision detection works
if cell:
rect = pygame.draw.rect(grid,'blue',(x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE))
tile_list.append(rect)
loop = True
clock = pygame.time.Clock()
while loop:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
#player controls
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= speed
if keys[pygame.K_RIGHT]:
player_x += speed
if keys[pygame.K_UP]:
player_y -= speed
if keys[pygame.K_DOWN]:
player_y += speed
screen.fill((255,255,255))
# draw the map surface to the screen
screen.blit(grid,(0,0))
player = pygame.draw.rect(screen,(255,0),(player_x,player_y,20,20))
# now collision detection works because for each 1 in the map
# there's a Rect in tile_list with the correct coordinates
for i in tile_list:
if player.colliderect(i):
print("colliding")
break
else:
print('not colliding')
pygame.display.update()
pygame.quit()
,
在移动之前保存玩家的位置:
pos = x,y
计算玩家移动后的row
和column
:
row = y // 50
column = x // 50
如果新位置在墙上,则重置玩家的位置:
if map[row][column] == 1:
x,y = pos
此外,您必须将 map
变量移动到全局命名空间。 speed
应该是图块大小的整数分隔符。将起始位置更改为网格中的某个位置:
speed = 25
x = 50
y = 50
完整代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((700,700))
pygame.display.set_caption("Game")
speed = 25
x = 50
y = 50
map = [
[1,1]
]
def game_map():
global rect_one
surface = pygame.Surface((100,100),pygame.SRCALPHA)
rect_one = pygame.draw.rect(surface,255),50,50))
global rect_two
surface_one = pygame.Surface((80,80),pygame.SRCALPHA)
rect_two = pygame.draw.rect(surface_one,50))
tileX = 0
tileY = 0
for y,row in enumerate(map):
tileX = 0
for x,cell in enumerate(row):
image = surface if cell == 1 else surface_one
screen.blit(image,[x*50,y*50])
pygame.display.update()
def player():
player = pygame.draw.rect(screen,(x,y,25,25))
loop = True
while loop:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
#player controls
keys = pygame.key.get_pressed()
pos = x,y
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_RIGHT]:
x += speed
if keys[pygame.K_UP]:
y -= speed
if keys[pygame.K_DOWN]:
y += speed
row = y // 50
column = x // 50
if map[row][column] == 1:
x,y = pos
screen.fill((255,255))
game_map()
player()
pygame.display.update()
pygame.quit()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。