root/gamemain2.c
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- onestepb
- dispb
- dispm
- bomba
- initobjects
- initmovingobjects
- setevents
- init
- getstate
- show
- gameloop
- main
1 /* gamemain2.c */
2 #include<scenario.h>
3 #include<screen.h>
4 #include<gameio.h>
5 #include<walls.h>
6 #include<stdio.h>
7 #include<enemy.h>
8 #include<movingobjectmanager.h>
9 #ifdef DEBUG
10 #include<log.h>
11 #endif
12
13 #define OBJNUM 2
14 #define MOBJNUM 3
15 #define EVNUM 6
16
17 object* objp[OBJNUM];
18 movingobject* mobjp[MOBJNUM];
19 event* ev[EVNUM];
20 vector2 mypos;
21 int isfire;
22 int score;
23
24 void onestepb(movingobject* mobj){
25 vec2add(&(mobj->pos), &(mobj->vel));
26 }
27
28 void dispb(movingobject* mobj){
29 putobject(*(mobj->obj), mobj->id, mobj->pos.x, mobj->pos.y);
30 }
31
32 void dispm(movingobject* mobj){
33 putobject(*(mobj->obj), 0, mobj->pos.x, mobj->pos.y);
34 }
35
36 int bomba(movingobject* mobj){
37 return 1;
38 }
39
40 void initobjects(void){
41 objp[0] = newobject(1,1,"x");
42 objp[1] = newobject(3,2," ^ =x=");
43 }
44
45 void initmovingobjects(void){
46 mobjp[0] = newenemy();
47 mobjp[1] = newmovingobject(&objp[0], onestepb, dispb, bomba, "bullet");
48 mobjp[2] = newmovingobject(&objp[1], NULL, dispm, bomba, "me");
49 }
50
51 void setevents(void){
52 int i;
53 ev[0] = newevent(3, makevector2(30,2),makevector2(0,0.1), mobjp[0],"plus0");
54 ev[1] = newevent(15, makevector2(10,2),makevector2(0,0.1), mobjp[0],"plus1");
55 ev[2] = newevent(20, makevector2(20,5),makevector2(0,0.1), mobjp[0],"plus2");
56 ev[3] = newevent(25, makevector2(10,2),makevector2(0,0.1), mobjp[0],"plus3");
57 ev[4] = newevent(25, makevector2(25,3),makevector2(0,0.3), mobjp[0],"plus4");
58 ev[5] = newevent(1000000,
59 makevector2(30,1),makevector2(0,1), mobjp[0],"dummy");
60 for(i=0;i<EVNUM;i++){
61 addevent(ev[i]);
62 }
63 resetmark();
64 }
65
66 void init(void){
67 initscreen();
68 initwalls();
69 initmom();
70 initobjects();
71 initmovingobjects();
72 initscenario();
73 setevents();
74 }
75
76
77 void getstate(vector2* ret, int* isfire){
78 static vector2 pos = {SCREENWIDTH/2-1, SCREENHEIGHT-3};
79 *isfire = 0;
80 int in = gameioread();
81 switch(in){
82 case 'f':
83 case 'F':
84 pos.x++;
85 break;
86 case 'b':
87 case 'B':
88 pos.x--;
89 break;
90 case 'p':
91 case 'P':
92 pos.y--;
93 break;
94 case 'n':
95 case 'N':
96 pos.y++;
97 break;
98 case ' ':
99 *isfire = 1;
100 }
101 *ret = pos;
102 }
103
104 void show(void){
105 int x,y;
106 for(y=0;y<SCREENHEIGHT;y++){
107 for(x=0;x<SCREENWIDTH;x++){
108 gameioput(x,y,dispbuffer[SCREENWIDTH*y+x]);
109 }
110 }
111 gameiorefresh();
112 }
113
114 void gameloop(void){
115 int speedlevel = 8;
116 int time=0;
117 vector2 tmpv;
118 event* e = getnextevent();
119 while(1){
120 gameiousleep(30000);
121 if(time%speedlevel == 0){
122 scrollwall();
123 while(e!= NULL && e->time*speedlevel < time){
124 addmo(e->mobj, e->pos, e->vel);
125 e = getnextevent();
126 }
127 score++;
128 }
129 getstate(&mypos, &isfire);
130 gameioput(SCREENWIDTH+3,1,'0'+(int)mypos.x/10);
131 gameioput(SCREENWIDTH+4,1,'0'+(int)mypos.x%10);
132 gameioput(SCREENWIDTH+6,1,'0'+(int)mypos.y/10);
133 gameioput(SCREENWIDTH+7,1,'0'+(int)mypos.y%10);
134 putwalls(SCREENHEIGHT);
135 putmo();
136 onestepmo();
137 if(checkcollision(mypos, objp[1])!=NONOBJ){
138 break;
139 }
140 if(isfire){
141 tmpv = mypos;
142 tmpv.x +=1;
143 tmpv.y -=2;
144 addmo(mobjp[1], tmpv, makevector2(0,-1));
145 }
146 mobjp[2]->pos = mypos;
147 dispm(mobjp[2]);
148 show();
149 time++;
150 }
151 }
152
153 int main(void){
154 gameioinit();
155 gameioclear();
156 init();
157
158 score=0;
159 gameloop();
160
161 gameiopost();
162 printf("score %d\n",score);
163 return 0;
164 }