-
Notifications
You must be signed in to change notification settings - Fork 5
/
missile-defence.py
153 lines (123 loc) · 4.49 KB
/
missile-defence.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pygame
#from pygame.locals import *
#import os
import random
#import math
import time
from config import *
from functions import *
from city import City
from missile import Missile
from explosion import Explosion
from defence import Defence
from mcgame import McGame
from text import InputBox
# Initialize game engine, screen and clock
pygame.init()
#pygame.mixer.init()
screen = pygame.display.set_mode(SCREENSIZE)
pygame.mouse.set_visible(SHOW_MOUSE)
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
def main():
global current_game_state
# load high-score file
high_scores = load_scores("scores.json")
# set the random seed - produces more random trajectories
random.seed()
# list of all active explosions
explosion_list = []
# list of all active missiles
missile_list = []
# TBC - generate the cities
# need to be replaced with working cities
city_list = []
for i in range(1, 8): # 8 == Max num cities plus defence plus one
if i == 8 // 2: # find centre point for gun
pass
else:
city_list.append(City(i, 7)) # 7 == max num cities plus guns
# Intercepter gun
defence = Defence()
# set the game running
current_game_state = GAME_STATE_RUNNING
show_high_scores(screen, high_scores)
# setup the MCGAME AI
mcgame = McGame(1, high_scores["1"]["score"])
while True:
# write event handlers here
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
# primary mouse button
defence.shoot(missile_list)
if event.button == 2:
# middle mouse button
pass
if event.button == 3:
# right mouse button
pass
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit_game(screen)
if event.key == K_SPACE:
defence.shoot(missile_list)
if event.key == K_p:
pause_game(screen)
if event.type == KEYUP:
pass
# clear the screen before drawing
screen.fill(BACKGROUND)
# Game logic and draws
# --- cities
for city in city_list:
city.draw(screen)
# --- interceptor turret
defence.update()
defence.draw(screen)
# --- missiles
for missile in missile_list[:]:
missile.update(explosion_list)
missile.draw(screen)
if missile.detonated:
missile_list.remove(missile)
# --- explosions
for explosion in explosion_list[:]:
explosion.update()
explosion.draw(screen)
if explosion.complete:
explosion_list.remove(explosion)
# --- Draw the interface
mcgame.draw(screen, defence)
# --- update game mcgame
if current_game_state == GAME_STATE_RUNNING:
current_game_state = mcgame.update(missile_list, explosion_list, city_list)
# load message for Game Over and proceed to high-score / menu
if current_game_state == GAME_STATE_OVER:
mcgame.game_over(screen)
# load a message and set new game values for start new level
if current_game_state == GAME_STATE_NEW_LEVEL:
mcgame.new_level(screen, defence)
# Update the display
pygame.display.update()
# hold for few seconds before starting new level
if current_game_state == GAME_STATE_NEW_LEVEL:
time.sleep(3)
current_game_state = GAME_STATE_RUNNING
# hold for few seconds before proceeding to high-score or back to menu or game over splash
if current_game_state == GAME_STATE_OVER:
input_box = InputBox(100, 100, 140, 32)
while input_box.check_finished() == False:
for event in pygame.event.get():
input_box.handle_event(event)
input_box.update()
input_box.draw(screen)
current_game_state = GAME_STATE_MENU
# display the high scores
if current_game_state == GAME_STATE_MENU:
show_high_scores(screen, high_scores)
current_game_state = 0
# run at pre-set fps
clock.tick(FPS)
if __name__ == '__main__':
main()