oRedNCS
oRedNCS

Reputation: 99

Images not updating pygame

I'm trying to update the Arrow Keys to look as if they are pressed down. However, they don't seem to update whenever they are clicked, and instead the screen flickers but nothing actually changes. I'm relatively new to programming in python with Pygame and am not sure how to fix the problem. This is the function i have for checking if it's clicked:

import pygame

class Button():
    def __init__(self, x, y, xSize, ySize):
        self.x = x
        self.y = y
        self.xSize = xSize
        self.ySize = ySize

    def checkClicked(self):
        if pygame.mouse.get_pressed()[0]:
            x, y = pygame.mouse.get_pos()
            if y > self.y and y < self.y + self.ySize and x > self.x and x < 
self.x + self.xSize:
                print('Clicked')
                return True
        return False

These are the Objects that represent the Buttons that you can click to change the images and move the Player:

leftObject = Button(765, 800, 68, 78)
rightObject = Button(910, 800, 70, 81)
upObject = Button(835, 725, 70, 80)
downObject = Button(835, 800, 70, 82)

leftArrow = pygame.transform.scale(pygame.image.load('leftarrow.png'), (68, 
78))
rightArrow = pygame.transform.scale(pygame.image.load('rightarrow.png'), (70, 
81))
upArrow = pygame.transform.scale(pygame.image.load('uparrow.png'), (70, 80))
downArrow = pygame.transform.scale(pygame.image.load('downarrow.png'), (70, 
82))

This is the Object that gets moved based off of the User key inputs or pressing the buttons on the screen:

class Player(object):
    def __init__(self, x, y, filename, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.image = pygame.transform.scale(pygame.image.load(filename), 
(self.width, self.height))

This is the event that checks if the user has clicked them or has pressed the Arrow Keys to trigger the image change:

    def moveEvent(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if leftObject.checkClicked():
                    leftArrow = 
pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))
                    pygame.display.update()
                    self.x -= 25

                if rightObject.checkClicked():
                    rightArrow = 
pygame.transform.scale(pygame.image.load('rightarrowdown.png'), (70, 81))
                    pygame.display.update()
                    self.x += 25

                if upObject.checkClicked():
                    upArrow = 
pygame.transform.scale(pygame.image.load('uparrowdown.png'), (70, 80))
                    pygame.display.update()
                    self.y -= 25

                if downObject.checkClicked():
                    downArrow = 
pygame.transform.scale(pygame.image.load('downarrowdown.png'), (70, 82))
                    pygame.display.update()
                    self.y += 25

        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                # Left
                leftArrow = 
pygame.transform.scale(pygame.image.load('leftarrow.png'), (68, 78))
                pygame.display.update()
                # Right
                rightArrow = 
pygame.transform.scale(pygame.image.load('rightarrow.png'), (70, 81))
                pygame.display.update()
                # Up
                upArrow = 
pygame.transform.scale(pygame.image.load('uparrow.png'), (70, 80))
                pygame.display.update()
                # Down
                downArrow = 
pygame.transform.scale(pygame.image.load('downarrow.png'), (70, 82))
                pygame.display.update()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
            leftArrow = 
pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))
                self.x -= 25

            if event.key == pygame.K_RIGHT:
                rightArrow = 
pygame.transform.scale(pygame.image.load('rightarrowdown.png'), (70, 81))
                self.x += 25

            if event.key == pygame.K_UP:
                upArrow = 
pygame.transform.scale(pygame.image.load('uparrowdown.png'), (70, 80))
                self.y -= 25

            if event.key == pygame.K_DOWN:
                downArrow = 
pygame.transform.scale(pygame.image.load('downarrowdown.png'), (70, 82))
                self.y += 25

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                leftArrow = 
pygame.transform.scale(pygame.image.load('leftarrow.png'), (68, 78))
                pygame.display.update()

            if event.key == pygame.K_RIGHT:
                rightArrow = 
pygame.transform.scale(pygame.image.load('rightarrow.png'), (70, 81))
                pygame.display.update()

            if event.key == pygame.K_UP:
                upArrow = 
pygame.transform.scale(pygame.image.load('uparrow.png'), (70, 80))
                pygame.display.update()

            if event.key == pygame.K_DOWN:
                downArrow = 
pygame.transform.scale(pygame.image.load('downarrow.png'), (70, 82))
                pygame.display.update()

    def draw(self, screen):
        screen.blit(self.image, (self.x, self.y))

The Images are initially blitted to the screen in the Game/Run portion of the game itself:

class Game(object):
    def __init__(self):
        self.screensize = [1000, 1000]
        self.white = [255, 255, 255]
        self.black = [0, 0, 0]
        self.screen = pygame.display.set_mode(self.screensize)
        #self.bunny = pygame.transform.scale(pygame.image.load('bunny.png'), 
(150, 200))
        self.clock = pygame.time.Clock()
        self.player = Player(50, 350, 'bunny.png', 150, 200)

    def Run(self):
        run = True
        while run:
            self.clock.tick(60)
            self.screen.fill(self.white)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                    pygame.quit()
                    exit()
                elif event.type == pygame.K_ESCAPE:
                    run = False
                    pygame.quit()
                    exit()
                # - Objects Event Handle - 
                self.player.moveEvent(event)

            # - Draws - 
            self.player.draw(self.screen)
            self.screen.blit(leftArrow, (765, 800))
            self.screen.blit(rightArrow, (910, 800))
            self.screen.blit(upArrow, (835, 725))
            self.screen.blit(downArrow, (835, 800))
            pygame.display.flip()


game = Game()
game.Run()

Any help or way/advice to simplify this would be much appreciated!

Upvotes: 3

Views: 120

Answers (1)

Rabbid76
Rabbid76

Reputation: 210890

The statment

leftArrow = pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))

sets the local variable leftArrow in the scope of the method moveEvent.
Use the global statement to interpret the variables as globals.

Further note, all the

pygame.display.update()

statement in moveEvent are superfluous, this meantime updates causes the flickering.
The single pygame.display.flip() statement, at the end of the main loop, is sufficient. Remove all the pygame.display.update() calles from the method moveEvent.

e.g.

def moveEvent(self, event):

    global leftArrow, rightArrow, upArrow, downArrow # <----- add

    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            if leftObject.checkClicked():
                leftArrow = pygame.transform.scale(pygame.image.load('leftarrowdown.png'), (68, 78))

                # pygame.display.update() <----- remove

                self.x -= 25

             # [...]

Upvotes: 1

Related Questions