root/gamemain.c
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- initobjects
- initmovingobjects
- dispm
- init
- getstate
- show
- gameloop
- main
1 /* gamemain.c */
2 #include<screen.h>
3 #include<gameio.h>
4 #include<walls.h>
5 #include<stdio.h>
6 #include<object.h>
7 #include<movingobject.h>
8 #ifdef DEBUG
9 #include<log.h>
10 #endif
11
12 #define OBJNUM 1
13 #define MOBJNUM 1
14
15 object* objp[OBJNUM];
16 movingobject* mobjp[MOBJNUM];
17 vector2 mypos;
18 int isfire;
19 int score;
20
21
22 void initobjects(void){
23 objp[0] = newobject(3,2," ^ =x=");
24 }
25
26 void initmovingobjects(void){
27 mobjp[0] = newmovingobject(&objp[0], NULL, NULL, NULL, "me");
28 }
29
30 void dispm(movingobject* mobj){
31 putobject(*(mobj->obj), 0, mobj->pos.x, mobj->pos.y);
32 }
33
34 void init(void){
35 initscreen();
36 initwalls();
37 initobjects();
38 initmovingobjects();
39 }
40
41 void getstate(vector2* ret, int* isfire){
42 static vector2 pos = {SCREENWIDTH/2-1, SCREENHEIGHT-3};
43 *isfire = 0;
44 int in = gameioread();
45 switch(in){
46 case 'f':
47 case 'F':
48 pos.x++;
49 break;
50 case 'b':
51 case 'B':
52 pos.x--;
53 break;
54 case 'p':
55 case 'P':
56 pos.y--;
57 break;
58 case 'n':
59 case 'N':
60 pos.y++;
61 break;
62 case ' ':
63 *isfire = 1;
64 }
65 *ret = pos;
66 }
67
68 void show(void){
69 int x,y;
70 for(y=0;y<SCREENHEIGHT;y++){
71 for(x=0;x<SCREENWIDTH;x++){
72 gameioput(x,y,dispbuffer[SCREENWIDTH*y+x]);
73 }
74 }
75 gameiorefresh();
76 }
77
78 void gameloop(void){
79 int speedlevel = 8;
80 int time=0;
81 vector2 tmpv;
82 while(1){
83 gameiousleep(30000);
84 if(time%speedlevel == 0){
85 scrollwall();
86 score++;
87 }
88 getstate(&mypos, &isfire);
89 gameioput(SCREENWIDTH+3,1,'0'+(int)mypos.x/10);
90 gameioput(SCREENWIDTH+4,1,'0'+(int)mypos.x%10);
91 gameioput(SCREENWIDTH+6,1,'0'+(int)mypos.y/10);
92 gameioput(SCREENWIDTH+7,1,'0'+(int)mypos.y%10);
93 putwalls(SCREENHEIGHT);
94 if(checkcollision(mypos, objp[0])!=NONOBJ){
95 break;
96 }
97 mobjp[0]->pos = mypos;
98 dispm(mobjp[0]);
99 show();
100 time++;
101 }
102 }
103
104 int main(void){
105 gameioinit();
106 gameioclear();
107 init();
108
109 score=0;
110 gameloop();
111
112 gameiopost();
113 printf("score %d\n",score);
114 return 0;
115 }