Log In  

Cart #birb_tweetcart-0 | 2020-03-14 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
35

Turn with the left and right arrows keys.
If you fall to the ground you can restart the cart (⏸ -> "reset cart").

x=9y=9u=0v=0 function _update60()cls(7)v+=.07l=sqrt(u*u+v*v)a=atan2(u,v)
if(btn(0))a+=.014
if(btn(1))a-=.014
w=.03-cos(a*2)/50u=l*cos(a)v=l*sin(a)x=(x+u+8)%144-8 y+=v
if(y>120)u*=.9v/=-2y=120
for b=a-w,a+w,w*2 do e=x-8*cos(b)line(x,121,e,121,6)line(x,y,e,y-8*sin(b),0)end end

Annotated:

-- Position
x=9
y=9

-- Velocity
u=0
v=0

function _update60()
  cls(7)

  -- Gravity!
  v+=.07

  -- Convert velocity into polar representation (length and angle).
  l=sqrt(u*u+v*v)
  a=atan2(u,v)

  -- Input! Rotate the angle based on arrow keys:
  -- Left  -> counterclockwise
  -- Right -> clockwise
  if(btn(0))a+=.014
  if(btn(1))a-=.014

  -- Convert velocity from polar back into cartesian.
  u=l*cos(a)
  v=l*sin(a)

  -- Update position using velocity!
  -- x is wrapped around the edges. We could just use x=x%128, but then you
  -- would see the bird teleport at the edges. Instead we add 8px of buffer
  -- at each edge.
  x=(x+u+8)%144-8
  y+=v

  -- Simple bounce at the bottom of the screen.
  -- u*=.9 slightly slows the horizontal velocity
  -- v/=-2 reverse and halves the vertical velocity
  -- y=120 pops it back above the bounce breakpoint
  if(y>120) u*=.9 v/=-2 y=120

  -- The bird is drawn using lines that start at the current position and extend
  -- in the direction opposite of the velocity.
  -- We'll add/subtract w from the polar angle to get the different
  -- angles of the two lines.
  w=.03-cos(a*2)/50

  for b=a-w,a+w,w*2 do
    -- The x position of the tail.
    e=x-8*cos(b)

    -- The shadow
    line(x,121,e,121,6)

    -- The bird
    line(x,y,e,y-8*sin(b),1)
  end
end

P#73903 2020-03-14 05:13 ( Edited 2020-03-14 05:22)

1

Neat and very smooth, though holding left or right down keeps you suspended indefinitely as far as I can tell so there's not much challenge.

P#73910 2020-03-14 07:16
1

So simple yet so fun!

P#73917 2020-03-14 13:06
1

Wonderful stuff!

P#73922 2020-03-14 18:22
1

Wow this is so impressive

P#73929 2020-03-14 23:30

we need a full flying game using this awesome cart

P#73970 2020-03-16 04:20

I loved this, and also appreciate the extra love you gave by providing annotated code!

I may be missing something obvious here, but I can't for the life of me decide what you are representing with the letter u

The math & code I get, I just want to know what that 'u' means in your mind. I've tried to substitute everything I cam imagine, but I'm drawing a blank.

Thanks for sharing this!

P#74535 2020-04-07 17:50

@iiviiGames So ideally I'd name the velocity variables vx and vy. But we gotta save characters, so I usually go with a pair of letters that are next to each other in the alphabet. I like u and v since they match the velocity theme!

u-v is also commonly used as an alternative to x-y/x-y-z, such as UV coordinates in 3D graphics.

P#74548 2020-04-08 02:14

Okay, I thought it was an abstract thing, so that makes sense!

P#74603 2020-04-09 19:38
1

Ok, now I want to make a paper airplane game.

P#109922 2022-04-08 17:50
1

Very nice, @lucatron. That just barely fit in the TWEET space maximum 280-chars whereas your cart is 275-chars.

Here, I did a little compression and made it 272-chars.

Cart #birb_tweetcart2-0 | 2022-04-08 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

In any case, gold star work !

P#109924 2022-04-08 18:32 ( Edited 2022-04-08 18:32)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 15:27:43 | 0.027s | Q:38