Subversion Repositories Kolibri OS

Rev

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