-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.py
76 lines (62 loc) · 2.57 KB
/
controllers.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
68
69
70
71
72
73
74
75
76
"""
Contains classes for the controllers of both the dres up and platformer games,
which are used to take user input and apply it to the game.
"""
import pygame
class DgController:
"""
Controller for changing the frog sprite's clothing.
Attributes:
game: An instance of the Game class, representing the current game
being played
player: An instance of the Player class, representing the player
being controlled by the controller
"""
def __init__(self, game, player):
self.game = game
self.player = player
def change_clothes(self):
"""
Changes the clothing on the player sprite using input from the arrows.
The left and right arrows are used to scroll between clothing items,
and the up and down arrows are used to select which item of clothing
is being changed (hat, top, or bottom)
"""
for event in self.game.events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
self.game.item = (self.game.item + 1) % 3
elif event.key == pygame.K_UP:
self.game.item = (self.game.item - 1) % 3
if event.key == pygame.K_RIGHT:
self.player.change_outfit(self.game.item, 1)
elif event.key == pygame.K_LEFT:
self.player.change_outfit(self.game.item, -1)
class FpController():
"""
Controller for the player sprite in the platformer.
Attributes:
_game: the instance of the game being controlled
_player: the instance of the player being controlled
"""
def __init__(self, game, player):
self._game = game
self._player = player
def move_player(self):
"""
Takes input from the arrow keys and moves the player around screen.
The up key causes the player to jump if they're on a platform,
the right key causes the player to move right, and the left key
causes the player to move left.
"""
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if self._player.on_platform:
self._player.jump()
if keys[pygame.K_RIGHT] and self._player.rect.bottomright[0] <= \
self._game.SCREEN_WIDTH:
self._player.set_xdir(1)
elif keys[pygame.K_LEFT] and self._player.rect.bottomleft[0] >= 0:
self._player.set_xdir(-1)
else:
self._player.set_xdir(0)