-
Notifications
You must be signed in to change notification settings - Fork 0
/
turret.py
127 lines (100 loc) · 3.52 KB
/
turret.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
import sys
from functions.display import TurretSprites, pygame, laser, get_mobs
from functions.display import background, turret_base_sprite, debug_mode
from functions.geometry import ref_points, detection
from functions.animation import MakeItRain, RotateTurret, get_rotation
from functions.animation import get_sounds, get_thunder
from functions.sound import MusicManager
# Pygame initialisation
pygame.init()
# Main surface initialisation
# Support for resolution changes will be added in a future release
WIDTH, HEIGHT = 1200, 800 # DEFAULT : 1200, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Turret")
# Classes
rotation = get_rotation()
turrets = TurretSprites()
mobs = get_mobs()
rainfall = MakeItRain(screen)
music = MusicManager()
sounds = get_sounds()
thunder = get_thunder()
turret_rotation = RotateTurret()
debug = False
rain = True
music_on = True
clock = pygame.time.Clock()
fps = 60
if music_on:
music.play_music()
# BACKGROUND
background_img = background()
# TURRET BASE
turret_base = turret_base_sprite()
turret_base_rect = turret_base.get_rect()
turret_base_rect.center = (WIDTH//2, HEIGHT//2)
# TURRET
turret_image = turrets.turret_sentinel
turret_rect = turret_image.get_rect()
turret_rect.center = (WIDTH//2, HEIGHT//2)
# MAIN LOOP
while True:
# Updating reference points at each rotation angle
refs = ref_points(screen, turret_rect, rotation.angle)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# LEFT CLICK
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mobs.add_mob(screen, pygame.mouse.get_pos(), turret_base, refs)
# RIGHT CLICK
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
pass
# Erase screen
screen.fill((25, 25, 25))
# If debug mode is activated the background and the
# turret base isn't displayed
if not debug:
screen.blit(background_img, (0,0))
screen.blit(turret_base, turret_base_rect)
# Display of living mobs
for mob in mobs.living_mobs:
screen.blit(mob['image'], mob['rect'])
# Rotates the turret by one angle value
turret_rotation.rotate(screen, turrets, rotation, mobs, refs)
# Displays the laser segment and returns the coordinates
# of its ends
laser_segment = laser(screen, refs["laser_start"], rotation.angle)
# Checks if a mob is intersected by the segment and returns
# the coordinates of that mob if so. If nothing is detected,
# the function returns None
laser_detect = detection(laser_segment[0], laser_segment[1], mobs.living_mobs)
# Displaying rain
if rain:
rainfall.rain()
# No mobs intersected by the laser segment
if laser_detect == None and rotation.mode != "retract":
rotation.mode="sentinel"
# Mob intersected by the laser segment
if laser_detect != None:
rotation.mode="alert"
# Checks if a mob is intersected by the cannon segment
# (segment visible only in debug mode)
cannon_detect = detection(refs["cannon"], refs["target"], mobs.living_mobs)
# Mob intersected by the cannon segment
if cannon_detect == None and rotation.mode == "retract":
mobs.in_target = None
if cannon_detect != None:
rotation.mode="fire"
mobs.in_target = cannon_detect
# Displaying debug mode
if debug:
debug_mode(screen, refs, turret_base_rect,
rotation, mobs, sounds, thunder,
rainfall, clock)
# Display upadate
pygame.display.flip()
# Limit loop speed
clock.tick(fps)