c++贪吃蛇代码

这是一个使用C++实现的单机版贪吃蛇游戏,包括游戏窗口设置、蛇的移动、碰撞检测、食物生成等功能。游戏有两版,分别展示了不同风格的蛇和界面。
摘要由CSDN通过智能技术生成

单机版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];

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值