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] My Pygame Sprite not appearing... noodlespinbot 3 4,856 Oct-30-2020, 06:51 AM
Last Post: robinmurphy
  My Pygame Sprite not appearing... noodlespinbot 1 2,861 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  [PyGame] Terrible Sprite controls, need help. michael1789 16 8,034 Dec-18-2019, 10:32 PM
Last Post: michael1789
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 5,703 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 3,841 Nov-10-2019, 03:54 AM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 3,412 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 4,269 Aug-11-2019, 09:12 PM
Last Post: metulburr
  creating sprite mask pfaber11 5 4,883 Jun-12-2019, 09:39 PM
Last Post: pfaber11
  [PyGame] assigning rect to sprite pfaber11 1 2,764 May-18-2019, 05:39 PM
Last Post: metulburr
  moving a sprite pfaber11 3 3,297 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