-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.nvgt
167 lines (149 loc) · 4 KB
/
game.nvgt
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "form.nvgt"#include "includes/state.nvgt"
#include "includes/bullet.nvgt"
#include "includes/virus.nvgt"
#include "includes/globals.nvgt"
#include "dynamic_menu.nvgt"
menu_state menu;
game_state game;
game_over_state go;
state_manager sm;
audio_form f;
void main() {
show_window("Virus game");
//p.pan_step=3.0;
sound_global_hrtf=true;
sm.push(menu);
//sm.push(go);
while (true) {
wait(5);
sm.update();
}
}
class menu_state:state {
dynamic_menu m;
void init_menu() {
m.reset(true); //We want the menu to reset each time this function is called else you'd have more than 1 item that will be duplicated.
m.add_item_tts("Start");
m.add_item_tts("Exit");
m.wrap=false;
m.enable_home_and_end=true;
m.set_speech_mode(3);
}
void on_push() {
init_menu();
}
void on_update() {
int r=m.run("Please choose an option", true);
if (r==2 || r==0) {
exit();
}
if (r==1) {
sm.push(game);
}
}
void on_pop() {
speak("Welcome");
}
}
class game_state:state {
sound ambience;
timer spawn_timer;
timer walktimer;
int walktime;
int spawn_time;
void on_push() {
ambience.load("sounds/ambience.wav");
ambience.play_looped();
walktime=150;
spawn_time=random(3000,10000);
reset_game();
}
void on_pop() {
ambience.stop();
p.destroy_all();
}
void on_update() {
p.update_listener_2d(x,0);
if (key_pressed(KEY_ESCAPE)) {
sm.push(go);
}
bullet_loop();
if (spawn_timer.elapsed>=spawn_time) {
spawn_virus();
spawn_time=random(3000,10000);
spawn_timer.restart();
}
for (int i=0; i<v.length(); i++) {
v[i].loop();
if (v[i].y==0) {
v[i].brake_snd=p.play_2d("sounds/glassBrake.wav",0,0,v[i].x,v[i].y,false,false);
p.destroy_sound(v[i].snd);
v.remove_at(i);
lives-=1;
}
}
if (key_down(KEY_RIGHT) and x<15 and walktimer.elapsed>=walktime) {
x++;
p.play_stationary("sounds/fs/"+random(1,5)+".wav",false);
walktimer.restart();
}
if (key_down(KEY_LEFT) and x>0 and walktimer.elapsed>=walktime) {
x--;
p.play_stationary("sounds/fs/"+random(1,5)+".wav",false);
walktimer.restart();
}
if (key_pressed(KEY_C)) {
speak("Position: "+x);
}
if (key_pressed(KEY_UP)) {
spawn_bullet(x,0);
p.play_stationary("sounds/click.wav",false);
fired_shots+=1;
}
if (key_pressed(KEY_S)) {
speak("Your score is "+score);
}
if (key_pressed(KEY_L)) {
speak(lives+" Lives remaining");
}
if (lives==0) {
sm.push(go);
}
if (key_pressed(KEY_A)) {
speak("Accuracy: "+calculate_accuracy(fired_shots,targets_hit)+" percent");
}
}
void reset_game() {
p.destroy_all();
v.resize(0);
bullets.resize(0); //just incase there are some bullets left
lives=3;
x=0;
score=0;
fired_shots=0;
targets_hit=0;
}
}
class game_over_state:state {
int list;
int ok;
void on_push() {
f.create_window();
list=f.create_list("Results", 3,false,true);
f.add_list_item(0,"Score: "+score);
f.add_list_item(0,"accuracy: "+calculate_accuracy(fired_shots,targets_hit)+" Percent");
f.add_list_item(0, "Shots fired: "+fired_shots);
ok=f.create_button("OK");
f.focus(list);
}
void on_pop() {
f.reset();
}
void on_update() {
f.monitor();
if (key_pressed(KEY_ESCAPE) || f.is_pressed(ok)) {
sm.pop();
sm.pop(); //We want to go to the main menu
}
}
}