-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
137 lines (110 loc) · 4.94 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
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
#Esteban Rojas and Danny McCormack
#CSE 30332 Final Project
import pygame
import math
import Utilities
from projectile import Projectile
from Utilities import ANGLE
from Utilities import COORD
from Utilities import FIRE
from Utilities import HEALTH
SPEED = 1
class Player(pygame.sprite.Sprite):
def __init__(self, fname, isLocal, gs = None):
# pass gamespace
self.gs = gs
# load image from argument
print str(fname)
self.image_name = "res/" + str(fname)
self.image = pygame.image.load(self.image_name).convert()
self.rect = self.image.get_rect()
self.explosionCounter = 0
#Used to determine if this player is the local or networked player
self.isLocal = isLocal
# values assigned in iteration loop loop function
self.sprite_info = {
COORD: (0, 0), # CHANGE IN x,y (i.e. moves right, then x=speed, y=0)
ANGLE: 0,
FIRE: False,
HEALTH: 1000
}
# original image to avoid resize errors
self.orig_image = self.image
self.explosionImages = []
# load the explosion images for when the player explodes
for i in range(0, 17):
filename = ""
if i < 10:
filename = "res/explosion/frames00" + str(i) + "a.png"
else:
filename = "res/explosion/frames0" + str(i) + "a.png"
self.explosionImages.append(pygame.image.load(filename).convert())
# Process keyboard input, update CHANGE IN COORDINATES for player n
def move(self, eventList):
if eventList[pygame.K_a]:
self.sprite_info[COORD] = (-SPEED, 0)
elif eventList[pygame.K_d]:
self.sprite_info[COORD] = (SPEED, 0)
elif eventList[pygame.K_w]:
self.sprite_info[COORD] = (0, -SPEED)
elif eventList[pygame.K_s]:
self.sprite_info[COORD] = (0, SPEED)
#Update the current player's variables
def update(self):
#Update the right set of images and variables depending on whether the player is still alive
self.rect = self.rect.move(self.sprite_info[COORD])
self.sprite_info[COORD] = (0,0)
if self.sprite_info[HEALTH] >= 0:
self.ship_update()
else:
self.explosion_update()
#The update function used if the player has not exploded yet
def ship_update(self):
#Calculate rotation, only if local player
if self.isLocal:
self.sprite_info[ANGLE] = self.calculate_angle()
if self.sprite_info[FIRE] == True:
rotationRad = math.radians(self.sprite_info[ANGLE])
# create projectile with the correct velocities to match the player's angle
speedMultiplier = 5
projectileRad = rotationRad - math.pi / 2
xspeed = -(math.cos(projectileRad) * speedMultiplier + (math.pi / 2))
yspeed = math.sin(projectileRad) * speedMultiplier + (math.pi / 2)
newProjectile = Projectile(self.gs, self.rect.centerx + (20 * xspeed), self.rect.centery + (20 * yspeed), xspeed, yspeed, self.sprite_info[ANGLE])
# add to projectile list
self.gs.projectileList.append(newProjectile)
#Update the image based on the mouse position
self.image = Utilities.rot_center(self.orig_image, self.sprite_info[ANGLE] + 40)
#Check if the player's ship is colliding with a projectile
hasCollision = False
for projectile in self.gs.projectileList:
if self.rect.colliderect( projectile.rect):
hasCollision = True
break
#Update the life points, and change the player into an explosion if necessary
if hasCollision:
#A collision occurred
self.sprite_info[HEALTH] = self.sprite_info[HEALTH] - 100
#Check if the health has been depleted
if self.sprite_info[HEALTH] <= 0:
self.explosionCounter = pygame.time.get_ticks()
self.image = self.explosionImages[0]
pygame.mixer.music.load("res/explode.wav")
pygame.mixer.music.play()
#The update function used if the player has exploded,
# or is in the process of exploding
def explosion_update(self):
#process the other ticks of the explosion
currTick = pygame.time.get_ticks()
index = ((currTick - self.explosionCounter) / 10) / 16
if index <= 16:
self.image =self.explosionImages[index]
else:
self.image = pygame.image.load("res/empty.png").convert()
# function to calculate rotation angle from mouse direction
def calculate_angle(self):
mx, my = pygame.mouse.get_pos()
px, py = self.rect.center
rotationRad = math.atan2(px - mx, py - my)
rotationDeg = math.degrees(rotationRad)
return rotationDeg