plane game

posted in phil67rpg's Blog
Published October 16, 2019
Advertisement

I am making a game where two planes shoot at each other to start with. here is my code so far.


#include <stdlib.h>
#include <freeglut.h>
#include <iostream>
#include "SOIL.h"
#include <math.h>

GLuint texture[8];

const float PI = 3.14159;

float rotate = 0.125f, vertical = 0.0001f, horizontal = 0.0001f, horizontal_one = 0.0001f, vertical_one = 0.0001f, rotate_one = 0.125f, up = 0.0001f, angle = 0.0f, screen = 0.001f, up_two=0.0001f;

int flag[4] = { 0 };

static int animate;
static int animate_two;

bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
{
	// AABB 1
	float x1Min = x;
	float x1Max = x + oWidth;
	float y1Max = y + oHeight;
	float y1Min = y;

	// AABB 2
	float x2Min = xTwo;
	float x2Max = xTwo + oTwoWidth;
	float y2Max = yTwo + oTwoHeight;
	float y2Min = yTwo;

	// Collision tests
	if (x1Max < x2Min || x1Min > x2Max) return false;
	if (y1Max < y2Min || y1Min > y2Max) return false;

	return true;
}

void SetupRC(void)
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, -1.0f);
}

int LoadGLTextures()
{
	texture[0] = SOIL_load_OGL_texture
	(
		"enemyplane.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_POWER_OF_TWO
	);

	if (texture[0] == 0)
		return false;

	texture[1] = SOIL_load_OGL_texture
	(
		"enemyplane.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_POWER_OF_TWO
	);

	if (texture[1] == 0)
		return false;

	texture[2] = SOIL_load_OGL_texture
	(
		"bullet.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_POWER_OF_TWO
	);

	if (texture[2] == 0)
		return false;

	texture[3] = SOIL_load_OGL_texture
	(
		"coll.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_POWER_OF_TWO
	);

	if (texture[3] == 0)
		return false;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	return true;
}


void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_UP:
		if (rotate_one == 0.875f)
		{
			vertical_one -= 0.25f;
			horizontal_one -= 0.25f;
		}

		if (rotate_one == 0.625f)
		{
			vertical_one -= 0.25f;
			horizontal_one += 0.25f;
		}
		if (rotate_one == 0.125f)
		{
			vertical_one += 0.25f;
			horizontal_one -= 0.25f;
		}
		if (rotate_one == 0.375f)
		{
			vertical_one += 0.25f;
			horizontal_one += 0.25f;
		}
		if (rotate_one == 0.75f)
		{
			vertical_one -= 0.25f;
		}
		if (rotate_one == 0.25f)
		{
			vertical_one += 0.25f;
		}

		if (rotate_one == 0.5f)
		{
			horizontal_one += 0.25f;
		}
		if (rotate_one == 0.0f)
		{
			horizontal_one -= 0.25f;
		}
		
		if (vertical_one >= 9.5f)
		{
			vertical_one = 9.5f;
		}
		if (vertical_one <= -9.5f)
		{
			vertical_one = -9.5f;
		}

		if (horizontal_one <= -14.5f)
		{
			horizontal_one = -14.5f;
		}
		if (horizontal_one >= 4.5f)
		{
			horizontal_one = 4.5f;
		}
		break;
	case GLUT_KEY_DOWN:
		if (rotate_one == 0.875f)
		{
			vertical_one += 0.25f;
			horizontal_one += 0.25f;
		}
		if (rotate_one == 0.625f)
		{
			vertical_one += 0.25f;
			horizontal_one -= 0.25f;
		}
		if (rotate_one == 0.375f)
		{
			vertical_one -= 0.25f;
			horizontal_one -= 0.25f;
		}
		if (rotate_one == 0.125f)
		{
			vertical_one -= 0.25f;
			horizontal_one += 0.25f;
		}
		if (rotate_one == 0.25f)
		{
			vertical_one -= 0.25f;
		}
		if (rotate_one == 0.75f)
		{
			vertical_one += 0.25f;
		}

		if (rotate_one == 0.5f)
		{
			horizontal_one -= 0.25f;
		}
		if (rotate_one == 0.0f)
		{
			horizontal_one += 0.25f;
		}
		
		if (vertical_one >= 9.5f)
		{
			vertical_one = 9.5f;
		}
		if (vertical_one <= -9.5f)
		{
			vertical_one = -9.5f;
		}

		if (horizontal_one >= 4.5f)
		{
			horizontal_one = 4.5f;
		}
		if (horizontal_one <= -14.5f)
		{
			horizontal_one = -14.5f;
		}
		break;
	case GLUT_KEY_RIGHT:
		rotate_one += 0.125f;
		if (rotate_one >= 1.0f)
		{
			rotate_one = 0.0f;
		}
		break;
	case GLUT_KEY_LEFT:
		rotate_one -= 0.125f;
		if (rotate_one <= -0.125f)
		{
			rotate_one = 0.875f;
		}
		break;
	}
	glutPostRedisplay();
}

void shoot()
{
	up += 0.5f;
	if (up >= 15.0f)
	{
		animate = 0;
		up = 0.0f;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}

void shoot_two()
{
	up_two += 0.5f;
	if (up_two >= 15.0f)
	{
		animate_two = 0;
		up_two = 0.0f;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 's':
		if (animate)
		{
			glutIdleFunc(shoot);
		}
		else
		{
			glutIdleFunc(NULL);
		}
		break;
	case ' ':
		if (animate_two)
		{
			glutIdleFunc(shoot_two);
		}
		else
		{
			glutIdleFunc(NULL);
		}
		break;
	case 97:
		rotate += 0.125f;
		if (rotate >= 1.0f)
		{
			rotate = 0.0f;
		}
		break;
	case 100:
		rotate -= 0.125f;
		if (rotate <= -0.125f)
		{
			rotate = 0.875f;
		}
		break;
	case 119:
		if (rotate == 0.875f)
		{
			vertical -= 0.25f;
			horizontal += 0.25f;
		}

		if (rotate == 0.625f)
		{
			vertical -= 0.25f;
			horizontal -= 0.25f;
		}
		if (rotate == 0.125f)
		{
			vertical += 0.25f;
			horizontal += 0.25f;
		}
		if (rotate == 0.375f)
		{
			vertical += 0.25f;
			horizontal -= 0.25f;
		}
		if (rotate == 0.75f)
		{
			vertical -= 0.25f;
		}
		if (rotate == 0.25f)
		{
			vertical += 0.25f;
		}

		if (rotate == 0.5f)
		{
			horizontal -= 0.25f;
		}
		if (rotate == 0.0f)
		{
			horizontal += 0.25f;
		}

		if (vertical >= 9.5f)
		{
			vertical = 9.5f;
		}
		if (vertical <= -9.5f)
		{
			vertical = -9.5f;
		}

		if (horizontal <= -4.5f)
		{
			horizontal = -4.5f;
		}
		if (horizontal >= 14.5f)
		{
			horizontal = 14.5f;
		}
		break;
	case 120:
		if (rotate == 0.875f)
		{
			vertical += 0.25f;
			horizontal -= 0.25f;
		}
		if (rotate == 0.625f)
		{
			vertical += 0.25f;
			horizontal += 0.25f;
		}
		if (rotate == 0.375f)
		{
			vertical -= 0.25f;
			horizontal += 0.25f;
		}
		if (rotate == 0.125f)
		{
			vertical -= 0.25f;
			horizontal -= 0.25f;
		}
		if (rotate == 0.25f)
		{
			vertical -= 0.25f;
		}
		if (rotate == 0.75f)
		{
			vertical += 0.25f;
		}

		if (rotate == 0.5f)
		{
			horizontal += 0.25f;
		}
		if (rotate == 0.0f)
		{
			horizontal -= 0.25f;
		}

		if (vertical >= 9.5f)
		{
			vertical = 9.5f;
		}
		if (vertical <= -9.5f)
		{
			vertical = -9.5f;
		}

		if (horizontal <= -4.5f)
		{
			horizontal = -4.5f;
		}
		if (horizontal >= 14.5f)
		{
			horizontal = 14.5f;
		}
		break;
	}
	glutPostRedisplay();
}

void drawcollision_one()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[3]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f + screen, 0.0f, 0.0f);

	glVertex3f(0.5f, -0.5f, 0.0f);
	glTexCoord3f(0.167f + screen, 0.0f, 0.0f);

	glVertex3f(0.5f, 0.5f, 0.0f);
	glTexCoord3f(0.167f + screen, 1.0f, 0.0f);

	glVertex3f(-0.5f, 0.5f, 0.0f);
	glTexCoord3f(0.0f + screen, 1.0f, 0.0f);

	glVertex3f(-0.5f, -0.5f, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void timer(int val)
{
	screen += 0.1667f;
	if (screen >= 1.0f)
	{
		screen = 1.0f;
	}
	glutPostRedisplay();
	glutTimerFunc(500, timer, 0);
}

void coll_plane_one()
{
	//draw bullet
	float x = 5.0f+horizontal_one;
	float y = 0.0f+vertical_one;
	float oWidth = 0.125f;
	float oHeight = 0.125f;
	//draw plane
	float xTwo = -5.0f+horizontal+up_two;
	float yTwo = 0.0f+vertical;
	float oTwoWidth = 1.0f;
	float oTwoHeight = 1.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		drawcollision_one();
	}
}

void drawplane_one()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[0]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f + rotate, 0.0f, 0.0f);

	glVertex3f(-5.5f + horizontal, -0.5f + vertical, 0.0f);
	glTexCoord3f(0.125f + rotate, 0.0f, 0.0f);

	glVertex3f(-5.5f + horizontal, 0.5f + vertical, 0.0f);
	glTexCoord3f(0.125f + rotate, 1.0f, 0.0f);

	glVertex3f(-4.5f + horizontal, 0.5f + vertical, 0.0f);
	glTexCoord3f(0.0f + rotate, 1.0f, 0.0f);

	glVertex3f(-4.5f + horizontal, -0.5f + vertical, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void drawplane_two()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[1]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f + rotate_one, 0.0f, 0.0f);

	glVertex3f(5.5f + horizontal_one, -0.5f + vertical_one, 0.0f);
	glTexCoord3f(0.125f + rotate_one, 0.0f, 0.0f);

	glVertex3f(5.5f + horizontal_one, 0.5f + vertical_one, 0.0f);
	glTexCoord3f(0.125f + rotate_one, 1.0f, 0.0f);

	glVertex3f(4.5f + horizontal_one, 0.5f + vertical_one, 0.0f);
	glTexCoord3f(0.0f + rotate_one, 1.0f, 0.0f);

	glVertex3f(4.5f + horizontal_one, -0.5f + vertical_one, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void drawbullet_one()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[2]);

	glPushMatrix();

	if (rotate == 0.25f)
	{
		angle = PI / 2.0f;
	}
	if (rotate == 0.75f)
	{
		angle = (3.0f*PI) / 2.0f;
		glTranslatef(0.0f, -1.0f, 0.0f);
	}
	if (rotate == 0.5f)
	{
		angle = PI;
		glTranslatef(-0.55f, -0.5f, 0.0f);
	}
	if (rotate == 0.0f)
	{
		angle = 0.0f;
		glTranslatef(0.55f, -0.5f, 0.0f);
	}

	if (rotate == 0.125f)
	{
		angle = PI / 4.0f;
		glTranslatef(0.35f, -0.15f, 0.0f);
	}

	if (rotate == 0.375f)
	{
		angle = (3.0f*PI) / 4.0f;
		glTranslatef(-0.35f, -0.15f, 0.0f);
	}

	if (rotate == 0.625f)
	{
		angle = (5.0f*PI) / 4.0f;
		glTranslatef(-0.35f, -0.75f, 0.0f);
	}

	if (rotate == 0.875f)
	{
		angle = (7.0f*PI) / 4.0f;
		glTranslatef(0.35f, -0.75f, 0.0f);
	}

	glTranslatef(0.0f, 0.5f, 0.0f);

	float r = sqrt(pow(1.0f, 2) + pow(1.0f, 2));

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);

	glVertex3f(-4.9375f+cos(angle)*r*up+horizontal, 0.0625f+sin(angle)*r*up+vertical, 0.0f);
	glTexCoord3f(1.0f, 0.0f, 0.0f);

	glVertex3f(-5.0625f+cos(angle)*r*up+horizontal, 0.0625f+sin(angle)*r*up+vertical, 0.0f);
	glTexCoord3f(1.0f, 1.0f, 0.0f);

	glVertex3f(-5.0625f+cos(angle)*r*up+horizontal, -0.0625f+sin(angle)*r*up+vertical, 0.0f);
	glTexCoord3f(0.0f, 1.0f, 0.0f);

	glVertex3f(-4.9375f+cos(angle)*r*up+horizontal, -0.0625f+sin(angle)*r*up+vertical, 0.0f);
	glEnd();

	glPopMatrix();

	glDisable(GL_TEXTURE_2D);
}

void drawbullet_two()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[2]);

	glPushMatrix();

	if (rotate_one == 0.25f)
	{
		angle = PI / 2.0f;
	}
	if (rotate_one == 0.75f)
	{
		angle = (3.0f*PI) / 2.0f;
		glTranslatef(0.0f, -1.0f, 0.0f);
	}
	if (rotate_one == 0.5f)
	{
		angle = 0.0f;
		glTranslatef(0.55f, -0.5f, 0.0f);
	}
	if (rotate_one == 0.0f)
	{
		angle = PI;
		glTranslatef(-0.55f, -0.5f, 0.0f);
	}

	if (rotate_one == 0.125f)
	{
		angle = (3.0f*PI) / 4.0f;
		glTranslatef(-0.35f, -0.15f, 0.0f);
	}

	if (rotate_one == 0.375f)
	{
		angle = PI / 4.0f;
		glTranslatef(0.35f, -0.15f, 0.0f);
	}

	if (rotate_one == 0.625f)
	{
		angle = (7.0f*PI) / 4.0f;
		glTranslatef(0.35f, -0.75f, 0.0f);
	}

	if (rotate_one == 0.875f)
	{
		angle = (5.0f*PI) / 4.0f;
		glTranslatef(-0.35f, -0.75f, 0.0f);
	}

	glTranslatef(0.0f, 0.5f, 0.0f);

	float r = sqrt(pow(1.0f, 2) + pow(1.0f, 2));

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);

	glVertex3f(5.0625f + cos(angle)*r*up_two + horizontal_one, 0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
	glTexCoord3f(1.0f, 0.0f, 0.0f);

	glVertex3f(4.9375f + cos(angle)*r*up_two + horizontal_one, 0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
	glTexCoord3f(1.0f, 1.0f, 0.0f);

	glVertex3f(4.9375f + cos(angle)*r*up_two + horizontal_one, -0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
	glTexCoord3f(0.0f, 1.0f, 0.0f);

	glVertex3f(5.0625f + cos(angle)*r*up_two + horizontal_one, -0.0625f + sin(angle)*r*up_two + vertical_one, 0.0f);
	glEnd();

	glPopMatrix();

	glDisable(GL_TEXTURE_2D);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glGenTextures(8, &texture[8]);
	drawplane_one();
	drawplane_two();
	drawbullet_one();
	drawbullet_two();
	coll_plane_one();
	glutSwapBuffers();
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowPosition(500, 300);
	glutInitWindowSize(800, 600);
	glutCreateWindow("1945");
	glutTimerFunc(500, timer, 0);
	glutSpecialFunc(handleSpecialKeypress);
	glutKeyboardFunc(handleKeypress);
	glutDisplayFunc(display);
	SetupRC();
	LoadGLTextures(); 
	glutMainLoop();
	return 0;
}

 

Previous Entry rgg platformer
Next Entry asteroids wars
0 likes 10 comments

Comments

jbadams
7 hours ago, fleabay said:

You should make it clear what this blog is about. "I am making a game, here is my code" is a $h|t post, not a description.

 

2 hours ago, fleabay said:

Giving advice where none is asked for on a blog is bad form.

My irony sense is tingling.

October 17, 2019 01:51 AM
phil67rpg

well let me explain my blog, I first have drawn two planes using  sprites they start on opposite sides of the  screen, then they can rotate left and right using either the left or right arrow keys, or the "a" or "d" keys. also the ships can move up and down using the up and down arrow keys or the "w" or "x" keys. furthermore the ships can shoot bullets using the space bar key or the "s" keys. however when a ship gets hit by a bullet I want to draw an animated collision sprite but it does not do this. 

 

October 17, 2019 01:53 AM
Euthyphro

My unsolicited advice if you would be so kind as to allow it:

1. Do not write if if if if if if. Use for loops.

2. Create two classes called Plane and Bullet then create instances of both of those objects. Store all planes into an array called planes, and all bullets into an array called bullets. This is called object-oriented programming.

October 17, 2019 10:50 AM
Euthyphro

@phil67rpg posted hundreds of lines of legible code with great variable names and demonstrated that he can use OpenGL and even create a game loop from scratch while others struggle with game engines such as RPG Maker and Unity that do it all for you.

I took one glance and knew exactly what was going on in a language I don't even use. Great job!

October 17, 2019 02:46 PM
phil67rpg

thanks Euthyphro I will work on my game some more. however I am still stuck on  how to draw a collision sprite when the bullet hits the plane, I know this is a small problem.

October 17, 2019 04:15 PM
Euthyphro
7 hours ago, phil67rpg said:

thanks Euthyphro I will work on my game some more. however I am still stuck on  how to draw a collision sprite when the bullet hits the plane, I know this is a small problem.

After you make the changes I mentioned above, you should make a third class called Explosion then create an instance of Explosion at any location of impact. Explosions should be destroyed after playing their animation, so essentially, think of an explosion as just another object like a plane or bullet.

October 17, 2019 11:38 PM
phil67rpg

I tweaked my timer function and have figured out my problem, thanks for all the help.

October 18, 2019 12:03 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2637 views

plane game

3388 views

rgg platformer

2022 views

win 32 pong

2500 views

bug invaders

2160 views

c# book

2450 views

c# bug invaders

2105 views

c# console snake game

23720 views
Advertisement