首页 > 其他分享 >【转载】Game 1

【转载】Game 1

时间:2023-06-17 22:44:35浏览次数:28  
标签:转载 goto sep int break Game ch include

奇葩贪吃蛇

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winable.h>
#include <ctime>
#define random(a,b) (rand() % (b-a+1))+ a;
#define sr srand((unsigned)time(0))
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;
int slen, x, y;
int pot,tot;
int nowlen;
int cx, cy;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};//右,下,左,上; 
bool sep=0;
int hx[2000000],hy[2000000];
int cnt;
int k;
int nx = 0, ny = 1, f = 0;

int pip[300][300];

void start();

void gotoxy(int y, int x)//移动光标 
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
/*--part 1--头文件*/
void frame()
{
    cout << "    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ¥                                                                   |" << endl;
    cout << "    ######################################################################" << endl;
    if(sep==1)
	{
		cout<<"您正处于极限模式!!!"; 
	}
    cout << "    请将输入法调至英文状态即可开始游戏\n\n    空格键暂停,Z键继续,WSAD操控贪吃蛇,'['减速,']'加速\n\n    撞到墙或按删除键结束\n\n    ";
}
/*--part 2--框架打印*/

int ux,uy;

void tanch()
{
    sr;//每次计算前都srand一下
    ux = random(5, 18);
    sr;
    uy = random(9, 65);
    while(pip[ux][uy] == 1)
    {
        sr,ux = random(5, 18);
        sr,uy = random(9, 65);
    }
    gotoxy(ux, uy);
     
    sr;//每次计算前都srand一下
    ux = random(5, 18);
    sr;
    uy = random(9, 65);
    while(pip[ux][uy] == 1)
    {
        sr,ux = random(5, 18);
        sr,uy = random(9, 65);
    }
    gotoxy(ux, uy);
    cout << "豆";
}
/*--part 3--随机豆子计算打印*/
void init()
{
    cx = cy = x = y = 10;
    slen = 4;
    nowlen = 0;
    gotoxy(10, 10);
    cout << "豆";
    tanch();
}

/*--part 4--初始化数据*/

void end()
{
    system("cls");
    cout<<"total "<<pot<<endl;
    if(sep==1)
    {
    	cout<<"很遗憾,由于您正处于极限模式,您的电脑将在5秒后关机。";
    	system("shutdown -s -f -t 5"); 
    	system("pause");
    	exit(0);
	}
	else
	{
	system("pause");
    exit(0);	
	}
}
/*--part 5--结束操作*/
int main()
{
    system("color f0");
    start();
    ab:
    system("cls");
    frame();
    init();
    while(1)
    {
    	
    	cout<<'\a';
        Sleep(300 - k);
        x += nx, y += ny;
        if(x == ux && y == uy)
        {
            slen++;
            if(k + 10 < 300)
                k += 10;
            tanch();
            tot++;
            pot += 10*tot*0.7;
        }
        if(x == 0 || x == 22 || y == 4 || y == 72)
        {
            end();
        }
        gotoxy(x,y);
        pip[x][y] = 1;
        cout<<"头";
        gotoxy(hx[cnt],hy[cnt]);
        cout<<"身";
        if(nowlen == slen)
        {
            gotoxy(cx,cy);
            cout<<" ";
            pip[cx][cy] = 0;
            cx = hx[cnt - slen + 1], cy = hy[cnt - slen + 1];
            gotoxy(cx,cy);
            cout<<" ";
            pip[cx][cy] = 0;
            cx = hx[cnt - slen + 2], cy = hy[cnt - slen + 2];
            gotoxy(x,y);
        pip[x][y] = 1;
        cout<<"头";
        gotoxy(hx[cnt],hy[cnt]);
        cout<<"身";
            
//                      由于没有时间判断蛇尾到底在哪里,所以前两个都删了,暂时还没发现bug
        }
        else{//这边是判断刚开始蛇的显示长度不到蛇的真实长度
            nowlen++;
            if(nowlen == slen)
                gotoxy(10,10),cout<<" ";
        }
        if(kbhit())
        {
            char ch;
            ch = getch();
            switch(ch)
            {
            	case 27:
            		system("cls");
            		gotoxy(1,35);
            		cout<<"暂停"<<endl;
            		gotoxy(2,35);
            		cout<<"Stop"<<endl;
            		gotoxy(9,30); 
            		cout<<"按c继续,按s退出\n";
            		while(1)
            		{
            			if(kbhit())
            			{
            				char kb;
            				kb=getch();
            				switch(kb)
            				{
            					case 'c':
            					    goto ab;	
            					break;
            					case 's':
            						end();
            					break;
            					case 'q':
            						system("title Commend Mode");
            						string cmd;
            						while(1)
            						{
            							gotoxy(10,30);
            							cin>>cmd;
            							if(cmd=="title")
            							{
            								char am[100]={'t','i','t','l','e',' '},bm[100];
            								cin>>bm;
            								strcat(am,bm);
            								system(am);
										}
										if(cmd=="win")
										{
											system("cls");
											gotoxy(1,35);
						            		cout<<"胜利......."<<endl;
						            		gotoxy(2,35);
						            		cout<<"Win"<<endl;
						            		gotoxy(9,30); 
						            		cout<<"实际上这个游戏根本就没有胜利\n                              但你居然用调试模式弄出来了\n                              你很牛!\n";
						            		system("pause");
											system("cls");
											 gotoxy(9,30);
											 cout<<"不过依然祝贺你!\n";
											  cout<<"Total:"<<-tot;
											  system("pause");
											  return 0;
										}
									}
            					break;
							}
						}
					}
					break; 
                case 8:end();break;
                case 91:{
                    if(k - 50 > 0)
                        k -= 50;
                        
                    break;
                }
                case 93:{
                    if(k + 50 < 250)
                        k += 50;
                    break;
                }
                case ' ':{
                    while(1)
                    {
                        if(kbhit())
                        {
                            ch = getch();
                            if(ch == 'z')
                                break;  
                        }   
                    }
                    break;
                } 
                case 'w':{
                    if(f == 1)break;//相反方向操作无效
                    f = 3;
                    nx = dx[f], ny = dy[f];
                    gotoxy(0,0);
                    cout<<"x="<<nx<<"y="<<ny;
                    break;
                }
                case 's':{
                    if(f == 3)break;
                    f = 1;
                    nx = dx[f], ny = dy[f];
                    gotoxy(0,0);
                    cout<<"x="<<nx<<"y="<<ny;
                    break;
                    
                }
                case 'a':{
                    if(f == 0)break;
                    f = 2;
                    nx = dx[f], ny = dy[f];
                    gotoxy(0,0);
                    cout<<"x="<<nx<<"y="<<ny;                                
                    break;
                }
                case 'd':{
                    if(f == 2)break;
                    f = 0;
                    nx = dx[f], ny = dy[f];
                    gotoxy(0,0);
                    cout<<"x="<<nx<<"y="<<ny;
                    break;
                }
            }
        }
        hx[++cnt] = x, hy[cnt] = y;//记录时刻的蛇头位置,可以帮助计算蛇尾
//              hx和hy数组只开了2000000没有滚动数组优化,所以不能运行太久,会有bug
	gotoxy(0,0);
        cout<<"x="<<hx[cnt]<<"y="<<hy[cnt];
    }
//  while(1)
//  {
//      system("cls");
//      gotoxy(0,0);
//      char ch;
//      ch = getch();
//      printf("%d", ch);
//  }这里只是一个算ASCII的小工具
}
/*--part 6--主函数*/
void start()
{
    for(int i = 1; i <= 1; i++)
    {
        gotoxy(10,33);
        cout<<"Booming!!"; 
        for(int j = 1; j <= i % 4; j++)
            cout<<".";
        cout<<"     ";
        gotoxy(9,0); 
        for(int j = 1; j <= 10; j++)
        {
        	cout<<"正";
            Sleep(200);
            cout<<"在";
            Sleep(200);
            cout<<"滑";
            Sleep(200);
            cout<<"稽";
            Sleep(200);
		}   
    }//小动画,挺有趣的当时写出来的时候
    system("cls");
    int num = rand() % 100;
    if(num<=60)
    {
    	cout<<"你已进入极限模式,若要更改为正常模式,键入y;要继续,键入n;要了解什么是极限模式,键入h.\n(友情提示:键入n代表您了解且同意极限模式,所有损失由您承担)"<<endl;
		 char ch;
		 cin.get(ch);
		 if(ch=='h')
		 {
		 	cout<<"什么是极限模式?\n在此模式下,您一旦失败,您的电脑将被关机。不过.........." <<endl;
		system("pause");
    	system("cls");
    	gotoxy(10,33);
    	system("color F4");
    	cout<<"十分刺激";
    	while(1)
    	{
    		char ch;
            ch = getch();
            break;
		}
    	system("cls");
		cout<<"若要更改为正常模式,键入y;要继续,键入n;"<<endl; 
		cin>>ch;
		if(ch=='y')
		{
			sep=0;
			goto a;
		}
		else
		{
			sep=1;
			goto a;
		}
		 }
		 else if(ch=='y')
		{
			sep=0;
			goto a;
		}
		else
		{
			sep=1;
			goto a;
		}
    
	}
	a:
    cout<<"    请将输入法调至英文状态即可开始游戏\n\n    空格键暂停,Z键继续\n\n    '['减速,']'加速\n\n    WSAD操控贪吃蛇\n\n    撞到墙或按删除键结束,若您开启了极限模式,按下删除键会导致您被关机\n\n    ";
    system("pause");
    system("cls"); 
}
/*--part 7--进入动画*/

标签:转载,goto,sep,int,break,Game,ch,include
From: https://www.cnblogs.com/haozexu/p/17488426.html

相关文章

  • 转载-linux与soc-移植U-Boot思路和实践 | 基于RK3399
    原文链接:https://mp.weixin.qq.com/s/T1BmaP2-XbJIpLNsFxKeEQ0.背景介绍我们手里这块RK3399开发板出厂时带的是2017.09版本的U-Boot。 U-Boot 2017.09 (Sep 26 2021 - 08:53:15 +0000)   Model: Forlinx OK3399 Evaluation Board Pr......
  • Python 字符编码转换(转载)
    Python字符编码转换1.在python2默认编码是ASCII,python3里默认是unicode2.unicode分为utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节),soutf-16就是现在最常用的unicode版本,不过在文件里存的还是utf-8,因为utf8省空间3.在py3中encode,在转码的同时......
  • Python 文件操作(转载)
    Python文件操作操作文件时,一般需要经历如下步骤打开文件操作文件一、打开文件文件句柄=open('文件路径','模式')打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。打开文件的模式有r,只读模式(默认)。w,只......
  • [转载]探索 StableDiffusion:生成高质量图片学习及应用
    转自公众号大淘宝技术 本文主要介绍了StableDiffusion在图片生成上的内容,然后详细说明了StableDiffusion的主要术语和参数,并探讨了如何使用prompt和高级技巧(如图像修复、训练自定义模型和图像编辑)来生成高质量的图片。 介绍StableDiffusion ▐ ......
  • [转载]ChatGPT:智能化游戏测试的新尝试
    转自公众号网易雷火测试中心当下,ChatGPT红遍了全网,作为一个被认为会对人类社会产生威胁的聊天机器人。ChatGPT是不是真的有那么神奇?好奇心驱使下,作者对ChatGPT做了一些简单的试用,并思考了ChatGPT以及类似相关的AI工具能给我们的游戏测试工作带来哪些启发和效率提......
  • Ubuntu 12.04LTS 更换源(转载)
    2022-03实测可用前言 本文主要介绍如何为已经不再支持的Ubuntu12.04LTS更换源,网上查到的源(很多是13-14年的帖子)都无法直接使用了,例如下面这个1234debhttp://archive.ubuntu.com/ubuntuprecisemainuniverserestrictedmultiversedebhttp://archive.ubu......
  • createelement的用法(转载)
    document.createElement()是在对象中创建一个对象,要与appendChild()或insertBefore()方法联合使用。其中,appendChild()方法在节点的子节点列表末添加新的子节点。insertBefore()方法在节点的子节点列表任意位置插入新的节点。​下面,举例说明document.createElement()的用......
  • wpf特殊属性:正确理解ContentPresenter(转载)
    原文地址:https://www.cnblogs.com/shawnzxx/p/3346975.html下图显示继承关系:ContentControl:Control(在Control類並沒有Content屬性,所以在這之上再寫了一個ContentControl,使控件有Content屬性可以顯示內容)ContentPresenter:FrameworkElement(ContentPresenter一般用在C......
  • ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效(oracle 锁表)(转载
    1、查看数据库内产生了哪些锁selectt2.username,t2.sid,t2.serial#,t2.logon_timefromv$locked_objectt1,v$sessiont2wheret1.session_id=t2.sidorderbyt2.logon_time;如:   USERNAMESIDSERIAL#LOGON_TIMElurou851241832013/7/3011:44:45知道被锁的用户l......
  • 转载:Win10删除锁屏界面右下角网络图标
    Win10删除锁屏界面右下角网络图标(win10怎么关闭右下角图标)Win10开机后,锁屏界面的右下角会显示个网络图标,方便我们查看电脑当前的网络状态,但是有些用户觉得,这样会破坏了锁屏画面的整体性。这篇文章是本站给大家带来的取消锁屏网络方法教程。  方法/步骤:   1、按Win+R......