Steve Steve - 28 days ago 15
Python Question

Displaying an image using a class on pygame

I am new to using pygame and i am trying to make a class that will display an image on pygame. I want to make it so i can eventually put multiple images onto the screen and move them around the screen. At the moment i just want to display the image on the screen using this format. Can anyone show me code to do that and explain why thanks.

import pygame
import time
import random

pygame.init()

display_width = 1000
display_height = 750

Gameplay = False

gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock() #Starts auto clock updater

background_image = pygame.image.load("Image/Background.jpg").convert()
gameDisplay.blit(background_image, [0, 0])

EnImg = pygame.image.load('Image\Enemy.gif')

class Enemy(pygame.sprite.Sprite):

def __init__(self,dx,dy,image):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.image.load('Image\Enemy.gif')
self.image = image
self.rect.x = dx
self.rect.y = dy

def update(self):
pass

while not Gameplay:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

gameDisplay.blit(background_image, [0, 0])
EnImg = Enemy(500,500,EnImg)
EnImg.draw(screen)
game_change()

pygame.display.update()
clock.tick(60)

Answer

Sprite doesn't have draw() function but other functions (ie. pygame.sprite.Group) use directly self.image and self.rect to draw this sprite.

You can create own function draw() to do the same

    def draw(self, screen):
        screen.blit(self.image, self.rect)

You don't have to load Enemy.gif two times. Do it inside Enemy class or outside and then use image as argument Enemy( ..., image) (or use filename Enemy( ..., filename) and load inside.)


It could look like this.

import pygame 

# --- constants --- (UPPER_CASE names)

DISPLAY_WIDTH = 1000 
DISPLAY_HEIGHT = 750  
DISPLAY_SIZE = (DISPLAY_WIDTH, DISPLAY_HEIGHT)

# --- classes --- (CamelCase names)

class Enemy(pygame.sprite.Sprite):

    def __init__(self, dx, dy, filename):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(filename).convert()

        self.rect = self.image.get_rect()
        self.rect.x = dx
        self.rect.y = dy

        # - or shorter -
        #self.rect = self.image.get_rect(x=dx, y=dy)

        self.moving_left = False
        self.moving_right = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        if self.moving_left:
            self.rect.x -= 10
        if self.moving_right:
            self.rect.x += 10
        if self.moving_up:
            self.rect.y -= 10
        if self.moving_down:
            self.rect.y += 10

    def handle_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.moving_left = True
            elif event.key == pygame.K_RIGHT:
                self.moving_right = True
            elif event.key == pygame.K_UP:
                self.moving_up = True
            elif event.key == pygame.K_DOWN:
                self.moving_down = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.moving_left = False
            elif event.key == pygame.K_RIGHT:
                self.moving_right = False
            elif event.key == pygame.K_UP:
                self.moving_up = False
            elif event.key == pygame.K_DOWN:
                self.moving_down = False

    def draw(self, screen):
        screen.blit(self.image, self.rect)

# --- fuctions --- (lower_case names)

    # empty

# --- main --- (lower_case names)

# - init -

pygame.init() 

screen = pygame.display.set_mode(DISPLAY_SIZE) 

# - objects -

background_image = pygame.image.load("Image/Background.jpg").convert()

enemy = Enemy(500,500, 'Image/Enemy.gif')

# - mainloop -

clock = pygame.time.Clock() 

gameplay = True

while gameplay: 

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameplay = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                gameplay = False

        # - objects event handle -
        enemy.handle_event(event)

    # - updates -

    enemy.update()

    # - draws -

    screen.blit(background_image, (0, 0))
    enemy.draw(screen)
    pygame.display.update() 

    # - FPS -

    clock.tick(60)

# - exit -

pygame.quit()

BTW: other Pygame code templates and image

Comments