Vincenzo Hamond
Vincenzo Hamond

Reputation: 55

I cant move my sprite after moving movement controls to a player class

I cant seem to get my sprite to move left or right after moving it to a class. I am using pygame. I have a velocity variable and add to that to my x for my shape yet it still doesnt move. Any help would be really appreciated. Movement was working before i decided to put all my the things to do with the player into a class.

import pygame
import Constants
from pygame.locals import *
pygame.init()

pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()

class player:
    def __init__(self,velocity):
        self.velocity = velocity
    def location(self,x,y):
        self.x = x
        self.y = y 
        self.xVel = 0

    def keys(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    self.xVel = - self.velocity
                elif event.key == pygame.K_RIGHT:
                    self.xVel =  self.velocity
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    self.xVel = 0



    def move(self):
        self.x = + self.xVel

    def draw(self):
        display = pygame.display.get_surface()
        pygame.draw.rect(window,(255,255,255),[self.x,self.y,20,40])

    def do(self):
        self.keys()
        self.move()
        self.draw()

P = player(10)
P.location(Constants.SCREEN_WIDTH/2,0)

#Map
def draw_map():
    window.fill(Constants.BLUE)
#sprites
def draw_player():
    player = Player(50,50)

    all_sprite_list.add(player)
#Game loop 
def game_loop():
    GameQuit = False
    while not GameQuit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                GameQuit = True


        draw_map()
        P.do()
        pygame.display.update()
        clock.tick(30)

#game initialisation
game_loop()
pygame.quit()
quit()

`

Upvotes: 2

Views: 44

Answers (2)

skrx
skrx

Reputation: 20438

Leon Z. has already explained that pygame.event.get() empties the event queue. I usually pass the events from the event loop to the objects that need the events. You could also assign the list that pygame.event.get() returns to a variable and then pass this variable to the player instance: events = pygame.event.get().

BTW, there was a little mistake in the move method: = + instead of +=.

import pygame
from pygame.locals import *
pygame.init()

pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()

class player:

    def __init__(self,velocity):
        self.velocity = velocity

    def location(self,x,y):
        self.x = x
        self.y = y 
        self.xVel = 0

    def keys(self, event):
        """Handle the events that get passed from the event loop."""
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.xVel = -self.velocity
            elif event.key == pygame.K_RIGHT:
                self.xVel =  self.velocity
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                self.xVel = 0

    def move(self):
        self.x += self.xVel  # Add the self.xVel to self.x.

    def draw(self):
        display = pygame.display.get_surface()
        pygame.draw.rect(display, (255,255,255), [self.x, self.y, 20, 40])

    def do(self):
        self.move()
        self.draw()

P = player(10)
P.location(window.get_width()/2, 0)

def draw_map():
    window.fill(pygame.Color('blue'))

def draw_player():
    player = Player(50,50)

    all_sprite_list.add(player)

def game_loop():
    GameQuit = False
    while not GameQuit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                GameQuit = True
            # Pass the event to all objects that need it.
            P.keys(event)

        draw_map()
        P.do()
        pygame.display.update()
        clock.tick(30)


game_loop()
pygame.quit()

Upvotes: 1

Leon Z.
Leon Z.

Reputation: 572

The problem is that pygame.event.get() clears all events from the event queue, so when your player calls it, all events are already gone. From the docs

This will get all the messages and remove them from the queue. If a type or sequence of types is given only those messages will be removed from the queue.

You can solve this by specifying which events should be removed from the event queue e.g.

for event in pygame.event.get(QUIT):
    GameQuit = True

The other events should still be in the queue for your player to use

Upvotes: 1

Related Questions