Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / 2D platformer code help.

Author
Message
HammerBro
13
Years of Service
User Offline
Joined: 14th Jan 2011
Location: Nowhere
Posted: 12th Feb 2011 05:49
Hello everyone, I've been working on a 2D sprite platformer, and I would like some advice with it.

My character moves around fine, but it gets stuck in walls occasionally, and I like to know how to delete a platform when you right click it.

My code is below:


What you do is left click to add a platform, arrow keys to move, and space key to jump. This is not a complete game.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Feb 2011 21:45
I have run the code (made up some temp images) and all it did was show the player sprite. It wouldn't move and clicking the mouse didn't work either. I got it to where the player did move around, but it was not fluent (i.e. I would press the arrow key, it would move sluggishly a couple times, stop, and then when I let go of the arrow key, it would keep moving by itself until it was off the screen). So, the movement code needs some tweaking. The jumping code didn't work for me either.

I would include more subroutines / functions than what you have currently. For example, I would make a subroutine (or function) for moving the player. It would get input and react to it.
I would also make a subroutine (or function) for the collision code. I would also add what I call a debug subroutine. It would print the current FPS and any game info that you need. For example, you could print out the variable 'on_floor' to show you what value it holds and see if it is correct while the game is running.

By the time you add these subroutines, your main do-loop will be concise and it will help your code to be more readable.

I am willing to make some edits to your code and try and help you with it, but I want to make sure you are still around, so post back if you are interested in this.

LB

HammerBro
13
Years of Service
User Offline
Joined: 14th Jan 2011
Location: Nowhere
Posted: 14th Feb 2011 19:45
Well, I fixed up the code, it should work but there are still some bugs, the collisions with platforms are iffy, and I can't find a way to delete a solid with the right click, I also don't know how to show the FPS.



Anyways, thanks for the advice.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 14th Feb 2011 21:56
Nice improvements! I can move and jump now as well as place platforms.

To delete a platform with a right-click, you would have to determine which one is at the mouse's current X / Y location. Store the mouse X / Y position. Cycle through all of the platforms, checking for an X position that is greater than the platform, but less than the platform X position plus the width of the platform. Do the same thing on the Y until you determine which platform has been clicked on. I would suggest adding the variable 'status' in your TYPE declaration. You could set a platform's status to 1 if it is active and 0 if not. When it needs to be deleted, just change the status to 0 and delete the sprite. Only check the active ones.

It seems you will need a way to save the locations of the platforms in order to make several screens up. You could then either simply load in a file upon starting or could give the user the option of choosing the file to load. At some point I would separate the screen editor from the game itself.

Login to post a reply

Server time is: 2024-09-29 02:38:48
Your offset time is: 2024-09-29 02:38:48