1

The problem I have is that I have imported my sprite into my game, Its a still image and I want to move it, however I am having issues although I have an idea something in the mainloop must be changed, I am clueless on what exactly to change. The variable avatar is the sprite.

import pygame
pygame.init()

window = pygame.display.set_mode((750, 750))

pygame.display.set_caption("PeaShooters")

avatar = pygame.image.load('Sprite 1 Red.png')
background = pygame.image.load('Bg.jpg')

x = 64
y =64
width = 40
height = 60
vel = 5 
white = (255, 255, 255)

def drawGrid():
    window.blit(background, (0,0))
    window.blit(avatar, (300,500))
    pygame.draw.line(window, white, [50,50], [50, 600], 5)
    pygame.draw.line(window, white, [50,50], [600, 50], 5)
    pygame.draw.line(window, white, [600,600], [600, 50], 5)
    pygame.draw.line(window, white, [50,600], [600, 600], 5)
    pygame.draw.line(window, white, [50,450], [600, 450], 5)
    pygame.display.update()

running = True
while running:
    pygame.time.delay(100) 

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_w] and y > 455:
        y += vel

    if keys[pygame.K_a] and x > 55:
        x -= vel

    if keys[pygame.K_s] and y < 565:
        y -= vel

    if keys[pygame.K_d] and x < 575 :
        x += vel
*I gathered something needs to be changed around here.
    drawGrid()



pygame.quit()

1 Answers1

1

From opensource.com:

To make your sprite move, you must create a property for your sprite that represents movement. When your sprite is not moving, this variable is set to 0.

If you are animating your sprite, or should you decide to animate it in the future, you also must track frames to enable the walk cycle to stay on track.

Create the variables in the Player class. The first two lines are for context (you already have them in your code, if you've been following along), so add only the last three:

def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.movex = 0 # move along X
    self.movey = 0 # move along Y
    self.frame = 0 # count frames

With those variables set, it's time to code the sprite's movement.

The player sprite doesn't need to respond to control all the time; sometimes it will not be moving. The code that controls the sprite, therefore, is only one small part of all the things the player sprite will do. When you want to make an object in Python do something independent of the rest of its code, you place your new code in a function. Python functions start with the keyword def, which stands for define.

Make a function in your Player class to add some number of pixels to your sprite's position on screen. Don't worry about how many pixels you add yet; that will be decided in later code.

def control(self,x,y):
    '''
    control player movement
    '''
    self.movex += x
    self.movey += y

To move a sprite in Pygame, you have to tell Python to redraw the sprite in its new location—and where that new location is.

Since the Player sprite isn't always moving, the updates need to be only one function within the Player class. Add this function after the control function you created earlier.

To make it appear that the sprite is walking (or flying, or whatever it is your sprite is supposed to do), you need to change its position on screen when the appropriate key is pressed. To get it to move across the screen, you redefine its position, designated by the self.rect.x and self.rect.y properties, to its current position plus whatever amount of movex or movey is applied. (The number of pixels the move requires is set later.)

def update(self):
    '''
    Update sprite position
    '''
    self.rect.x = self.rect.x + self.movex        

Do the same thing for the Y position:

    self.rect.y = self.rect.y + self.movey

For animation, advance the animation frames whenever your sprite is moving, and use the corresponding animation frame as the player image:

    # moving left
    if self.movex < 0:
        self.frame += 1
        if self.frame > 3*ani:
            self.frame = 0
        self.image = self.images[self.frame//ani]

    # moving right
    if self.movex > 0:
        self.frame += 1
        if self.frame > 3*ani:
            self.frame = 0
        self.image = self.images[(self.frame//ani)+4]
Alec
  • 8,529
  • 8
  • 37
  • 63