Window Pygame 中的边界不起作用

Window boundaries in Pygame are not working

我最近优化了我的游戏 goblin() 中的敌人以通过 OOP 方法工作,但是,我为 movement() 设置的边界不起作用。在这个简单的游戏中,我必须让骑士又名 player() 到达 princess 而不击中 goblin()。当我在不使用 OOP 方法的情况下执行此操作时,它工作得很好,妖精会在屏幕上左右弹跳。现在,它只是向右移动并一直持续下去!非常令人沮丧,因为在我开始添加所有敌人之前,我想要一个可以正常工作的 class!谢谢!

import pygame

# Initialize the pygame
pygame.init()

# Setting up the screen and background
screen = pygame.display.set_mode((800,600))

# Title and Icon of window
pygame.display.set_caption("Get Princess")

icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

#Player Image
playerImg = pygame.image.load('knight.png')
playerImg = pygame.transform.scale(playerImg, (50,50))
playerX = 360
playerY = 520
playerX_change = 0
playerY_change = 0

#Princess Image
princessImg = pygame.image.load('princess.png')
princessImg = pygame.transform.scale(princessImg, (50,50))
princessX = 360
princessY = 20

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

def princess(x,y):
    screen.blit(princessImg, (x, y))


class goblin():
    def __init__(self, goblinX, goblinY):
        self.goblinX = goblinX
        self.goblinY = goblinY
        self.goblinImg = pygame.image.load('goblin.png')
        self.goblinImg = pygame.transform.scale(self.goblinImg,(50,50))
    
    def draw(self):
        screen.blit(self.goblinImg, (self.goblinX, self.goblinY))
        
    
    def movement(self, goblinX_change):
        self.goblinX_change = goblinX_change
        self.goblinX += self.goblinX_change
        if self.goblinX <= 0:
            self.goblinX_change += 0.3
        elif self.goblinX >= 750:
            self.goblinX_change = -0.3


g = goblin(360,250)
running = True
while running:

    screen.fill((50,0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
                    

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                playerY_change = -0.4
            if event.key == pygame.K_DOWN:
                playerY_change = 0.4
            if event.key == pygame.K_LEFT:
                playerX_change = -0.4
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.4
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                playerY_change = 0

    playerX += playerX_change
    playerY += playerY_change
    
    if playerX <= 0:
        playerX = 0
    elif playerX >= 750:
        playerX = 750
    if playerY <= 0:
        playerY = 0
    elif playerY >= 550:
        playerY = 550


    player(playerX,playerY)
    princess(princessX, princessY)
    
    
    g.movement(0.3)
    g.draw()

    pygame.display.update()

goblinX_changegoblin 的属性。 movement 在应用程序循环中不断被调用。这会将属性设置为每个帧中的初始值。初始值必须在构造函数中设置,而不是在 movement 方法中设置:

class goblin():
    def __init__(self, goblinX, goblinY, goblinX_change):
        self.goblinX = goblinX
        self.goblinY = goblinY
        self.goblinX_change = 0.3
        self.goblinImg = pygame.image.load('goblin.png')
        self.goblinImg = pygame.transform.scale(self.goblinImg,(50,50))
    
    def draw(self):
        screen.blit(self.goblinImg, (self.goblinX, self.goblinY))
         
    def movement(self):
        self.goblinX += self.goblinX_change
        if self.goblinX <= 0:
            self.goblinX_change += 0.3
        elif self.goblinX >= 750:
            self.goblinX_change = -0.3
g = goblin(360, 250, 0.3)
running = True
while running:
    # [...]

    g.movement()
    g.draw()
    pygame.display.update()