-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathterminalhero
executable file
·385 lines (327 loc) · 10.3 KB
/
terminalhero
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/perl
BEGIN {
$FRAMERATE = 0.08;
# help
my $h = "\nTerminal Hero\n"
. "Linux society's response to Activision's Guitar Hero. :)\n\n"
. "Usage: terminalhero [options]\n\n"
. "Options:\n"
. "-e, --easy\t\t turn on easy mode\n"
. "-h, --help\t\t display this help\n\n"
. "Shortcuts:\n"
. "Ctrl+D or Esc\t\t exit\n\n"
. "Rules:\n"
. "Press keys with letters which are in the green area.\n"
. "Your score will increase if you do it well and decrease \n"
. "if you press wrong key. You can also lose health points \n"
. "and lives letters turns red. \n\n"
. "Levels:\n"
. "You will reach new levels every 64 points.\n"
. "Each level is a new line, so it is going harder.\n\n"
. "Now go and play! :)\n\n";
# take arguments
if ($#ARGV >= 0) {
foreach (@ARGV) {
if ($_ ne "-h"
and $_ ne "--help"
and $_ ne "-e"
and $_ ne "--easy") {
print "Unknown argument " . $_ . ".\n";
}
}
if ($ARGV[0] ne "-e" and $ARGV[0] ne "--easy") {
print($h);
exit;
}
else {
$FRAMERATE = 0.12;
}
}
# define Perl modules
my @modules = (
"strict",
"POSIX",
"IO::Handle",
"Term::ReadKey",
"Term::TermKey",
"POE",
"POE::Wheel::TermKey",
"Time::HiRes"
);
# load Perl modules
foreach (@modules) {
eval "use " . $_ . ";";
die "\nUnable to load " . $_
. " Perl module. Please, install it using cpan.\n\n"
. "Error details:\n$@\n" if $@;
}
}
########################################################################
# terminal features (calling tput is slow... don't use it in a loop)
my %esc = (
"hide_cursor" => `tput civis`,
"show_cursor" => `tput cnorm`,
"clear_scr" => `tput ed`,
"font_white" => `tput setf 7 || tput setaf 7`,
"font_green" => `tput setf 2 || tput setaf 2`,
"font_red" => `tput setf 4 || tput setaf 1`,
"font_black" => `tput setf 0 || tput setaf 0`,
"reset" => `tput sgr0`,
"bg_green" => `tput setb 2 || tput setab 2`,
"bg_black" => `tput setb 0 || tput setab 0`,
"bg_white" => `tput setb 7 || tput setab 7`,
"bold" => `tput bold`,
"line_up" => `tput cuu1`
);
# game levels
my @levels = (
"n00b",
"user",
"root",
"geek",
"hacker",
"God",
"cheater"
);
# lines with letters to shoot
my @lines = ();
my @letters = ('a'..'z');
my $HEALTH = 32;
my $NEXT_LEVEL_POINTS = 64;
my $LIVES = 4;
# we need a timestamp, the game should be smooth
my $timestamp = 0;
# state of the game
my %game_stat = (
"lives" => $LIVES,
"health" => $HEALTH,
"level" => 0,
"score" => 0
);
# terminal width
my ($width) = GetTerminalSize();
# range of hit area
my %hit_range = (
"start" => floor(($width) / 4) - 5,
"end" => floor(($width) / 4) + 5
);
# states of letters with their colors
my %sign_states = (
"normal" => $esc{"font_white"},
"shooted" => $esc{"font_green"},
"missed" => $esc{"font_red"}
);
########################################################################
POE::Session->create(
inline_states => {
_start => sub {
STDOUT->autoflush(1);
# hide cursor
print($esc{"hide_cursor"});
$_[KERNEL]->yield("next_life");
$_[HEAP]{termkey} = POE::Wheel::TermKey->new(
InputEvent => 'keypressed',
);
},
# user pressed a key, he want to hit a letter ######################
keypressed => sub {
my $key = $_[ARG0];
my $termkey = $_[HEAP]{termkey};
if (('<C-d>' eq $termkey->format_key($key, FORMAT_VIM))
or ('<Escape>' eq $termkey->format_key($key, FORMAT_VIM))) {
print($esc{"reset"});
# show cursor
print($esc{"show_cursor"});
# clear screen
print($esc{"clear_scr"});
exit;
}
# check if he hit a letter in the green area
my $test = 0;
for (my $j=0; $j <= $game_stat{"level"}; $j++) {
for (my $i=$hit_range{"start"}; $i<$hit_range{"end"}; $i++) {
if ( $lines[$j][$i]{"character"}
eq $termkey->format_key($key, FORMAT_VIM) ) {
if ("normal" eq $lines[$j][$i]{"state"}) {
$game_stat{"score"}++;
}
$lines[$j][$i]{"state"} = "shooted";
$test = 1;
}
}
}
if ($test == 0
and $game_stat{"score"}
> $NEXT_LEVEL_POINTS * ($game_stat{"level"})) {
$game_stat{"score"}--;
}
# gotta exit somehow
delete $_[HEAP]{termkey} if $key->type_is_unicode and
$key->utf8 eq "C" and
$key->modifiers & KEYMOD_CTRL;
},
# game over, clear the screen and write a message ##################
game_over => sub {
print($esc{"reset"});
# show cursor
print($esc{"show_cursor"});
# clear screen
print($esc{"clear_scr"});
print("\nGame over! You are a "
. @levels[$game_stat{"level"}] . ". ;)\n\n");
exit(0);
},
# win, clear the screen and write a message ########################
win => sub {
print($esc{"reset"});
# show cursor
print($esc{"show_cursor"});
# clear screen
print($esc{"clear_scr"});
print("\nYou win! Neo, you must be... the choosen one. O_O\n\n");
exit(0);
},
# let's start a new level ##########################################
next_level => sub {
if ($game_stat{"level"} < scalar(@levels)) {
$game_stat{"health"} = $HEALTH;
$_[KERNEL]->yield("next_life");
}
else {
$_[KERNEL]->yield("win");
}
},
# let's start a new life, with new letters #########################
next_life => sub {
if ($game_stat{"lives"} < 1) {
$_[KERNEL]->yield("game_over");
}
else {
$game_stat{"health"} = $HEALTH;
# prepare an array with empty lines
@lines = ();
for (my $j=0; $j <= $game_stat{"level"}; $j++) {
for (my $i=0; $i<$width; $i++) {
my %sign = (
"character" => " ",
"state" => "empty"
);
push @{ $lines[$j] }, \%sign;
}
}
$_[KERNEL]->yield("play");
}
},
# this is the main loop (recursion) ################################
play => sub {
# frame rate should be stable, this may helps
$timestamp = [ Time::HiRes::gettimeofday( ) ];
# save cursor position
print(`tput sc`);
my $output = $esc{"bg_white"};
$output .= $esc{"font_black"};
# prepare bar with the game's state
my $bar = " whoami: " . @levels[$game_stat{"level"}]
. " | "
. "lives: " . $game_stat{"lives"}
. " | "
. "health: " . $game_stat{"health"}
. " | "
. "score: " . $game_stat{"score"};
if (length($bar)<=$width) {
# print rest of a bar
for (my $i=length($bar); $i<$width; $i++) {
$bar .= " ";
}
}
else {
# cut it if it's too long
$bar = substr($bar, 0, $width);
}
$output .= $bar . "\n" . $esc{"bg_black"}
. $esc{"reset"};
# for each line with signs
for (my $j=0; $j<$game_stat{"level"}+1; $j++) {
# remove first letter
shift(@{$lines[$j]});
($width) = GetTerminalSize();
# generate new letter(s - if someone change terminal size)
for (my $i=scalar(@{ $lines[$j] }); $i<$width; $i++) {
my %sign = (
"character" => " ",
"state" => "normal"
);
if (int(rand(10)) < 1) {
$sign{"character"} = $letters[int rand @letters];
}
push(@{$lines[$j]}, \%sign);
}
# iterate over every sign in the line
for (my $i=0; $i<$width; $i++) {
# check for missed signs
if ($i < $hit_range{"start"}
and ($lines[$j][$i]{"state"} eq "normal")
and ($lines[$j][$i]{"character"} ne " ") ) {
$game_stat{"health"}--;
$lines[$j][$i]{"state"} = "missed";
}
# set hit area colors
if ($i eq $hit_range{"start"}) {
# green background
$output .= $esc{"bg_green"};
# bold font
$output .= $esc{"bold"};
}
# set standard area colors
if ($i eq $hit_range{"end"}) {
# black background
$output .= $esc{"bg_black"};
# standard text
$output .= $esc{"reset"};
}
# print color escape code
if ($sign_states{$lines[$j][$i]{"state"}}) {
$output .= $sign_states{$lines[$j][$i]{"state"}};
}
# print the letter
$output .= $lines[$j][$i]{"character"};
}
$output .= "\n";
}
# print the frame
print($output);
# clear screen to the end (if minified terminal)
print($esc{"clear_scr"});
# restore cursor position
# tput rc doesn't work whent cursor is in the last line :(
# I should find a way to get position of cursor
# print(`tput rc`);
# this solution will couse problems after terminal resizing
for (my $i=0; $i<=$game_stat{"level"}+1; $i++) {
print($esc{"line_up"});
}
# if he's good enought;)
if ($game_stat{"score"}
>= $NEXT_LEVEL_POINTS * ($game_stat{"level"} + 1)) {
$game_stat{"level"}++;
$_[KERNEL]->yield("next_level");
}
else {
# if he's dead
if ($game_stat{"health"} < 1) {
$game_stat{"lives"}--;
$_[KERNEL]->yield("next_life");
}
else {
# display next frame
# period may be below 0, don't worry, it actually will be 0 :)
$_[KERNEL]->delay(
play => $FRAMERATE - Time::HiRes::tv_interval($timestamp)
);
}
}
},
}
);
########################################################################
POE::Kernel->run;