Discuss Scratch

Kevin_Koala
Scratcher
2 posts

Need Help Making Platformer Game Code

Does anyone have any ideas on how to make code for a platformer game without using an engine or watching a long video?
I am good with levels and design, just not making the code
cs3540623
Scratcher
8 posts

Need Help Making Platformer Game Code

FIRST
GreatGamer737363_Alt
Scratcher
51 posts

Need Help Making Platformer Game Code

If you want the simple breakdown:
Make XV and YV variables and make sure that x is XV and y is YV.
Make it so that when you click the right arrow it makes XV go up by 1, and vice versa with the left arrow.
Now set XV x 0.9.
Make a sprite that will be your level.

If touching the color of the platforms in the level sprite (which in this case while be black), change y by 1, and put the if condition inside the first one so it looks like

if touching color black
change y by 1
if touching color black
change y by 1

Until you have four if conditions like that.

And by the way, if you haven't already, make sure everything like the right and left movement are in a forever condition

On the fifth condition, make it so that it changes X (not the XV variable) to XV x 2 and make change Y (not the YV variable) by -5.

Now, if up arrow is pressed (make sure this is an if else condition by the way)
if xv = 0 set xv to 50,

if else

set xv to 10.

I am so sorry, Scratch's forum post limit caps off here, I'll explain more in the next post after this one.

Last edited by GreatGamer737363_Alt (July 15, 2021 16:18:54)

GreatGamer737363_Alt
Scratcher
51 posts

Need Help Making Platformer Game Code

In the main if else condition (the if up arrow pressed one) set YV to 15. Now also in the if main if else condition, in the else category, set xv to 0.

And then change YV by -1. And then change Y by YV, and then if touching color black in this case, change y by * times 1, and set yv to 0.

and finally, if key up arrow is pressed, put an if touching color black condition in that, set yv to 15. This is very confusing and longwinded, because platformers are hard! I would've suggested watching a youtube tutorial tbh.
Kevin_Koala
Scratcher
2 posts

Need Help Making Platformer Game Code

GreatGamer737363_Alt wrote:

In the main if else condition (the if up arrow pressed one) set YV to 15. Now also in the if main if else condition, in the else category, set xv to 0.

And then change YV by -1. And then change Y by YV, and then if touching color black in this case, change y by * times 1, and set yv to 0.

and finally, if key up arrow is pressed, put an if touching color black condition in that, set yv to 15. This is very confusing and longwinded, because platformers are hard! I would've suggested watching a youtube tutorial tbh.

Could you maybe put that in scratch block form? (Use the javascript icon next to the check mark when posting a reply)

Thank You that is helpful
Thingied
Scratcher
1000+ posts

Need Help Making Platformer Game Code

why are so many people asking about how to make platformers??
Ctrl C and Ctrl V


Variables needed
  • Xv (X velocity)
  • Yv (Y velocity)
  • slope
when gf clicked
set [Xv v] to [0] // Have to reset everything when the game starts of course
set [Yv v] to [0]
forever
platform friction(0.7)movement speed(5)jump power (10) gravity (-1) slope (8) // You can play around with these settings
end

// the function below is running without screen refresh
define platform friction(friction)movement speed(movement speed)jump power (jump power) gravity (gravity) slope (slope)
change [Yv v] by (gravity) // Gravity
if <key[right arrow v]pressed?> then
change [Xv v] by (movement speed)
end
if <key[left arrow v]pressed?> then
change [Xv v] by ((0)-(movement speed)) // (0)-(n) makes n negative if it's positive and vise versa
end
set [Xv v] to ((Xv)*(friction)) // Friction so the player doesn't move forever
change x by (Xv) // Makes the player move left or right
if <touching[Level v]?> then // This part is for slopes and wall collision (kind of complicated)
set [slope v] to (0)
repeat until <<not<touching[Level v]>>or<(slope::variables)=(slope)>
change y by (1)
change [slope v] by [1]
end
if <touching[Level v]?> then // The the sprite is still touching the level, it will treat whatever it's running into like a wall
change y by (()-(slope)) // Have to reverse everything from that repeat loop
if <touching[Level v]?> then
repeat ([ceiling v]of([abs v] of (Xv))) // Will repeat the absolute (aka positive version of a number) of Xv and rounding it up. Can't risk the lag if it's a repeat until
if <touching [Level v]?> then
change x by (()-((Xv)/([abs v] of (Xv)))) // If Xv is negative it will report -1 and vise versa. The ()-(n) thing again is to make it go the other way (for wall collision)
end
end
change x by (()-((Xv)/([abs v] of (Xv)))) // Fixes some issues with wall collision
end
if <key[w v] pressed?> then // Remove this boolean (the blue block) if you want to disable wall jumping
set [Xv v] to ((wall jump bounce * -1::grey)*((Xv)/([abs v]of(Xv)))) // The gray block is a placeholder
set [Yv v] to ((wall jump boost ::grey)*((Yv)/([abs v]of(Yv))))
else
set [Xv v] to [0]
end
end
end
change y by((Yv)-(1)) // Because gravity is -1, the player is always going to be 1 pixel off the ground. To fix this we move a pixel down then check to see if it's touching the ground.
if <touching[Level v]?> then // Ground and ceiling detection
repeat ([abs v]of(Yv)) // Since Yv is always going to be integer (because we don't apply friction to it only gravity) we don't have to round it up
if <touching [Level v]?> then
change y by (()-((Yv)/([abs v] of(Yv)))) // Same thing I did with the wall collision but for Y instead
end
end
if <<key[w v] pressed?>and<(Yv)<[1]>> then // Added a ()<() boolean so that the player can't jump when on a ceiling
set [Yv v] to (jump power)
else
set [Yv v] to [0] // Have to reset Yv when you're on the ground because you're not moving down :P
end
end
change y by (1) // After moving down a pixel, you need to move back up one
(this is an improved version from the one I found in the wiki)
nevaila
Scratcher
11 posts

Need Help Making Platformer Game Code

Thingied wrote:

why are so many people asking about how to make platformers??
Ctrl C and Ctrl V


Variables needed
  • Xv (X velocity)
  • Yv (Y velocity)
  • slope
when gf clicked
set [Xv v] to [0] // Have to reset everything when the game starts of course
set [Yv v] to [0]
forever
platform friction(0.7)movement speed(5)jump power (10) gravity (-1) slope (8) // You can play around with these settings
end

// the function below is running without screen refresh
define platform friction(friction)movement speed(movement speed)jump power (jump power) gravity (gravity) slope (slope)
change [Yv v] by (gravity) // Gravity
if <key[right arrow v]pressed?> then
change [Xv v] by (movement speed)
end
if <key[left arrow v]pressed?> then
change [Xv v] by ((0)-(movement speed)) // (0)-(n) makes n negative if it's positive and vise versa
end
set [Xv v] to ((Xv)*(friction)) // Friction so the player doesn't move forever
change x by (Xv) // Makes the player move left or right
if <touching[Level v]?> then // This part is for slopes and wall collision (kind of complicated)
set [slope v] to (0)
repeat until <<not<touching[Level v]>>or<(slope::variables)=(slope)>
change y by (1)
change [slope v] by [1]
end
if <touching[Level v]?> then // The the sprite is still touching the level, it will treat whatever it's running into like a wall
change y by (()-(slope)) // Have to reverse everything from that repeat loop
if <touching[Level v]?> then
repeat ([ceiling v]of([abs v] of (Xv))) // Will repeat the absolute (aka positive version of a number) of Xv and rounding it up. Can't risk the lag if it's a repeat until
if <touching [Level v]?> then
change x by (()-((Xv)/([abs v] of (Xv)))) // If Xv is negative it will report -1 and vise versa. The ()-(n) thing again is to make it go the other way (for wall collision)
end
end
change x by (()-((Xv)/([abs v] of (Xv)))) // Fixes some issues with wall collision
end
if <key[w v] pressed?> then // Remove this boolean (the blue block) if you want to disable wall jumping
set [Xv v] to ((wall jump bounce * -1::grey)*((Xv)/([abs v]of(Xv)))) // The gray block is a placeholder
set [Yv v] to ((wall jump boost ::grey)*((Yv)/([abs v]of(Yv))))
else
set [Xv v] to [0]
end
end
end
change y by((Yv)-(1)) // Because gravity is -1, the player is always going to be 1 pixel off the ground. To fix this we move a pixel down then check to see if it's touching the ground.
if <touching[Level v]?> then // Ground and ceiling detection
repeat ([abs v]of(Yv)) // Since Yv is always going to be integer (because we don't apply friction to it only gravity) we don't have to round it up
if <touching [Level v]?> then
change y by (()-((Yv)/([abs v] of(Yv)))) // Same thing I did with the wall collision but for Y instead
end
end
if <<key[w v] pressed?>and<(Yv)<[1]>> then // Added a ()<() boolean so that the player can't jump when on a ceiling
set [Yv v] to (jump power)
else
set [Yv v] to [0] // Have to reset Yv when you're on the ground because you're not moving down :P
end
end
change y by (1) // After moving down a pixel, you need to move back up one
(this is an improved version from the one I found in the wiki)
HOLY MOLY, THIS IS ALOT OF CODE but thank you!
[

Powered by DjangoBB