asteroids wars

posted in phil67rpg's Blog
Published November 08, 2019
Advertisement

well I am making an asteroids and space wars hybrid. I got the ships to move around the screen and shoot bullets, I have also drawn a  random location UFO, here is my code so far, let me know what you think.


#include<freeglut.h>
#include<iostream>
#include<math.h>
#include<time.h>

using namespace std;

float shipenemyAngle = 0.0f;
float shipAngle = 0.0f;
float thrust = 0.0f;
float dx, dy, e_dx, e_dy, up = 0.0f, up_one = 0.0f;
int animate = 0, animate_one = 0;
int posX, posY;

const float PI = 3.1415926;

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 drawShip()
{
	glPushMatrix();
	glColor3f(1.0f, 0.0f, 0.0f);
	glTranslatef(50.0f, 0.0f, 0.0f);
	glTranslatef(dx,dy, 0.0f);
	glBegin(GL_LINE_LOOP);
	float offsets[4][2] = { {0.0f, 0.0f}, {-5.0f, -5.0f}, {0.0f, 10.0f}, {5.0f, -5.0f} };
	float cosA = cos(shipAngle);
	float sinA = sin(shipAngle);
	for (int i = 0; i < 4; i++)
		glVertex3f(offsets[i][0] * cosA - offsets[i][1] * sinA, offsets[i][0] * sinA + offsets[i][1] * cosA, 0.0f);
	glEnd();
	glPopMatrix();
}

void drawEnemyShip()
{
	glPushMatrix();
	glColor3f(0.0f, 1.0f, 0.0f);
	glLoadIdentity();
	glTranslatef(-50.0f, 0.0f, 0.0f);
	glTranslatef(e_dx,e_dy, 0.0f);
	glBegin(GL_LINE_LOOP);
	float offsets[4][2] = { {0.0f, 0.0f}, {-5.0f, -5.0f}, {0.0f, 10.0f}, {5.0f, -5.0f} };
	float cosA = cos(shipenemyAngle);
	float sinA = sin(shipenemyAngle);
	for (int i = 0; i < 4; i++)
		glVertex3f(offsets[i][0] * cosA - offsets[i][1] * sinA, offsets[i][0] * sinA + offsets[i][1] * cosA, 0.0f);
	glEnd();
	glPopMatrix();
}

void drawUFO()
{
	glPushMatrix();
	glColor3f(0.0f, 1.0f, 1.0f);
	glTranslatef(posX, posY, 0.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(-10.0f, 0.0f, 0.0f);
	glVertex3f(-5.0f, 5.0f, 0.0f);
	glVertex3f(5.0f, 5.0f, 0.0f);
	glVertex3f(10.0f, 0.0f, 0.0f);
	glVertex3f(5.0f, -5.0f, 0.0f);
	glVertex3f(-5.0f, -5.0f, 0.0f);
	glVertex3f(-10.0f, 0.0f, 0.0f);
	glVertex3f(10.0f, 0.0f, 0.0f);
	glVertex3f(5.0f, 5.0f, 0.0f);
	glVertex3f(5.0f, 10.0f, 0.0f);
	glVertex3f(-5.0f, 10.0f, 0.0f);
	glVertex3f(-5.0f, 5.0f, 0.0f);
	glEnd();
	glPopMatrix();
}

void timer(int val)
{
	posX = -10 + rand() % 50;
	posY = -10 + rand() % 50;
	glutPostRedisplay();
	glutTimerFunc(100, timer, 0);
}

void bullet()
{
	up++;
	if (up >= 20.0f)
	{
		up = 0.0f;
		animate = 0;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}

void bullet_one()
{
	up_one++;
	if (up_one >= 20.0f)
	{
		up_one = 0.0f;
		animate_one = 0;
		glutIdleFunc(NULL);
	}
	glutPostRedisplay();
}

void drawBullet()
{
	glPushMatrix();
	glColor3f(1.0f, 0.0f, 0.0f);
	glLoadIdentity();
	glTranslatef(50.0f, 0.0f, 0.0f);
	glTranslatef(dx, dy, 0.0f);
	glPointSize(2.0f);
	glBegin(GL_POINTS);
	float offsets[1][2] = { {0.0f, 10.0f} };
	float cosA = cos(shipAngle);
	float sinA = sin(shipAngle);
	for (int i = 0; i < 1; i++)
		glVertex3f(offsets[i][0] * cosA - offsets[i][1] * sinA*up, offsets[i][0] * sinA + offsets[i][1] * cosA*up, 0.0f);
	glEnd();
	glPopMatrix();
}

void drawBullet_one()
{
	glPushMatrix();
	glColor3f(0.0f, 1.0f, 0.0f);
	glLoadIdentity();
	glTranslatef(-50.0f, 0.0f, 0.0f);
	glTranslatef(e_dx, e_dy, 0.0f);
	glPointSize(2.0f);
	glBegin(GL_POINTS);
	float offsets[1][2] = { {0.0f, 10.0f} };
	float cosA = cos(shipenemyAngle);
	float sinA = sin(shipenemyAngle);
	for (int i = 0; i < 1; i++)
		glVertex3f(offsets[i][0] * cosA - offsets[i][1] * sinA*up_one, offsets[i][0] * sinA + offsets[i][1] * cosA*up_one, 0.0f);
	glEnd();
	glPopMatrix();
}

void coll_ship_one()
{
	//draw bullet
	float x=0;
	float y=0;
	float oWidth=0;
	float oHeight=0;
	//draw plane
	float xTwo=0;
	float yTwo=0;
	float oTwoWidth=0;
	float oTwoHeight=0;

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

void renderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	drawShip();
	drawEnemyShip();
	drawBullet();
	drawBullet_one();
	drawUFO();
	glPopMatrix();
	glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat aspectRatio;
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if (w <= h)
		glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		animate = !animate;
		if (animate)
		{
			glutIdleFunc(bullet);
		}
		else
		{
			glutIdleFunc(NULL);
		}
		break;
	case 'a':
		shipenemyAngle+=PI/8;
		break;
	case 'd':
		shipenemyAngle-=PI/8;
		break;
	case 'w':
		e_dx += -2.0f*sin(shipenemyAngle);
		e_dy += 2.0f*cos(shipenemyAngle);
		break;
	case 'x':
		e_dx += 2.0f*sin(shipenemyAngle);
		e_dy += -2.0f*cos(shipenemyAngle);
		break;
	case 's':
		animate_one = !animate_one;
		if (animate_one)
		{
			glutIdleFunc(bullet_one);
		}
		else
		{
			glutIdleFunc(NULL);
		}
	}
	glutPostRedisplay();
}

void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		shipAngle+=PI/8;
		break;
	case GLUT_KEY_RIGHT:
		shipAngle-=PI/8;
		break;
	case GLUT_KEY_UP:
		dx += -2.0f*sin(shipAngle);
		dy += 2.0f*cos(shipAngle);
		break;
	case GLUT_KEY_DOWN:
		dx += 2.0f*sin(shipAngle);
		dy += -2.0f*cos(shipAngle);
		break;
	}
	glutPostRedisplay();
}

int main(int argc, char**argv)
{
	srand(time(NULL));
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE);
	glutInitWindowPosition(600, 400);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Space Wars");
	glutDisplayFunc(renderScene);
	glutKeyboardFunc(handleKeypress);
	glutSpecialFunc(handleSpecialKeypress);
	glutReshapeFunc(ChangeSize);
	glutTimerFunc(100, timer, 0);
	glutMainLoop();
}

 

Previous Entry plane game
2 likes 2 comments

Comments

kseh

Screenshots and stories about what you have learned would be interesting to include.

November 08, 2019 06:36 PM
phil67rpg

https://imgur.com/WVdE5Ro here is a screenshot

November 09, 2019 12:04 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

2661 views

plane game

3410 views

rgg platformer

2042 views

win 32 pong

2523 views

bug invaders

2189 views

c# book

2473 views

c# bug invaders

2131 views

c# console snake game

23812 views
Advertisement