Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. #include <GLUT/glut.h>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <cstdlib>
  5. using namespace std;
  6. #define WIDTH 640
  7. #define HEIGHT 480
  8. #define PI 3.141592
  9.  
  10. class Enemy{
  11. public:
  12. float enemyr; //enemyの半径
  13. float x; // x座標
  14. float y; // y座標
  15.  
  16. void genel(int Num); // 円生成
  17. void judge(int Num); // 当たり判定
  18. Enemy(); // 初期化コストラクタ
  19. };
  20. /////////////////////////////////////////////////////////////////////////
  21. void player(); // 自機生成
  22. float movex = 0.0; // 自機のx軸移動値
  23. float mx = 320.0; // 自機のx軸移動値 // 自機変数
  24. float my = 430.0; // 自機のy軸
  25. float mh = (sqrt(3) / 2) * 60; // 自機の三角の頂点
  26. /////////////////////////////////////////////////////////////////////////
  27. float Emovex = 0.0; // 敵機のx軸移動値
  28. int l = 1; // 円を動かす変数の符号を判断する // 敵機変数
  29. int Life[5];
  30. /////////////////////////////////////////////////////////////////////////
  31. float smovey = 0.0; // 弾のy軸移動値
  32. float smovex = 0.0; // 弾のx軸移動値
  33. float shootx = 320.0; // 弾のx座標 // 弾変数
  34. float shooty = 430.0; // 弾のy座標
  35. float shootr = 5.0; // 弾の半径
  36. //////////////////////////////////////////////////////////////////////////
  37.  
  38.  
  39. Enemy::Enemy(){ // 円の半径
  40. enemyr = 30.0;
  41. }
  42.  
  43.  
  44. void Enemy::genel(int Num){ // 敵機生成
  45. x = 80.0 + 120.0 * Num; // 敵機x座標の定義 クラス配列を使うため、加算させる
  46. y = 80.0; // 敵機y座標の定義
  47. judge(Num); // 敵機を表示させるかさせないかの判定
  48. if(Life[Num] == 0){ // 敵機の表示変数がtrue (0) の場合表示
  49. glColor3d(1.0, 1.0, 0.0); // 色指定
  50. glBegin(GL_POLYGON);
  51. glVertex2f(x + Emovex, y); // 基準座標指定
  52. for(int i = 0; i < 360; ++i){
  53. glVertex2f(x + enemyr * cos(i) + Emovex, y + enemyr * sin(i)); // 円の周りの生成
  54. }
  55. }
  56. }
  57.  
  58. void shoottest(){ // 弾の生成
  59. Enemy e; // Enemyクラスのオブジェクト化
  60. glBegin(GL_POLYGON);
  61. glColor3d(1.0,0.0,0.0); // 色指定
  62. glVertex2f(shootx + smovex, shooty + smovey); // 基準座標指定
  63. for(int i = 0; i < 360; ++i){
  64. glVertex2f(shootx + smovex + shootr * cos(i) , shooty + smovey + shootr * sin(i)); // 周り生成
  65. }
  66. glEnd();
  67. if(-430.0 < smovey && smovey < 0.0){ // 弾が発射されてる場合、画面外にでるまで座標移動
  68. smovey -= 30.0;
  69. }else{
  70. smovey = 0.0; // 範囲外にでたらx座標とy座標の初期化
  71. smovex = movex;
  72. }
  73. }
  74.  
  75. void Enemy::judge(int Num){ // 当たり判定
  76. if((enemyr + shootr) <= sqrt(((x + Emovex) - (shootx + smovex)) * ((x + Emovex) - (shootx + smovex))
  77. + (y - (shooty + smovey)) * (y - (shooty + smovey))) ){
  78. /*何もしない */
  79. }else if(Life[Num] != 1){ // 敵機に当たった時、弾の座標を戻す
  80. smovey = 0.0;
  81. smovex = movex;
  82. Life[Num] = 1;
  83. }else{ // 当たり判定を出したあと、通りすぎるようにするため =1 にする
  84. Life[Num] = 1;
  85. }
  86. }
  87.  
  88.  
  89. void player(){ // 自機の生成
  90. glBegin(GL_POLYGON); // 三角(自機)の生成
  91. glColor3d(2.0, 4.0, .0); // 自機の色(黄色)
  92. glVertex2f(mx + movex, my);
  93. glVertex2f((mx + 30) + movex, my + (mh / 2));
  94. glVertex2f((mx - 30) + movex, my + (mh / 2));
  95. glVertex2f(mx + movex, my - (mh / 2));
  96. glVertex2f((mx + 30) + movex, my + (mh / 2));
  97.  
  98. glEnd();
  99. }
  100.  
  101.  
  102.  
  103. void display(){
  104. Enemy e[5]; // Enemyクラスを配列でオブジェクト化
  105.  
  106. glClear(GL_COLOR_BUFFER_BIT);
  107. for(int i = 0; i < 5; ++i){
  108. e[i].genel(i); // 敵機の作成
  109. glEnd(); // 5つループで生成するため、中にglEndを入れる
  110. }
  111.  
  112. shoottest(); // 弾の生成
  113. player(); // 自機の生成
  114.  
  115. glutSwapBuffers(); // ダブルバッファリングの関数
  116.  
  117. }
  118.  
  119. void leftmove(){ // 自機左移動
  120. if(mx + movex > 30){ // 左端の当たり判定
  121. movex -= 10.0;
  122. if(smovey == 430.0){ smovex -= 50.0; } // 弾の左移動
  123. glutPostRedisplay();
  124. }
  125. }
  126.  
  127. void rightmove(){ // 自機右移動
  128. if(mx + movex < 610){
  129. movex += 10.0; // 右端の当たり判定
  130. if(smovey == 430.0){ smovex += 50.0; } // 弾の右移動
  131. glutPostRedisplay();
  132. }
  133. }
  134.  
  135. void keyup(unsigned char key, int x, int y){ // キーを話した時の挙動
  136. switch(key){ // a A を離したときに移動を止める
  137. case 'a':
  138. case 'A':
  139. glutIdleFunc(0);
  140. break;
  141.  
  142. case 'd': // d D を話した時に移動を止める
  143. case 'D':
  144. glutIdleFunc(0);
  145. break;
  146.  
  147. default: break;
  148.  
  149. }
  150. }
  151.  
  152. void keyboard(unsigned char key, int x, int y){
  153.  
  154. switch(key){
  155. case 'a': // a を入力すると左に動く
  156. case 'A':
  157.  
  158. glutIdleFunc(leftmove);
  159. break;
  160.  
  161. case 'd': // d を入力すると右に動く
  162. case 'D':
  163.  
  164. glutIdleFunc(rightmove);
  165. break;
  166.  
  167. case ' ': // 弾発射
  168. smovey -= 50.0;
  169.  
  170. break;
  171.  
  172. case 'q': // q Q ESC を入力するとプログラム終了
  173. case 'Q':
  174. case '\033':
  175. exit(0);
  176.  
  177. default:
  178. break;
  179. }
  180. }
  181.  
  182.  
  183. void timer(int value){ // 毎秒ごとに再描画して、アニメーションさせる
  184. if(-50.0 < Emovex && Emovex < 80){ // 範囲内の時に動かす
  185. Emovex += l * 5.0;
  186. }else{
  187. l = -1 * l; // 範囲外にきたら符号を逆転させる
  188. Emovex += l * 5.0;
  189. }
  190.  
  191. glutPostRedisplay(); // 再描画
  192. glutTimerFunc(40, timer, 0); // 第一引数毎秒で繰り返す
  193. }
  194.  
  195. void reshape(int w, int h){ // 座標の定義変更
  196. glViewport(0, 0, w, h);
  197. glMatrixMode(GL_PROJECTION);
  198. glLoadIdentity();
  199. gluOrtho2D(0, 640, 480, 0); // 左上を (0, 0) 右下を (640, 480)とする
  200. }
  201.  
  202. void init(){
  203. glClearColor(0.0, 0.0, 0.0, 1.0); // 背景色 黒
  204. }
  205.  
  206. int main(int argc, char *argv[]){
  207. glutInitWindowPosition(100, 200); // ウィンドウ作成時の座標指定
  208. glutInitWindowSize(WIDTH, HEIGHT); // ウィンドウサイズの指定
  209. glutInit(&argc, argv);
  210. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); // Red Green Brue +α(透明度)= RGBA
  211. glutCreateWindow(argv[0]); // ウィンドウを生成。 argv[0]だとファイル名がそのままウィンドウ名になる
  212. glutDisplayFunc(display); // display関数内を表示
  213. glutKeyboardFunc(keyboard); // キーボードの入力挙動
  214. glutKeyboardUpFunc(keyup); // 離した時の挙動
  215. glutTimerFunc(100, timer, 0); // timer 関数内の実行
  216. glutReshapeFunc(reshape); // 画面の再定義
  217. init();
  218. glutMainLoop();
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement