如何保存/加载控制台游戏的状态 [英] How do I save/load the state of my console game
本文介绍了如何保存/加载控制台游戏的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个蛇控制台游戏,但我希望能够保存游戏的状态(蛇的位置,鼠标所在的位置,能量提升的位置以及猫鼬的位置)。我查看了Google上的很多网站,但没有一个对我帮助过多。这样我就可以加载文件,然后从剩下的地方继续播放。
如果有人知道我会非常感激,这是我的代码:
I'm making a snake console game but I want to be able to save the state of the game (where the snake is, where the mice are, where the power-ups are, and where the mongoose is). I've looked at quite a few sites on Google but none of them have helped me much. This is so that I can load the file and then play on from where it was left.
If anyone knows it would be greatly appreciated, here is my code:
//include standard libraries
#include <iostream> //for output and input: cout <<, cin >>
#include <iomanip> //for output manipulators
#include <conio.h> //for getch()
#include <string> //for string
#include <vector> // for vectors
#include <ctime> // for random
#include <cstdlib> // for rand_max
#include <fstream> // for text files
using namespace std;
//include our own libraries
#include "ConsoleUtils.h" //for Clrscr, Gotoxy, etc.
//---------------------------------------------------------------------------
//----- define constants
//---------------------------------------------------------------------------
//defining the size of the grid
const int SIZEY(10); //vertical dimension
const int SIZEX(10); //horizontal dimension
//defining symbols used for display of the grid & content
const char SNAKE('O'); //snake head
const char BODY('o'); //snake body
const char MOUSE('@'); //mouse
const char TUNNEL(' '); //tunnel
const char WALL('#'); //border
const char PILL('+'); //power up pill
const char MONGOOSE('M'); //mongoose
//defining the command letters to move the snake on the maze
const int UP(72); //up arrow
const int DOWN(80); //down arrow
const int RIGHT(77); //right arrow
const int LEFT(75); //left arrow
//defining the other command letters
const char QUIT('Q'); //to end the game
const char CHEAT('C'); //to do cheat
const char LOAD('L'); //to load
const char SAVE('S'); //to save
#define FPS 5
struct Item {
int x, y;
char symbol;
};
//---------------------------------------------------------------------------
//----- run game
//---------------------------------------------------------------------------
int main()
{
//function declarations (prototypes)
void initialiseGame(char g[][SIZEX + 1], char m[][SIZEX + 1], vector<Item>& snake, Item& mouse, Item& pill, int& score, bool& pillActive, int& moveCount, Item& mongoose);
void paintGame(const char g[][SIZEX + 1], string& mess, bool isCheatOn, int& score, const int target, string name, int highScore, int pillScore, bool setInvincibility, int invCount);
bool wantsToQuit(const int key);
bool isArrowKey(const int k);
int getKeyPress();
void moveSnake(char g[][SIZEX + 1], vector<Item>& snake, const int key, string& mess, Item& m, int& score, bool& isAlive, bool& hasWon,
const int target, int& highScore, bool& isCheatOn, Item& pill, bool& pillActive, int& moveCount, int& pillScore, bool& setInvincibility, int& invCount, bool& mongooseActive, int& lastDirection);
void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], const vector<Item> snake, Item& mouse, Item& pill, int& score, bool& pillActive, int& moveCount, Item& mongoose, bool& mongooseActive, bool& isAlive);
void getName(string& name);
void endProgram();
void invincibility(bool& setInvincibility, int& invCount);
bool isCheatKey(const int k);
bool wantsToLoad(const int k);
bool wantsToSave(const int k);
void cheatMode(const vector<Item>& snake, int& score);
//local variable declarations
char grid[SIZEY + 1][SIZEX + 1]; //grid for display
char maze[SIZEY + 1][SIZEX + 1]; //structure of the maze
bool isCheatOn = false;
bool isAlive = true;
bool hasWon = false;
bool pillActive = false;
bool mongooseActive = false;
bool setInvincibility = false;
int lastDirection = UP;
const int target = 10;
int moveCount = 0;
int invCount = 0;
int score = 0;
int pillScore = 0;
int highScore = 0;
std::string name;
//Snake and items setup
Item snakehead = { (SIZEX / 2) + 1, (SIZEY / 2) + 1, SNAKE }; //snake's position and symbol
Item body = { BODY };
Item body1 = { (SIZEX / 2), (SIZEY / 2), BODY };
Item body2 = { (SIZEX / 2) - 1, (SIZEY / 2) - 1, BODY };
Item body3 = { (SIZEX / 2) - 2, (SIZEY / 2) - 2, BODY };
Item mouse = { (SIZEX / 2) - 1, (SIZEY / 2) - 2, MOUSE }; // mouse symbol and position
vector<Item> snake = { snakehead, body1, body2, body3 };
vector<int> bestScores[6];
Item pill = { 0, 0, PILL };
Item mongoose = { 0, 0, MONGOOSE };
string message("LET'S START..."); //current message to player
//Input player's name before game is initialised
getName(name);
//action...
Clrscr();
initialiseGame(grid, maze, snake, mouse, pill, score, pillActive, moveCount, mongoose); //initialise grid (incl. walls & snake)
paintGame(grid, message, isCheatOn, score, target, name, highScore, pillScore, setInvincibility, invCount); //display game info, modified grid & messages
//creating a file using the player's name
ofstream fout;
fout.open((name + ".txt"), ios::out);
ofstream bestScore;
bestScore.open(("bestScores.txt"), ios::out);
int key(getKeyPress()); //read in selected key: arrow or letter command
while (!wantsToQuit(key) && isAlive&&!hasWon) //while user does not want to quit
{
if (isArrowKey(key))
{
invincibility(setInvincibility, invCount);
moveSnake(grid, snake, key, message, mouse, score, isAlive, hasWon, target, highScore, isCheatOn, pill, pillActive, moveCount, pillScore, setInvincibility, invCount, mongooseActive, lastDirection); //move snake in that direction
updateGrid(grid, maze, snake, mouse, pill, score, pillActive, moveCount, mongoose, mongooseActive, isAlive); //update grid information
}
else{
if (isCheatKey(key)){ //if the cheat key C is pressed then it will trigger cheat mode
if (!isCheatOn){
int size = snake.size();
cheatMode(snake, score); //setting the score to zero
message = "Cheat mode activated!";
cout << '\a' << '\a' << '\a';
snake.resize(4);
isCheatOn = true;
}
else{
message = "Cheat mode deactivated!";
cout << '\a' << '\a' << '\a';
isCheatOn = false;
}
}
if (wantsToLoad(key))
{
//Code to load
}
if (wantsToSave(key))
{
//Code to save
}
else
message = "INVALID KEY!"; //set 'Invalid key' message
}
paintGame(grid, message, isCheatOn, score, target, name, highScore, pillScore, setInvincibility, invCount); //display game info, modified grid & messages
key = getKeyPress(); //display menu & read in next option
}
if (hasWon)
{
SelectBackColour(clBlue);
Gotoxy(40, 8);
cout << '\a';
cout << "You win!!" << endl;
}
fout << highScore; // write the highscore into the player's text file
endProgram(); //display final message
return 0;
}
//---------------------------------------------------------------------------
//----- initialise game state
//---------------------------------------------------------------------------
void initialiseGame(char grid[][SIZEX + 1], char maze[][SIZEX + 1], vector<Item>& snake, Item& mouse, Item& pill, int& score, bool& pillActive, int& moveCount, Item& mongoose)
{ //initialise grid & place snake in middle
void setInitialMazeStructure(char g[][SIZEX + 1]);
void setInitialSnakeCoordinates(vector<Item>& snake);
void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
void placeSnake(char g[][SIZEX + 1], const vector<Item> snake);
void placeMouse(char g[][SIZEX + 1], Item& m);
void positionMouse(char g[][SIZEX + 1], Item& m);
void positionPill(char g[][SIZEX + 1], Item& p, int& score, bool& pillActive, int& moveCount);
setInitialMazeStructure(maze); //initialise maze
setMaze(grid, maze); //reset the empty maze configuration into grid
setInitialSnakeCoordinates(snake); //initialise snake's position
placeSnake(grid, snake); //set snake in grid
positionMouse(grid, mouse);
placeMouse(grid, mouse);
positionPill(grid, pill, score, pillActive, moveCount);
}
void setInitialMazeStructure(char maze[][SIZEX + 1])
{ //set the position of the walls in the maze
//initialise maze configuration
char initialMaze[SIZEY + 1][SIZEX + 1] //local array to store the maze structure
= { { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' },
{ 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
// with '#' for wall, ' ' for tunnel and 'X' for unused part of array
//copy into maze structure
for (int row(1); row <= SIZEY; ++row) //for each row (vertically)
for (int col(1); col <= SIZEX; ++col) //for each column (horizontally)
maze[row][col] = initialMaze[row][col];
}
void setInitialSnakeCoordinates(vector<Item>& snake)
{ //calculate snake's coordinates at beginning of game
snake[0].y = 3; //y-coordinate: vertically
snake[0].x = 5; //x-coordinate: horizontally
snake[1].y = 4;
snake[1].x = 5;
snake[2].y = 5;
snake[2].x = 5;
snake[3].y = 6;
snake[3].x = 5;
}
//---------------------------------------------------------------------------
//----- update grid state
//---------------------------------------------------------------------------
void updateGrid(char grid[][SIZEX + 1], const char maze[][SIZEX + 1], const vector<Item> snake, Item& mouse, Item& pill, int& score, bool& pillActive, int& moveCount, Item& mongoose, bool& mongooseActive, bool& isAlive)
{ //update grid configuration after each move
void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
void placeSnake(char g[][SIZEX + 1], const vector<Item> snake);
void placeMouse(char g[][SIZEX + 1], Item& m);
void positionPill(char g[][SIZEX + 1], Item& p, int& score, bool& pillActive, int& moveCount);
void placeMongoose(char g[][SIZEX + 1], Item& mn, int score, bool& mongooseActive, bool& isAlive);
setMaze(grid, maze); //reset the empty maze configuration into grid
//Placing all of the items into the grid
placeSnake(grid, snake); //set snake in grid
placeMouse(grid, mouse);
positionPill(grid, pill, score, pillActive, moveCount);
placeMongoose(grid, mongoose, score, mongooseActive, isAlive);
}
void setMaze(char grid[][SIZEX + 1], const char maze[][SIZEX + 1])
{ //reset the empty/fixed maze configuration into grid
for (int row(1); row <= SIZEY; ++row) //for each row (vertically)
for (int col(1); col <= SIZEX; ++col) //for each column (horizontally)
grid[row][col] = maze[row][col];
}
void placeSnake(char g[][SIZEX + 1], const vector<Item> s)
{ //place snake at its new position in grid
for (int i(0); i < s.size(); ++i)
{
g[s[i].y][s[i].x] = s[i].symbol;
}
}
void positionMouse(char g[][SIZEX + 1], Item& m)
{
srand(static_cast<int>(time(0)));
int randMaxX = 7;
int randMaxY = 6;
int y = rand() % randMaxY;
int x = rand() % randMaxX;
m.y = y + 1; //this creates a random value between 0 and 6 for the y axis of the mouse's placement
m.x = x + 1; //this then creates a random value between 0 and 7 for the x axis
while (g[m.y][m.x] != TUNNEL) //this loop is to make sure the mouse is placed on an empty space on the grid
{
int randMaxX = 7;
int randMaxY = 6;
int y = rand() % randMaxY;
int x = rand() % randMaxX;
m.y = y + 1;
m.x = x + 1;
}
}
void placeMouse(char g[][SIZEX + 1], Item& m) //this is used to place the mouse once the position has been set
{
g[m.y][m.x] = m.symbol;
}
void positionPill(char g[][SIZEX + 1], Item& p, int& score, bool& pillActive, int& moveCount)
{
srand(static_cast<int>(time(0)));
if ((score % 2 == 0) && (score != 0) && (!pillActive) && (moveCount <= 15))
{
pillActive = true;
++moveCount;
int randMaxX = 7;
int randMaxY = 6;
int y = rand() % randMaxY;
int x = rand() % randMaxX;
p.y = y + 1;
p.x = x + 1;
while (g[p.y][p.x] != TUNNEL)
{
int randMaxX = 7;
int randMaxY = 6;
int y = rand() % randMaxY;
int x = rand() % randMaxX;
p.y = y + 1;
p.x = x + 1;
}
}
if (moveCount > 5)
{
p.x = 0;
p.y = 0;
moveCount = 0;
}
g[p.y][p.x] = p.symbol;
}
void placePill(char g[][SIZEX + 1], Item& p)
{
g[p.y][p.x] = p.symbol;
}
//---------------------------------------------------------------------------
//----- move the snake
//---------------------------------------------------------------------------
void moveSnake(char grid[][SIZEX + 1], vector<Item>& s, const int key, string& mess, Item& m, int& score, bool& isAlive, bool& hasWon, const int target,
int& highScore, bool& isCheatOn, Item& pill, bool& pillActive, int& moveCount, int& pillScore, bool& setInvinciblity, int& invCount, bool& mongooseActive, int& lastDirection)
{ //move snake in required direction
void setKeyDirection(int k, int& dx, int& dy, int& lastDirection); //calculate direction of movement required by key - if any
void moveSnakeBody(vector<Item>& s, int& dx, int& dy);
void positionMouse(char g[][SIZEX + 1], Item& m);
void endProgram();
int dx(0), dy(0);
setKeyDirection(key, dx, dy, lastDirection); //find direction indicated by key
//check new target position in grid & update snake coordinates if move is possible
bool stopGrowCheat = false;
switch (grid[s[0].y + dy][s[0].x + dx])
{ //...depending on what's on the target position in grid...
case TUNNEL: //can move
moveSnakeBody(s, dx, dy);
if (pillActive)
{
++moveCount;
}
break;
case WALL: //hit a wall & stay there
if (setInvinciblity == false){
cout << '\a'; //beep the alarm
mess = "You Lose!";
isAlive = false;
}
else{
if (dx == 1){
cout << '\a';
s[0].y += dy; //go in that Y direction
s[0].x += (dx - (SIZEX - 2)); //go in that X direction
}
else{
if (dx == -1){
cout << '\a';
s[0].y += dy; //go in that Y direction
s[0].x += (dx + (SIZEX - 2)); //go in that X direction
}
else{
if (dy == -1){
cout << '\a';
s[0].y += (dy + (SIZEY - 2)); //go in that Y direction
s[0].x += dx; //go in that X direction
}
else{
if (dy == 1){
cout << '\a';
s[0].y += (dy - (SIZEY - 2)); //go in that Y direction
s[0].x += dx; //go in that X direction
}
}
}
}
//vector<Item>& s, int& dx, int& dy
//s[0].y += dx; //go in that Y direction
//s[0].x += dy;
//use dx,dy to find the wall that's being collided with
//use nested ifs
}
break;
case BODY:
if (setInvinciblity == false){
cout << '\a'; //beep the alarm
mess = "You Lose!";
isAlive = false;
}
else{
cout << '\a';
}
break;
case MOUSE:
if (isCheatOn == false){
s.push_back(s[s.size() - 1]);
s.push_back(s[s.size() - 1]);
}
++score;
moveSnakeBody(s, dx, dy);
positionMouse(grid, m);
if (score == target){
hasWon = true;
}
if (score >= highScore){
highScore = score;
moveCount = 0;
pillActive = false;
}
break;
case PILL:
s.resize(4);
moveSnakeBody(s, dx, dy);
pill.x = 0;
pill.y = 0;
pillActive = false;
pillScore++;
setInvinciblity = true;
invCount = 20;
break;
case MONGOOSE:
if (setInvinciblity == false){
cout << '\a'; //beep the alarm
mess = "Killed by mongoose, you Lose!";
isAlive = false;
}
}
}
void moveSnakeBody(vector<Item>& s, int& dx, int& dy)
{
int snakeSize = s.size();
for (int i(snakeSize - 1); i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}
s[0].y += dy; //go in that Y direction
s[0].x += dx; //go in that X direction
}
//---------------------------------------------------------------------------
//----- process key
//---------------------------------------------------------------------------
void setKeyDirection(const int key, int& dx, int& dy, int& lastDirection)
{ //
switch (key) //...depending on the selected key...
{
case LEFT: //when LEFT arrow pressed...
lastDirection = LEFT;
dx = -1; //decrease the X coordinate
dy = 0;
break;
case RIGHT: //when RIGHT arrow pressed...
lastDirection = RIGHT;
dx = +1; //increase the X coordinate
dy = 0;
break;
case UP: //when UP arrow pressed...
lastDirection = UP;
dx = 0;
dy = -1; //decrease the Y coordinate
break;
case DOWN: //when DOWN arrow pressed...
lastDirection = DOWN;
dx = 0;
dy = +1; //increase the Y coordinate
break;
}
}
int getKeyPress()
{ //get key or command selected by user
//KEEP THIS FUNCTION AS GIVEN
int keyPressed;
keyPressed = _getch(); //read in the selected arrow key or command letter
while (keyPressed == 224) //ignore symbol following cursor key
keyPressed = _getch();
return toupper(keyPressed); //return it in uppercase
}
bool isArrowKey(const int key)
{ //check if the key pressed is an arrow key (also accept 'K', 'M', 'H' and 'P')
return (key == LEFT) || (key == RIGHT) || (key == UP) || (key == DOWN);
}
bool wantsToQuit(const int key)
{ //check if the user wants to quit (when key is 'Q' or 'q')
return toupper(key) == QUIT;
}
bool isCheatKey(const int key)
{
return toupper(key) == CHEAT;
}
bool wantsToLoad(const int key)
{
return toupper(key) == LOAD;
}
bool wantsToSave(const int key)
{
return toupper(key) == SAVE;
}
//---------------------------------------------------------------------------
//----- display info on screen
//---------------------------------------------------------------------------
void paintGame(const char gd[][SIZEX + 1], string& mess, bool isCheatOn, int& score, const int target, string name, int highScore, int pillScore, bool setInvincibility, int invCount)
{ //display game title, messages, maze, snake & apples on screen
void paintGrid(const char g[][SIZEX + 1]);
//clear screen
Clrscr();
//display game title
SelectTextColour(clYellow);
Gotoxy(0, 0);
cout << "___SNAKE GAME___\n" << endl;
SelectBackColour(clWhite);
SelectTextColour(clRed);
Gotoxy(40, 0);
cout << "FoP Group Z3 - Connor Newton,";
Gotoxy(40, 1);
cout << "Kieran Cousens, Jordan Rhodes"; //PUT YOUR GROUP NUMBER AND NAMES HERE
// display grid contents
paintGrid(gd);
//display menu options available
SelectBackColour(clRed);
SelectTextColour(clYellow);
Gotoxy(40, 3);
cout << "TO MOVE USE KEYBOARD ARROWS ";
Gotoxy(40, 4);
cout << "TO QUIT ENTER 'Q' ";
Gotoxy(40, 5);
cout << "PRESS 'C' TO ENTER CHEAT MODE";
if (isCheatOn){
Gotoxy(40, 6);
cout << "Cheat mode on";
}
else{
Gotoxy(40, 6);
cout << "Cheat mode off";
}
Gotoxy(40, 10);
cout << "Score : " << score << " out of " << target;
Gotoxy(40, 11);
cout << "Pills eaten : " << pillScore; //show how many pills have been eaten
Gotoxy(40, 9);
cout << "Player's name: " << name; // show player's name
Gotoxy(40, 12);
if (setInvincibility == true){
SelectBackColour(clBlue);
SelectTextColour(clWhite);
cout << "Invinciblity ON";
}
Gotoxy(40, 12);
if (setInvincibility == false){
SelectBackColour(clRed);
SelectTextColour(clYellow);
cout << "Invincibility OFF";
}
Gotoxy(40, 13);
{
SelectBackColour(clRed);
SelectTextColour(clYellow);
cout << "To save - S, To load - L";
}
// Gotoxy(40, 13);
// cout << invCount;
//display date and time
SelectBackColour(clWhite);
SelectTextColour(clRed);
Gotoxy(40, 2);
time_t now = time(0);
tm *ltm = localtime(&now);
cout << setfill('0') << setw(2);
cout << ltm->tm_mday << "/"; //day
cout << setfill('0') << setw(2);
cout << 1 + ltm->tm_mon << "/"; //month
cout << 1900 + ltm->tm_year << " "; //year
cout << setfill('0') << setw(2);
cout << 1 + ltm->tm_hour << ":"; //hours
cout << setfill('0') << setw(2);
cout << 1 + ltm->tm_min << ":"; //minutes
cout << setfill('0') << setw(2);
cout << 1 + ltm->tm_sec; //seconds
//char* dateAndtime = ctime(&now);
//cout << dateAndtime;
//print auxiliary messages if any
SelectBackColour(clBlack);
SelectTextColour(clWhite);
Gotoxy(40, 8);
cout << mess; //display current message
mess = ""; //reset message to blank
}
void paintGrid(const char g[][SIZEX + 1])
{ //display grid content on screen
SelectBackColour(clBlack);
SelectTextColour(clWhite);
Gotoxy(0, 2);
for (int row(1); row <= SIZEY; ++row) //for each row (vertically)
{
for (int col(1); col <= SIZEX; ++col) //for each column (horizontally)
cout << g[row][col]; //output cell content
cout << endl;
}
}
void endProgram()
{
SelectBackColour(clRed);
SelectTextColour(clYellow);
Gotoxy(40, 15);
cout << "\n";
system("pause");
}
void cheatMode(const vector<Item>& snake, int& score)
{
score = 0;
}
void getName(string& name){
SelectTextColour(clWhite);
Gotoxy(20, 10);
cout << "Enter Player Name:";
SelectTextColour(clYellow);
cin >> name;
while (name.length() > 20)
{
Clrscr();
SelectTextColour(clWhite);
Gotoxy(20, 11);
cout << "Invalid length, try again! (20 characters max)";
Gotoxy(20, 10);
SelectTextColour(clYellow);
cout << "Enter Player Name:";
cin >> name;
}
}
void invincibility(bool& setInvincibility, int& invCount){
if (setInvincibility == true)
invCount--;
if (invCount == 0)
{
setInvincibility = false;
}
}
void placeMongoose(char g[][SIZEX + 1], Item& mn, int score, bool& mongooseActive, bool& isAlive)
{
srand(static_cast<int>(time(0)));
//this is for when the mongoose has already been placed and need to move 1(or 0) space in a random direction
if (mongooseActive == true){
int tempx;
int tempy;
tempx = mn.x;
tempy = mn.y;
int randMaxX = 3;
int randMaxY = 3;
int ym = rand() % randMaxY - 1;
int xm = rand() % randMaxX - 1;
mn.y += ym;
mn.x += xm;
int i = 0;
while (g[mn.y][mn.x] != TUNNEL)
{
mn.x = tempx;
mn.y = tempy;
int randMaxX = 3;
int randMaxY = 3;
int ym = rand() % randMaxY - 1;
int xm = rand() % randMaxX - 1;
mn.y += ym;
mn.x += xm;
}
g[mn.y][mn.x] = mn.symbol;
}
//this is to place the mongoose randomly across the grid once the player has eaten 3 mice
if ((score >= 3) && (mongooseActive == false))
{
int randMaxX = 7;
int randMaxY = 6;
int y = rand() % randMaxY;
int x = rand() % randMaxX;
mn.y = y + 1;
mn.x = x + 1;
while (g[mn.y][mn.x] != TUNNEL)
{
int randMaxX = 7;
int randMaxY = 6;
int y = rand() % randMaxY;
int x = rand() % randMaxX;
mn.y = y + 1;
mn.x = x + 1;
}
g[mn.y][mn.x] = mn.symbol;
mongooseActive = true;
}
}
What I have tried:
I’ve tried looking on cplusplus and stackoverflow
What I have tried:
I've tried looking on cplusplus and stackoverflow
推荐答案
There is no single answer.为什么? Because this is your code and your game. That means you need to decide what you want saved. We don’t know what you want saved. So, write code that saves \"where the snake is, where the mice are, where the power-ups are, and where the mongoose is\" and anything else you want to save. There is no magic code that will save anything. You have to write the code. Save it in a file, in a database or however you want, but decide that first.
There is no single answer. Why? Because this is your code and your game. That means you need to decide what you want saved. We don't know what you want saved. So, write code that saves "where the snake is, where the mice are, where the power-ups are, and where the mongoose is" and anything else you want to save. There is no magic code that will save anything. You have to write the code. Save it in a file, in a database or however you want, but decide that first.
这篇关于如何保存/加载控制台游戏的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文