如何随机创建不同类别的对象?c++



我正在用c++中的类来做这个项目,它由一辆可以避开障碍物的汽车组成。我想随机化类EnemiesCar、Nail、Hole的对象的外观(后两个是第一个的子类(。问题是,当我生成随机数时,它变化太快,出现的对象然后消失了(板上的对象在其他对象之间间歇性出现,因为随机数变化太快(。如何解决此问题?是否有其他解决方案可以随机化类对象?(我试着在Game::randomAppear(EnemiesCar&myEn,Nail&myNe,Hole&my Ho(中做这个函数,下面我只留下EnemiesCar类,因为它的子类相似(。

Game.cpp

#include "Game.h"
#include "Nail.h"
#include "EnemiesCar.h"
#include "Car.h"
#include "Hole.h"
#include <windows.h>
#include <iostream>
#include <thread>
#include <stdlib.h>     /* srand, rand */
#include <ctime>
using namespace std;
Game::Game(){
running = true;
counter = 100;
maxScore = 0;
maxLevel = 1;
timeS = 100.0;
level = 1;
levelUp = 1000;
levelDown = 0;
}
void Game::gotoXY(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void Game::resetBoard()
{
for(int j=0;j<20;j++){
for(int i=1;i<20;i++){
matrix[i][j]=0;
}
}
}
void Game::createTab()
{
for(int j=0;j<20;j++){
for(int i=0;i<21;i++){
if(i==0 || i==20){
gotoXY(i,j);
cout<<"b";
}else if(j == 19){
gotoXY(i,j);
cout<<"*";
}else if(matrix[i][j]==1){
gotoXY(i,j);
cout<<"m";
}else if(matrix[i][j]==2){
gotoXY(i,j);
cout<<"e";
}else if(matrix[i][j]==3){
gotoXY(i,j);
cout<<"c";
}else if(matrix[i][j]==4){
gotoXY(i,j);
cout<<"h";
}else {
gotoXY(i,j);
cout<<" ";
}
}
}
}

void Game::checkSamePosition(EnemiesCar EN, Nail NE)
{
if(NE.xPos == EN.xPos && NE.yPos == EN.yPos){
NE.xPos = NE.xPos + 2;
}
}
int Game::checkMaxScore()
{
if(maxScore < counter){
maxScore = counter;
}
return maxScore;
}
int Game::checkMaxLevel()
{
if(maxLevel < level){
maxLevel = level;
}
return maxLevel;
}
void Game::TabLevelUp()
{
system("cls");
gotoXY(5, 5);
cout << "Next Level" << endl;
gotoXY(5, 7);
cout << "Level: " << level;
}
void Game::TabLevelDown()
{
system("cls");
gotoXY(5, 5);
cout << "Previous Level" << endl;
gotoXY(5, 7);
cout << "Level: " << level;
}
void Game::gameLevels(Car &car)
{
if(counter == levelUp){
level++;
TabLevelUp();
Sleep(5000);
gotoXY(40, 9);
cout << "level " << level;
levelDown = levelUp;
levelUp = levelUp + 1000;
gotoXY(40, 10);
cout << "next " << levelUp;
if(timeS >= 40){
double update = timeS - 20.0;
timeS = update;
gotoXY(40, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else if(timeS == 20 || timeS == 30){
double update = timeS - 10.0;
timeS = update;
gotoXY(40, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else if(timeS >= 10 && timeS < 20){
double update = timeS - 5.0;
timeS = update;
gotoXY(40, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else if(timeS == 0){
Sleep(timeS);
}else{
double update = timeS - 0.5;
timeS = update;
gotoXY(40, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}
playGame(car);
}else if(counter < levelDown){
level--;
TabLevelDown();
Sleep(5000);
levelUp = levelDown;
levelDown = levelDown - 1000;
if(timeS >= 40 && timeS < 100){
double update = timeS + 20.0;
timeS = update;
gotoXY(45, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else if(timeS == 20 || timeS == 30){
double update = timeS + 10.0;
timeS = update;
gotoXY(45, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else if(timeS == 10 && timeS < 20){
double update = timeS + 5.0;
timeS = update;
gotoXY(45, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else if(timeS < 10 && level <= 17){
double update = timeS + 0.5;
timeS = update;
gotoXY(45, 7);
cout << "tmpIf " << timeS;
Sleep(update);
}else{
Sleep(timeS);
}
playGame(car);
}else{
Sleep(timeS);
}
}
void Game::showLevelScore()
{
gotoXY(50, 1);
cout<<"Score: " << counter;
gotoXY(50, 3);
cout<<"Level: " << level;
}
void Game::gameEnd()
{
if(counter <= 0){
running = false;
Sleep(1000);
system("cls");
gotoXY(5,4);
cout << "GAME OVER!!!";
gotoXY(5,6);
cout << "MAX LEVEL: " << checkMaxLevel();
gotoXY(5,7);
cout << "MAX SCORE: " << checkMaxScore();
gotoXY(0,0);
}
}
int Game::generateRandomNumber()
{
int rEne = rand() % 3;
gotoXY(50, 5);
cout << "R: " << rEne << endl;
return rEne;
}
void Game::randomAppear(EnemiesCar &myEn, Nail &myNe, Hole &myHo)
{
int rEne = generateRandomNumber();
if(rEne == 0){
myEn.appear();
myEn.drawEnemies(myEn.xPos, myEn.yPos, matrix);
myEn.move();
}else if(rEne == 1){
myNe.appear();
myNe.drawNails(myNe.xPos, myNe.yPos, matrix);
myNe.move();
}else{
myHo.appear();
myHo.drawHoles(myHo.xPos, myHo.yPos, matrix);
myHo.move();
}
}

void Game::playGame(Car &mycar){
EnemiesCar myEnmCar = EnemiesCar();
Nail nails = Nail();
Hole holes = Hole();
while(this->running){
srand (time(NULL));
resetBoard();
randomAppear(myEnmCar, nails, holes);

checkSamePosition(myEnmCar, nails);
/*myEnmCar.appear();
myEnmCar.drawEnemies(myEnmCar.xPos, myEnmCar.yPos, matrix);
myEnmCar.move();*/
/*gotoXY(3, 22);
cout << "enmpos: " << myEnmCar.xPos << myEnmCar.yPos << endl;*/
/*nails.appear();
nails.drawNails(nails.xPos, nails.yPos, matrix);
nails.move();
holes.appear();
holes.drawHoles(holes.xPos, holes.yPos, matrix);
holes.move();*/
mycar.drawCar(mycar.xPos, mycar.yPos, matrix);
mycar.checkCollusion(myEnmCar, nails, holes, &counter, &level);
createTab();
myEnmCar.addScore(mycar, &counter);
nails.addScore(mycar, &counter);
holes.addScore(mycar, &counter);

showLevelScore();
checkMaxLevel();
checkMaxScore();
gameEnd();
gameLevels(mycar);
}
}
void Game::startGame()
{
srand (time(NULL));
Car mycar = Car();

thread myThread(&Car::myListener, &mycar, &mycar);

playGame(mycar);

Sleep(5000);

myThread.detach();
}

EnemiesCar.h

#ifndef ENEMIESCAR_H
#define ENEMIESCAR_H
#include "Game.h"
class Game;
class Car;
class Nail;
class EnemiesCar
{
public:
const int xInit[9] = {2, 4, 6, 8, 10, 12, 14, 16, 18};
int xPos;
int yPos;
Game games = Game();
EnemiesCar();
void appear();
void drawEnemies(int x, int y, int myMatr[21][20]);
//void draw();
void move();
void addScore(Car car, int *countScore);
};
#endif // ENEMIESCAR_H

EnemiesCar.cpp

#include <windows.h>
#include <iostream>
#include "EnemiesCar.h"
#include "Game.h"
#include "Car.h"
#include "Nail.h"
using namespace std;
EnemiesCar::EnemiesCar()
{
int randomX = rand() % 6;
xPos=xInit[randomX];
yPos=0;
}
/*void EnemiesCar::drawEnemies(int x, int y)
{
Game games = Game();
if(y<20 && y>=-0){
games.matrix[x][y] = 2;
cout << "drawEne" <<endl;
}
}*/
void EnemiesCar::appear()
{
if(yPos==18){
int randomNo = rand() % 21;
/*games.gotoXY(35, 7);
cout << "a: " << randomNo << " ";*/
if(randomNo >= 0 && randomNo <= 3){
xPos = 2;
}else if(randomNo == 4 || randomNo == 5){
xPos = 4;
}else if(randomNo == 6 || randomNo == 7){
xPos = 6;
}else if(randomNo == 8 || randomNo == 9){
xPos = 8;
}else if(randomNo == 10 || randomNo == 11){
xPos = 10;
}else if(randomNo == 12 || randomNo == 13){
xPos = 12;
}else if(randomNo == 14 || randomNo == 15){
xPos = 14;
}else if(randomNo == 16 || randomNo == 17){
xPos = 16;
}else{
xPos = 18;
}
yPos = -3;
}
}
void EnemiesCar::drawEnemies(int x, int y, int myMatr[21][20])
{
if(y<20 && y>=-0){
myMatr[x][y] = 2;
myMatr[x-1][y+1] = 2;
myMatr[x+1][y+1] = 2;
myMatr[x][y+1] = 2;
myMatr[x][y+2] = 2;
}
}
void EnemiesCar::move()
{
yPos++;
}
void EnemiesCar::addScore(Car car, int *countScore)
{
if((this->xPos <= car.xPos-3 && this->yPos == 16) || (this->xPos >= car.xPos+3 && this->yPos == 16)){
*countScore = *countScore + 100;
}
}

似乎您需要做的是将外观/绘图与随机数的生成分开。您需要跟踪";随机敌人";你已经生成了,并且在绘制棋盘时只考虑它们;与此无关,每隔几圈或在适当的事件触发时,生成一个新的随机敌人/汽车/任何东西,并将其添加到已经生成的敌人集中。

最新更新