shooting bullets

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2394 Views - Last Post: 22 June 2014 - 07:15 PM Rate Topic: -----

#1 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

shooting bullets

Posted 24 May 2014 - 12:37 PM

well I am having trouble getting my space ship to shoot bullets in a machine gun fashion. basically I can shoot a bullet and have it move up the screen when I am pressing the space bar. but I want it to scroll all the way up the screen after I have briefly pressed the space bar. kind of like in asteroids. here is the code I am using so far.
void SpaceCraft(void)
{
	glBegin(GL_LINE_LOOP);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, -0.5f, 0.0f);
	glVertex3f(-0.5f, -1.0f, 0.0f);
	glVertex3f(-1.0f, -1.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, -1.0f, 0.0f);
	glVertex3f(0.5f, -1.0f, 0.0f);
	glEnd();
}

void bullet(void)
{
	glBegin(GL_POINTS);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, up, 0.0f);
	glEnd();

	up += 0.1f;

	if (up >= 10.0f)
	{
		up = 1.0f;
	}
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0f, 1.0f, 0.0f);
	glPushMatrix();
	SpaceCraft();
	bullet();
	glPopMatrix();
	glutSwapBuffers();
	glFlush();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		bullet();
		break;
	}
	glutPostRedisplay();
}



Is This A Good Question/Topic? 0
  • +

Replies To: shooting bullets

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: shooting bullets

Posted 25 May 2014 - 10:36 AM

Throw it into a loop then. When you click the space bar it calls bullet() where you have a loop that moves it up .1f on each iteration and tests when it is off the screen. When it is off the screen, you end the loop, reset up back to where it needs to be and you exit the function.

:)
Was This Post Helpful? 0
  • +
  • -

#3 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 25 May 2014 - 10:15 PM

cool thanks I will try that
Was This Post Helpful? 0
  • +
  • -

#4 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 26 May 2014 - 04:37 PM

should I put the loop in the bullet function?
Was This Post Helpful? 0
  • +
  • -

#5 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 531
  • Joined: 15-March 14

Re: shooting bullets

Posted 26 May 2014 - 09:27 PM

You should call the bullet function from the main loop, once every loop. The main loop handles all the game events and objects.
Was This Post Helpful? 0
  • +
  • -

#6 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 28 May 2014 - 04:04 PM

any more input ????? I am going to use a while loop and if that don't work then I am going to use a for loop. I am a little confused as to where exactly in my code I should put the loop, but I will try my best to figure it out.
Was This Post Helpful? 0
  • +
  • -

#7 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 28 May 2014 - 04:56 PM

well I tried some loops but no success yet.
here is my code so far.
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0f, 1.0f, 0.0f);
	glPushMatrix();
	SpaceCraft();
	while (x <= 10.0f)
	{
		glColor3f(0.0f, 1.0f, 0.0f);
		bullet();
		x += 0.1f;
		glColor3f(0.0f, 0.0f, 0.0f);
	}
	glPopMatrix();
	glutSwapBuffers();
	glFlush();
}


Was This Post Helpful? 0
  • +
  • -

#8 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 531
  • Joined: 15-March 14

Re: shooting bullets

Posted 29 May 2014 - 06:21 AM

I will give you a little code example.

This code will handle several bullets at once. The bullets are stored in a array. Each bullet has a life, if that life is zero the bullet is dead. When adding a bullet the code loops through the array of bullets and if it finds one with a life == 0 then set the life to whatever value posisions it and its now in the game.

The main loop updates everything required. It calls doBullets which decreases the bullets life and checks if the bullet goes outside the playfield.
the Main loop also checks input, draw bullets, ship and what not.

Set MAX_BULLETS to how ever many you want. This is rapid fire so you will have to check the time between fire. Just use a simple frame counter. (Each main loop is called a frame)


#define MAX_BULLETS 10           // max bullets
#define PLAYFIELD_WIDTH 1920     // size of playfield to check bullets against
#define PLAYFIELD_HEIGHT 1080

// functions defined
void initBullets(void);
void initShip(void);
void mainLoop(void);
void addBullet(x,y,dx,dy,life);
void getUserInput(void);
void doBullets(void);
void drawBullets(void);

// game vars and structures defined
bool running = true;             // this is true while running.Set to false to stop game

// the ship structure
struct spaceShip {
    float x;             // x pos 
    float y;             // y pos
    float dir;           // direction 
    float bulletPower;   // the life the bullets get
} ship;   // named ship


// struct for a bullet
struct bullet {
    float x;              // position x,y
    float y;              // 
    float deltaX;         // x,y movement per frame
    float deltaY;         // 
    int life;             // life of bullet
} bullets [MAX_BULLETS];  // array of bullets

//-------------------------------------------------------------------------------------
// game functions
// set up bullets array
void initBullets(void){  
    for(int i = 0; i < MAX_BULLETS; i++){  // for each bullet
        bullets[i].life = 0;              // set bullet as empty
    }
    return();
}

// set up ship
void initShip(void){
   ship.x = 100.0f;   // ship x start
   ship.y = 100.0f;   // 
   ship.dir = 3.14f;  // direction of ship
   ship.bulletPower = 100;  
}

//-----------------------------------------------------------------------------
// this is the main game loop
void mainLoop(void){
   initBullets();   // set up bullets
   initShip();      // set up Ship
   while(running){  // start main loop until running = false
        getUserInput();   // gets user input 
        doBullets();      // update bullets
        drawSpaceShip();  // draws the space ship
        drawBullets();    // draws the bullet
        drawSpaceShip();
    }
    return();
}

// get the user input
void getUserInput(void){
   ... get keyboard or game controller input ...
   ... what is needed to contol the game
   
   // do the bullet stuff
   if(fireButtonHit){   // if fire button/key hit
        // start a new bullet from the space ship travelling in the direction
        // the ship is facing
        addBullet(ship.x,ship.y,sin(ship.dir)*ship.bulletPower,cos(ship.dir)*ship.bulletPower,100);
    }
}

// update bullet arrays
void doBullets(void){
   for(int i = 0; i < MAX_BULLETS; i++){        // loop each bullet
        if(bullet[i].life > 0){                 // if bullet alive
            bullet[i].life -= 1;                // decrease life by one
            bullet[i].x += bullet[i].deltaX;    // move bullet
            bullet[i].y += bullet[i].deltaY;

            // check if bullet is still in playfield
            if(bullet[i].x < 0 || bullet[i].x > PLAYFIELD_WIDTH ||
                bullet[i].y < 0 || bullet[i].y > PLAYFIELD_HEIGHT
                                                // bullet outside play field
                bullet[i].life = 0;             // kill the bullet
            }
            ... check bullet hittin other objects ...
        }
    }
   return();
}

// draw all the bullets
void drawBullets(void){
   for(int i = 0; i < MAX_BULLETS; i++){  //loop each bullet
        if(bullets[i].life > 0){          // is the bullet alive
            ... draw each bullet here ....
        }
    }
   return();
}

void addBullet(x,y,dx,dy,life){
   for(int i = 0; i < MAX_BULLETS; i++){ // loop to find empty bullet slot
       if(bullets[i].life === 0){     // if bullet life is zero then its
          bullets[i].x = x;           // free to get another bullet
          bullets[i].y = y;           
          bullets[i].deltaX = dx;
          bullets[i].deltaY = dy;
          bullets[i].life = life;
          return(); // exit as bullet has been started
       }
   } 
   // no free bullets so just return without adding one
   return();
}




This is only pseudo code, there is much missing.
Was This Post Helpful? 0
  • +
  • -

#9 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 29 May 2014 - 10:20 AM

wow that is a lot of code
Was This Post Helpful? 0
  • +
  • -

#10 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 531
  • Joined: 15-March 14

Re: shooting bullets

Posted 29 May 2014 - 11:09 AM

That's not much at all. A simple (very simple) game will have several hundreds of lines of code. 20-40 thousand lines for a typical professional online game. 500 thousand and more lines of code for a commercial games. You build them one part at a time and you would be surprised how quickly the size of the code expands. Last game I wrote was 39000 lines, and it was just a 2D puzzel game. My biggest professional project was over 500,000 lines in 5 years.
Was This Post Helpful? 0
  • +
  • -

#11 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 29 May 2014 - 11:18 AM

well thanks for all the code I will do my best to figure out this problem, but I still think the solution is very simple and does not require an excessive amount of code.
Was This Post Helpful? 0
  • +
  • -

#12 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 07 June 2014 - 06:11 PM

well I have finished reading the opengl superbible from cover to cover and I am half way through the opengl programming guide, so I am working very hard at making my game. I just need a little more help. sorry for the trouble but I am still stuck on the firing the bullets problem. I will try to implement several different loops to solve the problem. Let me know if you need a little more code on my progress. Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

#13 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 07 June 2014 - 08:08 PM

well I have tried the following code but all I get is a long line of bullet points going up from my spaceship to the top of the screen
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0f, 1.0f, 0.0f);
	glPushMatrix();
	SpaceCraft();
	while (x <= 10.0f)
	{
		glColor3f(0.0f, 1.0f, 0.0f);
		bullet();
		x += 0.1f;
		glColor3f(0.0f, 0.0f, 0.0f);
	}
	glPopMatrix();
	glutSwapBuffers();
	glFlush();
}


Was This Post Helpful? 0
  • +
  • -

#14 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: shooting bullets

Posted 09 June 2014 - 03:03 AM

Well from the code you posted that is what you would expect to happen right?

What do you think this while loop does?

while (x <= 10.0f)
{
  glColor3f(0.0f, 1.0f, 0.0f);
  bullet();
  x += 0.1f;
  glColor3f(0.0f, 0.0f, 0.0f);
}



It will continually call this loop until the x value you have declared is less than 10.0f, if you are incrementing this by 0.1f each time through the loop then it will call it 100 times so it will draw 100 bullets before moving onto swap the buffers.

You need to structure this better and break your bullet values into something separate that you can update each frame rather than using a while loop.

// pseudo code for bullet struct/class
Bullet
{
   _x 
   _y

   create(x, y)
   {
      _x = x
      _y = y 
   }

   update()
   {
      y += speed
   }

   render()
   {
      draw(x, y);
   }
}

// psuedo code for main loop

clear(); // clear the renderer

for all bullets created
{
   update();
   render();
}


This post has been edited by stayscrisp: 09 June 2014 - 03:03 AM

Was This Post Helpful? 0
  • +
  • -

#15 pbivens85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 22-October 13

Re: shooting bullets

Posted 09 June 2014 - 11:25 AM

so am I close to the answer?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2