Author Topic: Jumping code?  (Read 2299 times)

0 Members and 1 Guest are viewing this topic.

animaaron

• LV1 Newcomer (Next: 20)
• Posts: 9
• Rating: +0/-0
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 JMPY+A>AIf A^2=0A/2>AEndIf A=1 or A=0 or A=-1A--EndIf A=-2A-2>AEndIf A=-44>AEnd
And this is how it's called:

Code: [Select]
Repeat getKey(15)//blah blah game loopIf getKey(54) or (A!=4)sub(JMP)End//blah blah blahEnd //game loop ends
And yeah, that's not optimized at all. Hahaha

Xeda112358

• they/them
• Moderator
• LV12 Extreme Poster (Next: 5000)
• Posts: 4684
• Rating: +718/-6
• Calc-u-lator, do doo doo do do do.
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
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

animaaron

• LV1 Newcomer (Next: 20)
• Posts: 9
• Rating: +0/-0
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 »

Xeda112358

• they/them
• Moderator
• LV12 Extreme Poster (Next: 5000)
• Posts: 4684
• Rating: +718/-6
• Calc-u-lator, do doo doo do do do.
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 JUMPReturn !If JIf V<<0-V→BWhile BB-1→BY-1→YFor(A,0,7)If pxl-Test(A+X,Y-7)7→AY+1→Y0→B→VEnd:End:End:EndIf V>>0V→BWhile BB-1→BY+1→YFor(A,0,7)If pxl-Test(A+X,Y)7→AY-1→Y0→J→B→VEnd:End:End:EndV+1→VReturn
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→V1→JEndJUMP()EndIt 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 J1→J0→VFor(A,0,7)If pxl-Test(X+A,Y+1)0→JEnd: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]
.JUMPFnOnClrDraw1→X62→Y0→JVLine(0)VLine(95)HLine(0)HLine(63)Line(0,50,24,50)Line(20,37,44,37)[3C4281818181423C]→Pic1Then in the mainloop, we draw the sprite to the screen and check key presses.
Code: [Select]
Lbl LOOPPt-Change(X,Y-7,Pic1)DispGraphPt-Change(X,Y-7,Pic1StopReturnIf getKey(15)If getKey(2):LEFT():EndIf getKey(3):RIGHT():End!If J1→J0→VFor(A,0,7)If pxl-Test(X+A,Y+1)0→JEnd:End:EndIf J or getKey(9)!If J-6→V1→JEndJUMP()EndGoto LOOP
Now for the subroutines, LEFT() and RIGHT(), we need to check for collisions.
Code: [Select]
Lbl LEFT0→CY→AFor(8)C+pxl-Test(X-1,A)→CA-1→AEndReturnIf CX-1→XReturnLbl RIGHT0→CY→AFor(8)C+pxl-Test(X+8,A)→CA-1→AEndReturnIf CX+1→XReturnAnd 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 JUMPIf V<<0-V→BWhile BB-1→BY-1→YFor(A,0,7)If pxl-Test(A+X,Y-7)7→AY+1→Y0→B→VEnd:End:End:EndIf V>>0V→BWhile BB-1→BY+1→YFor(A,0,7)If pxl-Test(A+X,Y)7→AY-1→Y0→J→B→VEnd:End:End:EndV+1→VReturn
I hope I didn't forget anything!