| 1 |  |  | import pygame | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from src.utils import load | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class Player: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     _texture = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         self.jump_sound = pygame.mixer.Sound("src/assets/sounds/jump.wav") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         self.rect = self.texture.get_rect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         self.rect.x, self.rect.y = 300, -100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         self.ax = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         self.ay = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         self.gravity = 0.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         self.max_speed_y = 8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         self.max_speed_x = 10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         self.jump_count = 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         self.currently_in_jump = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.jump_available = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self.behavior = "walk" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     def texture(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if self._texture is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             self._texture = load("src/assets/images/duck3.png") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return self._texture | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     def reset(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self.rect.x = 300 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         self.rect.y = -100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         self.behavior = "walk" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         self.ax = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         self.ay = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     def move(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if self.behavior == "walk": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             self.ax += 0.2 if self.ax < 4 else -0.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         elif self.behavior == "accelerate": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             self.ax = self.max_speed_x \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 if self.ax > self.max_speed_x else self.ax + 0.6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             self.ax = 1 if self.ax <= 1 else self.ax - 0.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     def jump(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if self.jump_available and self.jump_count: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             self.jump_sound.play() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             self.ay = -self.max_speed_y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             self.jump_count -= 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if not self.currently_in_jump: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 self.currently_in_jump = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             self.jump_available = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     def apply_gravity(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         self.ay += self.gravity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if self.ay > self.max_speed_y: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             self.ay = self.max_speed_y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         elif self.ay < -self.max_speed_y: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             self.ay = -self.max_speed_y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         self.rect.y += int(self.ay) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         self.rect.y = max(self.rect.y, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     def above(self, y): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return self.rect.y > y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     def accelerate(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         self.behavior = "accelerate" | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     def decelerate(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         self.behavior = "decelerate" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     def walk(self): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |         self.behavior = "walk" | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  |