2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OpenGL インベーダー・シューティング

Last updated at Posted at 2016-06-20

インベーダーのシューティングゲーム。
まだ未完成ですがMacで製作しました。
ユニットとインベーダー(8体)の表示、ユニットの移動など

#include <GLUT/glut.h>
#include <iostream>
using namespace std;

#define WIDTH 640	//画面の横幅
#define HEIGHT 480	//画面の縦幅

static int umove;	//ユニットの移動値の変数
static int u=0;
static int emove;	//インベーダーの移動値の変数
static int e=1;

void Quad(int x,int y){
 glBegin(GL_QUADS);	//点の大きさ
 glVertex2i(x,y);
 glVertex2i(x,y+4);
 glVertex2i(x+4,y+4);
 glVertex2i(x+4,y);
 glEnd();
}
/////////////////////////////////////////////////////////////////////
void unit(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	for(int n = 40; n < 640; n += 80){	//インベーダーの数
		glColor4f(1.0f,0.0f,0.0f,1.0f); //インベーダーの色
		if(e == 1)emove += 1; 			//インベーダーの移動値(右)
		if(e == 2)emove -= 1; 			//インベーダーの移動値(左)
		for(int i = 0; i <= 20; i += 20){
			Quad(i+8+n+emove,48);
		}
		for(int i = 0; i <= 12; i += 12){
			Quad(i+12+n+emove,52);
		}
		for(int i = 0; i <= 20; i += 1){
			Quad(i+8+n+emove,56);
		}
		for(int i = 0; i <= 28; i += 1){
			Quad(i+4+n+emove,60);
		}
		for(int i = 0; i <= 36; i += 1){
			Quad(i+n+emove,64);
		}
		for(int i = 0; i <= 36; i += 36){
			Quad(i+n+emove,68);
		}
		for(int i = 0; i <= 20; i += 1){
			Quad(i+8+n+emove,68);
		}
		for(int i = 0; i <= 36; i += 36){
			Quad(i+n+emove,72);
		}
		for(int i = 0; i <= 20; i += 20){
			Quad(i+8+n+emove,72);
		}
		for(int i = 0; i <= 12; i += 12){
			Quad(i+12+n+emove,76);
		}
		glColor4f(0.0f,0.0f,0.0f,1.0f);
		for(int i = 0; i <= 12; i += 12){
			Quad(i+12+n+emove,60);
		}
	}
/////////////////////////////////////////////////////////////////////
	glColor4f(1.0f,1.0f,1.0f,1.0f);		//ユニットの色
	if(u == 1)umove += 4; 				//ユニットの移動値(右)
	if(u == 2)umove -= 4; 				//ユニットの移動値(左)
	for(int i = 0; i <= 16; i += 1){
		Quad(i+300+umove,430);
	}
	for(int i = 0; i <= 16; i += 1){
		Quad(i+300+umove,434);
	}
	for(int i = 0; i <= 16; i += 1){
		Quad(i+300+umove,438);
	}
	for(int i = 0; i <= 40; i += 1){
		Quad(i+288+umove,442);
	}
	for(int i = 0; i <= 40; i += 1){
		Quad(i+288+umove,446);
	}
	for(int i = 0; i <= 40; i += 1){
		Quad(i+288+umove,450);
	}
	for(int i = 0; i <= 40; i += 1){
		Quad(i+288+umove,454);
	}
	glutSwapBuffers();
}
/////////////////////////////////////////////////////////////////////
//ユニット操作
void Keyboard(unsigned char key,int x,int y) {
	switch(key){
		case 'a': u = 2; //[a]キーを押すと左に動く
			glutPostRedisplay();
		break;

		case 'd': u = 1; //[d]キーを押すと右に動く
			glutPostRedisplay();
		break;
	}
}
//ユニット操作ストップ
void Keyboardup(unsigned char key,int x,int y){
	switch(key){
		case 'a': u = 0; //[a]キーを離すと止まる
		glutPostRedisplay();
		break;

		case 'd': u = 0; //[d]キーを離すと止まる
		glutPostRedisplay();
		break;

		default: break;
	}
}
/////////////////////////////////////////////////////////////////////
//インベーダー範囲移動
void timer(int value){
    if(-30.0 < emove && emove < 40){
        emove += e * 1.0;
    }
    glutPostRedisplay();
    glutTimerFunc(40, timer, 0);
}
/////////////////////////////////////////////////////////////////////
void Init(void){
 gluOrtho2D(0,WIDTH, HEIGHT,0);
}
/////////////////////////////////////////////////////////////////////
void Idle() {
	glutPostRedisplay();
}
/////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]){
	glutInit(&argc, argv); 
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutInitWindowSize(WIDTH, HEIGHT); 
	glutCreateWindow("MyMath");
	glutDisplayFunc(unit); 	
	glutKeyboardFunc(Keyboard); 
	glutKeyboardUpFunc(Keyboardup); 
	glutIdleFunc(Idle); 
	glutTimerFunc(100, timer, 0);
	Init(); 
	glutMainLoop(); 							
	return 0;
}

スクリーンショット 2016-06-20 12.22.51.png

今度作るときは弾の発射、当たり判定まで作り上げたいです。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?