Side scroller help

Hi,

I know you’ve probably seen this a million times before but a little help would be appreciated.

I have to make a game for a mini-project and would like to make a 2d side scroller (either platform or shooter).

I’m having problem getting my head around how to make the background and enemies to move from right to left. Can someone point me to a good step-by-step tutorial or post some simple code so I can get an idea of what’s needed. Also, I’ve looked on amazon.co.uk for a good java gaming book but I’m not sure which is best. Can anyone recommend one.

Cheers :wink:

The idea is to create an Array (or a Vector if you don’t want a fixed size), which would hold your enemy classes, and then, of course, write the actual enemy class to control the enemy. Here’s something for you:

// First create an array big enough to hold the
// amount of enemys you want or need.
Enemy enemys[] = new Enemy[5]; // Holds 5 enemys

// You'll also need to create the Enemy Class,
// like the following:
public class Enemy extends Applet {
	int x = 0;
	int y = 0;
	int dirx = 0;
	int diry = 0;
	int speed = 0;

	int minX = 0;
	int maxX = 0;
	int minY = 0;
	int maxY = 0;

	int width = 0;
	int height = 0;

	double frame = 0;
	Image frames[] = new Image[3];

	ImageObserver observer;

	public Enemy(int posX, int posY, int dx, int dy, int spd, int nminX, int nmaxX, int nminY, nmaxY, int enemyID, ImageObserver ob) {
		observer = ob;

		for (int i=0; i<frames.length; i++) {
			frames[i] = getImage(getCodeBase(), "enemy"+enemyID+"_"+i+".png");
		}

		x = posX;
		y = posY;
		dirx = dx;
		diry = dy;
		speed = spd;

		width = frames[0].getWidth(observer);
		height = frames[0].getHeight(observer);

		minX = x+nminX;
		maxX = x+nmaxX;
		minY = y+nminY;
		maxY = y+nmaxY;
	}

	public void update(Graphics g) {
		frame += 0.2;
		if (frame >= frames.length) frame = 0;

		x += speed*dirx;
		y += speed*diry;

		if (x <= minX) dirx *= -1;
		if (x >= maxX) dirx *= -1;
		if (y <= minY) diry *= -1;
		if (y >= maxY) diry *= -1;

		g.drawImage(frames[(int)frame], x, y, ob);
	}
}

Here’s an example of creating an enemy:

enemys[0] = new Enemy(40, 40, 1, 0, -100, 100, 0, 0, 0, this);

Then when you need to update and draw the enemy, in your game loop, call the update method. Like this:

enemys[0].update(g);

Where g represents the Graphics context to which image/applet your drawing on to.

I haven’t tested this class, but it should work. Hopefully. I’m sure it’s pretty self explanatory, but if it isn’t just ask.

David Brackeen’s book is excellent and also Andrew Davidson’s "Killer game programming in java is awesome. U have online versions too. Check them out.

Then how about instead of left to right or vice versa…

Can show me how to do from up to down or vice versa? ;D

Thanks!

Dude…when u r going left, u r decreasnig x, when going up decreasing y and when going down increasing y. It is that simple.

U should really check those 2 books. They cover everything in detail.

I think he’s wondering how to paint them when the camera is moving. I would also like to know how to implement a camera to scroll from left to right and up and down.

I didn’t ever do a game with scrolling camera but here’s my idea:
when you scroll in the game, you don’t move the camera, you move game objects instead. Like if you have a building in the middle of the screen and you scroll up (north) then you draw the building lower and lower as you scroll (to south). It’s basicly changing X and Y coordinates of all visible objects in the game and drawing them… again I never did it before or read about it, this is what I came up 5 mins ago while reading the post, there could (should) be much better ways to do this.

Moving the game objects instead of the camera when it is logically the camera which moves is not a good idea. What if there are more cameras, for instance?

I would suggest the following approach: have a standard coordinate system and let the camera have a set of coordinates. Everything that has to be drawn will first have the camera coordinates subtracted from its own coordinates. For example, in the rendering method,


int x = sprite.x - camera.x;
int y = sprite.y - camera.y;  //except you're probably using an inverted system
graphics.drawSomething(..., x, y, ...)

You just have to account for trivial stuff like viewport height/width and so on, but these things should be obvious.

Scrolling actually isn’t very complicated. I wrote a tile-based platform test which has scrolling.

Play via Java Web Start
Download the Source