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.

2D All the way! / Background slide scrolling on a 2d game.... Need help

Author
Message
Red Rain
14
Years of Service
User Offline
Joined: 28th Aug 2009
Location:
Posted: 28th Feb 2013 03:08
Remember when the NES first came out and it also included the game Mario Brothers? All other kinds of slide~scrolling games came out alittle after that.

Well I'm starting off small in the game programming world and thought of my favorite NES game called "Contra" by Konami. So, I'm trying to duplicate it from what I remember of it, but with alittle graphics power..... my style.

Anyhow, I'm having a problem sliding the background image to the left when the player runs/walks to the right. I have my player positioned alittle off to the left of the screen and want the background image to move left as soon as the player reaches the middle of the screen. Should I setup some invisable collisions to move the background? my screen display mode 800,600,32..
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 3rd Mar 2013 04:32
A simple way to do this is to just treat the game level as a big, long world. Have the player just move normally along this world. Then, when you're drawing everything, you shift it all to the left by some amount so it all appears relative to the player. Imagine having a fake "camera" that is panning along the world with the player.

I wrote a quick, very simplistic example to demonstrate this. I tried to comment it well. Let me know if you have any questions at all.


Red Rain
14
Years of Service
User Offline
Joined: 28th Aug 2009
Location:
Posted: 4th Mar 2013 05:02
BMacZero ~> Thanx for responding to my help. The coding works really well and it's a good idea about the pillars but I've already made my background. 20,000 width by 600 height, I had to break it up into 5 seperate sections(4000 width pixels each) and linked them side by side cause if i did the 20,000 long single sheet. DarkBasic pro will disorder or even mess the image up. Is it cause the file is too big? LOL probably a stupid question
Wolf013
10
Years of Service
User Offline
Joined: 15th May 2013
Location:
Posted: 15th May 2013 19:52
This code is exactly what I was looking for BMacZero, Thank you. I am having trouble getting it to work with sprites though. Is there any chance you could post an example of it with sprites instead?
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 17th May 2013 07:07
Sure thing.


There isn't much different between drawing boxes and drawing sprites. It's just important to remember that when you call SPRITE, it basically creates a persistent object that will just sit on the screen in the same place until you move it by calling SPRITE again. By contrast, when you draw BOXs or images, you have to draw them every frame - they get erased after each frame. Let me know if you have any specific questions about sprites.

Wolf013
10
Years of Service
User Offline
Joined: 15th May 2013
Location:
Posted: 17th May 2013 09:18
Thank you for the reply. I was also able to get a vertical camera working as well thanks to you. If I need help with anything in the future I'll be sure to ask you.
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th May 2013 12:11
Never heard it called "slide" scrolling before. While working on your game, some things you might want to research on the net are "side-scrolling" and "parallax scrolling"

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 17th May 2013 13:14
One other thing you could add, is some inertia to the scrolling. So, rather than setting the scroll exactly depending on the player position then limiting it - you would adjust a scroll value instead...

Maybe something like:

NcameraX = playerXPos - screen width() / 2
//Stop camera at screen edges
if (NcameraX < 0) then NcameraX = 0
if (NcameraX + 800 > 4000) then NcameraX = 4000-800

So you use NcameraX instead of cameraX - then you can adjust cameraX smoothly to suit...

If cameraX<NcameraX
cameraX=cameraX+1
if cameraX>NcameraX then cameraX=NcameraX
endif

If cameraX>NcameraX
cameraX=cameraX-1
if cameraX<NcameraX then cameraX=NcameraX
endif

So it would adjust the camera position by 1 pixel per frame - if you set cameraX before starting the level, you can have the screen scroll to the player sprite, if the player dies and re-spawns a little further back in the level, then the screen will scroll left until it finds them. I think that with a game like Contra, the smoother the camera movement the better.

Anyway, just an idea. We used to call the barrier method 'Push Scrolling' - because you have to push the screen barrier to scroll.

I got a fever, and the only prescription, is more memes.
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 17th May 2013 20:24
Parallax scrolling is pretty cool, definitely something to look into.

If you do the smooth scrolling, though, you'll need to base the scroll speed on the distance between the player's x position and the screen center. It should scroll faster if the player is further from the screen center. Otherwise if the camera speed is less than the player speed, the player will eventually run out of the frame, and if it's greater or equal, the camera will behave exactly like the non-smoothed one.

Login to post a reply

Server time is: 2024-02-23 22:48:58
Your offset time is: 2024-02-23 22:48:58