-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.py
44 lines (33 loc) · 1.53 KB
/
character.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
import pygame
class Character:
def __init__(self, c_settings, screen):
"""Initialize the character and set its starting position."""
self.screen = screen
self.c_settings = c_settings
# Load the character image and get its rect.
self.image = pygame.image.load("images/character1.png")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start character at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# Store a decimal value for the character's center.
self.center = float(self.rect.centerx)
# Movement flags
self.moveing_right = False
self.moveing_left = False
def update(self):
"""Update the character's movements based on the movement flags."""
# Update the character center value not the rect.
if self.moveing_right and self.rect.right < self.screen_rect.right:
self.center += self.c_settings.character_speed_facter
if self.moveing_left and self.rect.left > 0:
self.center -= self.c_settings.character_speed_facter
# Update the rect object from self.center
self.rect.centerx = self.center
def blitme(self):
"""Draw the character in its current position."""
self.screen.blit(self.image, self.rect)
def character_center(self):
"""Center the character at the center of the screen."""
self.center = self.screen_rect.centerx