Subversion Repositories Kolibri OS

Rev

Rev 7985 | Rev 8389 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6970 leency 1
/*
2
THE BUS
3
Copyright (C) 2008, 2012, 2017 Leency
4
Menu image from Freepik.com
5
D O N K E Y
6
Copyright (C) 2008 O.Bogomaz
7
*/
8
 
6984 leency 9
#ifndef AUTOBUILD
10
#include "lang.h--"
11
#endif
12
 
6970 leency 13
#define MEMSIZE 1024 * 60;
14
 
15
#include "..\lib\kolibri.h"
16
#include "..\lib\random.h"
17
 
7049 leency 18
#include "..\lib\obj\libio.h"
19
#include "..\lib\obj\libimg.h"
6970 leency 20
 
21
libimg_image menu;
22
libimg_image road;
6976 leency 23
libimg_image objects;
6970 leency 24
 
6995 leency 25
int frame_timeout = 1;
26
 
6997 leency 27
int lifes=0, level=0, score=0;
6984 leency 28
int don_x, don_y, don_h, don_w=68, don_type, don_image_y, don_step_y, don_step_y_default=3;
29
int don_h_mas[8] = { 36,72,36,74,24,64,48,74 };
30
int bus_x, bus_w=42, bus_y, bus_h=88, bus_y_default=290;
6970 leency 31
 
32
#define SCR_MENU_MAIN   1
33
#define SCR_GAME        2
34
#define SCR_PAUSE       3
35
 
6976 leency 36
#define RAND -1
6970 leency 37
 
6984 leency 38
#define WIN_X 516
39
#define WIN_Y 382
6970 leency 40
 
41
int screen_type=SCR_MENU_MAIN;
42
 
43
#define COLOR_ROAD 0x6D879B;
44
 
45
int active_menu_item=0;
46
 
6995 leency 47
#ifdef LANG_RUS
48
#define THE_BUS_TEXT "Маршрутка"
49
#define CONTROLS_TEXT "'Маршрутка
50
Управление:
51
Влево/Вправо или Пробел
52
Клавиша P ставит на паузу'tI"
53
#define ABOUT_TEXT "'Маршрутка
6996 leency 54
Версия v1.01
6995 leency 55
 
6997 leency 56
Сыграйте за дерзкого маршрутчика,
6995 leency 57
который несется домой после смены.
7578 leency 58
Он уже накатил рюмаху с мужиками
6995 leency 59
и теперь его ничто не остановит!
60
 
61
Автор Leency
62
Картинка в меню Freepik.com'tI"
63
#define PAUSE_TEXT "ПАУЗА"
64
#define GAME_OVER_TEXT "Игра окончена"
65
#define FINAL_SCORE_TEXT "Финальный счет"
6997 leency 66
#define LIFES_TEXT "Жизни"
6995 leency 67
#define LEVEL_TEXT "Уровень"
68
#define SCORE_TEXT "Счет"
6984 leency 69
char *MENU_LIST[]={
6995 leency 70
"Новая игра",
71
"Управление",
72
"Об игре",
73
"Выход",
74
0};
75
	#else
76
#define THE_BUS_TEXT "THE BUS"
77
#define CONTROLS_TEXT "'The Bus
78
Control keys:
79
Left, Right, Space
80
Press P key for pause'tI"
81
#define ABOUT_TEXT "'The Bus
6996 leency 82
Version v1.01
6995 leency 83
Author: Leency
84
Menu image from Freepik.com'tI"
85
#define PAUSE_TEXT "PAUSE"
86
#define GAME_OVER_TEXT "GAME OVER"
87
#define FINAL_SCORE_TEXT "Final Score"
6997 leency 88
#define LIFES_TEXT "Lifes"
6995 leency 89
#define LEVEL_TEXT "Level"
90
#define SCORE_TEXT "Score"
91
char *MENU_LIST[]={
6970 leency 92
"New game",
93
"Control keys",
94
"About",
95
"Exit",
96
0};
6995 leency 97
#endif
6970 leency 98
 
99
 
6976 leency 100
void DrawObstacle(signed int x, y) {
6970 leency 101
	int don_offset_y;
102
	int image_h;
103
 
6976 leency 104
	if (y >= 0) {
105
		image_h = don_h;
106
		don_offset_y = don_image_y;
6970 leency 107
	}
108
	else {
6976 leency 109
		image_h = don_h + y;
110
		don_offset_y = don_image_y - y;
6970 leency 111
		y = 0;
112
	}
6995 leency 113
	DrawBar(x, y-don_step_y, don_w, don_step_y, COLOR_ROAD);
114
	if (y>0) && (y
7995 leency 115
	if (image_h>0) objects.draw(x, y, don_w, image_h, 0, don_offset_y);
6970 leency 116
}
7995 leency 117
void DrawBus(dword x, y) { objects.draw(x, y, bus_w, bus_h, 0, 444); }
118
void DrawBoom(dword x, y) { objects.draw(x, y, 78, 66, 0, 536); }
119
void DrawHighway() { road.draw(0,0, WIN_X, WIN_Y, 0, 0); }
120
void DrawMenuBackground() { menu.draw(0, 0, WIN_X, WIN_Y, 0, 0); }
6970 leency 121
 
122
void main()
123
{
124
	randomize();
6984 leency 125
	StartNewGame();
6970 leency 126
 
127
	load_dll(libio,  #libio_init,1);
128
	load_dll(libimg, #libimg_init,1);
7977 leency 129
	menu.load(abspath("menu.png"));
130
	road.load(abspath("road.png"));
131
	objects.load(abspath("objects.png"));
6970 leency 132
 
7985 leency 133
	loop() switch(@WaitEventTimeout(frame_timeout))
6970 leency 134
	{
7985 leency 135
		case evKey:
136
			GetKeys();
137
			if (key_scancode == SCAN_CODE_ESC)
138
			{
139
				if (screen_type==SCR_GAME) SetScreen(SCR_MENU_MAIN);
140
				else if (screen_type==SCR_MENU_MAIN) ExitProcess();
141
			}
142
			if (key_scancode == SCAN_CODE_DOWN) && (screen_type==SCR_MENU_MAIN)
143
			{
144
				if (active_menu_item<>3) active_menu_item++; ELSE active_menu_item=0;
145
				DrawMenuList();
146
			}
147
			if (key_scancode == SCAN_CODE_UP) && (screen_type==SCR_MENU_MAIN)
148
			{
149
				if (active_menu_item<>0) active_menu_item--; ELSE active_menu_item=3;
150
				DrawMenuList();
151
			}
152
			if (key_scancode == SCAN_CODE_ENTER) && (screen_type==SCR_MENU_MAIN)
153
			{
154
				if (active_menu_item==0)
6970 leency 155
				{
7985 leency 156
					StartNewGame();
157
					SetScreen(SCR_GAME);
6970 leency 158
				}
7985 leency 159
				if (active_menu_item==1) notify(CONTROLS_TEXT);
160
				if (active_menu_item==2) notify(ABOUT_TEXT);
161
				if (active_menu_item==3) ExitProcess();
162
			}
163
			if (key_scancode == SCAN_CODE_SPACE) && (screen_type==SCR_GAME)
164
			{
165
				DrawBar(bus_x*80+200, bus_y, bus_w, bus_h+1, COLOR_ROAD);
166
				if (bus_x==1) bus_x=0; else bus_x=1;
167
			}
168
			if (key_scancode == SCAN_CODE_LEFT) && (screen_type==SCR_GAME)
169
			{
170
				if (bus_x==0) break;
171
				DrawBar(bus_x*80+200, bus_y, bus_w, bus_h+1, COLOR_ROAD);
172
				bus_x=0;
173
			}
174
			if (key_scancode == SCAN_CODE_RIGHT) && (screen_type==SCR_GAME)
175
			{
176
				if (bus_x==1) break;
177
				DrawBar(bus_x*80+200, bus_y, bus_w, bus_h+1, COLOR_ROAD);
178
				bus_x=1;
179
			}
180
			if (key_scancode == SCAN_CODE_KEY_P)
181
			{
182
				if (screen_type==SCR_MENU_MAIN) break;
183
				else if (screen_type==SCR_GAME) SetScreen(SCR_PAUSE);
184
				else if (screen_type==SCR_PAUSE) SetScreen(SCR_GAME);
185
			}
186
			break;
187
 
188
		case evReDraw:
189
			DefineAndDrawWindow(250,150,WIN_X-1,WIN_Y-1,0x01,0,THE_BUS_TEXT,0); //0x74 is also possible if you fix bottom border
190
			DrawScreen();
191
			break;
192
 
193
		case evButton:
194
			ExitProcess();
195
			break;
196
 
197
		default:
198
			if (screen_type==SCR_GAME)
199
			{
200
				if ((don_x == bus_x)&&(don_y + don_h > bus_y )&&(don_y < bus_y + don_h )) {
201
					lifes--;
202
					DrawBus(bus_x*80+200,bus_y);
203
					DrawBoom(bus_x*80+180,bus_y+10);
204
					pause(150);
205
					GetNewObstacle(RAND);
206
					DrawScreen();
6970 leency 207
				}
7985 leency 208
 
209
				if (lifes==0) {
210
					DrawGameOverMessage();
211
					break;
6970 leency 212
				}
7985 leency 213
 
214
				don_y += don_step_y;
215
 
216
				if (don_y - don_step_y >= WIN_Y)
6970 leency 217
				{
7985 leency 218
					GetNewObstacle(RAND);
219
					score++;
220
					bus_y -= don_step_y+1;
221
					DrawBar(bus_x*80+200, bus_y+bus_h, bus_w, don_step_y+1, COLOR_ROAD);
222
					WriteScore();
6970 leency 223
				}
7985 leency 224
 
225
				if (score) && (score % 15 == 0)
6970 leency 226
				{
7985 leency 227
					score++;
228
					NewLevel();
229
					DrawScreen();
230
					don_step_y++;
6970 leency 231
				}
6984 leency 232
 
7985 leency 233
				DrawRoad();
234
			}
6970 leency 235
	}
236
}
237
 
6984 leency 238
void NewLevel()
239
{
240
	level++;
241
	bus_y = bus_y_default;
242
}
243
 
244
void StartNewGame()
245
{
6997 leency 246
	lifes=3;
6984 leency 247
	level=0;
248
	score=0;
249
	bus_y = bus_y_default;
250
	don_step_y = don_step_y_default;
251
	GetNewObstacle(RAND);
252
}
253
 
6970 leency 254
void WriteScore() {
7995 leency 255
	road.draw(20, 166, 120, 24, 20, 164);
6995 leency 256
	WriteText(20, 140, 0x81, 0xFFFFFF, SCORE_TEXT);
6984 leency 257
	WriteText(20, 166, 0x81, 0xFFFFFF, itoa(score));
6970 leency 258
}
259
 
260
void SetScreen(dword _screen_type) {
261
	screen_type = _screen_type;
262
	DrawScreen();
263
}
264
 
265
void DrawScreen()
266
{
6976 leency 267
	int i;
6970 leency 268
	if (screen_type==SCR_MENU_MAIN)
269
	{
270
		DrawMenuBackground();
6995 leency 271
		WriteTextB(20, 20, 0x83, 0xE8783F, THE_BUS_TEXT);
6970 leency 272
		DrawMenuList();
273
	}
274
	if (screen_type==SCR_GAME) || (screen_type==SCR_PAUSE)
275
	{
276
		DrawHighway();
6997 leency 277
		WriteText(20, 20,  0x81, 0xFFFFFF, LIFES_TEXT);
278
		WriteText(20, 46,  0x81, 0xFFFFFF, itoa(lifes));
6995 leency 279
		WriteText(20, 80,  0x81, 0xFFFFFF, LEVEL_TEXT);
6984 leency 280
		WriteText(20, 106, 0x81, 0xFFFFFF, itoa(level));
6970 leency 281
		WriteScore();
282
		DrawRoad();
283
		if (screen_type==SCR_PAUSE) {
6984 leency 284
			DrawBar(0,0,140,60,0xFF0000);
6995 leency 285
			WriteText(10,14,0x83,0xFFFfff,PAUSE_TEXT);
6976 leency 286
		}
6970 leency 287
	}
288
}
289
 
290
 
291
void DrawMenuList()
292
{
293
	int j;
294
	for (j=0; j<4; j++) DrawMenuItem(j, j);
295
}
296
 
297
void DrawMenuItem(int item_n, text_n)
298
{
299
	dword color;
300
	if (active_menu_item==item_n) color = 0xFF0000; else color = 0xFFffff;
6984 leency 301
	WriteText(20+2, item_n*56+116+2, 0x81, 0xAAAaaa, MENU_LIST[text_n]);
302
	WriteText(20, item_n*56+116, 0x81, color, MENU_LIST[text_n]);
6970 leency 303
}
304
 
305
void DrawGameOverMessage()
306
{
6984 leency 307
	DrawBar(0, 0, WIN_X, WIN_Y, 0xF3E1BD);
6995 leency 308
	WriteText(40, 40, 0x81, 0xA48C74, GAME_OVER_TEXT);
309
	WriteText(40, 75, 0x81, 0xA48C74, FINAL_SCORE_TEXT);
6984 leency 310
	WriteTextB(40, 140, 0x85, 0xA48C74, itoa(score));
6970 leency 311
	pause(350);
312
	active_menu_item=0;
6976 leency 313
	SetScreen(SCR_MENU_MAIN);
6970 leency 314
}
315
 
6976 leency 316
void GetNewObstacle(int N)
317
{
318
	int i;
319
	don_x = random(2);
6984 leency 320
	if (N==RAND) don_type = random(7); else don_type = N;
6976 leency 321
	don_h = don_h_mas[don_type];
6984 leency 322
	don_y = -don_h;
6976 leency 323
	don_image_y = 0;
6984 leency 324
	for (i = 0; i < don_type; i++) don_image_y += don_h_mas[i]+2; //calculate image y offset for current obstacle
6976 leency 325
}
326
 
6970 leency 327
#define LINE_LENGTH 10
6984 leency 328
int line_y=0;
329
void DrawLineSeparator()
6970 leency 330
{
6984 leency 331
	int y;
332
	if (screen_type == SCR_GAME) line_y += don_step_y;
6970 leency 333
	//the beginning of the white dashed line between two roadways
334
	if (line_y>=20) {
335
		line_y=0;
336
	}
337
	else
338
	{
6984 leency 339
		DrawBar(258, 0, 2, line_y, COLOR_ROAD);
340
		DrawBar(258, 0, 2, line_y-LINE_LENGTH, 0xDDE9F2);
6970 leency 341
	}
6976 leency 342
	for (y=0; y
6970 leency 343
	{
6984 leency 344
		DrawBar(258, line_y+y, 2, LINE_LENGTH, 0xDDE9F2);
345
		DrawBar(258, line_y+y+LINE_LENGTH, 2, LINE_LENGTH, COLOR_ROAD);
6970 leency 346
	}
347
}
348
 
6984 leency 349
void DrawRoad()
350
{
351
	DrawLineSeparator();
352
	DrawObstacle(don_w+10*don_x+186,don_y);
353
	DrawBus(bus_x*80+200,bus_y);
354
}
6970 leency 355
 
356
 
357
 
358
 
6984 leency 359
 
6970 leency 360
stop: