Window boundaries in Pygame are not working

76 Views Asked by At

I have recently optimized the enemies in my game goblin() to work through an OOP approach, however, the boundaries that I set for movement() are not working. In this simple game, I have to get the knight aka player() to the princess without hitting the goblin(). When I did this without using an OOP approach, it worked perfectly and the goblin would bounce left and right across the screen. Now, it just goes to the right and keeps on going forever! Super frustrating because before I start adding all of my enemies, I want to have a class that is functional! Thanks!

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()
1

There are 1 best solutions below

2
On BEST ANSWER

goblinX_change is an attribute of goblin. movement is continuously called in the application loop. This sets the attribute to the initial value in each frame. The initial value has to be set in the constructor and not in the movement method:

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()