-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_script.py
36 lines (30 loc) · 1.13 KB
/
player_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self,animation_frames,jump_frame,jump_sound) -> None:
super().__init__()
self.animation_frames = animation_frames
self.jump_surf = jump_frame
self.rect = self.animation_frames[0].get_rect(midbottom=(100,300))
self.index = 0
self.image = self.animation_frames[self.index]
self.gravity = 0
self.jump_sound = jump_sound
self.jump_sound.set_volume(0.5)
def animate(self):
if self.rect.bottom < 300:
self.image = self.jump_surf
return
self.image = self.animation_frames[int(self.index)]
self.index = (self.index + 0.2) % len(self.animation_frames)
def apply_gravity(self):
self.gravity += 1
self.rect.bottom += self.gravity
if self.rect.bottom > 300:
self.rect.bottom = 300
def update(self):
self.apply_gravity()
self.animate()
def reset(animation_frames,jump_frame,jump_sound):
player = pygame.sprite.GroupSingle()
player.add(Player(animation_frames,jump_frame,jump_sound))
return player