Subversion Repositories Kolibri OS

Rev

Rev 7977 | 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\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.         menu.load(abspath("menu.png"));
  130.         road.load(abspath("road.png"));
  131.         objects.load(abspath("objects.png"));
  132.        
  133.         loop() switch(@WaitEventTimeout(frame_timeout))
  134.         {
  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)
  155.                                 {
  156.                                         StartNewGame();
  157.                                         SetScreen(SCR_GAME);
  158.                                 }
  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();
  207.                                 }
  208.  
  209.                                 if (lifes==0) {
  210.                                         DrawGameOverMessage();
  211.                                         break;
  212.                                 }
  213.  
  214.                                 don_y += don_step_y;
  215.  
  216.                                 if (don_y - don_step_y >= WIN_Y)
  217.                                 {
  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();
  223.                                 }
  224.  
  225.                                 if (score) && (score % 15 == 0)
  226.                                 {
  227.                                         score++;
  228.                                         NewLevel();
  229.                                         DrawScreen();
  230.                                         don_step_y++;
  231.                                 }
  232.  
  233.                                 DrawRoad();
  234.                         }
  235.         }
  236. }
  237.  
  238. void NewLevel()
  239. {
  240.         level++;
  241.         bus_y = bus_y_default;
  242. }
  243.  
  244. void StartNewGame()
  245. {
  246.         lifes=3;
  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.  
  254. void WriteScore() {
  255.         DrawLibImage(road.image, 20, 166, 120, 24, 20, 164);
  256.         WriteText(20, 140, 0x81, 0xFFFFFF, SCORE_TEXT);
  257.         WriteText(20, 166, 0x81, 0xFFFFFF, itoa(score));
  258. }
  259.  
  260. void SetScreen(dword _screen_type) {
  261.         screen_type = _screen_type;
  262.         DrawScreen();
  263. }
  264.  
  265. void DrawScreen()
  266. {
  267.         int i;
  268.         if (screen_type==SCR_MENU_MAIN)
  269.         {
  270.                 DrawMenuBackground();
  271.                 WriteTextB(20, 20, 0x83, 0xE8783F, THE_BUS_TEXT);
  272.                 DrawMenuList();
  273.         }
  274.         if (screen_type==SCR_GAME) || (screen_type==SCR_PAUSE)
  275.         {
  276.                 DrawHighway();
  277.                 WriteText(20, 20,  0x81, 0xFFFFFF, LIFES_TEXT);
  278.                 WriteText(20, 46,  0x81, 0xFFFFFF, itoa(lifes));
  279.                 WriteText(20, 80,  0x81, 0xFFFFFF, LEVEL_TEXT);
  280.                 WriteText(20, 106, 0x81, 0xFFFFFF, itoa(level));
  281.                 WriteScore();
  282.                 DrawRoad();
  283.                 if (screen_type==SCR_PAUSE) {
  284.                         DrawBar(0,0,140,60,0xFF0000);
  285.                         WriteText(10,14,0x83,0xFFFfff,PAUSE_TEXT);                     
  286.                 }
  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;
  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]);
  303. }
  304.  
  305. void DrawGameOverMessage()
  306. {
  307.         DrawBar(0, 0, WIN_X, WIN_Y, 0xF3E1BD);
  308.         WriteText(40, 40, 0x81, 0xA48C74, GAME_OVER_TEXT);
  309.         WriteText(40, 75, 0x81, 0xA48C74, FINAL_SCORE_TEXT);
  310.         WriteTextB(40, 140, 0x85, 0xA48C74, itoa(score));
  311.         pause(350);    
  312.         active_menu_item=0;
  313.         SetScreen(SCR_MENU_MAIN);      
  314. }
  315.  
  316. void GetNewObstacle(int N)
  317. {
  318.         int i;
  319.         don_x = random(2);
  320.         if (N==RAND) don_type = random(7); else don_type = N;
  321.         don_h = don_h_mas[don_type];
  322.         don_y = -don_h;
  323.         don_image_y = 0;
  324.         for (i = 0; i < don_type; i++) don_image_y += don_h_mas[i]+2; //calculate image y offset for current obstacle
  325. }
  326.  
  327. #define LINE_LENGTH 10
  328. int line_y=0;
  329. void DrawLineSeparator()
  330. {
  331.         int y;
  332.         if (screen_type == SCR_GAME) line_y += don_step_y;
  333.         //the beginning of the white dashed line between two roadways
  334.         if (line_y>=20) {
  335.                 line_y=0;
  336.         }
  337.         else
  338.         {
  339.                 DrawBar(258, 0, 2, line_y, COLOR_ROAD);
  340.                 DrawBar(258, 0, 2, line_y-LINE_LENGTH, 0xDDE9F2);
  341.         }
  342.         for (y=0; y<WIN_Y-20; y+=20) //white dashed line between two roadways
  343.         {
  344.                 DrawBar(258, line_y+y, 2, LINE_LENGTH, 0xDDE9F2);
  345.                 DrawBar(258, line_y+y+LINE_LENGTH, 2, LINE_LENGTH, COLOR_ROAD);
  346.         }
  347. }
  348.  
  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. }
  355.  
  356.  
  357.  
  358.  
  359.  
  360. stop:
  361.