Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 即使矩形不接触,Pygame也在不断打印碰撞_Python_Python 3.x_Pygame - Fatal编程技术网

Python 即使矩形不接触,Pygame也在不断打印碰撞

Python 即使矩形不接触,Pygame也在不断打印碰撞,python,python-3.x,pygame,Python,Python 3.x,Pygame,我的代码应该在每次玩家矩形接触门矩形时打印碰撞,但由于某些原因,它一直在打印。我正在使用库:pygame、glob、sys、pygame locals。大部分代码都来自这里: 代码: 在函数tiles中,您错过了global player\u rect: def磁贴(map2): 全球墙,门,玩家,敌人,地砖,玩家 门列表。清除() 对于y,枚举(map2)中的行: #数行 对于枚举中的x,c(第行): #计数字符 如果c==“p”: player\u rect=screen.blit(play

我的代码应该在每次玩家矩形接触门矩形时打印碰撞,但由于某些原因,它一直在打印。我正在使用库:pygame、glob、sys、pygame locals。大部分代码都来自这里:

代码:


在函数
tiles
中,您错过了
global player\u rect

def磁贴(map2):
全球墙,门,玩家,敌人,地砖,玩家
门列表。清除()
对于y,枚举(map2)中的行:
#数行
对于枚举中的x,c(第行):
#计数字符
如果c==“p”:
player\u rect=screen.blit(player,player\u位置)
如果c==“w”:
#字符是w
屏风板(墙壁,(x*16.18,y*15))
如果c==“d”:
rect=屏蔽板(门,(x*16.2,y*15))
门列表。附加(rect)
如果c==“e”:
屏幕光点(敌人,(x*16,y*15))
如果c==“f”:
屏风板(地板砖,(x*16,y*15))

如果要更改全局命名空间中的变量,则必须使用。看看你以前的问题的答案:

问题是解决了吗?请阅读并考虑到你发现最有用的。
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, 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, 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, door, player, enemy, 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, (x * 16, y * 15))



map2 = map2.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
    screen.fill((0,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)