Python Forum

Full Version: Need Help With Sprite
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import pygame, random, math
pygame.init() 
 
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
 
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
clock = pygame.time.Clock() 

Ship = pygame.image.load("galaga-nemco.png").convert()
Shipright = pygame.image.load("galaga-nemco-right.png").convert()
Shipleft = pygame.image.load("galaga-nemco-left.png").convert()
Background_Image = pygame.image.load("EhFLMbVGiXmVyhW9oCgURTxZ.jpeg").convert()
LaserBeam = pygame.image.load("laserbeam.png").convert()

Laser = pygame.mixer.Sound("laser5.ogg")
pygame.mixer.music.load('DST-TowerDefenseTheme.ogg')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()

rect_change_x = 0
rect_change_y = 0
ship_x = 50
ship_y = 50
    
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                rect_change_x = -3
            elif event.key == pygame.K_RIGHT:
                rect_change_x = 3
            elif event.key == pygame.K_UP:
                rect_change_y = -3
            elif event.key == pygame.K_DOWN:
                rect_change_y = 3
                
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                rect_change_x = 0   
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                rect_change_y = 0

        elif event.type == pygame.MOUSEBUTTONDOWN:
            Laser.play()

            
    ship_x += rect_change_x
    ship_y += rect_change_y
    
    
    screen.blit(Background_Image, (0, 0))
    screen.blit(Ship, (ship_x, ship_y))
        
    pygame.display.flip()
    clock.tick(60)
    
pygame.quit()
How do I change the sprite(Ship) to ShipRight and Shipleft when I change the direction?
you display a placeholder variable lets call it "current".

ship default
current = Ship
#draw current
when you move ship to right, you displace the image you want
current = Shipright
#draw current
Thank you for responding! Could you put the code in my script for me? Sorry to ask I'm very new to python

Never mind I got it! Thank you!