它说“pxc”未定义

问题描述

我从 youtube 教程中编写了代码代码

CS

我得到了这个错误,但视频中的那个人没有。这是错误

import pygame

# itilaze
pygame.init()

# title and icon
icon = pygame.image.load("satanic.png")
pygame.display.set_caption("demo")
pygame.display.set_icon(icon)
playerImg = pygame.image.load("pixil-frame-0 (1).png")
Px = 370
Py = 480


def player(x,y):
   screen.blit(playerImg,(x,y))


# ekran boyutu
screen = pygame.display.set_mode((800,600))

# LOOP
running = True
while running:
   screen.fill((50,3,10))  # rgb
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False

       if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_LEFT:
               Pxc = -0.5
           if event.key == pygame.K_RIGHT:
               Pxc = 0.5
       if event.type == pygame.KEYUP:
           if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
               Pxc = 0
   Px += Pxc
   player(Px,Py)  # adam
   pygame.display.update()

如果你们能帮助我就好了 我今天开始学习 phyton

解决方法

Pxy 仅在事件循环中设置。因此,如果没有事件发生,则永远不会设置 Pxc。在应用程序循环之前初始化 Pxc

Pxc = 0              # <--- ADD THIS

running = True
while running:
    # [...]

    Px += Pxc