/ * Fool fly game v1.0 by skywind
* Air combat touch source program, Lin Wei July 30, 1999
*
* This is a model example of an air combat program, and there is no optimization and expansion.
* Some functions or conventional processing methods of writing gaming programs only to people
* The program is DOS version, and now writes a game platform has changed many, but
* I try to get some constant theory, I hope that the game designer who will get started can be affected.
* To some inspiration, through it can also see the difficulties of designing under the day DOS.
*
* The entire program is less than 300 lines, please compile with the Large mode of BC31 or TC20.
* /
#include
#include
#include
#include
#include
#include
#include
#include
#define max_star 80 / * Most Stars Number * /
#define max_obj 30 / * Most objects (bullets and enemies) * /
#define maxY 169 / * Y coordinate maximum * /
Typedef unsigned long ulong;
Char far * videoBUF = (char far *) 0xA0000000L, * MEMBUF; / * MEMBUF graphic secondary cache * /
Char keys [128]; / * Keys [0..100] Status of the key on the keyboard * /
Const key_up = 72, key_down = 80, key_left = 75, key_right = 77, key_esc = 1, key_ctrl = 29;
/ * Keyboard scanning code * /
Void Interrupt Far (* OldinT9) (Void); / * Old Keyboard Interrupt Program Address * /
Void Interrupt NEWINT9 (VOID); / * New Keyboard Interrupt Service * /
Char Gamestart (Void);
Struct tstars {Int x, y, v;} stars [MAX_STAR]; / * Star Description * /
Struct Tobj {Int Mode, Type, INDEX, NEXT, X, Y;} Objs [MAX_OBJ]; / * Description of Bullets and Enemy * /
Long Gamescore = 0; / * Game score * /
Unsigned char fly_pic [260] = {/ * aircraft graphic, I use programs to convert PCX graphics * /
16, 10, 16, 0, 0, 0, 25, 25, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 0, 0, 0, 0, 25, 0, 196, 0,
0, 0, 10, 112, 112, 0, 25, 196, 40, 40, 29, 112, 112, 40, 112, 0, 112, 112, 112, 40, 40, 112, 112, 40, 40,
25, 25, 112, 40, 40, 112, 112, 112, 112, 112, 112, 40, 112, 112, 40, 112, 112, 112, 40, 40, 25,
40, 112, 112, 0, 0, 112, 112, 40, 40, 40, 25, 40, 112, 0, 0, 0, 112, 112, 112, 40, 112, 112, 112, 40, 25, 112, 0, 0, 0, 0, 0, 0, 29, 112, 0, 40, 0, 112, 0, 112, 29, 0, 0, 0, 0,
0, 0, 0, 29, 112, 0, 40, 54, 112, 0, 112, 29, 0, 0, 0, 0, 0, 0, 29, 0, 0, 40, 54, 112, 0, 0, 29, 0, 0,
0, 0, 0, 0, 0, 0, 40, 112, 25, 112, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 112, 25, 112, 112, 0, 0,
0, 0, 0, 0, 0, 10, 0, 10, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 112, 112, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 0, 0, 0, 0,
0}; / * Data heads four bytes respectively represent the length and width of the picture, respectively, and the behind is the color data of 8bit * /
Void CopyRight (Void)
{Printf ("Game Over. Your Score% LD / N", Gamescore;
Printf ("Thank You for Play IT, IF You Have Any Question Please Call ME / N");
Printf ("(0871) 7167710 or lwwwind@yeah.net, lin wei / n");
}
INT GAME_INIT (VOID);
INT GAME_RESTORE (VOID);
Void main (void)
{
IF (! game_init ()) Return; / * Game initialization * /
Printf ("- star war - / r"); / * Print title * /
Gamestart (); / * Game main loop * /
Game_Restore (); / * Restore initialization * /
Copyright (); / * Display Description * /
}
INT GAME_INIT (VOID)
{
Long i;
Union regs regs;
MEMBUF = (CHAR FAR *) Farmalloc (320L * (MAXY 1));
IF (! MEMBUF) {
Printf ("Not Enough Memory / N"); RETURN 0;
}
MEMSET (MEMBUF, 0, 320L * (MAXY 1));
Randomize (); / * Initialization Random function * /
For (i = 0; i <128; i ) keys [i] = 0; / * Keyboard code initialization * /
For (i = 0; i Stars [i] .x = random (320); Stars [i] .y = -20 random (250); Stars [i] .v = 1; IF (i } For (i = 0; i Oldint9 = getvect (9); disable (); SetVect (9, newint9); ENABLE (); / * Set 320x200x256c graphics mode * / Regs.x.ax = 0x13; INT86 (0x10, & regs, & regs); Return 1; } INT GAME_RESTORE (VOID) { Union regs regs; / * Restore settings: Restore old interrupt * / disable (); SetVect (9, Oldint9); ENABLE (); Regs.x.ax = 3; INT86 (0x10, & regs, & regs); Farfree (MEMBUF); Return 0; } / * Keyboard service program * / Void Interrupt NEWINT9 (VOID) { UNSIGNED CHAR Key; Key = inportb (0x60); / * read keyboard scanning code * / IF (Key <0x80) keys [key] = 1; / * If the highest bit is 0, it is pressed * / Else Keys [Key & 0x7f] = 0; / * If the highest bit is 1, let go * / Key = inportb (0x61); key | = 0x80; Outportb (0x61, key); / * Tell the keyboard has received * / OUTPORTB (0x61, key & 0x7f); OUTPORTB (0x20, 0x20); / * Send interrupt end signal * / } / * ---------------------------- game graphics engine ----------------- ---------------- * / Void Pixel (Unsigned X, Unsigned Y, CHAR C) / * Drawpoint * / { IF (x> = 320 || y> maxy) return; / * Judgment range * / MEMBUF [(Y << 8) (Y << 6) x] = C; / * MEMBUF [Y * 320 X] = C * / } / * Show the contents of the secondary cache * / Void Show (Void) { INT Offset = (199-MAXY) * 160; Memcpy (VideoBuf Offset, MEMBUF, 320L * (MAXY 1)); } / * Qing screen * / Void Clear (Void) { MEMSET (MEMBUF, 0, 320L * (MAXY 1)); } / * Is the drawing piece, (x, y) is the coordinate B is the memory address mode indicates whether it is up and down. * The enemy and the owner are the same picture to reverse * / Void Putimage (int X, int y, char * b, int mode) { INT LEN, WID, I, J; LEN = B [0] (int) b [1] * 256; WID = B [2] (int) B [3] * 256; X- = len / 2; y- = WID / 2; / * center symmetry * / for (j = 0, b = b 4; j {IF (* B &&! Mode) Pixel (x i, y j, * b); / * Whether mirror flip * / IF (* B && Mode) Pixel (x i, y wid-j-1, * b); } } Void DrawFire (int X, int y) / * draws laser * / {INT I, J; For (i = -6; i <6; i ) {Pixel (x-5, y i, 9); Pixel (x 3, y i, 9);} } / * --------------------------------------------------- ---------------- * / Unsigned long timepass; / * Time * / INT GameOver, Sound = 0; INT fly_x = 160, fly_y = maxY * 2/3, fly_flag = 1, fire_flag = 1; Void Drivers (Void); / * Controls the function of enemies * / Void Control (Void); / * Object Control (Event Processing) * / INT Allocobj (Void) / * Assignment Empty Object * / {INT i = 0; While (i IF (i> = max_obj) i = max_Obj-1; Return I; } INT Checkhit (int X1, int y1, int x2, int y2, int r) / * Check collision * / { IF (ABS (X1-X2) <= R && ABS (Y1-Y2) <= R) Return 1; Return 0; } Ulong fclock (void) / * read 1.19MHz 32-bit system clock * / { Ulong T; Disable (); OUTPORTB (0x43, 0); T = Inportb (0x40); T = (InportB (0x40) << 8); T = 0xffff ^ T; Enable (); / * (NOT T) (Clock () << 16) * / Return (Clock () << 16) T; } Char Gamestart (Void) { Ulong start = 0; TimePass = 0; While (! GameOver) { While (fclock () - start <= 45000L); start = fclock (); / * Time control * / CLEAR (); / * 1. Clean screen. The following 5 points are the main loop of the game * / TimePass ; / * 2. Time base * / Control (); / * 3. Event processing * / drivers (); / * 4. Event engine * / Show (); / * 5. Show * / IF (--Sound <= 0) nosound (); / * Sound Processing * / IF ((TIMEPASS & 3) == 0) Printf ("% D / R", GameScore; / * to a certain time update display score * / IF (Keys [key_esc]) GameOver = 1; } Sound (105); DELAY (700); nond (); / * Game over 发 发 * / Return 1; } / * Event processing of the game processes the stroke sky and the object and the protagonist control * Different by scanning object arrays: objs [max_obj], determine based on the scanned object attribute objs [i] .Mode * What objects are in the end: enemy or bullets. And enter the corresponding handler: Analyze the status of the object, then make * Adjust, and re-change the status, this is the core idea of game programming at the same time. * / Void Control (Void) { INT I, J, X, Y, OK; / * Star Processing * / For (i = 0; i Stars [i] .y = stars [i] .v; / * move down * / IF (stars [i] .y> maxy) {/ * If you remove the screen, let it reappear on the screen. * / Stars [i] .x = random (320); Stars [i] .y = -random (60); } IF (stars [i] .v == 1) Pixel (stars [i] .x, stars [i] .y, 23); / * The speed is different color is different * / Else Pixel (stars [i] .x, stars [i] .y, 28); } / * Object processing * / For (i = 0; i {x = objs [i] .x; y = objs [i] .y; ok = 1; Switch (Objs [i] .mode) / * Mode means enemy, bullets, etc. * / { / * Enemy handling * / Case 1: IF (Objs [i] .index == 0) { Switch (Objs [i] .type) / * Detecting the enemy's three status * / {Case 0: if (! random (30)) OBJS [i] .Type = 1; Break; / * 3 action status * / Case 1: IF (x IF (! random (40)) OBJS [i] .type = 2; Break; Case 2: Default: if (! random (3)) y = 3; Break; } IF (Checkhit (x, y, fly_x, fly_y, 15)) fly_flag = 0; / * collided with the protagonist * / PUTIMAGE (X, Y, Fly_Pic, 0); } else {/ * draw a plane if it is hit, then disappear * / IF ((OBJS [i] .index & 3) == 0) Putimage (x, y, fly_pic, 0); if ( Objs [i] .index> 50) OBJS [i] .Mode = 0; } Y ; if (y> maxy) OBJS [i] .Mode = 0; / * Shifts the screen to clear * / Break; / * Laser processing * / Case 2: Y- = 4; if (Y <-20) Objs [i] .Mode = 0; DrawFire (X, Y); For (j = 0; j IF (Objs [J] .Mode == 1 && Checkhit (x, y, objs [j] .x, objs [j] .y, 15) && ! Objs [j] .index&& ok) / * hit the enemy plane * / { Objs [j] .index = 1; ok = 0; OBJS [i] .Mode = 0; Sound (220); Sound = 10; Gamescore = 10; } Break; } Objs [i] .x = x; objs [i] .y = y; / * Update coordinate * / } / * Protagonist control * / IF (keys [key_up]) if (--fly_y <0) fly_y = 0; / * If the upper button is pressed * / IF (Keys [key_down]) if ( fly_y> maxy) fly_y = maxy; / * If the next button is pressed * / IF (Keys [key_left]) if (--fly_x <0) fly_x = 0; / * If the left button is pressed * / IF (Keys [Key_Right]) IF ( fly_x> 319) fly_x = 319; / * If the right button is pressed * / IF (Keys [Key_Ctrl] &&fire_flag) {/ * If you press Ctrl * / i = allocobj (); OBJS [i] .x = fly_x, Objs [i] .y = fly_y-10, objs [i] .Mode = 2; fire_flag = 0; } IF (! keys [key_ctrl]) fire_flag = 1; / * If ctrl * / Putimage (fly_x, fly_y, fly_pic, 1); / * Draw the protagonist * / IF (! fly_flag) GameOver = 1; / * Judging the protagonist status * / } / * Control part of the enemy * / Void Drivers (Void) { IF (Random (30) == 0) {INT i = allocobj (); objs [i] .x = random (320); OBJS [i] .y = -random (20); Objs [i] .Mode = 1; OBJS [i] .type = objs [i] .index = 0; IF (! random (20)) OBJS [i] .type = 2; } }