Skip to main content

Pygame moving only once when key pressed intagratedintegrated into player class

I'm having an issue when I have created a player class and want to move my sprite in between three lanes. I was able to get the sprite to move when I used "pygame.get_pressed()"pygame.get_pressed(), however, I only want the sprite to move in single large steps between three lanes, therefore one key press == moving say 50 pixlespixels up and then no further movement.

Therefore I swapped the "get_pressed"get_pressed code for a "for event in pygame.event.get()"for event in pygame.event.get() so I could use the "KEYDOWN" method. I have got this to work in a seperateseparate project file by including it in the main while loop. However I believe this should be able to be included in the Player class and currently nothing happens.

Any help appreciated:

Pygame moving only once when key pressed intagrated into player class

I'm having an issue when I have created a player class and want to move my sprite in between three lanes. I was able to get the sprite to move when I used "pygame.get_pressed()" however I only want the sprite to move in single large steps between three lanes, therefore one key press == moving say 50 pixles up and then no further movement.

Therefore I swapped the "get_pressed" code for a "for event in pygame.event.get()" so I could use the "KEYDOWN" method. I have got this to work in a seperate project file by including it in the main while loop. However I believe this should be able to be included in the Player class and currently nothing happens.

Any help appreciated:

Pygame moving only once when key pressed integrated into player class

I'm having an issue when I have created a player class and want to move my sprite in between three lanes. I was able to get the sprite to move when I used pygame.get_pressed(), however, I only want the sprite to move in single large steps between three lanes, therefore one key press == moving say 50 pixels up and then no further movement.

Therefore I swapped the get_pressed code for a for event in pygame.event.get() so I could use the "KEYDOWN" method. I have got this to work in a separate project file by including it in the main while loop. However I believe this should be able to be included in the Player class and currently nothing happens.

Source Link

Pygame moving only once when key pressed intagrated into player class

I'm having an issue when I have created a player class and want to move my sprite in between three lanes. I was able to get the sprite to move when I used "pygame.get_pressed()" however I only want the sprite to move in single large steps between three lanes, therefore one key press == moving say 50 pixles up and then no further movement.

Therefore I swapped the "get_pressed" code for a "for event in pygame.event.get()" so I could use the "KEYDOWN" method. I have got this to work in a seperate project file by including it in the main while loop. However I believe this should be able to be included in the Player class and currently nothing happens.

Any help appreciated:

Setup

import pygame
import get_image, player_class
from sys import exit
pygame.init()
screen = pygame.display.set_mode((960,480))
pygame.display.set_caption('Platform Test')
background_surface = pygame.image.load('assets/graphics/green.png').convert_alpha()
final_background = pygame.transform.scale(background_surface, (960,480))
background_rect = final_background.get_rect(topleft = (0,0))
clock = pygame.time.Clock()
sprite_sheet_image = pygame.image.load('assets/graphics/player.png').convert_alpha()
sprite_sheet = get_image.SpriteSheet(sprite_sheet_image)
GREEN_SCREEN_BACKGROUND = (32, 156, 0)
FPS = 60

Player Class

class Player(pygame.sprite.Sprite):
    global x_position, y_position
    x_position = 50
    y_position = 400
    def __init__(self):
        super().__init__()

        player_walk_1 = sprite_sheet.get_image(0, 24, 34, 3, GREEN_SCREEN_BACKGROUND)
        player_walk_2 = sprite_sheet.get_image(1, 24, 34, 3, GREEN_SCREEN_BACKGROUND)
        player_walk_3 = sprite_sheet.get_image(2, 24, 34, 3, GREEN_SCREEN_BACKGROUND)
        player_walk = [player_walk_1, player_walk_2, player_walk_3]
        player_walk_index = 0

        self.image = player_walk_2
        self.rect = self.image.get_rect(bottomleft = (x_position,y_position))

    def player_input(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.rect.y -= 20
    def update(self):
        self.player_input()

Body

player = pygame.sprite.GroupSingle()
player.add(Player())


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    player.update()
    screen.blit(final_background,background_rect)
    player.draw(screen)
    pygame.display.update()
    clock.tick(FPS)