首页 > 编程语言 >C++小游戏集3个(不定时更新)2

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

时间:2024-09-07 20:51:08浏览次数:6  
标签:const int C++ 小游戏 && printf y1 定时 include

前言

在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......

标签:const,int,C++,小游戏,&&,printf,y1,定时,include
From: https://blog.csdn.net/syxx_xxys/article/details/142003853

相关文章

  • [c++][笔记]浅谈几种排序方式---冒泡排序,选择排序,桶排序
     一、algorithm里的sort函数 #include<cstdio>//数据小的可以用iostream#include<algorithm>//不能忘记算法库,否则会编译失败。usingnamespacestd;intmain(){intn;scanf("%d",&n);inta[n+5]={};for(inti=1;i<=n;i++){......
  • C++ 模板基础知识——可变参数模板
    目录C++模板基础知识——可变参数模板1.可变参函数模板1.1基本含义1.2利用constexprif优化递归函数1.3关于constexprif的进一步理解1.4重载2.折叠表达式2.1一元左折(UnaryLeftFold)2.2一元右折(UnaryRightFold)2.3二元左折(BinaryLeftFold)2.4二元右折......
  • 用时间轮实现定时器
    前言    我们在网络编程中会遇到这样一个场景:我们需要维护大量的TCP连接以进行网络通信,但这些连接中有大量的空闲。我们如何高效地管理这些连接?答案是定时器,我们给每个连接设置定时器来自动管理,规定时间内未进行通信则自动关闭连接。    但是你应该能很快发......
  • C++对象模型
    C++对象模型在C++面向对象的类中,有两种数据成员和三种成员函数:classBase{public:Base(inti):baseI(i){};intgetI(){returnbaseI;}staticvoidcountI(){};virtualvoidprint(void){cout<<"Base::print()";}virtual~Base(){}privat......
  • C++继承基础
    虚函数默认实参C++默认实参是靠编译器实现的,因此默认实参不参与动态绑定,默认实参有静态类型决定。访问控制每个类分别控制自己的成员初始化过程,还分别控制其成员对于派生类来说是否可访问,友元不继承。成员:protected:派生类可见、自己、friend可见public:所有可见private:自己......
  • 【C++算法全真练习题】迷宫问题
    目录题目描述思路AC解答题目描述‌题目描述‌:‌给定一个二维迷宫,‌其中 0 表示可以走的路,‌1 表示障碍物。‌起点坐标为 (0,0),‌终点坐标为 (m-1,n-1),‌其中 m 和 n 分别是迷宫的行数和列数。‌你需要使用广度优先搜索(‌BFS)‌找到从起点到终点的一条路径......
  • C++常见异常汇总(二): undefined reference to
    文章目录1、undefinedreferencetoA2、undefinedreferenceto`vtable2.1模版函数定义方案1:定义与实现均一起定义在头文件中2.2模版函数定义方案2:定义的同一个文件中,显示声明具体类型3、multipledefinitionof1、undefinedreferencetoA检查所有main相......
  • C++复习day06
    一、内存管理1.课件上关于内存分配的题目intglobalVar=1;staticintstaticGlobalVar=1;voidTest(){staticintstaticVar=1;intlocalVar=1;intnum1[10]={1,2,3,4};charchar2[]="abcd";constchar*pChar3="abcd";int*ptr1=(in......
  • windows C++-并行编程-转换使用异常处理的 OpenMP 循环以使用并发运行时
    此示例演示如何将执行异常处理的OpenMP并行for循环转换为使用并发运行时异常处理机制。在OpenMP中,在并行区域中引发的异常必须由同一线程在同一区域中捕获和处理。未处理的异常处理程序会捕获逃离并行区域的异常,默认情况下会终止进程。在并发运行时中,在传递给任务组(例......
  • c++元对象实现
    c++元对象实现在C++中,元对象技术通常指的是运行时检查类型信息和对象信息的能力。C++11标准引入了typetraits和reflection的概念,允许我们在编译时获取和使用类型信息。下面是一个简单的C++类,使用了C++11的typetraits和C++17的std::any来实现元对象:  #include<iostrea......