Subversion Repositories Kolibri OS

Rev

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