Python Forum
[PyGame] Need Help With Sprite
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Need Help With Sprite
#1
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?
Reply
#2
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
Recommended Tutorials:
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,536 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,084 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 3,170 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,533 May-15-2019, 12:52 PM
Last Post: pfaber11

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020