0

I am new to Pygame and am facing issues getting the event handler to register my keystrokes to move my player when I call that class under the event handler in the main loop

I run this code and the level and player render correctly but not able to move my player. I've looked at various other answers such as adding pygame.event.pump() but this doesn't work and shouldn't be needed as it should be handled by the main event handler


import pygame, sys

# Settings

size = width, height = (800, 800)
FPS = 60
road_w = width//1.6
roadmark_w = width//80
right_lane = width/2 + road_w/4
left_lane = width/2 - road_w/4
speed = 5
player_start = right_lane, height*0.8
enemy_start = left_lane, height*0.2

# Create Player Class
class Player(pygame.sprite.Sprite):
    def __init__(self,pos,groups):
        super().__init__(groups)
        self.image = pygame.image.load("car.png").convert_alpha()
        self.rect = self.image.get_rect(center = pos)
 
        self.direction = pygame.math.Vector2(0,0)
        self.speed = 5
      
    def input(self):
        keys = pygame.key.get_pressed()  

        if keys[pygame.K_UP]:
            self.direction.y = -1
        elif keys[pygame.K_DOWN]:
            self.direction.y = 1
        else:
            self.direction.y = 0
        
        if keys[pygame.K_RIGHT]:
            self.direction.x = 1
            print(self.direction.x)
        elif keys[pygame.K_LEFT]:
            self.direction.x = -1
        else:
            self.direction.x = 0
  
    def move(self, speed):
        if self.direction.magnitude() != 0:
            self.direction = self.direction.normalize()
        
        self.rect.x += self.direction.x * speed
        self.rect.y += self.direction.y * speed
    
                    
    def update(self):
        self.input()
        self.move(self.speed)

#Create Level Class
        
class Level:
    def __init__(self):
        
        # get display surface
        self.display_surface = pygame.display.get_surface()
      
        #Sprite group set up
        self.player_sprite = pygame.sprite.Group()
       
    
    def create_map(self):
        pygame.draw.rect(self.display_surface,(50, 50, 50),\
        (width/2-road_w/2, 0, road_w, height))
            
        pygame.draw.rect(self.display_surface,(255, 240, 60),\
        (width/2 - roadmark_w/2, 0, roadmark_w, height))
            
        pygame.draw.rect(self.display_surface,(255, 255, 255),\
        (width/2 - road_w/2 + roadmark_w*2, 0, roadmark_w, height))
            
        pygame.draw.rect(self.display_surface,(255, 255, 255),\
        (width/2 + road_w/2 - roadmark_w*3, 0, roadmark_w, height))
        
    def run(self):
        # Update and draw level
        self.create_map()
        self.player = Player(player_start,[self.player_sprite])
        self.player_sprite.draw(self.display_surface)
        self.player_sprite.update
 
# Create Game Class
class Game:
    def __init__(self):
        
        # Setup
        pygame.init()
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption('Car Game')
        self.clock = pygame.time.Clock()
        self.level = Level()
    
    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                    
            self.screen.fill((60,220,0))
            self.level.run()
            
            pygame.display.update()
            
            self.clock.tick(FPS)

# Run Game            
if __name__ == '__main__':
    game = Game()
    game.run()


Car.png

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Tom
  • 1
  • 1
    Your player movement is performed by `Player.input()`, which is called by `Player.update()`... which does not appear to actually be called anywhere. – jasonharper Dec 06 '22 at 05:10

0 Answers0