-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
57 lines (40 loc) · 1.6 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
#####################################################
#
# breakout game with pygame
# for Python stage in Technofutur TIC (Belgium)
# Authors : Bastien Wiaux and Benoit Ronval
#
#####################################################
from constants import *
import pygame
class Player:
def __init__(self):
"""
Initialise un objet Player avec les constantes dans constants.py
"""
self.velocity = PLAYER_SPEED
self.rectangle = pygame.Rect(PLAYER_START_X, PLAYER_START_Y, PLAYER_WIDTH, PLAYER_HEIGHT)
self.color = "white"
self.save_pos = PLAYER_START_X
self.life = PLAYER_LIFE
def draw(self, window):
"""
Dessine le player sur window
"""
pygame.draw.rect(window, self.color, self.rectangle)
def move(self):
"""
Gere les inputs claviers et bouge le player
"""
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rectangle.x > 0:
self.save_pos = self.rectangle.x # sauvegarde la position avant de bouger
self.rectangle.x -= PLAYER_SPEED # bouge la position vers la gauche
if keys[pygame.K_RIGHT] and self.rectangle.x + PLAYER_WIDTH < SCREEN_WIDTH:
self.save_pos = self.rectangle.x # sauvegarde la position avant de bouger
self.rectangle.x += PLAYER_SPEED # bouge la position vers la droite
def move_back(self):
"""
Remets la position du player a celle precedente
"""
self.rectangle.x = self.save_pos