Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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 为什么我的rect不起作用,让玩家和门互动,就像pygame中应该的那样_Python_Python 3.x_Pygame_Rect - Fatal编程技术网

Python 为什么我的rect不起作用,让玩家和门互动,就像pygame中应该的那样

Python 为什么我的rect不起作用,让玩家和门互动,就像pygame中应该的那样,python,python-3.x,pygame,rect,Python,Python 3.x,Pygame,Rect,我试图让玩家精灵在触门时动作,但它不起作用。我试图让它打印单词冲突,但它没有出现。大多数代码来自: 代码: player不是pygame.Rect对象,而是player\u Rect对象。使用player\u rect代替player进行碰撞测试: def检查碰撞(车门): 对于门内管道: #对于管道中的pipr=检查管道列表中的所有矩形 如果player#rect.collide rect(管道):#player不是pygame.rect对象,而是player#rect对象。使用player\

我试图让玩家精灵在触门时动作,但它不起作用。我试图让它打印单词冲突,但它没有出现。大多数代码来自:

代码:


player
不是
pygame.Rect
对象,而是
player\u Rect
对象。使用
player\u rect
代替
player
进行碰撞测试:

def检查碰撞(车门):
对于门内管道:
#对于管道中的pipr=检查管道列表中的所有矩形

如果player#rect.collide rect(管道):#
player
不是
pygame.rect
对象,而是
player#rect
对象。使用
player\u rect
代替
player
进行碰撞测试:

def检查碰撞(车门):
对于门内管道:
#对于管道中的pipr=检查管道列表中的所有矩形

如果player\u-rect.collide-rect(pipe):#
player.collide-rect(pipe)
返回错误值,或者
门中管道的
迭代次数为零,或者不调用
检查碰撞。在调试这个代码时,你已经排除了哪些选项?问题得到解决了吗?请阅读并考虑到你发现最有用的。在调试这个代码时,你已经排除了哪些选项?问题得到解决了吗?请阅读并考虑到你发现最有用的东西。
import pygame
import sys
import glob
map1 = """wwwwwwwwwwwwwwwwwwwwwwwwwwwww
w                           w
w                           w
w                           w
w                           w
w                           
w                           d
w            p               
w                           
w                           w
w                           w
w                           w
w                           w
w                           w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""

map2 = """wwwwwwwwwwwwwwdwwwwwwwwwwwwww
w                           w
w                           w
w                           w
w                           w
w       w            w      w
w                           w
p             e             w
w                           w
w                           w
w       w            w      w
w                           w
w                           w
w                           w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""









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((480, 250))
moving_right = False
moving_left = False
moving_up = False
moving_down = False
player_location = [50,50]
door_list = []

#-----------------------------

door = pygame.image.load("C:/Users/cuerv/Downloads/Door.png").convert()
door_rect = door.get_rect(center=(100, 250))
door.set_colorkey((255, 255, 255))

tile = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png").convert()
tile_rect = tile.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):
    for pipe in door:
        #for pipr in pipes = checks forall the rects inside pipe list
        if player.colliderect(pipe):
            #colliderect = checks for collision
            print('collision')








def init_display():
    global screen, tile, door, player, enemy


def tiles(map1):
    global tile, door, player, enemy
    for y, line in enumerate(map1):
        #counts lines
        for x, c in enumerate(line):
            #counts caracters
            if c == "w":
                #caracter is w
                screen.blit(tile, (x * 16.18, y * 15))
            if c == "d":
                screen.blit(door, (x * 16.2, y * 15))
            if c == "p":
                screen.blit(player, player_location)
            if c == "e":
                screen.blit(enemy, (x * 16, y * 15))


map1 = map1.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
    screen.fill((0,0,0))
    tiles(map1)

    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_list)


    pygame.display.update()
    clock.tick(60)