Basic Shoot Game but I could not manage to encoding

Ads%26%23305%3Bz

  1. my target is moving up and down throughout the game and
  2. the bullet is heading towards the right when it fires and
  3. If it hits the game ends

I tried to convert samples into my own project, but it didn’t work.I found the code from this site and code sent by nexusone.

sorry for my bad english way of writing.i am sucked about it.I need your help :frowning:!

#include <Gl/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
 
using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::setiosflags;
using std::setprecision;
 
void display(void);
void exits(int);
 
void moveright(void);
void moveleft(void);
void moveup(void);
void movedown(void);
 
void shoot(void);
void update_objects(void);
 
static void key(unsigned char key, int x, int y);
 
void spinright(void);
void spinleft(void);
 
//void DrawCircle( double r );
//void DrawEllipse( double a, double b );
 
void reshape(int, int);
 
void init(void);
 
bool tank2 = true;
bool bullet = false;
 
float e;
 
int main (int argc,char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(450, 450);
glutInitWindowPosition(200, 100);
glutCreateWindow("Tank Attempt");
init();
glutDisplayFunc(display);
glutIdleFunc(update_objects); 
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}
 
void init(void)
{
glClearColor(0.1, 0.5, 0.1, 0.0);
glShadeModel(GL_SMOOTH);
 
glutCreateMenu(exits);
glutAddMenuEntry("Exit", 1);
glutAddMenuEntry("Willy P.", 2);
glutAddMenuEntry("Tank # 2", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
 
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
 
//DrawCircle(5.0);
 
if (tank2 == true)
{
glBegin(GL_QUADS);
glColor3f(0.5, 0.1, 0.2);
glVertex2f(-8.0, -20.0);
glVertex2f(-10.0, 5.0);
glVertex2f(10.0, 5.0);
glVertex2f(8.0, -20.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.5, 0.1, 0.2);
glVertex2f(-10.0, 5.0);
glVertex2f(-8.0, 10.0);
glVertex2f(8.0, 10.0);
glVertex2f(10.0, 5.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.5, 0.1, 0.2);
glVertex2f(-1.0, 10.0);
glVertex2f(-1.0, 21.0);
glVertex2f(1.0, 21.0);
glVertex2f(1.0, 10.0);
glEnd(); 
glBegin(GL_LINE_LOOP);
glColor3f(0.0, 0.1, 0.0);
glVertex2f(-4.0, 0.0);
glVertex2f(-1.5, 0.0);
glVertex2f(0.0, 3.0);
glVertex2f(1.5, 0.0);
glVertex2f(4.0, 0.0);
glVertex2f(2.25, -3.0);
glVertex2f(3.0, -6.5);
glVertex2f(0.0, -4.0);
glVertex2f(-3.0, -6.5);
glVertex2f(-2.25, -3.0);
glEnd();
}
 
if(bullet == true)
{
glTranslatef(0.0, e, 0.0);
 
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-1.0, 21.0);
glVertex2f(1.0, 21.0);
glVertex2f(1.0, 20.0);
glVertex2f(-1.0, 20.0);
glEnd();
}
 
/*if(bullet == true)
{
for(e = 0.0; e<=25; e = e + .001)
{
glTranslatef(0.0, e, 0.0);
 
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-1.0, 21.0);
glVertex2f(1.0, 21.0);
glVertex2f(1.0, 20.0);
glVertex2f(-1.0, 20.0);
glEnd();
glutPostRedisplay();
}
bullet = false;
}*/
 
glPopMatrix();
glutSwapBuffers();
}
 
void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); 
}
 
void exits(int value)
{
if(value==1) exit(0);
if(value==2) 
{
tank2 = false;
bullet = false;
glutPostRedisplay();
glClearColor(1.0, 1.0, 1.0, 0.0);
}
if(value==4)
{
tank2 = true;
glutPostRedisplay();
}
 
}
 
 
static void key(unsigned char key, int x, int y)
{
switch (key) 
{
case 'Q':
case 'q':
exit(0);
glutPostRedisplay();
break;
case 'H':
case 'h':
moveright();
glutPostRedisplay();
break;
case 'F':
case 'f':
moveleft();
glutPostRedisplay();
break;
case 'T':
case 't':
moveup();
glutPostRedisplay();
break;
case 'G':
case 'g':
movedown();
glutPostRedisplay();
break;
case 32:
shoot();
glutPostRedisplay();
break;
case 'O':
case 'o':
spinleft();
glutPostRedisplay();
break;
case 'P':
case 'p':
spinright();
glutPostRedisplay();
break;
default:
break;
}
cout << key << static_cast<int>(key) << endl;
}
 
void moveright(void)
{
 
for(int b = 0; b<=1; b++)
{
glTranslatef(b, 0.0, 0.0);
glutPostRedisplay();
}
 
}
 
void moveleft(void)
{
for(int a = 0; a>=-1; a--)
{
glTranslatef(a, 0.0, 0.0);
glutPostRedisplay();
}
 
}
 
void moveup(void)
{
for(int c = 0; c<=1; c++)
{
glTranslatef(0.0, c, 0.0);
glutPostRedisplay();
}
 
}
 
void movedown(void)
{
for(int d = 0; d>=-1; d--)
{
glTranslatef(0.0, d, 0.0);
glutPostRedisplay();
}
 
}
 
void shoot(void)
{
bullet = true;
cout << "shooting
" << endl;
}
 
void spinleft(void)
{
glRotatef(22.5, 0.0, 0.0, 1.0);
glutPostRedisplay();
}
 
void spinright(void)
{
glRotatef(-22.5, 0.0, 0.0, 1.0);
glutPostRedisplay();
}
 
void update_objects(void)
{
if (bullet == true)
{
e = e + .25;
if (e > 60 ) bullet = false;
}
glutPostRedisplay();
}

https://www.opengl.org/discussion_boards/showthread.php/197853-Try-to-draw-a-cannon?highlight=shoot

what are your compiler errors? screenshot?

jeff

I delete my work.Because didn’t work.

I didn’t exactly find out what to pull out of this code and :confused:
I don’t understand how to translate my project