Test Foro de elhacker.net SMF 2.1

Programación => Programación C/C++ => Mensaje iniciado por: cNoob en 29 Junio 2016, 00:30 AM

Título: Juego de naves y asteroides
Publicado por: cNoob en 29 Junio 2016, 00:30 AM
Hola!
Estoy haciendo un juego de una nave que debe esquivar asteroides que se acercan.
Para provocar que se acerquen hice que cada vez la x de estos sean menores, pero no se por que no funciona... He aquí el código; un saludo! (la parte que falla (que yo crea) es la que esta comentada como ASTEROID MOVEMENT (linea 20))

Código (cpp) [Seleccionar]
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width=20;
const int height=10;
int x, y, astdX=width-1, astdY,asteroidYrand, score;
enum eDirection {STOP=0, UP, DOWN};
eDirection dir;

void Setup()
{
   gameOver=false;
   dir=STOP;
   x=5;
   y=height/2;
   //ASTEROID MOVEMENT
   if(astdX==width-1){astdY=1+rand()%8;}
   if(astdX!=width&&astdX!=0){astdX--;}//ESTO DEBERIA HACER QUE LA X FUESE CADA VEZ MENOR, ES DECIR, QUE EL ASTEROIDE SE FUESE ACERCANDO
   if(astdX==0){astdX=width-1;}

}

void Draw()
{
   system("cls");
   for(int i=0;i<height+1;i++)
   {
       if(i==0)
       {
           for(int j=0;j<width+1;j++)
           {
               cout<<"#";
               if(j==width){cout<<endl;}
           }
       }
       if(i>0&&i<height)
           {
               for(int j=0;j<width+1;j++)
           {
               if(j==0){cout<<"#";}
               if(j>0&&j<width)
               {
                   if(i!=y){cout<<" ";}
                   if(j<5&&i==y){cout<<" ";}
                   if(j==5&&i==y){cout<<">";}
                   if(j>5&&i==y){cout<<" ";}
               }
               if(j==width){cout<<"#"<<endl;}
           }
           }
       if(i==height)
       {
           for(int j=0;j<width+1;j++)
           {
               cout<<"#";
               if(j==width){cout<<endl;}
           }
       }
   }
   cout<<"Score: "<<score<<endl;
   cout<<"astdY: "<<astdY<<endl<<"astdX: "<<astdX<<endl<<"Rand: "<<(1+rand()%8)<<endl;
}

void Input()
{
   if(_kbhit())
   {
       switch(_getch())
       {
       case 'w':
           dir=UP;
           break;
       case 's':
           dir=DOWN;
           break;
       case 'x':
           gameOver=true;
           break;
       }
   }
}

void Logic()
{
   switch(dir)
   {
   case UP:
       y--;
       break;
   case DOWN:
       y++;
       break;
   default:
       break;
   }
   if(y==1||y==height-1){dir=STOP;}
   if(y==0||y==height){y=height/2;}
   if(x==astdX&&y==astdY){gameOver=true;}
   if(x==astdX&&y!=astdY){score+=10;}
}

int main()
{
   Setup();
   while(!gameOver)
   {
       Draw();
       Input();
       Logic();
       Sleep(100);
   }
   return 0;
}
Título: Re: Juego de naves y asteroides
Publicado por: cNoob en 29 Junio 2016, 01:08 AM
Solucionado, he cambiado el código de lugar XD