c++作业,打球小游戏
我也不知道这个游戏真正名字叫什么姑且就叫这个名字吧
#include <iostream>
#include<fstream>
#include <conio.h>
#include <windows.h>
#include<time.h>
#include<stdio.h>
#include<string>
using namespace std;
class Count
{
private:
char time_save[21];
int num;
public:
Count(void)
{
num = -1;
}
Count(int a)
{
struct tm t;
time_t now;
time(&now);
localtime_s(&t, &now);
string date;
date = "";
date.append(to_string(t.tm_year + 1900));
date.append("-");
date.append(to_string(t.tm_mon + 1));
date.append("-");
date.append(to_string(t.tm_mday));
date.append(" ");
date.append(to_string(t.tm_hour));
date.append(":");
date.append(to_string(t.tm_min));
date.append(":");
date.append(to_string(t.tm_sec));
int i;
for (i = 0; i < date.length(); i++)
time_save[i] = date[i];
time_save[i] = '\0';//因为用string储存一直有问题所以转成char
this->num = a;
}
int getNum()
{
return num;
}
void show()
{
cout << time_save << '\t' << num << endl;;
}
//输出类
};
//这个类用于记录游戏历史
class Game
{
public:
void print(string s, int x, int y)
{
SetCursorPosition(x, y);
cout << s.c_str();
}
//光标移动
void redPrint(string s, int x, int y)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
SetCursorPosition(x, y);
cout << s.c_str();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
}
//光标移动并且字体变红
void print(int s, int x, int y)
{
SetCursorPosition(x, y);
cout << s;
}
//光标移动的重载
void print(int *map, int x, int y)
{
SetCursorPosition(x, y);
for (int i = 0; i < 900; i++)
{
switch (map[i])
{
case 0:
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
cout << "■";
break;
}
case 1:
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | BACKGROUND_INTENSITY | BACKGROUND_RED);
cout << "■";
break;
}
case 2:
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
cout << "●";
break;
}
case 3:
case 4:
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
cout << "▂";
break;
}
}
if ((i + 1) % 30 == 0)
cout << endl;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
}
//打印游戏界面
void SetCursorPosition(const int x, const int y)
{
COORD position;
position.X = 2 * x;
position.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
//光标设置
void mapCreate(int *map, int *player)
{
for (int i = 320; i < 900; i++)
{
if ((i + 1) % 30 == 15 || (i + 1) % 30 == 16)
map[i] = 1;
if (i == player[0])
map[i] = 3;
if (i == player[1])
map[i] = 4;
}
}
//创建地图
void actBall(int *map, int &ball, int &change