Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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?
Quote
#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
Quote
#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!
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)