-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller.py
139 lines (105 loc) · 4.07 KB
/
controller.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
138
139
import pycollision
import assets
from Tank import Bullet, Enemy
from random import choice
import sounds
class Controller:
bullets = set()
enemies = set()
player = None
screen = None
obstacles = set()
bg_x, bg_y = (0, 0) # background position
prev_bg_x, prev_bg_y = (0, 0) # previous background position
lives = 1
score = 0
def __init__(self, screen, spawn_pos=None, lives=0):
if spawn_pos is None:
spawn_pos = []
self.screen = screen
self.spawn_lst = spawn_pos
self.lives = lives
self.max_lives = lives
def setPlayer(self, tank):
self.player = tank
def setBgPos(self, bgpos, prev_pos):
self.bg_x, self.bg_y = bgpos
self.prev_bg_x, self.prev_bg_y = prev_pos
def addObstacle(self, obstacle, collidObj):
self.obstacles.add((obstacle, collidObj))
def createBullet(self, tank_object, normal_pos, fire_pos, angle, radius, speed):
bullet = Bullet(self.screen, tank_object, normal_pos, fire_pos, angle, radius, speed)
self.bullets.add(bullet)
sounds.shoot_sound.play()
def getPlayerPos(self):
return self.player.pos()
def updateTanks(self):
self.player.update()
bg_x, bg_y = self.bg_x - self.prev_bg_x, self.bg_y - self.prev_bg_y
for tank in self.enemies:
tank.setBgPos((bg_x, bg_y))
tank.update(self.player.center())
def updateObstacles(self):
for obs, _ in self.obstacles:
obs.update((self.bg_x, self.bg_y))
def update_bullets(self):
bg_x, bg_y = self.bg_x - self.prev_bg_x, self.bg_y - self.prev_bg_y
for bullet in self.bullets.copy():
bullet.update((bg_x, bg_y))
if bullet.destroyed():
self.bullets.remove(bullet)
def checkCollision(self):
""" checks for collision between tank, bullets and obstacles """
for bullet in self.bullets.copy():
for _, collid in self.obstacles:
if collid.rect_collide(bullet.getBbox())[0]:
self.bullets.remove(bullet)
break
for enemy in self.enemies:
collid_count = 0
for _, collid in self.obstacles:
if collid.rect_collide(enemy.getBbox())[0] or \
not (100 <= enemy.pos_x <= 800 and 100 <= enemy.pos_y <= 600):
collid_count += 1
enemy.change_angle()
if collid_count > 0:
enemy.setCollision(True)
else:
enemy.setCollision(False)
if self.player.colliderect(enemy.getRectObject()):
self.player.resetPreviousPos()
enemy.resetPreviousPos()
for bullet in self.bullets.copy():
for tank in self.enemies.copy():
if bullet.tankObject() == self.player and tank.colliderect(bullet.getRect()):
self.enemies.remove(tank)
self.bullets.remove(bullet)
self.score += 1
break
if bullet.tankObject() != self.player and self.player.colliderect(bullet.getRect()):
self.bullets.remove(bullet)
self.lives -= 1
def getEnemyCount(self):
return len(self.enemies)
def update(self):
self.update_bullets()
self.updateObstacles()
self.updateTanks()
self.checkCollision()
def setSpawnlst(self, spawnlst):
self.spawn_lst = spawnlst
def spawnEnemy(self):
pos = choice(self.spawn_lst)
enemy = Enemy(follow_radius=200, pos=pos, screen=self.screen, img_path=assets.ENEMY_TANK,
controller=self, speed=0.4, fire_speed=self.player.fire_speed,
fire_delay=450, fire_radius=150)
self.enemies.add(enemy)
def getLives(self):
return self.lives
def getScore(self):
return self.score
def reset(self):
self.enemies = set()
self.bullets = set()
self.score = 0
self.lives = self.max_lives