⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 SDL-Ball这款经典的弹球游戏克隆自arkanoid、dxball、breakout
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  void draw() {      GLfloat newsize;    if(setting.eyeCandy)      tail.draw();    if(growing)    {      newsize = growspeed * globalMilliTicks;      width += newsize;      height += newsize;            if(width >= destwidth)      {        width=destwidth;        height=destwidth;                growing=0;      }            tail.width = width;      tail.height = height;        } else if(shrinking)    {           newsize = growspeed * globalMilliTicks;      width -= newsize;      height -= newsize;      if(width <= destwidth)      {        width=destwidth;        height=destwidth;        shrinking=0;      }            tail.width = width;      tail.height = height;    }        if(glued && player.powerup[PO_LASER])    {      if(player.powerup[PO_AIM])      {        if(aimdir==0)        {          rad -= 1.2*globalMilliTicks;                    if(rad < 0.34906585)            aimdir=1;        } else {          rad += 1.2*globalMilliTicks;          if(rad  > 2.7925268)            aimdir=0;        }        setangle(rad*(180.0f/PI));      } else {        rad = getRad();      }            GLfloat bxb = cos(rad)*0.5, byb = sin(rad)*0.5;    glLoadIdentity();    glTranslatef( posx, posy, -3.0);    glDisable(GL_TEXTURE_2D);      glLineWidth ( 1.0 );      glEnable( GL_LINE_SMOOTH );      glBegin( GL_LINES );        glColor4f(rndflt(2,0), rndflt(1,0), 0.0, 0.0);        glVertex3f( 0.0, 0.0, 0.0 );        glColor4f(rndflt(2,0), 0.0, 0.0, 1.0);        glVertex3f( bxb, byb, 0.0 );      glEnd();            glPointSize( 5.0f );      glColor4f(1.0, 0.0, 0.0, 1.0);      glEnable(GL_POINT_SMOOTH);      glBegin( GL_POINTS );        glVertex3f(bxb, byb, 0.0);      glEnd( );          }    glLoadIdentity();    glTranslatef( posx, posy, -3.0);    glEnable(GL_TEXTURE_2D);    glColor4f(1.0, 1.0, 1.0, 1.0);    if(explosive)    {      fireTex.play();      glBindTexture(GL_TEXTURE_2D, fireTex.prop.texture);      glEnable(GL_BLEND);      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);      glBegin( GL_QUADS );        glTexCoord2f(fireTex.pos[0],fireTex.pos[1]); glVertex3f( -width, height, 0.0 );        glTexCoord2f(fireTex.pos[2],fireTex.pos[3]); glVertex3f(  width, height, 0.0 );        glTexCoord2f(fireTex.pos[4],fireTex.pos[5]); glVertex3f(  width,-height, 0.0 );        glTexCoord2f(fireTex.pos[6],fireTex.pos[7]); glVertex3f( -width,-height, 0.0 );      glEnd( );    } else {      glBindTexture(GL_TEXTURE_2D, tex.prop.texture);      glEnable(GL_BLEND);      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);      glBegin( GL_QUADS );        glTexCoord2f(0.05,0.06); glVertex3f( -width, height, 0.0 );        glTexCoord2f(0.98,0.06); glVertex3f(  width, height, 0.0 );        glTexCoord2f(0.98,0.93); glVertex3f(  width,-height, 0.0 );        glTexCoord2f(0.05,0.93); glVertex3f( -width,-height, 0.0 );      glEnd( );    }     }    GLfloat getRad()  {    rad = atan2(yvel,xvel);    return(rad);  }  void setangle(GLfloat o)  {    tail.texture = texTail.prop.texture;    if(o < 20)    {      o=20;    }        if( o > 340)    {      o=340;    }        rad = 2.0*PI*(o/360.0); //Vinklen i radianer    xvel = velocity * cos(rad);    yvel = velocity * sin(rad);  }    void setspeed(GLfloat v)  {    velocity = v;    getRad();    xvel = velocity * cos(rad);    yvel = velocity * sin(rad);  }    void setSize(GLfloat s)  {    float rad;        if(s > width)      growing=1;      else if(s < width)      shrinking=1;          destwidth=s;        int i=0;    //opdater points    for(rad=0.0; rad < 6.3; rad +=0.2)    {      if(i < 32)      {        bsin[i] = sin(rad)*s;        bcos[i] = cos(rad)*s;      }      i++;    }  }};void coldet(brick & br, ball & ba, pos & p, effectManager & fxMan);void padcoldet(ball & b, paddle_class & p, pos & po);class ballManager {  public:    int activeBalls;    list <ball> b;    textureClass tex[3];    void initBalls()    {      activeBalls=0;      clear();    }        ballManager(textureClass btex[])    {      tex[0] = btex[0];      tex[1] = btex[1];      tex[2] = btex[2];            tex[1].prop.ticks = 50;      tex[1].prop.cols = 4;      tex[1].prop.rows = 4;      tex[1].prop.xoffset = 0.25;      tex[1].prop.yoffset = 0.25;      tex[1].prop.frames = 8;      tex[1].prop.bidir = 1;      tex[1].prop.playing = 1;            initBalls();    }        //klon alle aktive bolde    void multiply()    {      pos op;      int a=0,i=0;        //How many balls are active?        for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)        {          a++;        }                for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)        {          if(i != a)          {            i++;            op.y = it->posy;            op.x = it->posx;            spawn(op,0,0.0f,it->velocity, rndflt(RAD,0)*(180.0/PI));          }        }    }        void unglue()    {      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {        it->glued=0;      }    }        void spawn(pos p, bool glued, GLfloat gx ,GLfloat speed, GLfloat angle)    {      ball tempBall;      tempBall.tex = tex[0];      tempBall.fireTex = tex[1];      tempBall.texTail = tex[2];      tempBall.glued=glued;      tempBall.width=0.0;      tempBall.height=0.0;      tempBall.gluedX=gx;      tempBall.active=1;      tempBall.collide=1;      tempBall.reflect=1;      tempBall.lastX = p.x;      tempBall.lastY = p.y;      tempBall.posx = p.x;      tempBall.posy = p.y;      tempBall.gravity=0;      tempBall.explosive=0;      tempBall.setspeed(speed);      tempBall.setangle(angle);      tempBall.setSize(0.02);                  //Find ud af om den skal "arve" powerups      if(player.difficulty < 3)      {        tempBall.explosive = player.powerup[PO_EXPLOSIVE];                if(player.powerup[PO_SMALLBALL])        {          powerup(PO_SMALLBALL);        }                if(player.powerup[PO_BIGBALL])        {          powerup(PO_BIGBALL);        }              }            b.push_back(tempBall);    }        void clear()    {      activeBalls=0;      b.clear();    }        void move()    {      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {        it->move();      }    }        void draw()    {      int a=0;      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {        it->draw();        if(!it->active)        {          it=b.erase(it);        } else {          a++;        }      }      activeBalls=a;    }        void bcoldet(brick & bri,effectManager & fxMan)    {      pos p;      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {          p.x=100;          coldet(bri, *it, p, fxMan);          if(p.x < 50)          {            if(setting.eyeCandy)            {              if(!it->gravity)              {                //spawn partikler                fxMan.set(FX_VAR_TYPE, FX_SPARKS);                fxMan.set(FX_VAR_COLDET,1);                                fxMan.set(FX_VAR_SPEED, 1.0f);                                fxMan.set(FX_VAR_LIFE, 1500);                fxMan.set(FX_VAR_NUM, 16);                fxMan.set(FX_VAR_SIZE, 0.015f);                    fxMan.set(FX_VAR_COLOR, 1.0,1.0,0.8);                                //Vi vil ikke ha collisions partikler på hvis bolden ligger på dem                  fxMan.spawn(p);              }            }                      }      }    }        void pcoldet(paddle_class & paddle,effectManager & fxMan)    {      pos p;      for(list<ball>::iterator it= b.begin(); it != b.end(); ++it)      {                  if(it->glued)            it->posx=paddle.posx+paddle.width-it->gluedX;          p.x=100;          padcoldet(*it, paddle, p);          if(p.x < 50)          {            if(it->gravity)            {              player.score += 1000;            }            if(setting.eyeCandy)            {              //spawn partikler              fxMan.set(FX_VAR_TYPE, FX_SPARKS);              fxMan.set(FX_VAR_LIFE, 2000);              fxMan.set(FX_VAR_GRAVITY, 0.6f);              fxMan.set(FX_VAR_NUM, 16);              fxMan.set(FX_VAR_COLDET,1);              fxMan.set(FX_VAR_SIZE, 0.01f);              fxMan.set(FX_VAR_COLOR, 1.0,1.0,0.8);               p.y = paddle.posy+paddle.height;              fxMan.set(FX_VAR_SPEED, 0.5f);              fxMan.spawn(p);                            if(it->gravity)              {                p.y = paddle.posy+paddle.height;                                fxMan.set(FX_VAR_LIFE, 1000);                fxMan.set(FX_VAR_NUM, 32);                fxMan.set(FX_VAR_COLOR, 1.0,1.0,0.7);                                         fxMan.set(FX_VAR_SPEED, 0.6f);                fxMan.spawn(p);                                        fxMan.set(FX_VAR_LIFE, 1500);                fxMan.set(FX_VAR_NUM, 32);                fxMan.set(FX_VAR_COLOR, 1.0,0.0,0.0);                 fxMan.spawn(p);                        fxMan.set(FX_VAR_SPEED, 0.7f);                fxMan.set(FX_VAR_LIFE, 1500);                fxMan.set(FX_VAR_NUM, 32);                fxMan.set(FX_VAR_COLOR, 1.0,1.0,1.0);                 fxMan.spawn(p);                                it->active=0;              }            } //eyecandy          } // if col      } //for loop    } //pcoldet        void updatelast()    {      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {        it->lastX=it->posx;        it->lastY=it->posy;      }    }        void powerup(int powerup)    {      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {          switch(powerup)          {            case PO_GRAVITY: //gravity              it->gravity=1;              it->died=globalTicks;              break;            case PO_BIGBALL: //big balls              it->setSize(0.03);                            //Sløv bolden ned 10 eller 20% alt efter hvilken sværhedsgrad              if(player.difficulty == EASY)              {                it->setspeed( it->velocity - ((it->velocity/100.f)*20.0f) );                if(it->velocity < EASY_BALLSPEED)                {                  it->velocity = EASY_BALLSPEED;                }              } else if(player.difficulty == NORMAL)              {                it->setspeed( it->velocity - (it->velocity/100.f)*10.0f);                if(it->velocity < NORMAL_BALLSPEED)                {                  it->velocity = NORMAL_BALLSPEED;                }              }              break;            case PO_SMALLBALL: //small balls              it->setSize(0.015);              //speed bolden op               if(player.difficulty == EASY)              {                it->setspeed( it->velocity + ((it->velocity/100.f)*10.0f) );                if(it->velocity > EASY_MAXBALLSPEED)                {                  it->velocity = EASY_MAXBALLSPEED;                }              } else if(player.difficulty == NORMAL)              {                it->setspeed( it->velocity + ((it->velocity/100.f)*20.0f) );                if(it->velocity > NORMAL_MAXBALLSPEED)                {                  it->velocity = NORMAL_MAXBALLSPEED;                }              }              break;            case PO_NORMALBALL: // normal balls              it->setSize(0.02);              if(player.difficulty == EASY)              {                it->setspeed(EASY_BALLSPEED);              } else if(player.difficulty == NORMAL)              {                it->setspeed(NORMAL_BALLSPEED);              }              break;            case PO_EXPLOSIVE: //exploderer brikker              it->explosive=1;              break;                      }              }    }    void powerup(int powerup, GLfloat var)    {      for(list<ball>::iterator it = b.begin(); it != b.end(); ++it)      {          switch(powerup)          {            case PO_SPEEDBALL: //lav alle bolde hurtigere              it->setspeed(var);              break;          }              }    }};class powerupClass : public moving_object {  public:    int score;    int type;    int level, maxlevel;     GLfloat gravity;        powerupClass()    {      posx=0.0;      posy=0.0;      xvel=0.0;      yvel=0.0;      width=0.05;      height=0.05;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -