Subversion Repositories Kolibri OS

Rev

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