Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- {$I SRL/osr.simba}
- const
- WIDTH = 720;
- HEIGHT = 480;
- type
- TGame = record
- image: TMufasaBitmap;
- viewpos: Vector2;
- charpos,oldpos: Vector2;
- velocity: Vector2;
- obstacles: TBoxArray;
- characterImg: TMufasaBitmap;
- end;
- TKey = (SPACE, LEFT, RIGHT, X_KEY);
- TKeys = set of TKey;
- var
- character_l, character_r: TMufasaBitmap;
- function TGame.GetCharLocation(): Vector2; constref;
- begin
- Result.x := charpos.x + 20;
- Result.y := height - charpos.y - 20;
- end;
- procedure TGame.DrawThings();
- var
- loc: Vector2;
- b: TBox;
- vp: TPoint;
- begin
- loc := Self.GetCharLocation();
- characterImg.DrawTransparent(Max(0,Ceil(loc.x)-20), Max(0,Ceil(loc.y)-20), Self.Image);
- vp := viewpos.ToPoint();
- for b in Self.obstacles do
- begin
- b := [b.x1,height - b.y1,b.x2,height - b.y2];
- b := [b.x1-vp.x, b.y1-vp.y, b.x2-vp.x, b.y2-vp.y];
- Self.Image.DrawBoxFilled(b, False, $33AA55);
- end;
- end;
- procedure TGame.Render();
- var tmp: TMufasaBitmap;
- begin
- Self.image.DrawClear(0);
- Self.DrawThings();
- DrawBitmapDebugImg(self.image);
- end;
- function TGame.GetInputKeys(): TKeys;
- begin
- if isKeyDown(VK_SPACE) then Result := [SPACE];
- if isKeyDown(VK_LEFT) then Result += [LEFT];
- if isKeyDown(VK_RIGHT) then Result += [RIGHT];
- if isKeyDown(VK_X) then Result += [X_KEY];
- end;
- procedure TGame.Setup();
- var b: TBox;
- begin
- Self.image.Init();
- Self.image.SetSize(WIDTH, HEIGHT);
- ShowBitmap(Self.image);
- DisplayDebugImgWindow(WIDTH, HEIGHT);
- Self.charpos := [430, 161];
- Self.velocity := [0,1];
- character_l.Init();
- character_l.LoadFromFile('images/mario_small_l.png');
- character_l.ReplaceColor($FFFFFF,0);
- character_r.Init();
- character_r.LoadFromFile('images/mario_small_r.png');
- character_r.ReplaceColor($FFFFFF,0);
- Self.obstacles += Box(150, 200, 400, 180);
- Self.obstacles += Box(100, 50, 150, 0);
- Self.obstacles += Box(400, 160, 450, 0);
- for 0 to 400 do
- begin
- b := Box(Random(50000), Random(40,500), 0, 0);
- if Random() > 0.5 then
- begin
- b.x2 := b.x1 + Random(100,400);
- b.y2 := Max(0, b.y1 - Random(80));
- end else
- begin
- b.x2 := b.x1 + Random(10,70);
- b.y2 := Max(0, b.y1 - Random(250));
- end;
- Self.obstacles += b;
- end;
- characterImg := character_r;
- end;
- // should be rewritten properly
- procedure TGame.WallTest();
- var
- W_M := WIDTH-40;
- H_M := HEIGHT-40;
- b: TBox;
- this,vp: TPoint;
- d1,d2: Double;
- bounds: TBox;
- top_l,top_r,btm_l,btm_r: TPoint;
- begin
- bounds := [2,2,W_M,H_M];
- // fix me for proper collision: To high speed sends us though objects
- this := charpos.ToPoint() + Point(20,0);
- vp := Self.viewpos.ToPoint();
- for b in Self.obstacles do
- begin
- b := [b.x1-vp.x, b.y1-vp.y, b.x2-vp.x, b.y2-vp.y];
- // floor //if higher up
- 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
- bounds.y1 := b.y1+3;
- // roof //if lower down
- 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
- bounds.y2 := b.y2-43;
- // ---> || wall to the right
- 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
- bounds.x2 := b.x1-43;
- // || <--- wall to the left
- 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
- bounds.x1 := b.x2+3;
- end;
- if charpos.x > bounds.x2 then begin charpos.x := bounds.x2; velocity.x := Min(velocity.x, 0); end;
- if charpos.x <= bounds.x1 then begin charpos.x := bounds.x1; velocity.x := Max(velocity.x, 0); end;
- if charpos.y > bounds.y2 then begin charpos.y := bounds.y2; end;
- if charpos.y <= bounds.y1 then begin charpos.y := bounds.y1; end;
- if charpos.y = bounds.y1 then velocity.y := Max(0,velocity.y); //hit a floor
- if charpos.y = bounds.y2 then velocity.y := Min(0,velocity.y); //hit a roof
- end;
- procedure TGame.Thing();
- var
- keys: TKeys;
- jmpTimer, dblJmpTimer: TCountDown;
- i: Int32;
- begin
- jmpTimer.Init(0);
- dblJmpTimer.Init(0);
- while True do
- begin
- keys := Self.GetInputKeys();
- //writeln keys;
- if (SPACE in keys) and (jmpTimer.IsFinished) then
- begin
- //WriteLn('velo: ', Self.velocity.y);
- if Self.velocity.y = 0 then
- begin
- Self.velocity.y := 1.8;
- end
- else if (Self.velocity.y > 0) and (Self.velocity.y < 1) and dblJmpTimer.IsFinished() then
- begin
- //WriteLn('Double!!', Self.velocity.y);
- Self.velocity.y := 1.3;
- dblJmpTimer.Init(400);
- end;
- jmpTimer.Init(150);
- end;
- if (RIGHT in keys) then
- begin
- Self.velocity.x := Max(10,Self.velocity.x+0.01);
- characterImg := character_r;
- end;
- if (LEFT in keys) then
- begin
- Self.velocity.x := Min(-10,Self.velocity.x-0.01);
- characterImg := character_l;
- end;
- self.oldpos := Self.charpos;
- Self.charpos.y += Self.velocity.y;
- if (charpos.x+Self.velocity.x/10+1 > WIDTH - 200) or (charpos.x+Self.velocity.x/10-1 < 200) then
- self.viewpos.x += Self.velocity.x/10
- else
- Self.charpos.x += Self.velocity.x/10;
- Self.Render();
- Sleep(0);
- // break down towards zero x-speed
- if Self.velocity.x <> 0 then
- begin
- if Self.velocity.x > 0 then Self.velocity.x := Max(0, Self.velocity.x-1);
- if Self.velocity.x < 0 then Self.velocity.x := Min(0, Self.velocity.x+1);
- end;
- // reduce jump velocity
- Self.velocity.y := Self.velocity.y - 0.0100;
- Self.WallTest();
- end;
- end;
- var
- game: TGame;
- begin
- RandSeed := 50;
- game.Setup();
- game.Thing();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement