1

I'm following along with a youtube tutorial but using my own resources.

The idea is for my player entity to have a neutral stance animation while not moving.

The movement has been defined in the player class as seen here:

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        player_stand_0 = pygame.image.load('0.png').convert()
        player_stand_1 = pygame.image.load('1.png').convert()
        player_stand_2 = pygame.image.load('2.png').convert()
        player_stand_3 = pygame.image.load('3.png').convert()
        player_stand_4 = pygame.image.load('4.png').convert()
        player_stand_5 = pygame.image.load('.png').convert()
        player_stand_6 = pygame.image.load('6.png').convert()
        player_stand_7 = pygame.image.load('7.png').convert()
        player_stand_8 = pygame.image.load('8.png').convert()
        player_stand_9 = pygame.image.load('9.png').convert()
        player_stand_10 = pygame.image.load('10.png').convert()
        player_stand_11 = pygame.image.load('11.png').convert()
        
        self.player_stand = [player_stand_0,player_stand_1,player_stand_2,player_stand_3,player_stand_4,player_stand_5,player_stand_6,player_stand_7,player_stand_8,player_stand_9,player_stand_10,player_stand_11]
        self.player_stand_index = 0
        self.player_jump = pygame.image.load(jump/1.png')


        self.image = self.player_stand[self.player_stand_index]
        self.rect = self.image.get_rect(midbottom = (80,225))
        self.gravity = 0

        def player_input(self):
            keys = pygame.key.get_pressed()
            if keys[pygame.K_SPACE] and self.rect.bottom >= 225:
                self.gravity = -20

        def apply_gravity(self):
            self.gravity += 1
            self.rect.y += self.gravity
            if self.rect.bottom >= 225:
                self.rect.bottom = 225

        def stand_state(self):
            if self.rect.bottom < 225:
                self.image = self.player_jump
            else:
                self.player_stand_index += 0.1
                if self.player_stand_index >= len(self.player_stand):self.player_stand_index = 0 
                self.image = self.player_stand[int(self.player_stand_index)]

        def update(self):
            self.player_input()
            self.apply_gravity()
            self.stand_state()

The code later calls on these frames using the time.Clock(): function

# SCREEN #

pygame.init()
screen = pygame.display.set_mode((784,348))
pygame.display.set_caption('Game')
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
game_active = False
start_time = 0


# groups

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

# surfaces

sky_surface = pygame.image.load('sky/1.png').convert()

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

        else:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                game_active = True

    if game_active:
        screen.blit(sky_surface,(0,0))

        player.draw(screen)
        player.update()

    else:
        screen.fill(('Black'))
        game_over = test_font.render('GAME',True,('Gray'))
        game_over_rect = game_over.get_rect(center = (392,174))
        screen.blit(game_over,game_over_rect)


    pygame.display.update()
    clock.tick(60)  

My thoughts are that the frames are updating too fast for my monitor to keep up, but i fear the real problem is that ive left a fundamental piece of script out.

help would be much appreciated.

thanks, Callan

Callan
  • 19
  • 1
  • 1
  • 2

0 Answers0