Advertisement
WarPie90

Random game stuff Simba 1.4

Oct 29th, 2023
1,733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.98 KB | None | 0 0
  1. program new;
  2. {$I SRL/osr.simba}
  3.  
  4. const
  5.   WIDTH  = 720;
  6.   HEIGHT = 480;
  7.  
  8. type
  9.   TGame = record
  10.     image: TMufasaBitmap;
  11.     viewpos:  Vector2;
  12.     charpos,oldpos:  Vector2;
  13.     velocity: Vector2;
  14.  
  15.     obstacles: TBoxArray;
  16.  
  17.     characterImg: TMufasaBitmap;
  18.   end;
  19.  
  20.   TKey  = (SPACE, LEFT, RIGHT, X_KEY);
  21.   TKeys = set of TKey;
  22.  
  23.  
  24. var
  25.   character_l, character_r: TMufasaBitmap;
  26.  
  27. function TGame.GetCharLocation(): Vector2; constref;
  28. begin
  29.   Result.x := charpos.x + 20;
  30.   Result.y := height - charpos.y - 20;
  31. end;
  32.  
  33. procedure TGame.DrawThings();
  34. var
  35.   loc: Vector2;
  36.   b: TBox;
  37.   vp: TPoint;
  38. begin
  39.   loc := Self.GetCharLocation();
  40.   characterImg.DrawTransparent(Max(0,Ceil(loc.x)-20), Max(0,Ceil(loc.y)-20), Self.Image);
  41.  
  42.   vp := viewpos.ToPoint();
  43.   for b in Self.obstacles do
  44.   begin
  45.     b := [b.x1,height - b.y1,b.x2,height - b.y2];
  46.     b := [b.x1-vp.x, b.y1-vp.y, b.x2-vp.x, b.y2-vp.y];
  47.     Self.Image.DrawBoxFilled(b, False, $33AA55);
  48.   end;
  49. end;
  50.  
  51. procedure TGame.Render();
  52. var tmp: TMufasaBitmap;
  53. begin
  54.   Self.image.DrawClear(0);
  55.   Self.DrawThings();
  56.   DrawBitmapDebugImg(self.image);
  57. end;
  58.  
  59. function TGame.GetInputKeys(): TKeys;
  60. begin
  61.   if isKeyDown(VK_SPACE) then Result := [SPACE];
  62.   if isKeyDown(VK_LEFT)  then Result += [LEFT];
  63.   if isKeyDown(VK_RIGHT) then Result += [RIGHT];
  64.   if isKeyDown(VK_X)     then Result += [X_KEY];
  65. end;
  66.  
  67. procedure TGame.Setup();
  68. var b: TBox;
  69. begin
  70.   Self.image.Init();
  71.   Self.image.SetSize(WIDTH, HEIGHT);
  72.   ShowBitmap(Self.image);
  73.   DisplayDebugImgWindow(WIDTH, HEIGHT);
  74.   Self.charpos := [430, 161];
  75.   Self.velocity := [0,1];
  76.  
  77.   character_l.Init();
  78.   character_l.LoadFromFile('images/mario_small_l.png');
  79.   character_l.ReplaceColor($FFFFFF,0);
  80.  
  81.   character_r.Init();
  82.   character_r.LoadFromFile('images/mario_small_r.png');
  83.   character_r.ReplaceColor($FFFFFF,0);
  84.  
  85.   Self.obstacles += Box(150, 200, 400, 180);
  86.   Self.obstacles += Box(100, 50,  150, 0);
  87.   Self.obstacles += Box(400, 160, 450, 0);
  88.  
  89.   for 0 to 400 do
  90.   begin
  91.     b := Box(Random(50000), Random(40,500), 0, 0);
  92.     if Random() > 0.5 then
  93.     begin
  94.       b.x2 := b.x1 + Random(100,400);
  95.       b.y2 := Max(0, b.y1 - Random(80));
  96.     end else
  97.     begin
  98.       b.x2 := b.x1 + Random(10,70);
  99.       b.y2 := Max(0, b.y1 - Random(250));
  100.     end;
  101.  
  102.     Self.obstacles += b;
  103.   end;
  104.  
  105.   characterImg := character_r;
  106. end;
  107.  
  108. // should be rewritten properly
  109. procedure TGame.WallTest();
  110. var
  111.   W_M := WIDTH-40;
  112.   H_M := HEIGHT-40;
  113.   b: TBox;
  114.   this,vp: TPoint;
  115.   d1,d2: Double;
  116.   bounds: TBox;
  117.   top_l,top_r,btm_l,btm_r: TPoint;
  118. begin
  119.   bounds := [2,2,W_M,H_M];
  120.  
  121.   // fix me for proper collision: To high speed sends us though objects
  122.   this := charpos.ToPoint() + Point(20,0);
  123.   vp := Self.viewpos.ToPoint();
  124.   for b in Self.obstacles do
  125.   begin
  126.     b := [b.x1-vp.x, b.y1-vp.y, b.x2-vp.x, b.y2-vp.y];
  127.  
  128.     // floor                                                                                        //if higher up
  129.     if InRange(this.x, b.x1-20,b.x2+20) and (InRange(this.y, b.y2,b.y1) or (this.y >= b.y1)) and (b.y1 >= bounds.y1) then
  130.       bounds.y1 := b.y1+3;
  131.  
  132.     // roof                                                                                         //if lower down
  133.     if InRange(this.x, b.x1-20,b.x2+20) and (InRange(this.y, b.y2,b.y1) or (this.y < b.y1)) and (b.y2 <= bounds.y2) then
  134.       bounds.y2 := b.y2-43;
  135.  
  136.  
  137.     // ---> || wall to the right
  138.     if InRange(this.y, b.y2-39,b.y1-5) and (InRange(this.x, b.x1,b.x2) or (this.x <= b.x1)) and (b.x1 <= bounds.x2) then
  139.       bounds.x2 := b.x1-43;
  140.  
  141.     // || <--- wall to the left
  142.     if InRange(this.y, b.y2-39,b.y1-5) and (InRange(this.x, b.x1,b.x2) or (this.x >= b.x1)) and (b.x2 >= bounds.x1) then
  143.       bounds.x1 := b.x2+3;
  144.   end;
  145.  
  146.  
  147.   if charpos.x >  bounds.x2 then begin charpos.x := bounds.x2; velocity.x := Min(velocity.x, 0); end;
  148.   if charpos.x <= bounds.x1 then begin charpos.x := bounds.x1; velocity.x := Max(velocity.x, 0); end;
  149.   if charpos.y >  bounds.y2 then begin charpos.y := bounds.y2; end;
  150.   if charpos.y <= bounds.y1 then begin charpos.y := bounds.y1; end;
  151.   if charpos.y = bounds.y1 then velocity.y := Max(0,velocity.y); //hit a floor
  152.   if charpos.y = bounds.y2 then velocity.y := Min(0,velocity.y); //hit a roof
  153. end;
  154.  
  155. procedure TGame.Thing();
  156. var
  157.   keys: TKeys;
  158.   jmpTimer, dblJmpTimer: TCountDown;
  159.   i: Int32;
  160. begin
  161.   jmpTimer.Init(0);
  162.   dblJmpTimer.Init(0);
  163.  
  164.   while True do
  165.   begin
  166.     keys := Self.GetInputKeys();
  167.  
  168.     //writeln keys;
  169.     if (SPACE in keys) and (jmpTimer.IsFinished) then
  170.     begin
  171.       //WriteLn('velo: ', Self.velocity.y);
  172.  
  173.       if Self.velocity.y = 0 then
  174.       begin
  175.         Self.velocity.y := 1.8;
  176.       end
  177.       else if (Self.velocity.y > 0) and (Self.velocity.y < 1) and dblJmpTimer.IsFinished() then
  178.       begin
  179.         //WriteLn('Double!!', Self.velocity.y);
  180.         Self.velocity.y := 1.3;
  181.         dblJmpTimer.Init(400);
  182.       end;
  183.  
  184.       jmpTimer.Init(150);
  185.     end;
  186.  
  187.     if (RIGHT in keys) then
  188.     begin
  189.       Self.velocity.x := Max(10,Self.velocity.x+0.01);
  190.       characterImg := character_r;
  191.     end;
  192.  
  193.     if (LEFT in keys) then
  194.     begin
  195.       Self.velocity.x := Min(-10,Self.velocity.x-0.01);
  196.       characterImg := character_l;
  197.     end;
  198.  
  199.     self.oldpos    := Self.charpos;
  200.     Self.charpos.y += Self.velocity.y;
  201.  
  202.     if (charpos.x+Self.velocity.x/10+1 > WIDTH - 200) or (charpos.x+Self.velocity.x/10-1 < 200) then
  203.       self.viewpos.x += Self.velocity.x/10
  204.     else
  205.       Self.charpos.x += Self.velocity.x/10;
  206.  
  207.     Self.Render();
  208.  
  209.     Sleep(0);
  210.  
  211.     // break down towards zero x-speed
  212.     if Self.velocity.x <> 0 then
  213.     begin
  214.       if Self.velocity.x > 0 then Self.velocity.x := Max(0, Self.velocity.x-1);
  215.       if Self.velocity.x < 0 then Self.velocity.x := Min(0, Self.velocity.x+1);
  216.     end;
  217.  
  218.     // reduce jump velocity
  219.     Self.velocity.y := Self.velocity.y - 0.0100;
  220.     Self.WallTest();
  221.   end;
  222. end;
  223.  
  224.  
  225. var
  226.   game: TGame;
  227. begin
  228.   RandSeed := 50;
  229.   game.Setup();
  230.   game.Thing();
  231. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement