-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayerEngine.py
More file actions
128 lines (102 loc) · 4.32 KB
/
Copy pathplayerEngine.py
File metadata and controls
128 lines (102 loc) · 4.32 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# class that allows sprites to play the game like a player.
import pygame, spriteEngine, actorEngine, math, sys
class Player(actorEngine.Actor):
def __init__(self, screen, background, image, health, **kwargs):
super().__init__(screen, background, image, health, **kwargs)
# do we use the joystick/alt layout?
self.isJoystick = False
# Can we move?
self.canMove = True
#proxy collision object.
self.collideRect = pygame.rect.Rect((0, 0), (self.image.get_width()/4,self.image.get_height()))
self.collideRect.center = (int(self.x), int(self.y))
def actorUpdate(self):
# update collision box
self.collideRect.center = (int(self.x), int(self.y))
#update keys
self.playerKeys()
self.playerUpdate()
def playerUpdate(self):
#update the player.
pass
def playerKeys(self):
if self.isJoystick == False:
# move with keyboard (WASD)
if self.canMove == True:
if self.isKeyPressed(pygame.K_w) == True:
self.front()
self.onKeyPress()
if self.isKeyPressed(pygame.K_a) == True:
self.left()
self.onKeyPress()
if self.isKeyPressed(pygame.K_d) == True:
self.right()
self.onKeyPress()
if self.isKeyPressed(pygame.K_s) == True:
self.back()
self.onKeyPress()
self.keyCustomKeys()
elif self.isJoystick == True:
if self.isJoystickValid() == True:
if self.canMove == True:
# move with left joystick
if (self.isJoystickAxisChanged(1, True) == True) or (self.isJoystickHatAreaPressed((2,1)) == True):
self.front()
self.onKeyPress()
if (self.isJoystickAxisChanged(0, True) == True) or (self.isJoystickHatAreaPressed((-1,2)) == True):
self.left()
self.onKeyPress()
if (self.isJoystickAxisChanged(0, False) == True) or (self.isJoystickHatAreaPressed((1,2)) == True):
self.right()
self.onKeyPress()
if (self.isJoystickAxisChanged(1, False) == True) or (self.isJoystickHatAreaPressed((2,-1)) == True):
self.back()
self.onKeyPress()
self.joyCustomKeys()
else:
if self.canMove == True:
# move with alt. keyboard layout. (UP, DOWN, LEFT, RIGHT arrows)
if self.isKeyPressed(pygame.K_UP) == True:
self.front()
self.onKeyPress()
if self.isKeyPressed(pygame.K_LEFT) == True:
self.left()
self.onKeyPress()
if self.isKeyPressed(pygame.K_RIGHT) == True:
self.right()
self.onKeyPress()
if self.isKeyPressed(pygame.K_DOWN) == True:
self.back()
self.onKeyPress()
self.keyCustomKeys()
def keyCustomKeys(self):
# allows us to define custom keyboard code
pass
def joyCustomKeys(self):
# allows us to define custom joypad code
pass
def onKeyPress(self):
# Do something on keypress
pass
def collide(self,rect):
#collision event. push the player/enemy back a bit depending on the side we hit.
if self.isDirectlyOnY(rect.top, self.collideRect.bottom, 30):
self.y -= 3
elif self.isDirectlyOnY(rect.bottom, self.collideRect.top, 30):
self.y += 3
if self.isDirectlyOnX(rect.left, self.collideRect.right, 30):
self.x -= 3
elif self.isDirectlyOnX(rect.right, self.collideRect.left, 30):
self.x += 3
def front(self):
#going forward
pass
def left(self):
#going left
pass
def right(self):
# going right
pass
def back(self):
# going backward
pass