1

I've been at it for a while, and I cannot seem to figure out why my sprite is not moving up or down when the program is run.

Player Sprite Class

def __init__(self,name,x,y):
    pygame.sprite.Sprite.__init__(self)
    self.name = str(name)
    self.speed = 5
    self.image = pygame.image.load(name+'.png').convert_alpha()
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
def move_up(self):
    self.rect.y -= self.speed
def move_down(self):
    self.rect.y += self.speed
def move_left(self):
    self.rect.x -= self.speed
def move_right(self):
    self.rect.x += self.speed

def update(self):
    self.screen.blit(self.image,(self.rect))

Main File / Loop

class Console:
    def __init__(self,width,height):
        pygame.init()
        self.w = width
        self.h = height
        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode((self.w,self.h))
        self.screenName = pygame.display.set_caption("Star's Labyrinth")

class Background(pygame.sprite.Sprite):
    def __init__(self,image_file,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

def main():
    gameExit = False
    game_display = Console(1024,576)
    game_display.screen
    starboy = Player("starboy",200,200)
    background = Background("background.png",0,0)
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    starboy.move_left()
                elif event.key == pygame.K_d:
                    starboy.move_right()
                elif event.type == pygame.K_w:
                    starboy.move_up()
                elif event.type == pygame.K_s:
                    starboy.move_down()
    print(pygame.event.get())
    game_display.screen.blit(background.image,background.rect)
    starboy.update
    game_display.screen.blit(starboy.image,(starboy.rect.x,starboy.rect.y))
    pygame.display.update()
    game_display.clock.tick(30)

I have tried different types of commands trying to figure out whether it's a variable or function problem. Also is there a way I can make it move without having to tap the key.

  • 1
    Please post a [minimal, complete and verfiable example](https://stackoverflow.com/help/mcve). Where are the `move_...` methods of the `Player` class? – skrx Apr 17 '18 at 14:16
  • The missing parentheses behind `starboy.update` are probably the reason for this bug, but we have to see a complete example to be sure. – skrx Apr 17 '18 at 14:38
  • @skrx That is the whole program I have so far. Actually, I was wrong. – Brandon S Toledo Gallardo Apr 17 '18 at 15:22
  • As for the continuous movement, [these answers](https://stackoverflow.com/q/33537959/6220679) will be helpful to you. BTW, please ask only one specific question at a time and search for existing answers on Stack Overflow. – skrx Apr 17 '18 at 16:20

1 Answers1

1

In the event loop you're checking if the event.type is pygame.K_w or pygame.K_s, but you have to check if the event.key is equal to these constants. Change

elif event.type == pygame.K_w:
    starboy.move_up()
elif event.type == pygame.K_s:
    starboy.move_down()

to:

elif event.key == pygame.K_w:
    starboy.move_up()
elif event.key == pygame.K_s:
    starboy.move_down()

You also forgot the parentheses behind starboy.update.

skrx
  • 19,980
  • 5
  • 34
  • 48