Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
// T2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"

#include <gl/glut.h>
#include <stdio.h>
#include <string.h>
#include<math.h>
//#include"cPlayer.h"
//#include"cImage.h"
//-------------------------------------------------------------------
float ttx=0;
float tty=0;
float ttz=0;
float tx=0;
float ty=0;
float tz=0;

float MoveBird1=0;
float MoveSmlShip=0;
float MoveBigShip = 0;
float MoveBomb=0;
bool  gameOver = true;
int j,i,q;
void init(){
    glClearColor(  0.00 , 0.00 ,0.61,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,950,0,1000);
}

void drawText(char ch[],int xpos, int ypos)//draw any text
{
    //count number of chars
    int numofchar = strlen(ch);
    //glLoadIdentity ();
    glRasterPos2f( xpos , ypos);
    for (i = 0; i <= numofchar - 1; i++)
    {
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ch[i]);
    }
}
/**void WelcomeScreen(){
    char tmp_str[40];

    glColor3f(1, 0, 0);
    glRasterPos2f(0, 20);
    drawText("Welcome To Snake 3D Game.",300,400);

    glColor3f(0, 0, 1);
    glRasterPos2f(0, 10);
    drawText("Prepared By Salim AWAD",200,500);

    glColor3f(0, 0, 1);
    glRasterPos2f(0, 0);
   drawText("To Start Playing please press 'n'. Enjoy",100,600);
}**/

void drawBomb(){
// the bomber that will shoot the ball(bomb)
    glBegin(GL_POLYGON);
        glColor3f( 0 , 0 , 0);
        glVertex2f(125,550);
        glVertex2f(125,600);
        glVertex2f(225,600);
        glVertex2f(225,550);
        glVertex2f(175,600);
        glVertex2f(290,650);
        glVertex2f(270,680);
        glVertex2f(250,610);
    glEnd();
}

void drawSun(){

    //sun

float x1,y1,x2,y2;

float angle;

double radius=150.0;

x1 = 450.0,y1=200.0;

glColor3f(1.0,1.0,0.0);


glBegin(GL_TRIANGLE_FAN);

glVertex2f(x1,y1);

glColor3f(0.98, 0.625, 0.12);

for (angle=1.0f;angle<361.0f;angle+=0.2)

{

x2 = x1+sin(angle)*radius;

y2 = y1+cos(angle)*radius;



glVertex2f(x2,y2);


}

glEnd();


}

void drawSea(){
//sea

glColor3f(0.196078 , 0.196078 ,0.8);
    glBegin(GL_POLYGON);
    glVertex2f(250,200);
    glVertex2f(950,200);
    glColor3f(0.0,0.7,1.0);
    glVertex2f(950,0);
    glVertex2f(250,0);


    glColor3f(0.196078 , 0.196078 ,0.8);

    glEnd();

}

void drawSky(){
        // sky
    glColor3f(0.00 , 0.00 ,0.61);
    glBegin(GL_POLYGON);
    glVertex2f(0,1000);
    glVertex2f(950,1000);

    glColor3f(0.00 , 0.00 ,0.61);
    glVertex2f(950,550);
    glVertex2f(0,550);
    glEnd();


    glBegin(GL_POLYGON);
    glColor3f(0.00 , 0.00 ,0.61);
    glVertex2f(250,550);
    glVertex2f(950,550);

    glColor3f(  0.74902 , 0.847059 , 0.847059);
    glVertex2f(950,0);
    glVertex2f(250,0);


    glEnd();

}

void drawMountain(){
// mountain
    glBegin(GL_POLYGON);
        glColor3f(  0.36 , 0.25 ,0.20);
        glVertex2f(0,0);
        glVertex2f(0,550);
        glVertex2f(250,550);
        glVertex2f(250,0);
    glEnd();
}

void drawBigShip(){
// the big ship
    glPushMatrix();
    glTranslatef(tx-MoveBigShip,ty,tz);

    glBegin(GL_POLYGON);
        glColor3f(0.35 , 0.16 , 0.14);
        glVertex2f(675,120);
        glVertex2f(625,225);
        glVertex2f(925,225);
        glVertex2f(875,120);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(0.35 , 0.16 , 0.14);
        glVertex2f(775,225);
        glVertex2f(790,495);
        glVertex2f(775,495);
        glVertex2f(790,225);
    glEnd();
    /////////////////////triangle
    glBegin(GL_TRIANGLES);
        glColor3f(0.91 , 0.76, 0.65);
        glVertex2f(775,225);
        glVertex2f(675,270);
        glVertex2f(765,450);
    glEnd();


    glBegin(GL_TRIANGLES);
        glColor3f(0.91 , 0.76, 0.65);
        glVertex2f(790,225);
        glVertex2f(875,270);
        glVertex2f(800,450);
    glEnd();

    glPopMatrix();

    //glutSwapBuffers();
}

void drawSmallShip(){
// the small ship
    glPushMatrix();
    glTranslatef(MoveSmlShip,0,0);

    glBegin(GL_POLYGON);
        glColor3f(0.52 ,0.37 ,0.26);
        glVertex2f(300,20);
        glVertex2f(275,50);
        glVertex2f(425,50);
        glVertex2f(400,20);
    glEnd();
    glBegin(GL_TRIANGLES);
        glColor3f(0.92 , 0.78 , 0.62);
        glVertex2f(275,60);
        glVertex2f(350,120);
        glVertex2f(425,60);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(0.52 ,0.37 ,0.26);
        glVertex2f(350,50);
        glVertex2f(350,80);
        glVertex2f(355,80);
        glVertex2f(355,50);
    glEnd();
    glPopMatrix();
}

void drawBird1(){
// bird number 1
    glPushMatrix();
    glTranslatef(MoveBird1,0,0);
    glBegin(GL_LINE_STRIP);
        glColor3f(1.0 , 1.0 ,1.0);
        glVertex2f(90,800);//1
        glVertex2f(110,830);//2
        glVertex2f(130,800);//3
        glVertex2f(150,830);//4
        glVertex2f(170,800);//5
        glVertex2f(150,820);//6
        glVertex2f(130,790);//7
        glVertex2f(110,820);//8
        glVertex2f(90,800);//9
    glEnd();
    glBegin(GL_TRIANGLES);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(110,830,0);//2
        glVertex3f(110,820,0);//8
        glVertex3f(90,800,0);//1
    glEnd();
    glBegin(GL_TRIANGLES);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(150,830,0);//4
        glVertex3f(150,820,0);//6
        glVertex3f(170,800,0);//5
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0 , 1.0 ,1.0);
        glVertex2f(110,830);//2
        glVertex2f(110,820);//8
        glVertex2f(130,790);//7
        glVertex2f(130,800);//3
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0 , 1.0 ,1.0);
        glVertex2f(130,800);//3
        glVertex2f(150,830);//4
        glVertex2f(150,820);//6
        glVertex2f(130,790);//7
    glEnd();
    glPopMatrix();
    glFlush();
}

void drawBird2(){

//bird number 2
glPushMatrix();
glTranslatef(MoveBird1,0,0);
glBegin(GL_LINE_STRIP);
glColor3f(1.0 , 1.0 ,1.0);
glVertex2f(200,700);//1
glVertex2f(220,730);//2
glVertex2f(240,700);//3
glVertex2f(260,730);//4
glVertex2f(280,700);//5
glVertex2f(260,720);//6
glVertex2f(240,690);//7
glVertex2f(220,720);//8
glVertex2f(200,700);//9
glEnd();
glFlush();
glBegin(GL_TRIANGLES);
glColor3f(1.0 , 1.0 ,1.0);
glVertex3f(220,730,0);//2
glVertex3f(220,720,0);//8
glVertex3f(200,700,0);//1
glEnd();
glFlush();
glBegin(GL_TRIANGLES);
glColor3f(1.0 , 1.0 ,1.0);
glVertex3f(260,730,0);//4
glVertex3f(260,720,0);//6
glVertex3f(280,700,0);//5
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3f(1.0 , 1.0 ,1.0);
glVertex2f(220,730);//2
glVertex2f(220,720);//8
glVertex2f(240,690);//7
glVertex2f(240,700);//3
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3f(1.0 , 1.0 ,1.0);
glVertex2f(240,700);//3
glVertex2f(260,730);//4
glVertex2f(260,720);//6
glVertex2f(240,690);//7
glEnd();
glPopMatrix();
glFlush();
}

void drawBird3(){

//bird number 3
glPushMatrix();
glTranslatef(MoveBird1,0,0);
glBegin(GL_LINE_STRIP);
glColor3f(1.0 , 1.0 ,1.0);
glVertex2f(300,900);//1
glVertex2f(320,930);//2
glVertex2f(340,900);//3
glVertex2f(360,930);//4
glVertex2f(380,900);//5
glVertex2f(360,920);//6
glVertex2f(340,890);//7
glVertex2f(320,920);//8
glVertex2f(300,900);//9
glEnd();
glFlush();
glBegin(GL_TRIANGLES);
glColor3f(1.0 , 1.0 ,1.0);
glVertex3f(320,930,0);//2
glVertex3f(320,920,0);//8
glVertex3f(300,900,0);//1
glEnd();
glFlush();
glBegin(GL_TRIANGLES);
glColor3f(1.0 , 1.0 ,1.0);
glVertex3f(360,930,0);//4
glVertex3f(360,920,0);//6
glVertex3f(380,900,0);//5
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3f(1.0 , 1.0 ,1.0);
glVertex2f(320,930);//2
glVertex2f(320,920);//8
glVertex2f(340,890);//7
glVertex2f(340,900);//3
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3f(1.0 , 1.0 ,1.0);
glVertex2f(340,900);//3
glVertex2f(360,930);//4
glVertex2f(360,920);//6
glVertex2f(340,890);//7
glEnd();
glPopMatrix();
glFlush();
}



void Translate()

{
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(5, 2, 0.0);
    drawSmallShip();
    glPopMatrix();
}

/**bool collision(){
    int i;

    for(i=0; i<5; i++){
        if(ttx == tx &&tty == ty&&ttz == tz)
        return true;
    }
    return false;
}

void Rotate(GLfloat tx, GLfloat ty, GLfloat theta)

{

 glMatrixMode(GL_MODELVIEW);

 glClear(GL_COLOR_BUFFER_BIT);

 glTranslatef(tx, ty, 0.0);

 glRotatef(theta, 1.0, 0.0, 1.0);

 glTranslatef(-1.0*tx, -1.0*ty, 0.0);

 drawSun();


 glFlush();

 glutSwapBuffers();

}**/

void drawBoom(){

    //the ball(bomb) that coming from the bomber 

float x1,y1,x2,y2;

float angle;

double radius=20.0;

x1 = 300,y1=670;
glPushMatrix();
//glTranslatef(-ttx+MoveBomb,-tty,-ttz);
//glRotatef(45, 0.0, 0.0,1.0);
glTranslatef(ttx+MoveBomb,tty,ttz);
//glRotatef(45, 0.0, 1.0, 0.0);
//glRotatef(45, 1.0, 0.0, 0.0);
//glRotatef(45, 0.0, 0.0, 1.0);
glColor3f(0.0,0.0,0.0);
 // glutSolidSphere(100,200,200);


glBegin(GL_TRIANGLE_FAN);

glVertex2f(x1,y1);

for (angle=1.0f;angle<361.0f;angle+=0.2)

{

x2 = x1+sin(angle)*radius;

y2 = y1+cos(angle)*radius;



glVertex2f(x2,y2);


}


glEnd();
 glPopMatrix();


glFlush();


}

void myDisplay(void)

{




  drawSky();
  drawSun();
  drawSea();

  drawMountain();
  drawBomb();
  drawBoom();

  drawSmallShip();
  drawBigShip();
  drawBird1();
  drawBird2();
  drawBird3();

glColor3f(1,1,1);
      drawText("PIRATES BOMB", 20, 950);
      drawText("TO EXIT PRESS F4", 20, 920);
    //WelcomeScreen();

  glFlush();

  glutSwapBuffers();

}
void mySpecialKey(int key, int x, int y)
{
    //GLfloat tx = 0.0, ty = 0.0,tz=0.0;
    switch(key)
    {
    case GLUT_KEY_UP    : ty += 10; break;
    case GLUT_KEY_DOWN  : ty -= 10; break;
    case GLUT_KEY_RIGHT : tx += 10; break;
    case GLUT_KEY_LEFT  : tx -= 10; break;
    case GLUT_KEY_F1    : tz -= 10; break;
    case GLUT_KEY_F2    : tz += 10; break;
    case GLUT_KEY_F3    : tty -= 10; break;
        case GLUT_KEY_F5    : ttx += 10; break;
    //   case GLUT_KEY_F5    : glutSwapBuffers();
          //glutPostRedisplay();
         // break;
          case GLUT_KEY_F4:exit(0);

    }
    //render_1(tx,ty,tz);   //no need for this call the system will call it automatcly when calling the follwoing fucntion glutPostRedisplay
    glutPostRedisplay();
}
void mainloopF(int v)
{
    MoveBird1 += 4;
    MoveSmlShip +=2;
    MoveBigShip  +=3;
    MoveBomb+=7;

    if (MoveBomb>150) tty -= 20 ;//isBomb=false;
    //if(tty==300) tz -=10;
    //if(ttz==50)
//  MoveBomb = 0;
    //if (isBomb==true) MoveBomb= 0;
    //if(collision()) tz -=10;
    if(MoveBird1>400)   MoveBird1 = 0;
    if(MoveSmlShip>300) MoveSmlShip = 0;
    if(MoveBigShip>300) MoveBigShip = 0;
    glutPostRedisplay();
    glutTimerFunc(100,mainloopF,0);
}
int main(int argc, char **argv){
    glutInit(& argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(900,900);
    glutInitWindowPosition(0,0);
    glutCreateWindow("pirates bomb");
    glutSpecialFunc(mySpecialKey);
    glutTimerFunc(500,mainloopF,0);
    glutDisplayFunc(myDisplay);

    init();
    glutMainLoop();

}


my project is a game called pirates bomb
the bomber shoot the bomb (ball) and then if it hit the ship it will explode and drown
i just want one object to be 3d
i tried to do drawBoom() in 3d but i failed<<< that's the ball that coming from the bomber (drawBomb)
i want the drawBoom() to be in 3d just that
please help me and i will be very thankful
Posted
Updated 5-May-15 3:29am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900