c++贪吃蛇代码

单机版1

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;

const int COLS = 22;//游戏窗口列数 
const int ROWS = 22;//游戏窗口行数 
const string S_WALL = "∷";//围墙元素 
const string S_SPACE = " ";//空白元素 
const string S_HEAD = "●";//蛇头 
const string S_BODY = "⊙";//蛇身 
const string S_FOOD = "★";//苹果

int snake [COLS * ROWS][2];
int food_x, food_y;//苹果的坐标 
int Score = 5;
bool gameOver = false;
bool isPosInSnake( int x , int y );
char ch = 'd';

HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;

void p_wall();
void p_snake();
void p_food();
void move( char ch );
void locate( int x, int y )
{
    coord.X = x * 2;
    coord.Y = y;
    SetConsoleCursorPosition(hout, coord);
}

double random( double start , double end )//生成一个start到end之间的随机数 
{
    return start + ( end - start ) * rand() / ( RAND_MAX + 1.0 );
}

int main()
{
    
    system("pause");
    
    system ( "cls" );
    
    //初始化屏幕的一些参数,隐藏光标
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(hout, &cursor_info);
    
    snake [0][0] = 6;//蛇头的坐标 
    snake [0][1] = 2;
    snake [1][0] = 5;//蛇的第二个关节 
    snake [1][1] = 2;
    snake [2][0] = 4;//蛇的第二个关节 
    snake [2][1] = 2;
    snake [3][0] = 3;//蛇的第三个关节 
    snake [3][1] = 2;
    snake [4][0] = 2;//蛇的第四个关节 
    snake [4][1] = 2;
    
    //初始化苹果位置
    do
    {
        food_x = random( 3 , COLS - 2 );
        food_y = random( 2 , ROWS - 2 );
    }
    while ( isPosInSnake( food_x , food_y ) );
    p_wall();//调用绘制墙面
    clock_t a , b;
    while( true )//死循环 
    {    
        //初始化屏幕的一些参数,隐藏光标
        CONSOLE_CURSOR_INFO cursor_info = {1, 0};
        SetConsoleCursorInfo(hout, &cursor_info);
        
        if( gameOver )
        {
            system ( "cls" );
            cout << "Game Over!" << endl;
            cout << "You lose!" << endl;
            cout << "Score : " << Score << endl;
            system("pause");
            return 0;
        }
        p_snake();//调用绘制蛇头
        p_food();//调用绘制苹果 
        a = clock();
        while ( 1 )
        {
            b = clock();
            if( b - a >= 230 )
            {
                break;
            }
        }
        if( kbhit() )
        { 
            char next_ch = getch();//死循环获取用户按下的字符 
            if( next_ch >= 'A' && next_ch <= 'Z' )
            {
                next_ch += 'a' - 'A';
            }
            if( ( 'd' == ch || 'a' == ch ) && ( next_ch == 'w' || next_ch == 's' ) )
            {
                ch = next_ch;
            }
            else if( ( 'w' == ch || 's' == ch ) && ( next_ch == 'a' || next_ch == 'd' ) )
            {
                ch = next_ch;
            }
        }
        move( ch );//调整坐标的x,y位置
        locate( 0 , COLS + 1 );
        cout << "Now Score : " << Score; 
    }
}

bool isPosInSnake( int x , int y )
{
    for( int i = 0; i < COLS * ROWS; i++ )
    {
        if( snake [i][0] == x && snake [i][1] == y )
        {
            return true;
        }
    }
    return false;
}

void move( char ch )
{
    int x = snake [0][0] , y = snake [0][1];
    switch( ch )
    {
        case 'W' ://按下W,向上 
        case 'w' ://按下w,向上 
            y--;
            break;
        case 'S' ://按下S,向下 
        case 's' ://按下s,向下 
            y++;
            break;
        case 'A' ://按下A,向左 
        case 'a' ://按下a,向左 
            x--;
            break;
        case 'D' ://按下D,向右 
        case 'd' ://按下d,向右 
            x++;
            break;
        default :
            return;
    }
    if( x < 2 || x > COLS - 1 || y < 2 || y > ROWS - 1 )//检测是否撞墙 
    {
        gameOver = true;
        return;
    }
    if( isPosInSnake( x , y ) )
    {
        gameOver = true;
        return;
    }
    for( int i = COLS * ROWS - 1; i >= 1; i-- )//身体所有部位向前移 
    {
        snake [i][0] = snake [i - 1][0];
        snake [i][1] = snake [i - 1][1];

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用windows api 做的贪吃蛇 #include #include"resource.h" #include"Node.h" #include #include TCHAR szAppname[] = TEXT("Snack_eat"); #define SIDE (x_Client/80) #define x_Client 800 #define y_Client 800 #define X_MAX 800-20-SIDE //点x的范围 #define Y_MAX 800-60-SIDE //点y的范围 #define TIME_ID 1 #define SECOND 100 #define NUM_POINT 10 //点的总个数 #define ADD_SCORE 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; //窗口句柄 MSG msg; //消息 WNDCLASS wndclass; //窗口类 HACCEL hAccel;//加速键句柄 wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口的水平和垂直尺寸被改变时,窗口被重绘 wndclass.lpfnWndProc = WndProc; //窗口过程为WndProc函数 wndclass.cbClsExtra = 0; //预留额外空间 wndclass.cbWndExtra = 0; //预留额外空间 wndclass.hInstance = hInstance; //应用程序的实例句柄,WinMain的第一个参数 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //设置图标 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //载入预定义的鼠标指针 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置画刷 wndclass.lpszMenuName = szAppname; //设置菜单 wndclass.lpszClassName = szAppname; //设置窗口类的名字 if (!RegisterClass(&wndclass))//注册窗口类 { MessageBox(NULL, TEXT("这个程序需要windows NT!"), szAppname, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname, TEXT("Snack_eat"),//CreateWindow函数调用时,WndProc将受到WM_CREATE WS_OVERLAPPEDWINDOW&~WS_THICKFRAME& ~WS_MAXIMIZEBOX,//普通的层叠窗口&禁止改变大小&禁止最大化 CW_USEDEFAULT, //初始x坐标(默认) CW_USEDEFAULT, //初始y坐标 x_Client, //初始x方向尺寸 770 y_Client, //初始y方向尺寸 750 NULL, //父窗口句柄 NULL, //窗口菜单句柄 hInstance, //程序实例句柄 WinMain函数中第二个参数 NULL); //创建参数 ShowWindow(hwnd, iCmdShow);//显示窗口,iCmdShow是WinMain的第四个参数,决定窗口在屏幕中的初始化显示形式,例:SW_SHOWNORMAL表示正常显示 UpdateWindow(hwnd);//使窗口客户区重绘,通过向WndProc发送一条WM_PAINT消息而完成的 hAccel = LoadAccelerators(hInstance, szAppname);//加载加速键 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }/* while (GetMessage(&msg, NULL, 0, 0))//GetMessage函数从消息队列中得到消息,填充msg。如果msg.message等于WM_QUIT,返回0,否则返回非0 { TranslateMessage(&msg);//将msg返回给windows已进行某些键盘消息的转换 DispatchMessage(&msg);//将msg再次返回给windows }*/ return msg.wParam;//msg.wParam是PostQuitMessage函数的参数值,通常是0 } ...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值