Subversion Repositories Kolibri OS

Rev

Rev 6976 | 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. #define MEMSIZE 1024 * 60;
  10.  
  11. #include "..\lib\kolibri.h"
  12. #include "..\lib\random.h"
  13.  
  14. #include "..\lib\obj\libio_lib.h"
  15. #include "..\lib\obj\libimg_lib.h"
  16. #include "..\lib\patterns\libimg_load_skin.h"
  17.  
  18. #define SCALE 2
  19.  
  20. #include "draw_scalled.h"
  21.  
  22. libimg_image menu;
  23. libimg_image road;
  24.  
  25. int lives=0, level=0, score=0;
  26. int don_x, don_y, don_h=40, don_type;
  27. int bus_x, bus_y=147;
  28.  
  29. #define SCR_MENU_MAIN   1
  30. #define SCR_GAME        2
  31. #define SCR_PAUSE       3
  32.  
  33.  
  34. #define WIN_X 258 * SCALE
  35. #define WIN_Y 191 * SCALE
  36.  
  37. int screen_type=SCR_MENU_MAIN;
  38.  
  39. #define COLOR_ROAD 0x6D879B;
  40.  
  41. int active_menu_item=0;
  42.  
  43. char *ITEMS_LIST[]={
  44. "New game",
  45. "Control keys",
  46. "About",
  47. "Exit",
  48. 0};
  49.  
  50.  
  51. //actually it has to be named DrawObstacle(), but I like Donkey more :)
  52. void DrawDonkey(signed int x, y) {
  53.         int don_offset_x;
  54.         int don_offset_y;
  55.         int image_h;
  56.  
  57.         if (don_type<4) {
  58.                 don_offset_y=0;
  59.                 don_offset_x=don_type*40;
  60.         }
  61.         else {
  62.                 don_offset_y = don_h + 1;
  63.                 don_offset_x=don_type-3*40;
  64.         }
  65.  
  66.         image_h = don_h;
  67.         if (y < 0) {
  68.                 image_h = don_h + y+1;
  69.                 don_offset_y = don_h + y;
  70.                 y = 0;
  71.         }
  72.  
  73.         DrawScaledImage(road.image, x, y, 39, image_h, don_offset_x, don_offset_y);
  74. }
  75. void DrawBus(dword x, y) { DrawScaledImage(road.image, x, y, 21, 45, 237, 0); }
  76. void DrawBoom(dword x, y) { DrawScaledImage(road.image, x, y, 39, 34, 208, 47); }
  77. void DrawHighway() { DrawScaledImage(road.image, 0,0, WIN_X, WIN_Y, 0, 82); }
  78. void DrawMenuBackground() { DrawScaledImage(menu.image, 0, 0, WIN_X, WIN_Y, 0, 0); }
  79.  
  80. void main()
  81. {
  82.         randomize();
  83.  
  84.         load_dll(libio,  #libio_init,1);
  85.         load_dll(libimg, #libimg_init,1);
  86.         Libimg_LoadImage(#menu, "/sys/fonts/home2x.png");
  87.         Libimg_LoadImage(#road, "/sys/fonts/busimg2x.png");
  88.        
  89.         loop()
  90.         {
  91.                 WaitEventTimeout(1);
  92.  
  93.                 switch(EAX & 0xFF)
  94.                 {
  95.                         case evKey:
  96.                                 GetKeys();
  97.                                 if (key_scancode == SCAN_CODE_ESC)
  98.                                 {
  99.                                         if (screen_type==SCR_GAME) SetScreen(SCR_MENU_MAIN);
  100.                                         else if (screen_type==SCR_MENU_MAIN) ExitProcess();
  101.                                 }
  102.                                 if (key_scancode == SCAN_CODE_DOWN) && (screen_type==SCR_MENU_MAIN)
  103.                                 {
  104.                                         if (active_menu_item<>3) active_menu_item++; ELSE active_menu_item=0;
  105.                                         DrawMenuList();
  106.                                 }
  107.                                 if (key_scancode == SCAN_CODE_UP) && (screen_type==SCR_MENU_MAIN)
  108.                                 {
  109.                                         if (active_menu_item<>0) active_menu_item--; ELSE active_menu_item=3;
  110.                                         DrawMenuList();
  111.                                 }
  112.                                 if (key_scancode == SCAN_CODE_ENTER) && (screen_type==SCR_MENU_MAIN)
  113.                                 {
  114.                                         if (active_menu_item==0)
  115.                                         {
  116.                                                 lives=3;
  117.                                                 level=0;
  118.                                                 score=0;
  119.                                                 SetScreen(SCR_GAME);
  120.                                         }
  121.                                         if (active_menu_item==1) notify("'The Bus\nControl keys:\nLeft, Right, Space\nPress P key for pause'tI");
  122.                                         if (active_menu_item==2) notify("'The Bus\nVersion v0.2 Alpha\n\nAuthor: Leency\nMenu image from Freepik.com'tI");
  123.                                         if (active_menu_item==3) ExitProcess();
  124.                                 }                              
  125.                                 if (key_scancode == SCAN_CODE_SPACE) && (screen_type==SCR_GAME)
  126.                                 {
  127.                                         DrawScaledBar(bus_x*40+100,147, 21,45, COLOR_ROAD);
  128.                                         if (bus_x==1) bus_x=0; else bus_x=1;
  129.                                 }
  130.                                 if (key_scancode == SCAN_CODE_LEFT) && (screen_type==SCR_GAME)
  131.                                 {
  132.                                         if (bus_x==0) break;
  133.                                         DrawScaledBar(bus_x*40+100,147, 21,45, COLOR_ROAD);
  134.                                         bus_x=0;
  135.                                 }
  136.                                 if (key_scancode == SCAN_CODE_RIGHT) && (screen_type==SCR_GAME)
  137.                                 {
  138.                                         if (bus_x==1) break;
  139.                                         DrawScaledBar(bus_x*40+100,147, 21,45, COLOR_ROAD);
  140.                                         bus_x=1;
  141.                                 }
  142.                                 if (key_scancode == SCAN_CODE_KEY_P)
  143.                                 {
  144.                                         if (screen_type==SCR_MENU_MAIN) break;
  145.                                         else if (screen_type==SCR_GAME) SetScreen(SCR_PAUSE);
  146.                                         else if (screen_type==SCR_PAUSE) SetScreen(SCR_GAME);
  147.                                 }
  148.                                 break;
  149.                                
  150.                         case evReDraw:
  151.                                 DefineAndDrawWindow(250,150,WIN_X-1+10,WIN_Y-1+skin_height+7,0x74,0,"The Bus",0);
  152.                                 DrawScreen();
  153.                                 break;
  154.                                
  155.                         case evButton:
  156.                                 ExitProcess();
  157.                                 break;
  158.                                
  159.                         default:
  160.                                 if (screen_type==SCR_GAME) DrawRoad();
  161.                                 break;
  162.                 }
  163.         }
  164. }
  165.  
  166. void WriteScore() {
  167.         DrawScaledImage(road.image, 10, 83, 60, 12, 10, 83+82);
  168.         WriteScaledText(10, 70, 0x80, 0xFFFFFF, "Score");
  169.         WriteScaledText(10, 83, 0x80, 0xFFFFFF, itoa(score));
  170. }
  171.  
  172. void SetScreen(dword _screen_type) {
  173.         screen_type = _screen_type;
  174.         DrawScreen();
  175. }
  176.  
  177. void DrawScreen()
  178. {
  179.         if (screen_type==SCR_MENU_MAIN)
  180.         {
  181.                 DrawMenuBackground();
  182.                 WriteScaledText(10, 10, 0x80, 0xE8783F, "TAKE THE CHILDREN HOME");
  183.                 $add ebx, 2 << 16
  184.                 $int 64
  185.                 DrawMenuList();
  186.         }
  187.         if (screen_type==SCR_GAME) || (screen_type==SCR_PAUSE)
  188.         {
  189.                 DrawHighway();
  190.                 WriteScaledText(10, 10, 0x80, 0xFFFFFF, "Lives");
  191.                 WriteScaledText(10, 23, 0x80, 0xFFFFFF, itoa(lives));
  192.                 WriteScaledText(10, 40, 0x80, 0xFFFFFF, "Level");
  193.                 WriteScaledText(10, 53, 0x80, 0xFFFFFF, itoa(level));
  194.                 WriteScore();
  195.                 DrawRoad();
  196.                 if (screen_type==SCR_PAUSE) {
  197.                         DrawScaledBar(0,0,70,30,0xFF0000);
  198.                         WriteScaledText(5,7,0x81,0xFFFfff,"PAUSE");                    
  199.                 }      
  200.         }
  201. }
  202.  
  203.  
  204. void DrawMenuList()
  205. {
  206.         int j;
  207.         for (j=0; j<4; j++) DrawMenuItem(j, j);
  208. }
  209.  
  210. void DrawMenuItem(int item_n, text_n)
  211. {
  212.         dword color;
  213.         if (active_menu_item==item_n) color = 0xFF0000; else color = 0xFFffff;
  214.         WriteScaledText(10+1, item_n*28+48+1, 0x80, 0xAAAaaa, ITEMS_LIST[text_n]);
  215.         WriteScaledText(10, item_n*28+48, 0x80, color, ITEMS_LIST[text_n]);
  216. }
  217.  
  218.  
  219. void DrawGameOverMessage()
  220. {
  221.         DrawScaledBar(0, 0, WIN_X, WIN_Y, 0xF3E1BD);
  222.         WriteScaledText(20, 20, 0x80, 0xA48C74, "GAME OVER");
  223.         WriteScaledText(20, 40, 0x80, 0xA48C74, "FINAL SCORE");
  224.         WriteScaledText(20, 70, 0x84, 0xA48C74, itoa(score));
  225.         $add ecx, 2 << 16
  226.         $int 64
  227.         pause(350);    
  228.         active_menu_item=0;
  229.         SetScreen(SCR_MENU_MAIN);
  230.         return;
  231. }
  232.  
  233. void DrawAccident()
  234. {
  235.         DrawBus(bus_x*40+100,bus_y);
  236.         DrawBoom(bus_x*40+90,152);
  237.         pause(150);
  238.         lives--;
  239.         don_y = -don_h;
  240.         DrawScreen();
  241.         if (lives>0) DrawScaledBar(bus_x*40+90, 147-17, 39, 45+23, COLOR_ROAD);
  242. }
  243.  
  244. #define LINE_LENGTH 10
  245. void DrawRoad()
  246. {
  247.         byte y,line_y;
  248.        
  249.         if ((don_x == bus_x)&&(don_y + don_h > bus_y )&&(don_y + don_h < bus_y )) DrawAccident();
  250.        
  251.         if (lives==0) {
  252.                 DrawGameOverMessage();
  253.                 return;
  254.         }
  255.        
  256.         if (screen_type != SCR_PAUSE)
  257.         {
  258.                 line_y+=2;
  259.                 don_y+=2;
  260.         }
  261.  
  262.         //the beginning of the white dashed line between two roadways
  263.         if (line_y>=20) {
  264.                 line_y=0;
  265.         }
  266.         else
  267.         {
  268.                 DrawScaledBar(129, 0, 1, line_y, COLOR_ROAD);
  269.                 DrawScaledBar(129, 0, 1, line_y-LINE_LENGTH, 0xDDE9F2);
  270.         }
  271.         for (y=0; y<190; y+=20) //white dashed line between two roadways
  272.         {
  273.                 DrawScaledBar(129, line_y+y, 1, LINE_LENGTH, 0xDDE9F2);
  274.                 DrawScaledBar(129, line_y+y+LINE_LENGTH, 1, LINE_LENGTH, COLOR_ROAD);
  275.         }
  276.         if (don_y>=210)
  277.         {
  278.                 don_x = random(2);
  279.                 don_y = -don_h;
  280.                 don_type = random(7);
  281.                 score++;
  282.                 WriteScore();
  283.         }
  284.         DrawScaledBar(don_x*don_h+93, don_y-2, 30, 2, COLOR_ROAD); //Fill donkey old parts
  285.         DrawDonkey(don_x*don_h+90,don_y);
  286.         DrawBus(bus_x*don_h+100,147);
  287. }
  288.  
  289.  
  290.  
  291.  
  292.  
  293. stop:
  294.