-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtower.py
128 lines (103 loc) · 4.83 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import pygame
from game_constants import *
from projectile import Projectile
# TODO: Try to make a tower with splash damage.
# TODO: Implement hit cooldown.
class Tower(pygame.sprite.Sprite):
def __init__(self , level ,x ,y):
super().__init__()
self.surf = pygame.Surface((TOWER_WIDTH , TOWER_HEIGHT))
self.level = level
self.range = TOWER_RANGE
self.X = x
self.Y = y
self.selected = False
self.radius = TOWER_RANGE
self.image = pygame.image.load(os.path.join("Assets/Pictures" , "tower.png")).convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = (self.X , self.Y)
self.cooldown_tracker = 20
self.cooldown_number = 20
self.lives = 10
self.angle = 0
self.base_img = pygame.transform.scale(pygame.image.load(os.path.join("Assets/Pictures/TOWERS" , "tower.png")) , (70 , 70)).convert_alpha()
self.base_rect = self.base_img.get_rect()
self.base_rect.center = (self.X + (self.image.get_width() / 2) , self.Y + (self.image.get_height() / 2))
self.upgrade_text = pygame.font.SysFont('comicsans' , 20).render("Upgrade!" , True , RED)
self.upgrade_text_rect = self.upgrade_text.get_rect()
self.upgrade_text_rect.midtop = self.base_rect.midbottom
self.upgradable = (self.level <= 1)
print(self.base_rect.center)
def draw(self , display):
display.blit(self.base_img , self.base_rect)
display.blit(self.image , self.rect)
def draw_range(self, display):
surface = pygame.Surface((self.range * 4, self.range * 4), pygame.SRCALPHA, 32)
pygame.draw.circle(surface, (128, 128, 128, 100), (self.range, self.range ), self.range, 0)
display.blit(surface, (self.X - self.range, self.Y - self.range))
if self.upgradable:
display.blit(self.upgrade_text , self.upgrade_text_rect)
def move(self , x , y):
self.X = x
self.Y = y
#Update the position of images.
self.rect.center = (self.X , self.Y)
self.base_rect.center = (self.X + BASE_IMAGE_DISP[self.level][0] , self.Y + 16)
self.upgrade_text_rect.midtop = self.base_rect.midbottom
def cooldown(self):
if self.cooldown_tracker >= self.cooldown_number:
self.cooldown_tracker = 0
elif self.cooldown_tracker != self.cooldown_number:
self.cooldown_tracker += 1
def shoot(self, enemies , projectiles):
enemy_inrange = [s for s in pygame.sprite.spritecollide(self , enemies , False, pygame.sprite.collide_circle) if s != self]
if enemy_inrange:
self.cooldown()
enemy = enemy_inrange[0]
# dirn = (enemy.X - self.X , enemy.Y - self.Y)
# length = math.hypot(*dirn)
# if length == 0.0:
# pass
# else:
# dirn = (dirn[0]/length, dirn[1]/length)
# angle_new = math.degrees(math.atan2(-dirn[1] , dirn[0]))
# angle_change = angle_new - self.angle
# self.angle = angle_new
# self.rotate(angle_change)
if self.cooldown_tracker == 0:
P1 = Projectile(enemy , TOWER_HITPOINT[self.level] , self.range , self)
projectiles.add(P1)
else:
if self.cooldown_tracker < self.cooldown_number:
self.cooldown_tracker += 1
return
def hit(self):
self.lives -= 1
if(self.lives <= 0):
self.kill()
# def rotate(self, angle):
# self.image = pygame.transform.rotate(self.image , angle)
# self.rect = self.image.get_rect()
# self.rect.center = (self.X , self.Y)
# def blitRotate(self , surf, pos, originPos, angle):
# # offset from pivot to center
# image_rect = self.image.get_rect(topleft = (pos[0] - originPos[0], pos[1]-originPos[1]))
# offset_center_to_pivot = pygame.math.Vector2(pos) - image_rect.center
# # roatated offset from pivot to center
# rotated_offset = offset_center_to_pivot.rotate(-angle)
# # roatetd image center
# rotated_image_center = (pos[0] - rotated_offset.x, pos[1] - rotated_offset.y)
# # get a rotated image
# rotated_image = pygame.transform.rotate(image, angle)
# rotated_image_rect = rotated_image.get_rect(center = rotated_image_center)
# # rotate and blit the image
# surf.blit(rotated_image, rotated_image_rect)
# # draw rectangle around the image
# pygame.draw.rect(surf, (255, 0, 0), (*rotated_image_rect.topleft, *rotated_image.get_size()),2)
def level_up_cost(self):
# TODO: Make update possible
if self.level <= 1:
return (TOWER_COST[self.level + 1] - TOWER_COST[self.level] + 200 )
else:
return 0