File: game.c

package info (click to toggle)
invaders 1.0.0-13
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 296 kB
  • ctags: 384
  • sloc: ansic: 1,944; sh: 54; asm: 28; makefile: 16
file content (295 lines) | stat: -rw-r--r-- 5,245 bytes parent folder | download
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
#include "video.h"
#include "keyboard.h"
#include "sound.h"
#include "delay.h"
#include "io.h"

struct shot_t {
  int8 x,y; // x==-1 -> inactive shot
};
#define NUMSHOTS 5
static struct shot_t shots[NUMSHOTS];
static int8 ship,shipmove;
static int8 shotcolor=4,shipcolor=5,aliencolor=3;
static bool psychedelic=false;

struct alien_t {
  int8 x,y; // x==-1 -> dead alien
  int8 xadd;
};
#define NUMALIENS 40
static struct alien_t aliens[NUMALIENS];

static bool gameover, winner;

static inline void reboot()
{
  /* tell the BIOS to do a warm start */
  *((unsigned short *)0x472) = 0x1234;
  outb(0xfe,0x64);
};

void changecolors(int c)
{
        shotcolor+=c;
        if (shotcolor>15) shotcolor=1;
        else if (shotcolor<1) shotcolor=15;
        shipcolor+=c;
        if (shipcolor>15) shipcolor=1;
        else if (shipcolor<1) shipcolor=15;
        aliencolor+=c;
        if (aliencolor>15) aliencolor=1;
        else if (aliencolor<1) aliencolor=15;
}

void displaypause()
{
  uint8 key;
  bool pressed;

  video_usecolor(6,1);
  video_putstring(30,10,"    PAUSE    ");
  video_putstring(30,11,"  PRESS \'P\'  ");
  video_update();
  do{
    key_decode(&key,&pressed);
    key_polling();
  } while (!(pressed&&(key=='p')));
};

void resetgame()
{
  gameover=false;
  winner=false;
  {
    uint8 i;
    for (i=0;i<NUMSHOTS;++i) shots[i].x=-1;
  };
  ship=40;
  shipmove=0;
  {
    uint8 x,y;
    for (x=0;x<8;++x) {
      for (y=0;y<5;++y) {
	struct alien_t *a = &aliens[y*8+x];
	a->x=x*7+12;
	a->y=y*2;
	a->xadd = ( (x%2) ^ (y%2) ) ? -1 : 1;
      };
    };
  };
};

void display()
{
  // clear screen
  video_usecolor(0,0);
  video_fill(0,0,80,25,0);

  // display shots
  {
    uint8 i;
    video_usecolor(shotcolor,0);
    for (i=0;i<NUMSHOTS;++i) {
      if (shots[i].x!=-1) {
	video_putchar(shots[i].x,shots[i].y,'|');
      };
    };
  };

  // display ship
  {
    video_usecolor(shipcolor,0);
    video_putchar(ship-1,24,'/');
    video_putchar(ship,23,'_');
    video_putchar(ship+1,24,'\\');
  };

  // display aliens
  {
    uint8 i;
    video_usecolor(aliencolor,0);
    for (i=0;i<NUMALIENS;++i) {
      if (aliens[i].x!=-1) {
	video_putchar(aliens[i].x-1,aliens[i].y,'-');
	video_putchar(aliens[i].x,aliens[i].y,'*');
	video_putchar(aliens[i].x+1,aliens[i].y,'-');
      };
    };
  };

  // show the picture
  video_update();
};

void keywork()
{
  uint8 key;
  bool pressed;
  for (;;) {
    key_decode(&key,&pressed);
    switch (key) {
    case '<':
      if (pressed) {
	shipmove=-1;
      }else{
	if (shipmove==-1) shipmove=0;
      };
      break;
    case '>':
      if (pressed) {
	shipmove=1;
      }else{
	if (shipmove==1) shipmove=0;
      };
      break;
    case ' ':
      {
	uint8 i;
	if (!pressed) break;
	for (i=0;i<NUMSHOTS;++i) {
	  if (shots[i].x==-1) {
	    shots[i].x=ship;
	    shots[i].y=24;
	    break;
	  };
	};
      };
      break;
    case 'x':
      return;
    case '@':
      if (pressed) {
        reboot();
      }
      break;
    case 'u':
      if (pressed) {
        changecolors(+1);
      }
      break;
    case 'd':
      if (pressed) {
        changecolors(-1);
      }
      break;
    case '0':
      if (pressed) {
        if (psychedelic==false) psychedelic=true;
        else {
          psychedelic=false;
          shotcolor=4,shipcolor=5,aliencolor=3;
        }
      }
      break;
    case 'p':
      if (pressed) {
        displaypause();
      }
      break;
    };
  };
};

void calculate()
{
  uint8 i,k;
  bool foundalien=false;

  ship+=shipmove;
  if (ship<1) ship=1;
  if (ship>78) ship=78;

  for (i=0;i<NUMSHOTS;++i) {
    if (shots[i].x!=-1) {
      shots[i].y--;
	if (shots[i].y==-1) {
	  shots[i].x=-1;
	};
    };
  };
  for (i=0;i<NUMALIENS;++i) {
    if (aliens[i].x!=-1) {
      foundalien=true;
      for (k=0;k<NUMSHOTS;++k) {
	if (shots[k].x!=-1) {
	  if (shots[k].y==aliens[i].y) {
	    if ( (shots[k].x<=aliens[i].x+1) &&
		 (shots[k].x>=aliens[i].x-1) ) {
	      shots[k].x=-1;
	      aliens[i].x=-1;
	      goto nextalien;
	    };
	  };
	};
      };
      
      aliens[i].x += aliens[i].xadd;
      if (aliens[i].x<1) {
	aliens[i].x=1;
	aliens[i].xadd=1;
	aliens[i].y++;
      }else if (aliens[i].x>78) {
	aliens[i].x=78;
	aliens[i].xadd=-1;
	aliens[i].y++;
      };
      if (aliens[i].y>=20) gameover=true;
    };
  nextalien:;
  };

  if (!foundalien) gameover = winner = true;
};

void sounder()
{
  {
    uint8 i;
    for (i=0;i<NUMSHOTS;++i) {
      if (shots[i].x!=-1) {
	sound_freq(shots[i].y*100+100);
      }else{
	sound_nosound();
      };
      delay_wait(1193180/80);
    };
  };
  sound_nosound();
};

void displaygameover()
{
  uint8 key;
  bool pressed;

  video_usecolor(6,1);
  video_putstring(30,10,"  GAME OVER  ");
  video_putstring(30,11,winner ? "   YOU WIN   " : "  YOU LOSE   ");
  video_putstring(30,12,"  PRESS ESC  ");
  video_update();
  do{
    key_decode(&key,&pressed);
    key_polling(); 
  } while (!(pressed&&(key=='@')));
};

void game()
{
  //   video_blinkchars(false);
  video_hidecursor();

  for (;;) {
    resetgame();

    while (!gameover) {
      display();
      sounder();
      keywork();
      calculate();
      if (psychedelic==true) changecolors(+1);
    };

    displaygameover();
  };
};