-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtank.py
More file actions
106 lines (90 loc) · 3.94 KB
/
tank.py
File metadata and controls
106 lines (90 loc) · 3.94 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
import pygame as pg
from gameObject import GameObject
from pygame.math import Vector2
from bullet import Bullet
import math
import gameConsts
import random
screen = gameConsts.screen
class Tank(GameObject):
def __init__(self, color, position, number, addToGameObject):
# general attributes
image = f'img/{color}_tank.png'
size = gameConsts.TANK_SIZE
direction = (1,0) # direction the image naturally faces
speed = gameConsts.TANK_MAX_SPEED
angle = 0
# specific to tank
self.color = color
self.destination = position
self.startingPosition = position
self.selected = False
self.font = pg.font.SysFont(gameConsts.TANK_FONT, gameConsts.TANK_FONT_SIZE)
self.text = self.font.render(str(number), False, gameConsts.SELECTED_COLOR)
self.flag = None
self.respawn = False
self.timeOfDeath = 0
self.ghost = False
self.addToGameObject = addToGameObject
super().__init__(image, position, size, direction, speed, angle)
# start at random angle
randomAngle = random.randint(0, 360)
self.direction.rotate_ip(randomAngle)
self.angle = round((self.angle + randomAngle) % 360, 2)
self.image = pg.transform.rotate(self.original_image, -self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
def update(self):
xDiff = self.destination[0] - self.position[0]
yDiff = self.destination[1] - self.position[1]
if(abs(xDiff) > 1 and abs(yDiff) > 1): # if at destination, don't calc angleSpeed
destVector = Vector2(xDiff, yDiff)
destAngle = round(self.direction.angle_to(destVector), 2)
if (destAngle > 180): # compensate for destAngle_to picking the wrong direction in these cases
destAngle = -360 + destAngle
if (destAngle < -180):
destAngle = 360 + destAngle
self.angleSpeed = round(self.getMaxRotation(destAngle), 2)
distance = math.hypot(self.position[0] - self.destination[0], self.position[1] - self.destination[1])
if distance < self.radius:
self.speed = round(4 * distance / self.radius, 2)
else:
self.speed = gameConsts.TANK_MAX_SPEED
if (self.ghost and not(self.preventDirection['up'] or self.preventDirection['right'] or self.preventDirection['down'] or self.preventDirection['left'])):
self.ghost = False
super().update()
if self.selected:
pg.draw.circle(screen, gameConsts.SELECTED_COLOR, self.position, self.radius, 1)
# self.text = self.font.render(f'{str(self.angle)}-{str(self.direction)}', False, gameConsts.SELECTED_COLOR) # DEBUG
screen.blit(self.text, (self.position[0], self.position[1] + self.radius))
def getMaxRotation(self, desiredAngle):
if (desiredAngle > gameConsts.TANK_MAX_ROTATION):
return gameConsts.TANK_MAX_ROTATION
elif (desiredAngle < -gameConsts.TANK_MAX_ROTATION):
return -gameConsts.TANK_MAX_ROTATION
else:
return desiredAngle
def setDestination(self, pos):
self.destination = pos
def fire(self):
bulletRadius = gameConsts.BULLET_SIZE / 2 / math.cos(45)
frontOfTank = self.position + self.direction * (self.radius + bulletRadius + gameConsts.TANK_MAX_SPEED) # TANK_MAX_SPEED cases when tank postion is updated before bullet
bullet = Bullet(self.color, frontOfTank, self.direction, self.angle)
self.addToGameObject(bullet)
def select(self):
self.selected = True
def unselect(self):
self.selected = False
def setFlag(self, flag):
self.flag = flag
def setRespawn(self, timeOfDeath):
self.respawn = True
self.timeOfDeath = timeOfDeath
self.position = Vector2((-gameConsts.TANK_SIZE[0], -gameConsts.TANK_SIZE[1])) # off screen
self.setDestination(self.position)
self.updateSides()
def checkRespawn(self, currentTime):
if (currentTime > self.timeOfDeath + gameConsts.RESPAWN_TIME):
self.respawn = False;
self.position = self.startingPosition
self.setDestination(self.position)
self.ghost = True