Advertisement

2d side scroller

Started by April 30, 2022 10:13 PM
16 comments, last by Tom Sloper 2ย years, 7ย months ago

Well I am working on defender/space invaders game. I have a very simple question. I have drawn bug sprite and a bullet sprite. I shoot at the bug with a bullet but when the bullet hits the bug it draws a collision sprite which stays on the screen. I want the collision sprite to not be drawn after it is drawn. I am using a boolean to determine what sprite is to be drawn.

void renderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	drawShip();
	drawBullet();
	coll_ship_one();
	if (coll == false)
	{
	drawBug_one();
	}
	else if (coll == true)
	{
	drawCollision_one();
	}
	glutSwapBuffers();
}

I picked up SDL2 a couple of months ago and find it to be a very useful set of libraries.

Iโ€™m also developing a 2D side scroller in the defender mould (see my gamedev.net Project and Blog - Parallax Zoom!!).

I literally just posted about implementing collision detection an hour ago. Iโ€™ve still got the collision boxes because itโ€™s useful to see everything is working, but when youโ€™re ready to clean up just remove the draw routine from the collision detection method while leaving the AABB collision logic in place.

Advertisement

pbivens67 said:
I shoot at the bug with a bullet but when the bullet hits the bug it draws a collision sprite which stays on the screen. I want the collision sprite to not be drawn after it is drawn. I am using a boolean to determine what sprite is to be drawn.

I think you don't want a bool but a timer to play some explosion animation.
This could be implemented for example like this:
Collision happens: Destroy bug; create explosion and set its time.
Every frame: For each explosion render it's sprite and decrement it's timer. If timer โ‰ค 0, delete the explosion.

That's some state management, and eventually you want to move the explosion with the same velocity the bug had when it was hit. Which gives the idea that you could eventually reuse the bug object, but change it to be a explosion from now on and disable it's property to be collideable with other objects, etc.
Or you could implement the explosion using the same classes or components which make up your enemies.

Such design decisions are not easy to make. You may choose an option, but later it turns out there are issues of bad restrictions, or being cumbersome to use, or causing an explosion of code complexity as the game grows, or being inefficient.
This is why many people now use Entity Component Systems after they failed to do it with OOP class hierarchies before. So that's an advanced programming topic.

Looking at your code snippet, it seems you're not ready yet for this, and you are still on a path of failures.
I think so because the renderScene function, and the functions it call do not take any parameters. Instead parameters, you probably use global variables, which is very bad practice because you can not subdivide your program into smaller pieces of functionality and responsibility. Your approach works for very small and simple games, but as complexity grows, it will become unmanageable spaghetti code. At this point you'll give up, start from scratch, but then likely run into the same issues again.

Notice, the path of failures is totally natural, happens to everyone, and is just needed so you can learn from your own mistakes.
But if learning from your own mistakes is not enough, or worse - if you do not even see your mistakes, then you need to learn from other people how to do it right. Look for other peoples small projects of simple games, and pay attention to their overall code structure and how they divide stuff into multiple, smaller systems, each having a clearly defined functionality and responsibility.

And consider to use libraries. You learn nothing from writing sections of glVertex() again and again.
Contrary, you would learn a lot by using sprites, tiled backgrounds, etc., properly implemented in some framework. By using such functionality, you would guess their implementation even without studying the underlying code, and you have more time to work on your game, not just basic low level stuff to draw things, set up alpha blending, etc.

You do this since 15 years? Then your progress is not happening as it should. After 15 years you should know all the answers to the questions you ask here.
Thus, you need to change some things, so the learning and progress finally starts to happen.

pbivens67 said:
I want the collision sprite to not be drawn after it is drawn.

Always this. You are a troll.

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

fleabay said:
You are a troll.

Not sure. What is more likely?
1. Being stuck for 15 years on pointless trolling.
2. Being stuck for 15 years on learning programming, combined with some lack on communication skills.

@JoeJ He ask the same question repeatedly and gets the same answer always. I've seen the same question at least half a dozen times, โ€œhow do I NOT draw something?โ€. It is the easiest thing ever to comprehend. FFS, shit or get off the toilet.

He has successfully made games/demos where he has worked out that problem. So yes, he is a troll at this point.

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

Advertisement

Thread locked.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement