-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
67 lines (50 loc) · 1.72 KB
/
player.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pygame
from settings import Settings
from pygame.rect import Rect
class Player(Rect):
"""A class to move the player"""
def __init__(self, screen, x, y):
self.width = 50
self.height = 80
super().__init__(x, y, self.width, self.height)
self.settings = Settings()
self.horizontal_speed = self.settings.horizontal_speed
self.vertical_speed = self.settings.vertical_speed
self.screen = screen
self.moving_left = False
self.moving_right = False
self.jumping = False
self.color = self.settings.player_color
def move_left(self):
self.moving_left = True
def move_right(self):
self.moving_right = True
def jump(self):
self.jumping = True
def stop_moving_left(self):
self.moving_left = False
def stop_moving_right(self):
self.moving_right = False
def stop_jumping(self):
self.jumping = False
def move(self):
self.vertical_speed += self.settings.gravity
if self.jumping:
self.vertical_speed = -10
if self.moving_left:
self.x -= int(self.horizontal_speed)
if self.moving_right:
self.x += int(self.horizontal_speed)
self.y += int(self.vertical_speed)
self._check_boundaries()
def _check_boundaries(self):
if self.x > self.settings.screen_width:
self.x = self.settings.screen_width
elif self.x < 0:
self.x = 0
if self.y > self.settings.screen_height:
self.y = self.settings.screen_height
elif self.y < 0:
self.y = 0
def blitme(self):
pygame.draw.rect(self.screen, self.color, self)