我在 Pygame 中移动玩家对象时遇到问题

I'm having problem with moving a player object in Pygame

我在 Pygame 中移动玩家对象时遇到问题。我已经创建了 Player 的 class 并在我的主文件中调用它,但是每当我尝试移动播放器对象时它都不会移动。我也在游戏循环中调用了它,但它仍然不会移动。我不知道发生了什么:这是我到目前为止所做的代码:

screen.py

import pygame

screen.py
class Screen:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def screen_display(self):
        return pygame.display.set_mode((self.width,self.height))

player.py

import pygame


class Player:
    playerY_change = 0.5
    def __init__(self, playerX,playerY, playerWidth,playerHeight,screen,):
        self.playerX = playerX
        self.playerY = playerY
        self.playerWidth = playerWidth
        self.playerHeight = playerHeight
        
        self.screen = screen
    def create_player(self):
        return pygame.draw.rect(self.screen, [0, 0, 0], [self.playerX, self.playerY, self.playerWidth, self.playerHeight])
    

enemy.py

import pygame


class Enemy:
    def __init__(self, enemyX,enemyY, enemyWidth,enemyHeight,screen):
        self.enemyX = enemyX
        self.enemyY = enemyY
        self.enemyWidth = enemyWidth
        self.enemyHeight = enemyHeight
        self.screen = screen

    def create_enemy(self):
        return pygame.draw.rect(self.screen, [0, 0, 0], [self.enemyX, self.enemyY, self.enemyWidth, self.enemyHeight])
        

这是我的主文件 main.py:

import pygame,random,math
from screen import Screen
from player import Player
from enemy import Enemy

# Pygame initilaize
pygame.init()

#Game Screen
screenWidth = 800
screenHeight = 500
window = Screen(screenWidth,screenHeight)
screen = window.screen_display()

# Title and Logo
pygame.display.set_caption("ShootBhoot")
icon = pygame.image.load("logo.png")
pygame.display.set_icon(icon)

# Player
playerX = 10

playerY = 10
playerY_change = 200
playerWidth = 15
playerHeight = 50

player = Player(playerX,playerY,playerWidth,playerHeight,screen)

#Enemy
enemyWidth = 15 
enemyHeight = 50
enemyX = screenWidth - (enemyWidth + 10)
enemyY = 10
enemy = Enemy(enemyX,enemyY,enemyWidth,enemyHeight,screen)

# Ball
ballRadius = 10
ballX = random.randint(0, screenWidth - 10)
ballY = random.randint(0, screenHeight - 10)
ballX_change = 0.01
ballY_change = 0
def ball_create(screen, ballX, ballY, radius):
    return pygame.draw.circle(screen, (10, 10, 10), (ballX, ballY), radius)


def distance(playerX,playerY,ballX,ballY):
    calc = math.sqrt((playerX - ballX)**2 + (playerY - ballY)**2)
    print(calc)

#Game loop
running = True

while running:
    screen.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        #Even while I click btn It won't move
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                playerY_change = -0.5

            if event.key == pygame.K_DOWN:
                playerY_change = 0.5

    

    # Player Move Object not moving
    playerY = playerY_change


    enemy.create_enemy()
    ball_create(screen, ballX, ballY, ballRadius)
    player.create_player()
    pygame.display.flip()
    


    pygame.display.update()

playerY只是在创建player时使用。您必须更改播放器的坐标属性:

playerY = playerY_change

player.playerY += playerY_change

但是我建议使用 pygame.key.get_pressed() 而不是键盘事件。

键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN 事件在每次按下一个键时发生一次。 KEYUP 每松开一个键就会出现一次。使用键盘事件进行单个操作或逐步移动。
pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() 评估按钮的当前状态并获得连续移动:

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.playerY -= 1
    if keys[pygame.K_RIGHT]:
        player.playerY += 1

    screen.fill((255,255,255))
    enemy.create_enemy()
    ball_create(screen, ballX, ballY, ballRadius)
    player.create_player()
    pygame.display.flip()