Subversion Repositories Kolibri OS

Rev

Rev 8389 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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