Monday, July 25, 2022
HomeGame Developmentmotion - Pygame shifting solely as soon as when key pressed intagrated...

motion – Pygame shifting solely as soon as when key pressed intagrated into participant class


I am having a problem when I’ve created a participant class and need to transfer my sprite in between three lanes. I used to be in a position to get the sprite to maneuver once I used “pygame.get_pressed()” nevertheless I solely need the sprite to maneuver in single massive steps between three lanes, due to this fact one key press == shifting say 50 pixles up after which no additional motion.

Subsequently I swapped the “get_pressed” code for a “for occasion in pygame.occasion.get()” so I may use the “KEYDOWN” methodology. I’ve acquired this to work in a seperate venture file by together with it in the principle whereas loop. Nevertheless I consider this could have the ability to be included within the Participant class and presently nothing occurs.

Any assist appreciated:

import pygame
import get_image, player_class
from sys import exit
pygame.init()
display screen = pygame.show.set_mode((960,480))
pygame.show.set_caption('Platform Check')
background_surface = pygame.picture.load('belongings/graphics/inexperienced.png').convert_alpha()
final_background = pygame.remodel.scale(background_surface, (960,480))
background_rect = final_background.get_rect(topleft = (0,0))
clock = pygame.time.Clock()
sprite_sheet_image = pygame.picture.load('belongings/graphics/participant.png').convert_alpha()
sprite_sheet = get_image.SpriteSheet(sprite_sheet_image)
GREEN_SCREEN_BACKGROUND = (32, 156, 0)
FPS = 60
class Participant(pygame.sprite.Sprite):
    international x_position, y_position
    x_position = 50
    y_position = 400
    def __init__(self):
        tremendous().__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.picture = player_walk_2
        self.rect = self.picture.get_rect(bottomleft = (x_position,y_position))

    def player_input(self):
        for occasion in pygame.occasion.get():
            if occasion.sort == pygame.KEYDOWN:
                if occasion.key == pygame.K_UP:
                    self.rect.y -= 20
    def replace(self):
        self.player_input()
participant = pygame.sprite.GroupSingle()
participant.add(Participant())


whereas True:
    for occasion in pygame.occasion.get():
        if occasion.sort == pygame.QUIT:
            pygame.give up()
            exit()

    participant.replace()
    display screen.blit(final_background,background_rect)
    participant.draw(display screen)
    pygame.show.replace()
    clock.tick(FPS)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments