简体   繁体   English

C ++ / GLUT:生成多个小行星/子弹

[英]C++/GLUT: Generating multiple asteroids/bullets

I am having trouble displaying and animation multiple asteroids for my GLUT asteroids game. 我的GLUT小行星游戏无法显示和动画化多个小行星。 I can generate 1 ok but when I try to add more the output is not right, createasteroid keeps getting called and just flashing up asteroids for milliseconds in different parts of the screen for some reason. 我可以生成1 ok,但是当我尝试添加更多输出时,输出不正确,createasteroid不断被调用,由于某种原因,只是在屏幕的不同部分将小行星闪烁了毫秒。 I believe it may be because the objects are getting destroyed when the display function ends but I cant figure out a workaround Here is my code so far: 我相信这可能是因为当显示功能结束时,对象被破坏了,但是我无法找出解决方法,这是到目前为止的代码:

asteroid.h 小行星

class asteroid
{
public:
asteroid(void); //constructer
~asteroid(void); //deconstructer

void createAsteroid();
float generateAsteroidLocation(float a, float b);
void animateAsteroid();
};

asteroid.cpp 小行星

#include "asteroid.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include <GL/glut.h>

float asteroidX,asteroidY, V;
int numOfAsteroids=0;

asteroid::asteroid(void){

}

asteroid::~asteroid(void){

}

void asteroid::createAsteroid(){
// Translate to an area outside of the screen and then give a random velocity in     the direction of the player
asteroidX = generateAsteroidLocation(0, 30);
asteroidY = generateAsteroidLocation(0, 30);
V = generateAsteroidLocation(0.000030, 0.000050);

printf("Asteroid Y Location %f \n", asteroidY);
printf("Asteroid X Location %f \n", asteroidX);
printf("Asteroid Velocity %f \n", V);
printf("Asteroid Created! \n");

glPushMatrix();
glTranslatef(asteroidX, asteroidY, 0.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(1.0, 1.0);
glVertex2f(1.8, 0.0);
glVertex2f(1.4, -0.8);
glVertex2f(1.0, -1.7);
glVertex2f(-1.5, -1.0);
glVertex2f(-1.0, 0.0);
glVertex2f(0.0, 1.4);
glVertex2f(1.0, 1.0);
glEnd();
glPopMatrix();

}

float asteroid::generateAsteroidLocation(float a, float b){
float random = ((float) rand()) / (float) RAND_MAX;
float range = b - a; 
return (random*range) + a;
}

void asteroid::animateAsteroid(){
float dt = 3500;
float Dx = 25 - asteroidX;
float Dy = 25 - asteroidY;
float Cx = asteroidX + Dx / sqrt(Dx*Dx+Dy*Dy) * V * dt;
float Cy = asteroidY + Dy / sqrt(Dx*Dx+Dy*Dy) * V * dt;
asteroidX = Cx;
asteroidY = Cy;
}

main.cpp ( the functions causing problems ) main.cpp(导致问题的函数)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include "asteroid.h"
#include <GL/glut.h>

asteroid a1;
asteroid a2;

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// check to see if the player is ready, if not, show the splashscreen
if(ready == false){
    splashScreen();
    showText();
}
else{

    createSpaceship();
    // PROBLEM HERE
    a1.createAsteroid();
    a2.createAsteroid();

}
// Setup the scene
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 50, 0, 50, 0, 50);
// call the needed functions
glutSwapBuffers();

}

void idle(void)
{
glutPostWindowRedisplay(glutGetWindow());
// PROBLEM HERE
a1.animateAsteroid();
a2.animateAsteroid();

}

Any help on this would be great. 任何帮助都会很棒。 I'm stumped as to what approach to take. 我对采取什么方法感到困惑。 I can provide more information or a pastebin if needed. 如果需要,我可以提供更多信息或粘贴框。 Thanks! 谢谢! -Dan -担

You simply want to break your createAsteroid function in two, one which creates the Asteroid , setting its initial values, and another one that does the rendering. 您只想将createAsteroid函数分为两部分,一个函数创建Asteroid ,设置其初始值,另一个函数进行渲染。

You seem to have a ready function to know when the loading is done, it's around there that you should create and initialize your asteroids. 您似乎已经ready可以知道何时完成加载,因此应该在其附近创建和初始化小行星。 In display you should call a function that does only this part: 在显示中,您应该调用仅执行此部分的功能:

glPushMatrix();
glTranslatef(asteroidX, asteroidY, 0.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(1.0, 1.0);
glVertex2f(1.8, 0.0);
glVertex2f(1.4, -0.8);
glVertex2f(1.0, -1.7);
glVertex2f(-1.5, -1.0);
glVertex2f(-1.0, 0.0);
glVertex2f(0.0, 1.4);
glVertex2f(1.0, 1.0);
glEnd();
glPopMatrix();

Also, you seem to recreate your player's spaceship at every display too, it should only be created in the initialization phase like the Asteroids! 同样,您似乎也在每次display时都重新创建了玩家的太空飞船,它只能像小行星一样在初始化阶段创建!

Edit: If I'm not clear, when creating a game object you usually want at least 4 main methods: Create/Init , Update (where you would animate your object, do gameplay code like testing for bullet hits, etc...), Render and Clean up / Destroy . 编辑:如果我不清楚,在创建游戏对象时,通常通常需要至少4种主要方法: 创建/初始化更新 (您可以为对象设置动画,执行游戏代码,例如测试项目符号命中率等)。 , 渲染清理/销毁 Sticking to that should really help you getting your game going! 坚持这一点确实可以帮助您使游戏顺利进行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM