
Lv.1
This guy is very lazy, leaving nothing behind
签名:发烧了怎么办?直接退火降温。
在 初学C++ 中回复
#include <iostream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
char Hide() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
return 0;
}
#define high 20 //定义地图的高度
#define width 70 //定义地图的宽度
#define snake_maxlength 100000 //蛇长最大值
int snake_x[snake_maxlength], snake_y[snake_maxlength]; //蛇的坐标
int snake_length; //蛇现在的长度
int map[high][width] = {0}; //初始化地图,0表示空格,1表示蛇身,2表示食物。
//3表示墙,4表示蛇头,现在地图中什么也没有
int food_x, food_y; //食物的坐标
int score; //本局分数
char input; //读取用户键入的字符
//数据的初始化函数
void setdata() {
snake_x[0] = high / 2;
snake_y[0] = width / 3; //初始化蛇头,在地图内的位置随意
snake_x[1] = high / 2;
snake_y[1] = width / 3 + 1; //初始化蛇身
snake_length = 2; //蛇的原始尺寸(一格头一格身子)
food_x = high / 3, food_y = width / 3; //初始化食物的位置,在地图内位置随意
score = 0;
int i, j;
map[snake_x[0]][snake_y[0]] = 4; //蛇头
map[snake_x[1]][snake_y[1]] = 1; //蛇身
map[food_x][food_y] = 2; //食物
for (i = 0; i < high; i++) //墙
for (j = 0; j < width; j++)
if (i == 0 || j == 0 || i == high - 1 || j == width - 1)
map[i][j] = 3;
}
//画面打印
void gotoxy(int x, int y) { //解释1
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void show() {
gotoxy(0, 0);
int i, j;
for (i = 0; i < high; i++) {
for (j = 0; j < width; j++) {
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
if (map[i][j] == 0)
cout << " ";
else if (map[i][j] == 1) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << "*";
} else if (map[i][j] == 2) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED |
FOREGROUND_GREEN);
cout << "@";
} else if (map[i][j] == 3) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_GREEN |
FOREGROUND_BLUE);
cout << "#";
} else if (map[i][j] == 4) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << "O";
}
}
printf("\n");
}
printf("得分:%d", score);
}
//与输入无关的更新
void updatewithoutinput() {
int i;
if (snake_x[0] == food_x && snake_y[0] == food_y) {
score++;
srand((unsigned)time(NULL)); //srand和rand函数连用产生随机数,一般以时间作为产生随机数的种子
food_x = rand() % (high - 3) + 1; //解释2
food_y = rand() % (width - 3) + 1;
map[food_x][food_y] = 2;
snake_length++;
}
if (snake_x[0] == 0 || snake_x[0] == high - 1 || snake_y[0] == 0 || snake_y[0] == width - 1) { //碰墙了
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
system("cls");
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << " 游戏结束!" << endl;
getch();
exit(1); //解释3
}
for (i = 1; i < snake_length; i++) //碰到自己了 ,遍历蛇身找是否和蛇头重复
if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i]) {
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
system("cls");
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << " 游戏结束!" << endl;
getch();
exit(1);
}
}
void updatewithinput() {
int i = 1;
short p = 0;
if (kbhit()) //解释4
input = getch();
if (input == 'w' || input == 'a' || input == 's' || input == 'd') {
if (p == 1) {
system("cls");
p == 0;
}
map[snake_x[snake_length - 1]][snake_y[snake_length - 1]] = 0; //蛇走过一格,蛇尾处变为空格
for (i = snake_length - 1; i > 0; i--) {
snake_x[i] = snake_x[i - 1]; //走过一格后蛇身的坐标覆盖前面的坐标 (从后往前覆盖,蛇头也会被蛇身覆盖)
snake_y[i] = snake_y[i - 1]; //解释5
map[snake_x[i]][snake_y[i]] = 1;
}
}
if (input == 'w')
snake_x[0]--;
else if (input == 's')
snake_x[0]++;
else if (input == 'a')
snake_y[0]--;
else if (input == 'd')
snake_y[0]++;
else {
cout << " 已暂停";
p = 1;
}
map[snake_x[0]][snake_y[0]] = 4; //蛇头的坐标覆盖前面的坐标
}
int main() {
setdata();//数据初始化
Hide();//隐藏光标
while (1) {
show(); //画面打印
updatewithoutinput(); //与输入无关的更新
updatewithinput(); //与输入有关的更新
Sleep(100);
}
return 0;
}
2022-05-28T20:55:41 点赞:0
在 初学C++做的小游戏:贪吃蛇 中回复
#include <iostream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
char Hide() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
return 0;
}
#define high 20 //定义地图的高度
#define width 70 //定义地图的宽度
#define snake_maxlength 100000 //蛇长最大值
int snake_x[snake_maxlength], snake_y[snake_maxlength]; //蛇的坐标
int snake_length; //蛇现在的长度
int map[high][width] = {0}; //初始化地图,0表示空格,1表示蛇身,2表示食物。
//3表示墙,4表示蛇头,现在地图中什么也没有
int food_x, food_y; //食物的坐标
int score; //本局分数
char input; //读取用户键入的字符
//数据的初始化函数
void setdata() {
snake_x[0] = high / 2;
snake_y[0] = width / 3; //初始化蛇头,在地图内的位置随意
snake_x[1] = high / 2;
snake_y[1] = width / 3 + 1; //初始化蛇身
snake_length = 2; //蛇的原始尺寸(一格头一格身子)
food_x = high / 3, food_y = width / 3; //初始化食物的位置,在地图内位置随意
score = 0;
int i, j;
map[snake_x[0]][snake_y[0]] = 4; //蛇头
map[snake_x[1]][snake_y[1]] = 1; //蛇身
map[food_x][food_y] = 2; //食物
for (i = 0; i < high; i++) //墙
for (j = 0; j < width; j++)
if (i == 0 || j == 0 || i == high - 1 || j == width - 1)
map[i][j] = 3;
}
//画面打印
void gotoxy(int x, int y) { //解释1
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void show() {
gotoxy(0, 0);
int i, j;
for (i = 0; i < high; i++) {
for (j = 0; j < width; j++) {
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
if (map[i][j] == 0)
cout << " ";
else if (map[i][j] == 1) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << "*";
} else if (map[i][j] == 2) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED |
FOREGROUND_GREEN);
cout << "@";
} else if (map[i][j] == 3) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_GREEN |
FOREGROUND_BLUE);
cout << "#";
} else if (map[i][j] == 4) {
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << "O";
}
}
printf("\n");
}
printf("得分:%d", score);
}
//与输入无关的更新
void updatewithoutinput() {
int i;
if (snake_x[0] == food_x && snake_y[0] == food_y) {
score++;
srand((unsigned)time(NULL)); //srand和rand函数连用产生随机数,一般以时间作为产生随机数的种子
food_x = rand() % (high - 3) + 1; //解释2
food_y = rand() % (width - 3) + 1;
map[food_x][food_y] = 2;
snake_length++;
}
if (snake_x[0] == 0 || snake_x[0] == high - 1 || snake_y[0] == 0 || snake_y[0] == width - 1) { //碰墙了
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
system("cls");
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << " 游戏结束!" << endl;
getch();
exit(1); //解释3
}
for (i = 1; i < snake_length; i++) //碰到自己了 ,遍历蛇身找是否和蛇头重复
if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i]) {
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
system("cls");
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout << " 游戏结束!" << endl;
getch();
exit(1);
}
}
void updatewithinput() {
int i = 1;
short p = 0;
if (kbhit()) //解释4
input = getch();
if (input == 'w' || input == 'a' || input == 's' || input == 'd') {
if (p == 1) {
system("cls");
p == 0;
}
map[snake_x[snake_length - 1]][snake_y[snake_length - 1]] = 0; //蛇走过一格,蛇尾处变为空格
for (i = snake_length - 1; i > 0; i--) {
snake_x[i] = snake_x[i - 1]; //走过一格后蛇身的坐标覆盖前面的坐标 (从后往前覆盖,蛇头也会被蛇身覆盖)
snake_y[i] = snake_y[i - 1]; //解释5
map[snake_x[i]][snake_y[i]] = 1;
}
}
if (input == 'w')
snake_x[0]--;
else if (input == 's')
snake_x[0]++;
else if (input == 'a')
snake_y[0]--;
else if (input == 'd')
snake_y[0]++;
else {
cout << " 已暂停";
p = 1;
}
map[snake_x[0]][snake_y[0]] = 4; //蛇头的坐标覆盖前面的坐标
}
int main() {
setdata();//数据初始化
Hide();//隐藏光标
while (1) {
show(); //画面打印
updatewithoutinput(); //与输入无关的更新
updatewithinput(); //与输入有关的更新
Sleep(100);
}
return 0;
}2022-05-29T14:55:36 点赞:0
在 初学C++ 中回复
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//三色相加,白色
cout<<"white"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|
FOREGROUND_RED);//红色
cout<<"red"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN);//绿色
cout<<"green"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_BLUE);//蓝色
cout<<"blue"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);//红色和绿色相加,黄色
cout<<"yellow"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE);//红色和蓝色相加,粉色
cout<<"pink"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);//绿色和蓝色相加,青色
cout<<"cyan"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY);//没有添加颜色,原色
cout<<"grey"<<endl;
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//三色相加,白色
return 0;
}
//这些就是此函数所有颜色,system也行2022-05-31T21:00:41 点赞:1
在 初学C++ 中回复
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <windows.h>
using namespace std;
char Hide() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
return 0;
}
void gotoxy(int x, int y) { //定位光标,x为行坐标,y为列坐标
COORD pos = {x, y}; //(坐标 位置);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //得到标准处理(标准输出处理);
SetConsoleCursorPosition(hOut, pos);//设置控制台光标位置;
}
void hidden() { //隐藏光标
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0; //赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
class Timer {
private:
long start_time;
long pause_time;
//两个bool值标记四种状态
bool is_pause; //记录计时器的状态 (是否处于暂停状态)
bool is_stop;//是否处于停止状态
public:
Timer();
bool isPause(); //返回计时器状态
bool isStop();
//计时器的三种动作(功能)
void Start();
void Pause();
void Stop();
inline long getStartTime() {
return start_time;
}
void show();
};
Timer::Timer() {
is_pause = false; //初始化计时器状态
is_stop = true;
}
bool Timer::isPause() {
if (is_pause)
return true;
else
return false;
}
bool Timer::isStop() {
if (is_stop)
return true;
return false;
}
void Timer::Start() { //开始
if (is_stop) {
start_time = time(0);
is_stop = false;
} else if (is_pause) {
is_pause = false;
start_time += time(0) - pause_time; //更新开始时间:用此时的时间 - 暂停时所用的时间 + 上一次开始的时间 = 此时的开始时间
}
}
void Timer::Pause() { //暂停
if (is_stop || is_pause) //如果处于停止/暂停状态,此动作不做任何处理,直接返回
return;
else { //否则调制为暂停状态
is_pause = true;
pause_time = time(0); //获取暂停时间
}
}
void Timer::Stop() { //停止
if (is_stop) //如果正处于停止状态(不是暂停状态),不做任何处理
return ;
else if (is_pause) { //改变计时器状态
is_pause = false;
is_stop = true;
} else if (!is_stop) {
is_stop = true;
}
}
void Timer::show() {
long t = time(0) - start_time;
gotoxy(35, 12);
cout << setw(2) << setfill('0') << t / 60 / 60 << ":"
<< setw(2) << setfill('0') << t / 60 << ":"
<< setw(2) << setfill('0') << t % 60 << endl;
}
int main() {
Hide();
Timer t;
char ch;
hidden();//隐藏光标
system("color 02");
gotoxy(35, 12);
cout << "00:00:00";
gotoxy(20, 18);
cout << "按a开始,按空格暂停,按s停止";
while (1) {
if (kbhit()) {
ch = getch();
switch (ch) {
case 'a':
t.Start();
break;
case 's':
t.Stop();
break;
case ' ':
t.Pause();
break;
default :
break;
}
}
if (!t.isStop() && !t.isPause()) {
t.show();
}
}
}
//简单计时器2022-06-01T09:37:28 点赞:2