C++小游戏集3个(不定时更新)2

前言

在Dve c++中想做游戏是很难的,但我不这么想,在下写了一些小游戏给客官看看

废话不多说,上代码!!!

一、表白神器

表白很好用(真的)

#include<stdio.h>
#include<math.h>
#include<windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	float y, x, z;

	printf("那一天\n");
	printf("第一次遇见你\n");
	printf("忘不了\n");
	printf("你的容颜\n");
	printf("若轻云之蔽月,如流风之回雪\n");
	printf("\n\n\n");
	printf("其实\n");
	printf("有一句话\n");
	printf("我一直想对你说:\n");

	for (double y = 2.5; y >= -1.6; y = y - 0.2)
	{
		for (double x = -3; x <= 4.8; x = x + 0.1)
		{
			(pow((x * x + y * y - 1), 3) <= 3.6 * x * x * y * y * y
				|| (x > -2.4 && x < -2.1 && y<1.5 && y>-1)
				|| (((x < 2.5 && x>2.2) || (x > 3.4 && x < 3.7)) && y > -1 && y < 1.5)
				|| (y > -1 && y < -0.6 && x < 3.7 && x>2.2)) ? printf("*") : printf(" ");
				Sleep(1);
		}
		Sleep(1);
		printf("\n");
	}
    char answer[4];
    Sleep(1000);
    printf("遇到你\n我才发现\n曾经所有的条件\n似乎都成了我等你的借口\n\n");
    Sleep(1000);
    printf("我对你的感情已经决堤\n所以\n请允许我,从今往后映入你\n明媚的眼\n");
    Sleep(1000);
    printf("我\n想和你\n耳鬓厮磨,相濡以沫!\n\n");
    Sleep(1000);
    printf("答应我吧!\n\n输入yes,你可以看到我的真心!\n\n");
    Sleep(1000);
    scanf("%s", &answer);

    float y1, x1, z1, f;
    for (y1 = 1.5f; y1 > -1.5f; y1 -= 0.1f)
    {
        for (x1 = -1.5f; x1 < 1.5f; x1 += 0.05f)
        {
            z1 = x1 * x1 + y1 * y1 - 1;
            f = z1 * z1 * z1 - x1 * x1 * y1 * y1 * y1;
            putchar(f <= 0.0f ? "*********"[(int)(f * -8.0f)] : ' ');
        }
        putchar('\n');
    }

    long time;
    for (; ;)
    {
        system("color a");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color b");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color c");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color d");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color e");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color f");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 0");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 1");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 2");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 3");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 4");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 5");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 6");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 7");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 8");
        for (time = 0; time < 99999999; time++);
        Sleep(1000);
        system("color 9");
    }

	return 0;
}

二、菜单

  可以存放人员(不是云变量)

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
const int N = 1e5+7;
int i=0;
struct book
{
    string name,num;
    int grade;
} s[N];
void ExitSystem()
{
    
}
void AddInfo(){
    int x=0,j;
    if(x==0)
    {
        i++;
        cout<<"请输入:"<<endl;
        cout<<"姓名:";
        cin>>s[i].name;
        cout<<"\n学号:";
        cin>>s[i].num;
        cout<<"\n成绩:";
        cin>>s[i].grade;
    }
}
void DisplayInfo()
{
    int j;
    system("cls");
    
    for(int j=1; j<=i; j++)
    {
        cout<<"姓名:"<<s[j].name<<endl;
        cout<<"学号:"<<s[j].num<<endl;
        cout<<"成绩:"<<s[j].grade<<endl << endl;
    }
    
}
void LocateInfo()
{
    cout<<"请输入关键字:";
    string t;
    cin>>t;
    int x=0,j;
    for(j=1; j<=i; j++)
    {
        if(s[j].num==t)
        {
            x++;
            break;
        }
    }
    if(x==0)
    {
        cout<<"未曾找到,无法操作!"<<endl;
    }
    else
    {
        cout<<"姓名:"<<s[j].name<<endl;
        cout<<"学号:"<<s[j].num<<endl;
        cout<<"成绩:"<<s[j].grade<<endl;
    }
}
void ModifyInfo()
{
    cout<<"请输入关键字:";
    string t,b;
    cin>>t;
    int x=0,j;
    for(j=1; j<=i; j++)
    {
        if(s[j].num==t)
        {
            x++;
            break;
        }
    }
    if(x==0)
    {
        cout<<"未曾找到,无法操作!"<<endl;
    }
    else
    {
        cout<<s[j].num<<"关键字已找到"<<endl;
        cout<<"姓名:"<<s[j].name<<endl;
        cout<<"学号:"<<s[j].num<<endl;
        cout<<"成绩:"<<s[j].grade<<endl;
        cout<<"请问是否更改?(Y/N)";
        cin>>b;
        if(b=="Y"||b=="y"||b=="yes"||b=="Yes"||b=="YES")
        {
            cout<<"请输入:"<<endl;
            cout<<"姓名:";
            cin>>s[j].name;
            cout<<"\n学号:";
            cin>>s[j].num;
            cout<<"\n成绩:";
            cin>>s[j].grade;
        }
    }
}
void DeleteInfo()
{
    cout<<"请输入要删除的学生的关键字:";
    string t,b;
    cin>>t;
    int x=0,j;
    for(j=1; j<=i; j++)
    {
        if(s[j].num==t)
        {
            x++;
            break;
        }
    }
    if(x==0)
    {
        cout<<"未曾找到,无法操作!"<<endl;
    }
    else
    {
        cout<<s[j].num<<"关键字已找到"<<endl;
        cout<<"姓名:"<<s[j].name<<endl;
        cout<<"成绩:"<<s[j].num<<endl;
        cout<<"学号:"<<s[j].grade<<endl;
        cout<<"请问是否删除?(Y/N)";
        cin>>b;
        if(b=="Y"||b=="y"||b=="yes"||b=="Yes"||b=="YES")
        {
            for(int a=j;a<=i-1;a++)
            {
                s[a].name=s[a+1].name;
                s[a].num=s[a+1].num;
                s[a].grade=s[a+1].grade;
            }
            i--;
            cout<<"删除成功!"<<endl;
        }
    }
}
int main()
{
    int a;
    char t;
    ExitSystem();
    do
    {
        system("cls");
        cout<<"学生学号是关键字!!\n					学生信息的管理与统计";
        cout<<"\n1.信息输入";
        cout<<"\n2.信息显示";
        cout<<"\n3.信息查找";
        cout<<"\n4.信息修改";
        cout<<"\n5.信息删除";
        cout<<"\n0.退出\n\n\n";
        cout<<"请按数字键选择菜单选项:";
        cin>>a;
        switch(a)
        {
        case 1:
            system("cls");
            AddInfo();
            printf("按任意键返回上一级。\n");
            t=getch();
            break;
        case 2:
            system("cls");
            DisplayInfo();
            printf("按任意键返回上一级。\n");
            t=getch();
            break;
        case 3:
            system("cls");
            LocateInfo();
            printf("按任意键返回上一级。\n");
            t=getch();
            break;
        case 4:
            system("cls");
            ModifyInfo();
            printf("按任意键返回上一级。\n");
            t=getch();
            break;
        case 5:
            system("cls");
            DeleteInfo();
            printf("按任意键返回上一级。\n");
            t=getch();
            break;
        case 0:
            system("cls");

        }
    }
    while(a!=0);
}

三、汉诺塔

真的可以动!!!

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
const int COLUMN[4] = { 0, 2, 5, 8 };
const int DISC_CNT_MAX = 10;
const int ROW_OP_CNT = 2, COL_OP_CNT = 16;
const int ROW_MESSAGE = 3, COL_MESSAGE = 16;
const int ROW_HELP = 15, COL_HELP = 1;
const int ROW_MAX = 30, COL_MAX = 120;
const int BLUE = 1;
const int GREEN = 2;
const int CYAN = 3;
const int AQUA = 3;
const int RED = 4;
const int PURPLE = 5;
const int YELLOW = 6;
const int WHITE = 7;
int n;
stack<int> rod[4];
int sz[4] = { 0 };
int pos1, pos2;
int key;
bool prev_key_is_esc;
int op_cnt;
bool is_moving;
int moved_disc;
template <typename T>
inline T read() {
T x = 0;
T multiplier = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
    if (ch == '-') {
        multiplier = -1;
    }
    ch = getchar();
}
while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch & 15);
    ch = getchar();
}
return x * multiplier;
}
void set_caret_pos(int row = 1, int col = 1) {
COORD pos;
pos.X = col - 1;
pos.Y = row - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int get_caret_row() {
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return info.dwCursorPosition.Y + 1;
}
int get_caret_col() {
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return info.dwCursorPosition.X + 1;
}
pair<int, int> get_caret_pos() {
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return make_pair(info.dwCursorPosition.Y + 1, info.dwCursorPosition.X + 1);
}
void set_foreground_color(int x, bool intensity = false) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                        x | (intensity << 3));
}
void set_cursor_visibility(bool visibility = true) {
CONSOLE_CURSOR_INFO cc_info;
cc_info.bVisible = visibility;
cc_info.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cc_info);
}
void disp_init_state(int n) {
for (int i = 1; i <= n; i++) {
    set_caret_pos(i, COLUMN[1]);
    printf("%d", i);
}
for (int i = 1; i <= 3; i++) {
    set_caret_pos(n + 1, COLUMN[i] - 1);
    printf("---");
    set_caret_pos(n + 2, COLUMN[i]);
    putchar('A' + i - 1);
}
 set_caret_pos(ROW_OP_CNT, COL_OP_CNT);
printf("0");
}
void disp_help() {
set_caret_pos(ROW_HELP, COL_HELP);
printf("如何玩:\n"
       "数字表示光盘的尺寸.\n"
       "将A杆上圆盘移动到C杆上,每次一个.\n"
       "每个杆上的圆盘必须按大小升序堆叠.\n"
       "使用左右箭头键选择杆.\n"
       "按Enter键拾取选定杆上的顶部圆盘.\n"
       "然后使用左右键移动.\n"
       "按ESC取消当前移动.\n"
       "再次按Enter键可将圆盘放置.\n"
       "按R重新启动.\n"
       "按ESC键两次退出.\n");
}
void disp_pos(int pos1, int pos2 = 0) {
for (int i = 1; i <= 3; i++) {
    set_caret_pos(n + 3, COLUMN[i]);
    printf(" ");
}
set_caret_pos(n + 3, COLUMN[pos1]);
printf("^");
if (pos2) {
    set_caret_pos(n + 3, COLUMN[pos2]);
    set_foreground_color(GREEN, true);
    printf("^");
    set_foreground_color(WHITE);
}
}
void clear() {
for (int i = 1; i <= DISC_CNT_MAX + 3; i++) {
    for (int j = 1; j <= COL_MAX; j++) {
        set_caret_pos(i, j);
        putchar(' ');
    }
}
}
void moving_disc(int pos1, int pos2) {
int x = rod[pos1].top();
 set_caret_pos(n + 1 - sz[pos1], COLUMN[pos1]);
set_foreground_color(RED, true);
printf("%d", x);
set_foreground_color(WHITE);
 set_caret_pos(n - sz[pos2] + (pos1 == pos2), COLUMN[pos2]);
set_foreground_color(GREEN, true);
printf("%d", x);
set_foreground_color(WHITE);
}
void update_discs(int pos1, int pos2) {
int x = rod[pos1].top();
 set_caret_pos(n + 1 - sz[pos1], COLUMN[pos1]);
printf("  ");
rod[pos1].pop();
sz[pos1]--;
 rod[pos2].push(x);
sz[pos2]++;
set_caret_pos(n + 1 - sz[pos2], COLUMN[pos2]);
printf("%d", x);
}
void remove_temp_disc(int pos) {
set_caret_pos(n - sz[pos], COLUMN[pos]);
printf("  ");
}
void update_op_cnt() {
op_cnt++;
set_caret_pos(ROW_OP_CNT, COL_OP_CNT);
printf("%d", op_cnt);
}
int main() {
printf("输入光盘数量(不超过 %d): ", DISC_CNT_MAX);
n = min(read<int>(), DISC_CNT_MAX);
 set_cursor_visibility(false);
disp_help();
 for (; n <= DISC_CNT_MAX; n++) {
    clear();
     for (int i = 1; i <= 3; i++) {
        while (!rod[i].empty()) {
            rod[i].pop();
        }
        sz[i] = 0;
    }
     for (int i = n; i >= 1; i--) {
        rod[1].push(i);
    }
    sz[1] = n;
     is_moving = false;
    pos1 = 1;
    op_cnt = 0;
    prev_key_is_esc = false;
     disp_init_state(n);
    disp_pos(1);
    while (true) {
        if (sz[3] == n) {
            set_caret_pos(ROW_MESSAGE, COL_MESSAGE);
            if (op_cnt != (1 << n) - 1) {
                printf("你用%d个动作完成了谜题.",op_cnt);
            } else {
                printf("祝贺你用最少的动作完成了谜题 " );
            }
            Sleep(2000);
            break;
        }
         key = getch();
        if (key == 224) {
            key = getch();
            if (!is_moving) {
                if (key == 75) { // Left arrow key
                    pos1 = (pos1 + 1) % 3 + 1;
                } else if (key == 77) { // Right arrow key
                    pos1 = pos1 % 3 + 1;
                }
                disp_pos(pos1);
            } else {
                remove_temp_disc(pos2);
                if (key == 75) { // Left arrow key
                    pos2 = (pos2 + 1) % 3 + 1;
                } else if (key == 77) { // Right arrow key
                    pos2 = pos2 % 3 + 1;
                }
                moving_disc(pos1, pos2);
                disp_pos(pos1, pos2);
            }
        } else if (key == 13) { // Enter key
            if (!is_moving) {
                if (rod[pos1].empty()) {
                    continue;
                }
                is_moving = true;
                moved_disc = rod[pos1].top();
                pos2 = pos1;
                moving_disc(pos1, pos2);
                disp_pos(pos1, pos2);
            } else {
                if (!rod[pos2].empty() && rod[pos2].top() < moved_disc) {
                    set_caret_pos(ROW_MESSAGE, COL_MESSAGE);
                    printf("提示:光盘未按升序堆叠在棒上。");
                    continue;
                }
                is_moving = false;
                update_discs(pos1, pos2);
                update_op_cnt();
                pos1 = pos2;
                disp_pos(pos1);
            }
        } else if (key == 27) { // Escape key
            if (prev_key_is_esc) { // Double ESC
                break;
            }
            if (is_moving) {
                is_moving = false;
                remove_temp_disc(pos2);
                update_discs(pos1, pos1);
                disp_pos(pos1);
            } else {
                prev_key_is_esc = true;
            }
        } else if (key == 'R' || key == 'r') {
            n--;
            break;
        }
    }
     if (prev_key_is_esc && key == 27) { // Double ESC
        break;
    }
}
 set_caret_pos(ROW_MAX - 1, 1);
 return 0;
}

以上就是文章的内容

THE  END......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值