-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower.py
109 lines (86 loc) · 3.03 KB
/
tower.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
class graphical_object:
alive = 1
def __init__(self, xloc, yloc,
xsize, ysize):
self.xloc = xloc
self.yloc = yloc
self.xsize = xsize
self.ysize = ysize
def draw(self, screen):
import pygame
if self.alive == 1:
pygame.draw.rect(screen,
self.color,
pygame.Rect(self.xloc,
self.yloc,
self.xsize,
self.ysize))
class tower(graphical_object):
range = 10
power = 1
health = 1
color = (0, 128, 255)
def shoot(self, targets):
import math
targ_return = []
for choice in targets:
if math.sqrt((choice.xloc - self.xloc)**2 + (choice.yloc - self.yloc)**2) <= self.range:
choice.health = choice.health - self.power
if choice.health > 0:
targ_return.append(choice)
return targ_return
class attackers(graphical_object):
velocity = 1
health = 10
power = 1
color = (255, 100, 0)
def move(self, targets):
targ_return = []
if len(targets) == 0:
self.yloc = self.yloc + self.velocity
else:
flag = 0
for choice in targets:
if (choice.xloc <= (self.xloc+self.xsize)) and ((choice.xloc + choice.xsize) >= self.xloc) and ((self.yloc+self.ysize) >= choice.yloc) and (self.yloc+self.ysize) <= (choice.yloc + choice.ysize):
choice.health = choice.health - self.power
flag = 1
if choice.health > 0:
targ_return.append(choice)
if flag == 0:
self.yloc = self.yloc + self.velocity
return [self, targ_return]
import pygame
import numpy
pygame.init()
xmax = 800
ymax = 600
screen = pygame.display.set_mode((xmax, ymax))
done = False
towers = []
listofattackers = []
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if pygame.mouse.get_pressed()[0]:
loc = pygame.mouse.get_pos()
towers.append(tower(loc[0], loc[1],
20,20))
if len(listofattackers) < 10:
rand_xloc = numpy.random.randint(xmax)
listofattackers.append(attackers(rand_xloc, 0,
20, 20))
for shooters in towers:
listofattackers = shooters.shoot(listofattackers)
for army in listofattackers:
newvalues = army.move(towers)
army = newvalues[0]
towers = newvalues[1]
screen.fill((0, 0, 0))
for instance in towers:
instance.draw(screen)
for instance in listofattackers:
instance.draw(screen)
pygame.display.flip()
clock.tick(30)