不起作用并使玩家与门互动

不起作用并使玩家与门互动

本文介绍了为什么我的 rect 不起作用并使玩家与门互动,因为它应该在 pygame 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让玩家的精灵在他接触门时采取行动,但它不起作用.我试图让它打印单词碰撞但它没有出现.大部分代码来自:https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=12653598144531259926553125955312553120p

代码:

导入pygame导入系统导入全局map1 = """wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww瓦天湿瓦wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwmap2 = """wwwwwwwwwwwwwwdwwwwwwwwwwwwwwwwwwwwwww w w wwwpewwwwww w w wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwpygame.display.set_icon(pygame.image.load(C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/favicon.ico"))pygame.display.set_caption(敲骑士")屏幕 = pygame.display.set_mode((480, 250))move_right = 错误移动左 = 假move_up = 错误move_down = 错误player_location = [50,50]门列表 = []#----------------------------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))敌人 = pygame.image.load("C:/Users/cuerv/Downloads/Enemy.png").convert()敌人_rect =敌人.get_rect(中心=(100, 250))敌人.set_colorkey((255, 255, 255))def check_collision(门):门内管道:#for pipr in pipe = 检查管道列表中的所有矩形如果 player.colliderect(pipe):#colliderect = 检查碰撞打印('碰撞')定义 init_display():全局屏幕,瓷砖,门,玩家,敌人定义瓷砖(地图1):全局磁贴、门、玩家、敌人对于 y,enumerate(map1) 中的行:#counts 行对于 x, c 在 enumerate(line):#计数字符如果 c == w":#角色是wscreen.blit(tile, (x * 16.18, y * 15))如果 c == d":screen.blit(门,(x * 16.2,y * 15))如果 c == p":screen.blit(播放器,播放器位置)如果 c == e":screen.blit(敌人,(x * 16,y * 15))map1 = map1.splitlines()pygame.init()init_display()时钟 = pygame.time.Clock()为真:screen.fill((0,0,0))瓷砖(地图1)如果moving_right == True:玩家位置[0] += 4如果moving_left == True:player_location[0] -= 4如果moving_up == True:player_location[1] -=4如果moving_down == True:player_location[1] +=4对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:系统退出()如果 event.type == KEYDOWN:如果 event.key == K_RIGHT:move_right = 真如果 event.key == K_LEFT:move_left = 真如果 event.key == K_UP:move_up = 真如果 event.key == K_DOWN:move_down = 真如果 event.type == KEYUP:如果 event.key == K_RIGHT:move_right = 错误如果 event.key == K_LEFT:移动左 = 假如果 event.key == K_UP:move_up = 错误如果 event.key == K_DOWN:move_down = 错误check_collision (door_list)pygame.display.update()时钟滴答(60)

player 不是 pygame.Rect 对象,但 player_rect 是.使用 player_rect 而不是 player 进行碰撞测试:

def check_collision(doors):用于门内管道:#for pipr in pipe = 检查管道列表中的所有矩形如果 player_rect.colliderect(pipe): # <---#colliderect = 检查碰撞打印('碰撞')

door_list 是一个空列表,player_rect 不在玩家的位置.更新函数 tiles 中的 player_rectdoor_list.blit 返回受影响像素的矩形区域.用它来更新 player_rect 并填充 door_list:

定义图块(map1):全局磁贴、门、玩家、敌人、player_rect # <---door_list.clear() # <---对于 y,enumerate(map1) 中的行:#counts 行对于 x, c 在 enumerate(line):#计数字符如果 c == w":#角色是wscreen.blit(tile, (x * 16.18, y * 15))如果 c == d":rect = screen.blit(door, (x * 16.2, y * 15)) # <---door_list.append(rect) # <---如果 c == p":player_rect = screen.blit(player, player_location) # <---如果 c == e":screen.blit(敌人,(x * 16,y * 15))


不要忘记使用 global 语句,当你想改变全局命名空间中的变量时:

定义图块(map1):全局 player_rect# [...]

I am trying to make the player sprite act when he touches the door but it is not working. I am trying to make it print the word collision but it is not appearing.Most of the code comes from: https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

code:

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)
解决方案

player is not pygame.Rect object but player_rect is. Use player_rect instead of player for the collision test:

def check_collision(doors):
    for pipe in doors:
        #for pipr in pipes = checks forall the rects inside pipe list
        if player_rect.colliderect(pipe):                           # <---
            #colliderect = checks for collision
            print('collision')

door_list is an empty list and player_rect is not in the player's position. Update player_rect and door_list in function tiles. blit returns the rectangular area of ​​the affected pixels. Use it to update player_rect and to fill door_list:

def tiles(map1):
    global tile, door, player, enemy, player_rect                   # <---

    door_list.clear()                                               # <---

    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":
                rect = screen.blit(door, (x * 16.2, y * 15))        # <---
                door_list.append(rect)                              # <---
            if c == "p":
                player_rect = screen.blit(player, player_location)  # <---
            if c == "e":
                screen.blit(enemy, (x * 16, y * 15))


Don't forget to use the global statement, when you want to change a variable in the global namespace:

def tiles(map1):
    global player_rect

    # [...]

这篇关于为什么我的 rect 不起作用并使玩家与门互动,因为它应该在 pygame 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:19