Python Pygame游戏地图的碰撞
我试图在Pygame中制作一个迷宫游戏,但无法实现阵列中1(迷宫墙)的碰撞。我试图将碰撞检测放在创建地图的循环中,但它不起作用。我还将碰撞检测放在主循环中,但只有左上角的矩形检测到了碰撞,而不是所有的1矩形。我该如何着手解决这个问题?谢谢大家!Python Pygame游戏地图的碰撞,python,pygame,Python,Pygame,我试图在Pygame中制作一个迷宫游戏,但无法实现阵列中1(迷宫墙)的碰撞。我试图将碰撞检测放在创建地图的循环中,但它不起作用。我还将碰撞检测放在主循环中,但只有左上角的矩形检测到了碰撞,而不是所有的1矩形。我该如何着手解决这个问题?谢谢大家! import pygame pygame.init() screen = pygame.display.set_mode((700,700)) pygame.display.set_caption("Game") speed =
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, 0, 255), (0, 0, 50, 50))
global rect_two
surface_one = pygame.Surface((80, 80), pygame.SRCALPHA)
rect_two = pygame.draw.rect(surface_one, (255, 255, 255), (0, 0, 50, 50))
tileX = 0
tileY = 0
global tile_list
tile_list = []
map = [
[1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,1,0,0,0,0,0,1],
[1,0,1,0,0,0,1,0,1,1,1,0,1],
[1,0,0,0,1,1,1,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,0,0,1],
[1,0,1,0,1,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,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, (255,0,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,255))
game_map()
player()
pygame.display.update()
pygame.quit()
移动播放机前保存播放机的位置:
pos=x,y
玩家移动后,计算行
和列
:
row=y//50
列=x//50
如果新位置在墙上,则重置玩家的位置:
如果映射[行][列]==1:
x、 y=位置
此外,您还必须将map
变量移动到全局名称空间。速度
应为磁贴大小的整数分隔符。将起始位置更改为栅格中的某个位置:
速度=25
x=50
y=50
完整代码:
导入pygame
pygame.init()
screen=pygame.display.set_模式((700700))
pygame.display.set_标题(“游戏”)
速度=25
x=50
y=50
地图=[
[1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,1,0,0,0,0,0,1],
[1,0,1,0,0,0,1,0,1,1,1,0,1],
[1,0,0,0,1,1,1,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,0,0,1],
[1,0,1,0,1,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1]
]
def game_map():
全局矩形1
surface=pygame.surface((100100),pygame.SRCALPHA)
rect_one=pygame.draw.rect(曲面,(0,0255)、(0,0,50,50))
全局矩形二
surface_one=pygame.surface((80,80),pygame.SRCALPHA)
rect_two=pygame.draw.rect(surface_one,(255,255,255),(0,0,50,50))
tileX=0
tileY=0
对于y,枚举(映射)中的行:
tileX=0
对于x,枚举(行)中的单元格:
如果单元格==1,则图像=曲面,否则为曲面
blit(图像,[x*50,y*50])
pygame.display.update()
def player():
player=pygame.draw.rect(屏幕,(255,0,0),(x,y,25,25))
循环=真
while循环:
pygame.时间延迟(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
循环=错误
#玩家控制
keys=pygame.key.get_pressed()
pos=x,y
如果键[pygame.K_左]:
x-=速度
如果键[pygame.K_RIGHT]:
x+=速度
如果键[pygame.K_UP]:
y-=速度
如果键[pygame.K_DOWN]:
y+=速度
行=y//50
列=x//50
如果映射[行][列]==1:
x、 y=位置
屏幕填充((255255))
游戏地图()
玩家()
pygame.display.update()
pygame.quit()
移动播放机前保存播放机的位置:
pos=x,y
玩家移动后,计算行
和列
:
row=y//50
列=x//50
如果新位置在墙上,则重置玩家的位置:
如果映射[行][列]==1:
x、 y=位置
此外,您还必须将map
变量移动到全局名称空间。速度
应为磁贴大小的整数分隔符。将起始位置更改为栅格中的某个位置:
速度=25
x=50
y=50
完整代码:
导入pygame
pygame.init()
screen=pygame.display.set_模式((700700))
pygame.display.set_标题(“游戏”)
速度=25
x=50
y=50
地图=[
[1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,1,0,0,0,0,0,1],
[1,0,1,0,0,0,1,0,1,1,1,0,1],
[1,0,0,0,1,1,1,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,0,0,1],
[1,0,1,0,1,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1]
]
def game_map():
全局矩形1
surface=pygame.surface((100100),pygame.SRCALPHA)
rect_one=pygame.draw.rect(曲面,(0,0255)、(0,0,50,50))
全局矩形二
surface_one=pygame.surface((80,80),pygame.SRCALPHA)
rect_two=pygame.draw.rect(surface_one,(255,255,255),(0,0,50,50))
tileX=0
tileY=0
对于y,枚举(映射)中的行:
tileX=0
对于x,枚举(行)中的单元格:
如果单元格==1,则图像=曲面,否则为曲面
blit(图像,[x*50,y*50])
pygame.display.update()
def player():
player=pygame.draw.rect(屏幕,(255,0,0),(x,y,25,25))
循环=真
while循环:
pygame.时间延迟(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
循环=错误
#玩家控制
keys=pygame.key.get_pressed()
pos=x,y
如果键[pygame.K_左]:
x-=速度
如果键[pygame.K_RIGHT]:
x+=速度
如果键[pygame.K_UP]:
y-=速度
如果键[pygame.K_DOWN]:
y+=速度
行=y//50
列=x//50
如果映射[行][列]==1:
x、 y=位置
屏幕填充((255255))
游戏地图()
玩家()
pygame.display.update()
pygame.quit()
您的平铺列表
只包含一个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,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,1,0,0,0,0,0,1],
[1,0,1,0,0,0,1,0,1,1,1,0,1],
[1,0,0,0,1,1,1,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,0,0,1],
[1,0,1,0,1,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,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,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()
您的
tile\u列表
只包含一个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,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,1,0,0,0,0,0,1],
[1,0,1,0,0,0,1,0,1,1,1,0,1],
[1,0,0,0,1,1,1,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,0,0,1],
[1,0,1,0,1,0,0,0,1,1,1,0,1],
[1,0,1,0,1,1,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,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,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()
非常感谢你!“很抱歉再次打扰你,”树懒,如果你不介意我问的话。如何检测矩形与之碰撞的边?例如,如果矩形在右侧碰撞,它将无法再向右移动。非常感谢!“很抱歉再次打扰你,”树懒,如果你不介意我问的话。我怎样才能发现侧面