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)
{
map[ball] = 0;
ball += change;
map[ball] = 2;
}
void ballAction(int *map, int &ball, int &change, int &flag, int &Num)
{
while (1)
{
if (ball == 0)
{
change = 31;
actBall(map, ball, change);
break;
}
else if (ball == 29)
{
change = 29;
actBall(map, ball, change);
break;
}
else if ((ball + 1) % 30 == 0 || ball % 30 == 0)
{
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
actBall(map, ball, change);
break;
}
else if (map[ball + change] == 3)
{
if (ball + change == 883)
{
map[ball] = 0;
change = -31;
map[ball + change] = 0;
ball = ball - 1;
}
else
{
map[ball] = 0;
change = -29;
map[ball + change] = 0;
}
redPrint("Player1 Num:", 45, 10);
print(++Num, 55, 10);
break;
}
else if (map[ball + change] == 4)
{
if (ball + change == 886)
{
map[ball] = 0;
change = -29;
map[ball + change] = 0;
ball = ball +1;
}
else
{
map[ball] = 0;
change = -31;
map[ball + change] = 0;
}
break;
}
else if (ball < 30 || (ball == 314 && change == 31) || (ball == 315 && change == 29))
{
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
change = 0 - change;
actBall(map, ball, change);
break;
}
else if (ball > 868)
{
flag = 0;
break;
}
else if (map[ball + change] == 0)
{
actBall(map, ball, change);
break;
}
else {
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
map[ball] = 0;
ball += change;
map[ball] = 2;
break;
}
}
}
//单人模式移动球
void ballAction(int *map, int &ball, int &change, int &flag, int &Num, int &Num_2)
{
while (1)
{
if (ball == 0)
{
change = 31;
actBall(map, ball, change);
break;
}
else if (ball == 29)
{
change = 29;
actBall(map, ball, change);
break;
}
else if ((ball + 1) % 30 == 0 || ball % 30 == 0)
{
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
actBall(map, ball, change);
break;
}
else if (map[ball + change] == 3)
{
if (ball + change == 883)
{
map[ball] = 0;
change = -31;
map[ball + change] = 0;
ball = ball - 1;
}
else
{
map[ball] = 0;
change = -29;
map[ball + change] = 0;
}
redPrint("Player1 Num:", 45, 10);
print(++Num, 55, 10);
break;
}
else if (map[ball + change] == 4)
{
if (ball + change == 886)
{
map[ball] = 0;
change = -29;
map[ball + change] = 0;
ball = ball + 1;
}
else
{
map[ball] = 0;
change = -31;
map[ball + change] = 0;
}
redPrint("Player2 Num:", 45, 12);
print(++Num_2, 55, 12);
break;
}
else if (ball < 30 || (ball == 314 && change == 31) || (ball == 315 && change == 29))
{
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
change = 0 - change;
actBall(map, ball, change);
break;
}
else if (ball > 868)
{
flag = 0;
break;
}
else if (map[ball + change] == 0)
{
actBall(map, ball, change);
break;
}
else {
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
map[ball] = 0;
ball += change;
map[ball] = 2;
break;
}
}
}
//双人模式移动球
void playerAction_auto(int *map, int *play)
{
char input = _getch();
if (input == 'a')
map[play[0]--] = 0;
else if (input == 'd')
map[play[0]++] = 0;
/*if (input == 'j')
map[play[1]--] = 0;
else if (input == 'l')
map[play[1]++] = 0;*/
if (play[0] < 870)
play[0] = 870;
if (play[0] > 883)
play[0] = 883;
if (play[1] > 899)
play[1] = 899;
if (play[1] < 886)
play[1] = 886;
}
void playerAction(int *map, int *play)
{
char input = _getch();
if (input == 'a')
map[play[0]--] = 0;
else if (input == 'd')
map[play[0]++] = 0;
if (input == 'j')
map[play[1]--] = 0;
else if (input == 'l')
map[play[1]++] = 0;
if (play[0] < 870)
play[0] = 870;
if (play[0] > 883)
play[0] = 883;
if (play[1] > 899)
play[1] = 899;
if (play[1] < 886)
play[1] = 886;
}
int robotAction(int *map_origin, int ball_origin, int change_origin)
{
int map[950];
int ball = ball_origin;
int change = change_origin;
for (int i = 0; i < 900; i++)
{
map[i] = map_origin[i];
}
while (ball < 886)
{
if (ball == 0)
{
change = 31;
actBall(map, ball, change);
continue;
}
else if (ball == 29)
{
change = 29;
actBall(map, ball, change);
continue;
}
else if ((ball + 1) % 30 == 0 || ball % 30 == 0)
{
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
actBall(map, ball, change);
continue;
}
else if (map[ball + change] == 3)
{
return ball;
}
else if (map[ball + change] == 4)
{
return ball;
}
else if (ball < 30 || (ball == 314 && change == 31) || (ball == 315 && change == 29))
{
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
change = 0 - change;
actBall(map, ball, change);
continue;
}
else if (map[ball + change] == 0)
{
actBall(map, ball, change);
continue;
}
else {
if (change == 31 || change == -29)
change -= 2;
else
change += 2;
actBall(map, ball, change);
continue;
}
}
print(ball,10,50);
return ball;
}
//人机移动判断
void actRobot(int destination, int &play2, int *map)
{
if (destination - play2 < 0)
map[play2--] = 0;
if (destination - play2 > 0)
map[play2++] = 0;
}
//人机移动
int autoMain()
{
full_screen();
int map[900] = { 0 };
int Num = 0;
int ball = 32;
redPrint("Player1 Num:", 45, 10);
print(Num, 55, 10);
int change = 31;
int player[2] = { 872,896 };
int flag = 1;
print("Player1 keys is A D", 40, 25);
print("Player2 keys is J L", 40, 26);
while (1)
{
if (_kbhit())
playerAction_auto(map, player);
else
{
HideCursor();
int robotSel = robotAction(map, ball, change);
mapCreate(map, player);
ballAction(map, ball, change, flag, Num);
Sleep(1);
print(map, 0, 0);
if (robotSel > 885 && robotSel < 900)
{
actRobot(robotSel, player[1], map);
}
if (!flag)
break;
}
}
Count pl(Num);
save(pl);
system("cls");
print("GAME OVER\n", 44, 26);
system("pause");
system("cls");
return 0;
}
//单人模式主函数
int playMain()
{
full_screen();
int map[900] = { 0 };
int Num = 0;
int Num_1 = 0;
int ball = 32;
redPrint("Player1 Num:", 45, 10);
print(Num, 55, 10);
redPrint("Player2 Num:", 45, 12);
print(Num_1, 55, 12);
int change = 31;
int player[2] = { 872,896 };
int flag = 1;
print("Player1 keys is A D", 40, 25);
print("Player2 keys is J L", 40, 26);
while (1)
{
if (_kbhit())
playerAction(map, player);
else
{
HideCursor();
mapCreate(map, player);
ballAction(map, ball, change, flag, Num, Num_1);
Sleep(1);
print(map, 0, 0);
if (!flag)
break;
}
}
Count pl(Num);
save(pl);
Count p2(Num_1);
save(p2);
system("cls");
print("GAME OVER\n", 44, 26);
system("pause");
system("cls");
return 0;
}
//双人模式主函数
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
//隐藏光标
void full_screen()
{
HWND hwnd = GetForegroundWindow();
int cx = GetSystemMetrics(SM_CXSCREEN); /* 屏幕宽度 像素 */
int cy = GetSystemMetrics(SM_CYSCREEN); /* 屏幕高度 像素 */
LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE); /* 获取窗口信息 */
/* 设置窗口信息 最大化 取消标题栏及边框 */
SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}
//全屏显示
void save(Count st)
{
ofstream out("save.dat", ios::app | ios::binary);
out.write((char*)&st, sizeof(st));
out.close();
}
//保存分数到文件
};
//游戏类
class Meau
{
public:
void load()
{
int i;
Count st[200];
Count t;
ifstream in("save.dat", ios::binary);
for (i = 0; i < 200; i++)
{
in.read((char*)&st[i], sizeof(st[i]));
if (st[i].getNum() == -1)
break;
}
//读取文件夹
in.close();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "\n\n\n\n\n\n\n\n\n";
cout << "\t\t\t\t\t\t\t\t\t\t ";
cout << " " << "time" << "\t " << "Num" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
for (int j = 0; j < i - 1; j++)
{
for (int k = 0; k < i - j - 1; k++)
{
if (st[k].getNum() < st[k + 1].getNum())
{
t = st[k];
st[k] = st[k + 1];
st[k + 1] = t;
}
}
}
//对分数进行排序
for (int j = 0; j < i; j++)
{
cout << "\t\t\t\t\t\t\t\t\t\t";
st[j].show();
}
}
int meauMain()
{
int sel = 22;
Game one;
one.full_screen();
int button = 22;
one.print("buttons is 'w','a','s','d'", 10, 50);
while (1)
{
one.HideCursor();
if (_kbhit())//
{
char input = _getch();
if ((int)input == 13)
{
if (sel == 22)
{
system("cls");
one.autoMain();
}
if (sel == 23)
{
system("cls");
one.playMain();
}
if (sel == 24)
{
system("cls");
load();
getchar();
system("cls");
}
if (sel == 25)
{
return 0;
}
}
else
{
if (input == 'a')
sel--;
if (input == 'd')
sel++;
if (input == 'w')
sel--;
if (input == 's')
sel++;
if (sel > 25)
sel = 25;
if (sel < 22)
sel = 22;
}
}
//主菜单控制
else
{
if (sel == 22)
one.redPrint("1.Single player mode", 44, 22);
else
one.print("1.Single player mode", 44, 22);
if (sel == 23)
one.redPrint("2.Two players mode", 44, 23);
else
one.print("2.Two players mode", 44, 23);
if (sel == 24)
one.redPrint("3.Search history", 44, 24);
else
one.print("3.Search history", 44, 24);
if (sel == 25)
one.redPrint("4.Exit game", 44, 25);
else
one.print("4.Exit game", 44, 25);
}//选中变红
}
}
};
//菜单类
int main()
{
Meau tow;
tow.meauMain();
return 0;
}