c++——写点游戏
肯定有些同学想用c++写点游戏,今天,cht就来说说这个(必须有Windows处理器或电脑,Linux不支持)。
关于acwing
一、准备
1、必备算法
(1)高精度
(2)DFS(或BFS)
(3)递推与递归
(4)字符串运算
(5)位运算
(6)部分STL(sort,swap……)
2、头文件
(1)#include<bits/stdc++.h>//万能头文件
(2)#include<conio.h>//getch()
(3)#include<stdlib.h>//system()
(4)#include<ctime>
二、模板与语法
1、getch();
用法:可以在不打换行的前提下输入一个字符(要用conio.h)。
模板
char op;
op = getch();
while(op == ' '/*条件*/)
{
op = getch();
}
事例(按空格键继续):
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
int main()
{
cout << "空格继续" << endl;
char op;
op = getch();
while(op != ' ')
{
op = getch();
}
cout << "再见" << endl;
return 0;
}
效果:
2、system()
这个函数对应的头文件是:#include<stdlib.h>
具体语法:
(1)、system("cls")//清空屏幕
(2)、
system("color *F);//这里的*可以填0-7。
*的具体填法(写个system("color 10F")
就能出来):
(3)、system("mode con cols=100 lines=40");//初始化缓冲区大小
3、读入读出句柄与字体设置
读入读出句柄是这行:
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄(写在using namespace std后面)
CloseHandle(hOut);//关闭标准输出句柄(写在return 0前面)
就是这么设置(后面的均用这个句柄)。
那它能干什么呢?
(1)、字体颜色:
蓝色:
void blue_border()
{
WORD blue=FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN;//设置字体颜色、背景颜色
SetConsoleTextAttribute(hOut,blue);//字体样式
}
白色:
void white_back()
{
WORD white=FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY;
SetConsoleTextAttribute(hOut,white);
}
青色:
void cyan_choose()
{
WORD cyan=FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,cyan);
}
不太好的红色和橙色(其实都是可以自己做的,有兴趣的小伙伴不妨试试):
void red_choose()
{
WORD red=FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,red);
}
void orange_choose()
{
WORD orange=FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_BLUE|BACKGROUND_BLUE;
SetConsoleTextAttribute(hOut,orange);
}
自己做的例子:
void on_Select()
{
WORD select=FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_RED;
SetConsoleTextAttribute(hOut,select);
}
(2)、输出的位置(光标):
游戏里最常用的函数。
pos(x,y);
代码:
void pos(int x,int y)
{
COORD posPoint = {x,y}; //设置坐标
SetConsoleCursorPosition(hOut,posPoint);
}
(4)、blahblahblah
要写标题请加这行(引号里写标题):
SetConsoleTitle("");
三、游戏事例
1、小程序:一元一次方程神器:
#include <bits/stdc++.h>//C++万能头
#include<windows.h>//控制台编程主要头文件Sleep();
#include<conio.h>//getch()函数
#include<stdlib.h>//system()函数
using namespace std;
void pos(int x,int y);//确定光标位置
void blue_border();//蓝色字体
void white_back();//还原亮白色字体
void cyan_choose();//青色字体
void on_Select();//被选中时的样式
void onChoose(int x,int y);//确定所选中的位置
void star();//初始化界面
struct node
{
double a, b;
};
int priority(char c)
{
if (c == '*' || c == '/')
return 2;
if (c == '+' || c == '-')
return 1;
return 0;
}
void calc(stack <char> &op, stack <node> &num)
{
node bb = num.top();
num.pop();
node aa = num.top();
num.pop();
node temp_node;
switch (op.top())
{
case '+':
temp_node.a = aa.a + bb.a;
temp_node.b = aa.b + bb.b;
num.push(temp_node);
break;
case '-':
temp_node.a = aa.a - bb.a;
temp_node.b = aa.b - bb.b;
num.push(temp_node);
break;
case '*':
temp_node.a = aa.a * bb.b + aa.b * bb.a;
temp_node.b = aa.b * bb.b;
num.push(temp_node);
break;
case '/':
temp_node.a = aa.a / bb.b;
temp_node.b = aa.b / bb.b;
num.push(temp_node);
break;
}
op.pop();
}
void d()
{
cyan_choose();
pos(31,14);
cout << "欢迎来到一元一次方程神器!";
Sleep(1000);//等待
system("cls");
system("color 2F");
blue_border();
int x=25,y=10;
char sel;
sel=getch();
while(sel!=' ')
{
pos(x,y);
onChoose(x,y);
sel=getch();
}
system("cls");
system("color 3F");
cyan_choose();
pos(25,23);
cout << "请输入方程:" ;
while (1)
{
string str, str_l, str_r;
cin >>str;
for (int i = 0; i < str.size(); ++ i)
{
if (str[i] == '=')
{
str_l = str.substr(0, i);
str_r = str.substr(i + 1, str.size());
}
}
stack <node> num_l;
stack <node> num_r;
stack <char> op_l;
stack <char> op_r;
int len_l = str_l.size();
int len_r = str_r.size();
for (int i = 0; i < len_l; ++ i)
{
if (isdigit(str_l[i]))
{
node temp_node;
double temp = atof(&str_l[i]);
while (isdigit(str_l[i]) || str_l[i] == '.')
++ i;
if (str_l[i] == 'x')
{
temp_node.a = temp;
temp_node.b = 0;
num_l.push(temp_node);
}
else
{
temp_node.a = 0;
temp_node.b = temp;
num_l.push(temp_node);
-- i;
}
}
else if (str_l[i] == 'x')
{
node temp_node;
temp_node.a = 1;
temp_node.b = 0;
num_l.push(temp_node);
}
else if (str_l[i] == '(')
{
op_l.push(str_l[i]);
}
else if (str_l[i] == ')')
{
while (op_l.top() != '(')
calc(op_l, num_l);
op_l.pop();
}
else if (op_l.empty())
{
op_l.push(str_l[i]);
}
else if (priority(op_l.top()) < priority(str_l[i]))
{
op_l.push(str_l[i]);
}
else if (priority(op_l.top()) >= priority(str_l[i]))
{
while (!op_l.empty() && priority(op_l.top()) >= priority(str_l[i]))
calc(op_l, num_l);
op_l.push(str_l[i]);
}
}
for (int i = 0; i < len_r; ++ i)
{
if (isdigit(str_r[i]))
{
node temp_node;
double temp = atof(&str_r[i]);
while (isdigit(str_r[i]) || str_r[i] == '.')
++ i;
if (str_r[i] == 'x')
{
temp_node.a = temp;
temp_node.b = 0;
num_r.push(temp_node);
}
else
{
temp_node.a = 0;
temp_node.b = temp;
num_r.push(temp_node);
-- i;
}
}
else if (str_r[i] == 'x')
{
node temp_node;
temp_node.a = 1;
temp_node.b = 0;
num_r.push(temp_node);
}
else if (str_r[i] == '(')
{
op_r.push(str_r[i]);
}
else if (str_r[i] == ')')
{
while (op_r.top() != '(')
calc(op_r, num_r);
op_r.pop();
}
else if (op_r.empty())
{
op_r.push(str_r[i]);
}
else if (priority(op_r.top()) < priority(str_r[i]))
{
op_r.push(str_r[i]);
}
else if (priority(op_r.top()) >= priority(str_r[i]))
{
while (!op_r.empty() && priority(op_r.top()) >= priority(str_r[i]))
calc(op_r, num_r);
op_r.push(str_r[i]);
}
}
while (!op_l.empty())
calc(op_l, num_l);
while (!op_r.empty())
calc(op_r, num_r);
double x1 = num_l.top().a, y1 = num_l.top().b;
double x2 = num_r.top().a, y2 = num_r.top().b;
system("cls");
system("color 3F");
pos(20,20);
printf("%.3lf\n", (y2 - y1) / (x1 - x2));
Sleep(2000);
pos(20,21);
cout << "要继续吗?" << endl;
char op;
op = getch();
while(op != ' ')
{
op = getch();
}
system("cls");
system("color 3F");
pos(20,20);
cout << "谢谢使用,再见!";
S