jeudi 3 juin 2021

Srand() and rand() still generating the same random number

Sorry I am new to c++ but I tried to follow a game tutorial in youtube and I think the logic is a bit wrong because it seems that the position of the enemy creates a pattern when it runs even though the srand and rand function are used, I think the same random number is being used.

#include <iostream>
#include <curses.h>
#include <time.h>
using namespace std;

void gotoxy (int x, int y){
  move (y, x);
}

bool exitGame;
bool isMoving;
int mycatX;
int mycatY;
int enemyX[4];
int enemyY[4] = {-9, -19, -29,-39};
int enemyPositionX[3] = {3, 18 ,32};

void startUp () {
  mycatX = 18;
  mycatY = 15;
  srand(time(NULL));
  for(int i = 0; i < 4; i++){
    enemyX[i] = enemyPositionX[rand() % 3];
  }
}

void layout () {
  if (isMoving){
      gotoxy (0, 0); printw ("##                                       ##");
      gotoxy (0, 1); printw ("##            ||            ||           ##");
      gotoxy (0, 2); printw ("##                                       ##");
      gotoxy (0, 3); printw ("##            ||            ||           ##");
      gotoxy (0, 4); printw ("##                                       ##");                     
      gotoxy (0, 5); printw ("##            ||            ||           ##");
      gotoxy (0, 6); printw ("##                                       ##");
      gotoxy (0, 7); printw ("##            ||            ||           ##");
      gotoxy (0, 8); printw ("##                                       ##");
      gotoxy (0, 9); printw ("##            ||            ||           ##");
      gotoxy (0, 10);printw ("##                                       ##");
      gotoxy (0, 11);printw ("##            ||            ||           ##");
      gotoxy (0, 12);printw ("##                                       ##");
      gotoxy (0, 13);printw ("##            ||            ||           ##");
      gotoxy (0, 14);printw ("##                                       ##");
      gotoxy (0, 15);printw ("##            ||            ||           ##");
      gotoxy (0, 16);printw ("##                                       ##");
      gotoxy (0, 17);printw ("##            ||            ||           ##");
      gotoxy (0, 18);printw ("##                                       ##");
      gotoxy (0, 19);printw ("##            ||            ||           ##");
      gotoxy (0, 20);printw ("##                                       ##");
      isMoving = false;
    }else{
      gotoxy (0, 0); printw ("##            ||            ||           ##");
      gotoxy (0, 1); printw ("##                                       ##");
      gotoxy (0, 2); printw ("##            ||            ||           ##");
      gotoxy (0, 3); printw ("##                                       ##");
      gotoxy (0, 4); printw ("##            ||            ||           ##");
      gotoxy (0, 5); printw ("##                                       ##");
      gotoxy (0, 6); printw ("##            ||            ||           ##");
      gotoxy (0, 7); printw ("##                                       ##");
      gotoxy (0, 8); printw ("##            ||            ||           ##");
      gotoxy (0, 9); printw ("##                                       ##");
      gotoxy (0, 10);printw ("##            ||            ||           ##");
      gotoxy (0, 11);printw ("##                                       ##");
      gotoxy (0, 12);printw ("##            ||            ||           ##");
      gotoxy (0, 13);printw ("##                                       ##");
      gotoxy (0, 14);printw ("##            ||            ||           ##");
      gotoxy (0, 15);printw ("##                                       ##");
      gotoxy (0, 16);printw ("##            ||            ||           ##");
      gotoxy (0, 17);printw ("##                                       ##");
      gotoxy (0, 18);printw ("##            ||            ||           ##");
      gotoxy (0, 19);printw ("##                                       ##");
      gotoxy (0, 20);printw ("##            ||            ||           ##");
      isMoving= true;
        
    }
}

void mycat ()
{
  gotoxy (mycatX, mycatY);     printw ("   ^^   ");
  gotoxy (mycatX, mycatY +1);  printw ("=( ^ ^)=");
  gotoxy (mycatX, mycatY + 2); printw ("  o  o ");
  gotoxy (mycatX, mycatY + 3); printw ("  (  )  ");
  gotoxy (mycatX, mycatY + 4); printw ("    \\   ");

}

void enemymouse ()
{
  for(int i = 0; i < 4; i++){
      if(enemyY[i] >= 0){
        gotoxy (enemyX[i], enemyY[i]);       printw ("  \\/ ");
        }
      if(enemyY[i]+1 >= 0){gotoxy (enemyX[i], enemyY[i] + 1); printw ("  ()  ");}
      if(enemyY[i]+2 >= 0){gotoxy (enemyX[i], enemyY[i] + 2); printw (" /  \\");}
      if(enemyY[i]+3 >= 0){gotoxy (enemyX[i], enemyY[i] + 3); printw ("((__))");}
      if(enemyY[i]+4 >= 0){gotoxy (enemyX[i], enemyY[i] + 4); printw ("   |  ");}    
         
         
          
      if(enemyY[i] > 20){gotoxy (enemyX[i], enemyY[i]);       printw ("         ");}
      if(enemyY[i]+1 > 20){gotoxy (enemyX[i], enemyY[i] + 1); printw ("         ");}
      if(enemyY[i]+2 > 20){gotoxy (enemyX[i], enemyY[i] + 2); printw ("         ");}
      if(enemyY[i]+3 > 20){gotoxy (enemyX[i], enemyY[i] + 3); printw ("         ");}
      if(enemyY[i]+4 > 20){gotoxy (enemyX[i], enemyY[i] + 4); printw ("         ");}
      if(enemyY[i] > 20) enemyY[i] = -10;
      enemyY[i]++;  
    }    
}
 

void controller (){
  nodelay (stdscr, TRUE);
  switch (getch ()) {
    case 'a':
      mycatX = mycatX - 13;
      break;
    case 'd':
      mycatX = mycatX + 13;
      break;
    case 'w':
      mycatY = mycatY - 2;
      break;
    case 's':
      mycatY = mycatY + 2;
      break;


    }

}

int main (){
  initscr ();
  startUp ();
  noecho ();
  scrollok (stdscr, TRUE);
  nodelay (stdscr, TRUE);
  while (!exitGame){
      layout ();
      mycat ();
      enemymouse ();
      controller ();
      napms(200);
      curs_set(0);
    }

  return 0;
}



Aucun commentaire:

Enregistrer un commentaire