Created
September 30, 2024 17:18
-
-
Save Rayquaza01/802ccea3ac1ce22a9a812d17f84e6bd6 to your computer and use it in GitHub Desktop.
balloon-run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function move_towards(x, y) | |
| if abs(x - y) < s then | |
| return y | |
| elseif x > y then | |
| return x - s | |
| elseif x < y then | |
| return x + s | |
| end | |
| end | |
| function repeat_value(t) | |
| tbl = {} | |
| for i = 1, t, 1 do | |
| add(tbl, 1) | |
| end | |
| return unpack(tbl) | |
| end | |
| function range(start, stop) | |
| tbl={} | |
| for i = start, stop, 1 do | |
| add(tbl, i) | |
| end | |
| return tbl | |
| end | |
| Entity = { velocity = vec(0, 0), scale = 3 } | |
| function Entity:init(o) | |
| o = o or Entity | |
| setmetatable(o, self) | |
| self.__index = self | |
| return o | |
| end | |
| function Entity:update() | |
| local x, y = self.position:get(0, 2) | |
| local real_height = self.sprite:height() * self.scale | |
| local real_width = self.sprite:width() * self.scale | |
| if m:get(flr(x / 16), flr((y + real_height) / 16)) == 1 then | |
| self.on_ground = true | |
| -- stop downwards velocity when on ground | |
| self.velocity:set(1, 0) | |
| offset = (y + real_height) % 16 | |
| self.position:set(1, y - offset) | |
| else | |
| self.on_ground = false | |
| -- if hit ceiling, invert y speed | |
| if m:get(flr(x / 16),flr((y - 1) / 16)) == 1 or y < 0 then | |
| self.velocity *= vec(1, -1) | |
| end | |
| -- if hit wall, invert x speed | |
| for i in all(range(1, real_height - 1)) do | |
| if m:get(flr((x - 1) / 16), flr((y + i) / 16)) == 1 or m:get(flr((x + real_width) / 16), flr((y + i) / 16)) == 1 then | |
| self.velocity *= vec(-1, 1) | |
| break | |
| end | |
| end | |
| end | |
| if (x + real_width) < 0 then | |
| self.position:set(0, 480) | |
| end | |
| if x > 480 then | |
| self.position:set(0, 0) | |
| end | |
| if y > 300 and self.sprite == player then | |
| return init_player() | |
| end | |
| if not self.on_ground then | |
| self.velocity += vec(0, .98) * s | |
| end | |
| end | |
| function Entity:move() | |
| self.velocity:set(0, move_towards(self.velocity:get(0), 0)) | |
| self.position += self.velocity | |
| end | |
| function Entity:draw() | |
| local x, y = self.position:get(0, 2) | |
| sspr(self.sprite, 0, 0, self.sprite:width(), self.sprite:height(), x, y, self.sprite:width() * self.scale, self.sprite:height() * self.scale) | |
| --print(string.format("%.0f, %.0f", x, y), x, y) | |
| end | |
| function init_player() | |
| m = userdata("i16", 30, 17) | |
| m:set(0, 16, repeat_value(10)) | |
| m:set(20, 16, repeat_value(10)) | |
| m:set(7, 10, repeat_value(8)) | |
| m:set(13, 5, repeat_value(8)) | |
| p = Entity:init{ position = vec(32, 232), sprite = player } | |
| enemies = {} | |
| timer = 0 | |
| end | |
| function _init() | |
| s = 1 / 30 | |
| --hi = fetch("/appdata/br") or 0 | |
| hi = 0 | |
| player = userdata("[gfx]0308080888080500ff0ff0cc0110[/gfx]") | |
| --player = unpod("b64:bHo0AB0AAAAbAAAA8AxweHUAQyADCAQACAAoAAgABRAfDwAeABwAEQA=") | |
| ground = userdata("[gfx]08083333333333333333334433333444454344544444444444444444544444444444[/gfx]") | |
| --ground = unpod("b64:bHo0ABkAAAAXAAAA8AhweHUAQyAICATzAhRDNAUEAxQF9AEFpA==") | |
| enemy = userdata("[gfx]060800cc000cccc0cccccc060060009900009900009900009900[/gfx]") | |
| --enemy = unpod("b64:bHo0AB4AAAAcAAAA8A1weHUAQyAGCAQQHCA8AFwABhAGIBkwGTAZMBkQ") | |
| init_player() | |
| end | |
| function _update() | |
| x, y = p.position:get(0, 2) | |
| if timer != 0 then | |
| timer += 1 | |
| elseif btn(0) or btn(1) or btn(4) then | |
| timer = 1 | |
| end | |
| if timer > hi then | |
| hi = timer | |
| end | |
| if timer % 300 == 1 then | |
| add(enemies, Entity:init{ | |
| position = vec(rnd(480), 0), | |
| sprite = enemy | |
| }) | |
| end | |
| p:update() | |
| for e in all(enemies) do | |
| local e_x = e.position:get(0) | |
| local e_y = e.position:get(1) | |
| e:update() | |
| if y < e_y then | |
| e.velocity -= vec(0, 2) * s | |
| e.on_ground = false | |
| end | |
| local dist = x - e_x | |
| if dist < 0 and abs(e.velocity:get(0)) < abs(dist) then | |
| e.velocity -= vec(2, 0) * s | |
| elseif dist > 0 and abs(e.velocity:get(0)) < abs(dist) then | |
| e.velocity += vec(2, 0) * s | |
| end | |
| -- reset game if player hits enemy | |
| --if e_x < x + 9 and e_x + 12 > x and e_y > y + 24 and e_y + 24 < y then | |
| if e_x >= x and e_x <= x + 9 and e_y >= y and e_y <= y + 24 then | |
| -- store("/appdata/br", hi) | |
| init_player() | |
| end | |
| e:move() | |
| end | |
| -- left | |
| if btn(0) then p.velocity -= vec(2, 0) * s end | |
| -- right | |
| if btn(1) then p.velocity += vec(2, 0) * s end | |
| -- z | |
| if btn(4) then | |
| p.velocity -= vec(0, 2) * s | |
| p.on_ground = false | |
| end | |
| p:move() | |
| end | |
| function _draw() | |
| cls() | |
| for w = 0, 30, 1 do | |
| for h = 0, 17, 1 do | |
| if m:get(w, h) == 1 then sspr(ground, 0, 0, 8, 8, w * 16, h * 16, 16, 16) end | |
| end | |
| end | |
| for e in all(enemies) do | |
| e:draw() | |
| end | |
| p:draw() | |
| ?string.format("\f7%.2f", timer / 30) | |
| ?string.format("\fa%.2f", hi / 30) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment