-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
146 lines (118 loc) · 3.31 KB
/
main.c
File metadata and controls
146 lines (118 loc) · 3.31 KB
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
#include <stdlib.h>
#include <unistd.h>
#include "shoot.h"
// where everything goes down
void ingame(void);
// where everything is stitched together
// to actually work
void main(void) {
// initiate our screen, but
// run some checks first
initscr();
// get the
int ymax = getmaxy(stdscr); // tallest the screen can be, and
int xmax = getmaxx(stdscr); // the widest
// there has to be enough to fit our game in tho', 85x53 chars
if (ymax < 53 || xmax < 85) {
endwin();
printf("%s\n%s\n",
"Your terminal isn't at least 85x53 characters!",
"Please resize your window and try again.");
exit(1);
}
noecho(); // do NOT show the user's keystrokes here
curs_set(0); // nor the cursor, this is a game after all
raw(); // and don't do any input buffering
// finally, render where the
game = create_win(50, 80, (ymax-50)/2, (xmax-80)/2, true); // game itself is shown
// which's where we'll first show the title screen
// for a cool little boot
titlescr();
// and now the HUDs, just a little below the main window
player.hud = create_win(2, 10, (ymax+50)/2, (xmax-9)/2, false);
enemy.hud = create_win(1, 7, (ymax-52)/2, (xmax-7)/2, false);
// create sprites for the
player.win = newspr(player); // player
enemy.win = newspr(enemy); // and opponent
// then get to the game itself
ingame();
// and once the above is done, we
// are done
endgame();
}
// where the actual game happens
void ingame(void) {
enemy.hp = 6; // higher than the player's but not by much
// show their respective HPs
health(player);
health(enemy);
// and the level display
counter();
keypad(game, TRUE); // support for arrow keys
// now, the main loop
while (level <= LVL_MAX) {
key = wgetch(game);
switch(key) {
// yes, you can use Vim keys here
case KEY_UP:
case 'k':
if (player.y >= 2) --player.y;
break;
case KEY_DOWN:
case 'j':
if (player.y <= 47) ++player.y;
break;
case KEY_LEFT:
case 'h':
if (player.x >= 2) --player.x;
break;
case KEY_RIGHT:
case 'l':
if (player.x <= 73) ++player.x;
break;
// 2, 47, 2, 73··· these are all the player's boundaries (Y, Y, X, X), so
// that they don't straight-up get thru the walls here
case 'z': // 'z' for shooting!
if (player.y > 2) // but why would you shoot the wall brah
// this function not only shoots but also kills, so
// the enemy's HP is tracked here as well
enemy.hp = shoot(player, enemy);
// if we killed our opponent···
if (enemy.hp == 0 && enemy.win != NULL) {
enemy.win = NULL; // end the sprite
++*lvlptr; // raise the current level
newlvl();
// ^ and create it again, on a different
// level with new stats
}
// if not, try again!
break;
case 'x': // 'x' to skip your turn!
break;
case 'q': // 'q' exits the game!
// do a little cleanup by
// closing the HUDs
delwin(enemy.hud);
delwin(player.hud);
// now get back to main()
return;
break;
default:
continue;
break;
}
// then, update the sprite
mvspr(player, player.y, player.x);
// and now it's the enemy's turn!
enemctrl();
// what to do whenever the player dies
if (player.hp == 0) {
player.win = NULL;
gameover();
}
}
// when you "clear" the game, you're
// actually doing exactly that ;)
*lvlptr = 0;
ending();
}