Presentation is loading. Please wait.

Presentation is loading. Please wait.

Platformer Java Guide by Mr. Monroe.

Similar presentations


Presentation on theme: "Platformer Java Guide by Mr. Monroe."— Presentation transcript:

1 Platformer Java Guide by Mr. Monroe

2 Step 1: Setup Make a new Greenfoot Java scenario and copy the following images from the work drive into your scenario’s images folder:

3 Create the following classes 
Step 2: World & Actors Create the following classes  The MyWorld class will extend Scrolling to allow the screen to move. The code for Scrolling can be found on the work drive. It’s a little complex! All blocks can extend a Obstacle and all enemies can extend Goomba instead of extending Actor.

4 Step 3: Scrolling code This code is on the work drive for you to copy and paste! It works via setLocation!

5 Step 4: MyWorld code This class will extend Scrolling and currently sets up a 1000x800 world with Mario and some floor blocks!

6 Step 5: Obstacle and Blocks
These classes don’t need much code! Make Obstacle an abstract class with no image. Then, make Block1 and Block2 extend Obstacle!

7 Step 6: Mario’s Variables
Mario needs a lot of variables to keep track of what he is up to! He’s got a ton of images, how much to scale them by, horizontal and vertical speeds, timers for frames and animations, and booleans for different states!

8 Step 7: Mario’s Constructor
Mario’s constructor needs to assign and scale all of his images. Make sure that you type in the names of all of the PNG pictures correctly or you will get a “FileNotFound” error!

9 Step 8: Mario’s Scale Method
Mario needs a scale method so that he can scale his images to be larger based on the “scaleAmount” variable. Right now, it is assigned the value 3, which means that all of his images will appear three times bigger than they were drawn.

10 Step 9: Mario’s Act Method
Mario’s act method has a ton of necessary code segments: Figure out if Mario is on the ground. Move him based on gravity and key presses. Cap his speed to some limit. Apply friction if no keys are pressed. Update his location. Change his picture to the correct one. Fix his location if it is colliding with something. Respawn if Mario fell or touched enemy.

11 Step 9.1: onGroundCheck The first thing we need to do is figure out if Mario is on the ground. This will determine if he is allowed to jump! This code can either be in a method called onGroundCheck or just placed at the beginning of the act method:

12 Step 9.2: Gravity and Key Presses
The second thing we need to do is apply gravity by increasing Mario’s ySpeed by one. Next, let’s do the A and D key presses! Fill in the blanks and comments with the appropriate code!

13 Step 9.2: Gravity and Key Presses
Now, let’s do the W key for jumping! Try picking numbers to find a good jump height! Fill in the blanks with the appropriate code!

14 Step 9.3: Speed Limit Mario shouldn’t be able to run or fall at an unlimited speed. That could lead to some problems, like him becoming Sonic! Try picking numbers and trying it out! Fill in the blanks with the appropriate code!

15 Step 9.4: Friction Mario’s horizontal speed should slow down due to “friction” or “air resistance”. One way of doing this is shown below, where Mario’s xSpeed gets closer to zero every other frame. If you use this technique, make sure you have frame++; written at the beginning of your act method!

16 Step 9.5: Update Location Now that we are done with gravity, key presses, limits, and friction, let’s update Mario’s position for the next frame! This could set him half-way inside of a block, but we will fix it later by doing some collision checking in the last step!

17 Step 9.6: Animation Set Mario’s image based on his current state!
See if you can read the code to figure out which image to use!

18 Step 9.6: Animation Set Mario’s image based on his current state!
See if you can read the code to figure out which image to use!

19 Step 9.7: Collision Our last step is to call a collision method to fix Mario’s position if he is touching a obstacle! This method checks these eight points to determine if Mario is colliding with an obstacle 

20 public static RespawnPoint rp; rp = new RespawnPoint();
Step 9.8: Respawn Before we add the separate method to handle collisions, let’s create a respawn mechanic. Create a new Actor called RespawnPoint with no additional code or image assigned to it. Then, add the following to MyWorld: public static RespawnPoint rp; and in the world’s constructor: rp = new RespawnPoint(); addObject(rp, 500, 400); and finally, in Mario’s act method: if(getY() > 800) { setLocation(MyWorld.rp.getX(), MyWorld.rp.getY()); } Then, try jumping off of your platform and see if you respawn!

21 Step 10.1: Collision Method
For the collision method, we first need to figure out if an obstacle exists at one of the eight points! Fill in the blanks with the correct offset values!

22 Step 10.2: Collision Method
Then, we need to set Mario’s X or Y speed to zero and move him out of the block he is colliding with! I will give you BL, BR, RB, RT, but you must write four more if-statements to complete all the collisions!

23 Can you program a Goomba AI?
Step 11: Goomba Enemies! Can you program a Goomba AI? Hints: The Goomba code will reuse some of Mario’s code, such as setting the images up and scaling them. They will also have gravity, setLocation, and the collision method! Switch them between pictures 1 and 2 every 12 frames. Finally, make them walk left and flip the direction when BR or BL is null or bump into something!


Download ppt "Platformer Java Guide by Mr. Monroe."

Similar presentations


Ads by Google