Author Topic: Jumping code?  (Read 3845 times)

0 Members and 1 Guest are viewing this topic.

Offline animaaron

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 9
  • Rating: +0/-0
    • View Profile
Jumping code?
« on: February 19, 2016, 02:10:48 pm »
I'm trying to make a platformer - and for the most basic platformer ever, you need jumping. I tried to implement somewhat logical physics, no matter how simple, but I obviously did something wrong. Despite seemingly making it so that the character goes up 7 pixels (4 + 2 + 1 + 0) and then down 7 blocks (0 - 1 - 2 - 4), they just fall straight down, off the screen. Here's my jumping code:
Code: [Select]
Lbl JMP
Y+A>A

If A^2=0
A/2>A
End
If A=1 or A=0 or A=-1
A--
End
If A=-2
A-2>A
End
If A=-4
4>A
End

And this is how it's called:

Code: [Select]
Repeat getKey(15)

//blah blah game loop

If getKey(54) or (A!=4)
sub(JMP)
End

//blah blah blah

End //game loop ends

And yeah, that's not optimized at all. Hahaha

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Jumping code?
« Reply #1 on: February 20, 2016, 03:23:51 pm »
I'm not sure if you know this, but '^' is the modulo operation (returns the remainder after division), not a power as in BASIC. So A^2 just checks if A is odd or even.
Code: [Select]
If A^2=0    ;Basically, this just checks if A is even.
Further, in Axe math the order of operations is left to right as you read them. So 1+3*7 can be broken down to:
1+3*7
4*7
4*7
28

Even more, ' or ' is a mathematical operation of bit-wise logic. The line A=1 or A=0 or A=-1 probably needs parentheses unless there is some super cool magic hax going on :P
Code: [Select]
(A=1) or (A=0) or (A=-1)
But it would probably be faster to do:
Code: [Select]
abs(A)<=2     ;<= is the 'less than or equal to' token

Offline animaaron

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 9
  • Rating: +0/-0
    • View Profile
Re: Jumping code?
« Reply #2 on: February 21, 2016, 02:46:14 pm »
Yes, I knew that '^' is a modulo, if A is even I want to halve it, and if it's not I want to do other things. I changed
Code: [Select]
If A=1 or (A=0) or (A=-1)to this:
Code: [Select]
If abs(A)<2  //checks if A is less than 2 (if it's 2 it should be divided by 2)
Also, I changed all of the 'If' s after my first If to ElseIfs so that only one of them evaluates to true. Now when I hit the 2nd key, my sprite doesn't fall off the screen, but it goes down 7 pixels and then doesn't move anymore, even if I hit 2nd again.

EDIT: I changed the first "If" to this:
Code: [Select]
If A^2=0 and (A>0)
and now when I hit 2nd my character goes down 7 pixels and then up infinitely. I also forgot that (0,0) is in the UPPER left hand corner, so a positive value of A would make them go down. So I need the starting value of A to be negative for them to go up and then back down
« Last Edit: February 21, 2016, 02:51:47 pm by animaaron »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Jumping code?
« Reply #3 on: February 23, 2016, 10:49:38 am »
So, I was going to reply with ideas, but then as I was testing ideas on my calc, I accidentally made some pretty decent jumping code :|

So basically, if J=1, it means a jump is in progress, J=0 means no current jump. I use V for velocity and gravity = 1pixel/iteration2.
(X,Y) are the coords for the lower left of the 8x8 sprite. The following code checks for collision detection, too!
Code: [Select]
Lbl JUMP
Return !If J
If V<<0
-V→B
While B
B-1→B
Y-1→Y
For(A,0,7)
If pxl-Test(A+X,Y-7)
7→A
Y+1→Y
0→B→V
End:End:End:End
If V>>0
V→B
While B
B-1→B
Y+1→Y
For(A,0,7)
If pxl-Test(A+X,Y)
7→A
Y-1→Y
0→J→B→V
End:End:End:End
V+1→V
Return

So if you want [Enter] to be the button for jumping, you could have this in your mainloop:
Code: [Select]
If J or getKey(9)
!If J
-6→V
1→J
End
JUMP()
End
It checks if a jump is in progress. If not, it initializes the jump with velocity==6pixels/iter. It's negative only because we want it going toward the top of the screen, so we need the Y coordinate to go down.

Further, right before the above code, you might want to check if there is any floor (on pixels) below the sprite. If not, it needs to drop down:
Code: [Select]
!If J
1→J
0→V
For(A,0,7)
If pxl-Test(X+A,Y+1)
0→J
End:End:End




My entire program looks like this (and you are free to take this code as is, modify it, whatever):
First some setup code. Setup some vars, draw a border around the screen as well as some platforms
Code: [Select]
.JUMP
FnOn
ClrDraw
1→X
62→Y
0→J
VLine(0)
VLine(95)
HLine(0)
HLine(63)
Line(0,50,24,50)
Line(20,37,44,37)
[3C4281818181423C]→Pic1
Then in the mainloop, we draw the sprite to the screen and check key presses.
Code: [Select]
Lbl LOOP
Pt-Change(X,Y-7,Pic1)
DispGraph
Pt-Change(X,Y-7,Pic1
Stop
ReturnIf getKey(15)
If getKey(2):LEFT():End
If getKey(3):RIGHT():End
!If J
1→J
0→V
For(A,0,7)
If pxl-Test(X+A,Y+1)
0→J
End:End:End
If J or getKey(9)
!If J
-6→V
1→J
End
JUMP()
End
Goto LOOP

Now for the subroutines, LEFT() and RIGHT(), we need to check for collisions.
Code: [Select]
Lbl LEFT
0→C
Y→A
For(8)
C+pxl-Test(X-1,A)→C
A-1→A
End
ReturnIf C
X-1→X
Return
Lbl RIGHT
0→C
Y→A
For(8)
C+pxl-Test(X+8,A)→C
A-1→A
End
ReturnIf C
X+1→X
Return
And then the jump code from before (note we don't need to check if J=1 since the only time we call it is when we know J=1)
Code: [Select]
Lbl JUMP
If V<<0
-V→B
While B
B-1→B
Y-1→Y
For(A,0,7)
If pxl-Test(A+X,Y-7)
7→A
Y+1→Y
0→B→V
End:End:End:End
If V>>0
V→B
While B
B-1→B
Y+1→Y
For(A,0,7)
If pxl-Test(A+X,Y)
7→A
Y-1→Y
0→J→B→V
End:End:End:End
V+1→V
Return

I hope I didn't forget anything!