-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
178 lines (138 loc) · 5.33 KB
/
player.py
File metadata and controls
178 lines (138 loc) · 5.33 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import pygame
import math
import globalvar as g
from pygame.locals import K_SPACE
from projectile import Projectile
from turret import Turret
class Player(pygame.sprite.Sprite):
def __init__(self, bg):
super(Player, self).__init__()
# self.surf = pygame.Surface((16, 32))
# self.surf.fill((255, 0, 0))
self.surf = pygame.image.load("sprites\\player_sprite.png")
self.rect = self.surf.get_rect()
self.width = self.rect.width
self.height = self.rect.height
self.rect.move_ip(g.SCREEN_WIDTH / 2, g.SCREEN_HEIGHT / 2)
self.velXMax = 5
self.velYMax = 5
self.velX = 0
self.velY = 0
self.jumpSpeed = 1.5
self.accX = 0
self.grav = 0.1
self.accY = self.grav
self.accM = 0.1
self.frictionX = 0.9
self.frictionY = 0.95
debugMode = 0
if debugMode == 1:
self.score = 1000000
self.cash = 1000000
self.HP = 1000
else:
self.score = 0
self.cash = 300
self.HP = 100
self.cazziatoni = 0
self.flipped = False
self.limitLeft = bg.left
self.limitRight = bg.right
self.limitTop = bg.top
self.limitBottom = bg.bottom
self.projList = []
self.turretList = []
self.turretPrice = [100, 100, 100]
self.turretNumber = [0, 0, 0]
self.bg = bg
self.justJumped = False
self.canDoubleJump = False
self.justDoubleJumped = False
def shoot(self, aim):
dX = aim[0] - self.rect.centerx
dY = aim[1] - self.rect.centery
if dY == 0:
dY = 0.001
newProj = Projectile(self.rect.centerx, self.rect.centery, self.width, self.height, math.atan(dX / dY), self.bg)
ratio = math.sqrt(dX ** 2 + dY ** 2)
newProj.velX = newProj.speed * dX / ratio
newProj.velY = newProj.speed * dY / ratio
self.projList.append(newProj)
def facingPos(self):
mousePos = pygame.mouse.get_pos()
if mousePos[0] > self.rect.centerx:
if self.flipped:
self.surf = pygame.transform.flip(self.surf, True, False)
self.flipped = False
else:
if not self.flipped:
self.surf = pygame.transform.flip(self.surf, True, False)
self.flipped = True
def placeTurret(self, bg, x, y, selectedTurret):
if self.cash >= self.turretPrice[selectedTurret]:
self.cash -= self.turretPrice[selectedTurret]
newTurret = Turret(bg, x, y, selectedTurret)
self.turretNumber[selectedTurret] += 1
self.turretPrice[selectedTurret] = 2**(self.turretNumber[selectedTurret]) * 100
self.turretList.append(newTurret)
def updateTurrets(self, tick, bg, enemies):
for turret in self.turretList:
turret.update(tick, bg, enemies)
def update(self, pressed_keys, tick, enemies, bg):
self.updateTurrets(tick, bg, enemies)
self.facingPos()
projToRemove = []
for proj in self.projList:
if proj.update(tick, enemies):
projToRemove.append(proj)
for proj in projToRemove:
self.projList.remove(proj)
# User inputs
if (pressed_keys[ord('w')] or pressed_keys[K_SPACE]) and not self.justJumped and self.rect.bottom == self.limitBottom:
self.velY -= self.jumpSpeed
self.accY = self.grav / 5
self.justJumped = True
elif (pressed_keys[ord('w')] or pressed_keys[K_SPACE]) and not self.justDoubleJumped and self.canDoubleJump:
self.velY = -self.jumpSpeed
self.justDoubleJumped = True
self.accY = self.grav / 5
elif not (pressed_keys[ord('w')] or pressed_keys[K_SPACE]) and self.rect.bottom != self.limitBottom:
self.canDoubleJump = True
self.accY = self.grav
elif self.velY > 0:
self.accY = self.grav
elif self.rect.bottom == self.limitBottom:
self.canDoubleJump = False
self.justDoubleJumped = False
self.justJumped = False
if pressed_keys[ord('s')]:
self.velY += self.accM
if pressed_keys[ord('a')]:
self.velX -= self.accM
if pressed_keys[ord('d')]:
self.velX += self.accM
# Velocity calculations
self.velX += self.accX
self.velY += self.accY
# Friction
self.velX *= self.frictionX
self.velY *= self.frictionY
# Max speed controls
self.velX = min(self.velX, self.velXMax)
self.velX = max(self.velX, -self.velXMax)
self.velY = min(self.velY, self.velYMax)
self.velY = max(self.velY, -self.velYMax)
# Actual movement
self.rect.move_ip(self.velX * tick, self.velY * tick)
if self.rect.left < self.limitLeft:
self.rect.left = self.limitLeft
self.velX = 0
if self.rect.right > self.limitRight:
self.rect.right = self.limitRight
self.velX = 0
if self.rect.top < self.limitTop:
self.rect.top = self.limitTop
self.velY = 0
if self.rect.bottom > self.limitBottom:
self.rect.bottom = self.limitBottom
self.velY = 0