Search code examples
pythonpygamepygame-surface

How do I fix the image in my Python game?


enter image description here

I'm making a game using pygame module. Whenever I make my pig move it comes out like this: see picture above. How can I fix this? I'm thinking it's something to do with how I blitted my image or it's something to do with how I coded the main loop. Also, it started doing that when I set the background image. So I took the background image off to stop the pig image from coming out like that, but it's still coming out like that even when the background image is gone. Here is my code:

Main game script:

import sys

import pygame

from pig import Pig

pygame.init()

# Dimensions of screen.
screen_width = 800
screen_height = 600

# Set the screen display and caption.
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pig Heaven")


# Objects
pig = Pig(screen)


def sky_blitme(screen):
    """Set background sky image."""
    sky_img = pygame.image.load('images/sky.png')
    sky_img = pygame.transform.scale(sky_img, (800, 600))
    screen.blit(sky_img, (0, 0))


sky_blitme(screen)

while True:

    # Accept events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        # Keydown events
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = True
                pig.orientation = 'right'
            elif event.key == pygame.K_LEFT:
                pig.moving_left = True
                pig.orientation = 'left'
            elif event.key == pygame.K_UP:
                pig.moving_up = True
            elif event.key == pygame.K_DOWN:
                pig.moving_down = True

        # Keyup events
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = False
            elif event.key == pygame.K_LEFT:
                pig.moving_left = False
            elif event.key == pygame.K_UP:
                pig.moving_up = False
            elif event.key == pygame.K_DOWN:
                pig.moving_down = False

    pig.blitme()
    pig.update()

    pygame.display.flip()

Here is the pig module:

import pygame


class Pig():

    def __init__(self, screen):
        """Initialize the pig and set its starting position."""
        self.screen = screen

        # Load the pig image and set pig and screen to rect.
        self.image = pygame.image.load('images/pig.png')
        self.image2 = pygame.transform.flip(self.image, True, False)
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Start the pig at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Pig direction
        self.orientation = 'down'

        # Speed of the pig
        self.pig_speed = 1.5
        self.center = float(self.pig_speed)

        # Set a variable for each movement.
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """Update the position of the pig.
        Set boundaries on the edges of the screen."""

        if self.rect.right <= self.screen_rect.right:
            if self.moving_right:
                self.rect.centerx += self.pig_speed

        if self.rect.left > 0:
            if self.moving_left:
                self.rect.centerx -= self.pig_speed

        if self.rect.top > 0:
            if self.moving_up:
                self.rect.bottom -= self.pig_speed

        if self.rect.bottom <= self.screen_rect.bottom:
            if self.moving_down:
                self.rect.bottom += self.pig_speed

    def blitme(self):
        if self.orientation == 'right':
            self.screen.blit(self.image, self.rect)
        elif self.orientation == 'left':
            self.screen.blit(self.image2, self.rect)
        else:
            self.screen.blit(self.image, self.rect)

I'm new in programming and this is the first game I'm creating so I would appreciate if you could give me advice as well and critique my coding structure.


Solution

  • Redraw the background before you draw the pig. That way it will cover the previous pigs. That is how you are supposed to do it. To make it so you don't load the image every frame, take the image load and transform out of the function at the top, and then just redraw the background each time

    #...
    screen.blit(sky_img, (0, 0))
    pig.blitme()
    pig.update()
    #...