如何解决Pygame一直在打印碰撞,即使直肠未触及
我的代码应该在每次玩家rect碰到门rect时都会打印冲突,但是由于某种原因,它一直在打印。我正在使用库:pygame,glob,sys,pygame locals。大多数代码都来自这里:https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125
代码:
map2 = """wwwwwwd wwwww
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w p
w w
w w
w w
w w
w w
w w
w w
wwwwwwpwwwwwwp"""
pygame.display.set_icon(pygame.image.load("C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/favicon.ico"))
pygame.display.set_caption("Knock Knight")
screen = pygame.display.set_mode((226,318))
moving_right = False
moving_left = False
moving_up = False
moving_down = False
player_location = [50,50]#remember its a fucking list
door_list = []
#-----------------------------
floor_tile = pygame.image.load("C:/Users/cuerv/Downloads/floor.png").convert()
floor_rect = floor_tile.get_rect(center=(100,250))
door = pygame.image.load("C:/Users/cuerv/Downloads/Door.png").convert()#if you dont convert it colorkey wont work
door_rect = door.get_rect(center=(100,250))
door.set_colorkey((255,255,255))
wall = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png").convert()
wall_rect = wall.get_rect(center=(100,256))
player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png").convert()
player_rect = player.get_rect(center=(100,256))
player.set_colorkey((255,255))
enemy = pygame.image.load("C:/Users/cuerv/Downloads/Enemy.png").convert()
enemy_rect = enemy.get_rect(center=(100,250))
enemy.set_colorkey((255,255))
def check_collision(door,player):
for player in door:
#for pipr in pipes = checks forall the rects inside pipe list
if player_rect.colliderect(door_rect):
#colliderect = checks for collision
print('collision')
def init_display():
global screen,wall,door,player,enemy,floor_tile
def tiles(map2):
global wall,floor_tile
door_list.clear()
for y,line in enumerate(map2):
#counts lines
for x,c in enumerate(line):
#counts caracters
if c == "p":
player_rect = screen.blit(player,player_location)
if c == "w":
#caracter is w
screen.blit(wall,(x * 16.18,y * 15))
if c == "d":
rect = screen.blit(door,(x * 16.2,y * 15))
door_list.append(rect)
if c == "e":
screen.blit(enemy,(x * 16,y * 15))
if c == "f":
screen.blit(floor_tile,y * 15))
map2 = map2.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
screen.fill((0,0))
tiles(map2)
if moving_right == True:
player_location[0] += 4
if moving_left == True:
player_location[0] -= 4
if moving_up == True:
player_location[1] -=4
if moving_down == True:
player_location[1] +=4
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
if event.key == K_LEFT:
moving_left = True
if event.key == K_UP:
moving_up = True
if event.key == K_DOWN:
moving_down = True
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_right = False
if event.key == K_LEFT:
moving_left = False
if event.key == K_UP:
moving_up = False
if event.key == K_DOWN:
moving_down = False
check_collision(door_rect,player_rect)
pygame.display.update()
clock.tick(60)
解决方法
您在功能global player_rect
中错过了tiles
:
def tiles(map2):
global wall,door,player,enemy,floor_tile,player_rect
door_list.clear()
for y,line in enumerate(map2):
#counts lines
for x,c in enumerate(line):
#counts caracters
if c == "p":
player_rect = screen.blit(player,player_location)
if c == "w":
#caracter is w
screen.blit(wall,(x * 16.18,y * 15))
if c == "d":
rect = screen.blit(door,(x * 16.2,y * 15))
door_list.append(rect)
if c == "e":
screen.blit(enemy,(x * 16,y * 15))
if c == "f":
screen.blit(floor_tile,y * 15))
要更改全局名称空间中的变量,必须使用global
statement。查看上一个问题的答案:Why are my rects not working and making the player interact with the door as it is supposed to in pygame
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。