运动是不工作的pygame,它告诉我x没有定义,即使它是在第64行定义的

2024-10-03 02:39:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pygame中的移动不起作用,出于某种原因,出现以下错误: player=pygame.image.load(“C:/Users/cuerv/Downloads/player.png”(x,y,width,height)).convert() 名称错误:未定义名称“x” 即使我在第64行定义了它。还有,我怎样才能知道我的直肠在工作。你可以在这个网站上找到部分代码:https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

from pygame.locals import *
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"""

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

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

tile = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png")
tile_rect = tile.get_rect(center=(100, 256))

player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png"(x, y, width, height)).convert()
player_img.set_colorkey((255,255,255))
player_rect = player.get_rect(center=(100, 256))



def init_display():
    global screen, tile, door, player
    screen = pygame.display.set_mode((480, 250))


def tiles(map1):
    global tile, door, player
    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, (x * 16.2, y * 15))







width = 20
height = 20
vel = 10
x = 200
y = 200




map1 = map1.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
    tiles(map1)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            pygame.time.delay((5))

    keys = pygame.key.get_pressed()


    if keys[pygame.K_LEFT] and x > 0:
        x -= vel


    if keys[pygame.K_RIGHT] and x < 500 - width:
        x += vel


    if keys[pygame.K_UP] and y > 0:
        y -= vel


    if keys[pygame.K_DOWN] and y < 500 - height:
        y += vel




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

Tags: rectimagegetifloadkeysscreenpygame
1条回答
网友
1楼 · 发布于 2024-10-03 02:39:44

您不小心在代码中的一行中添加了(x, y, width, height)。这很可能是一个副本/过去的问题。移除它

player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png"(x, y, width, height)).convert()

player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png").convert()

相关问题 更多 >