C++实现贪吃蛇(3.0!)

上代码:

操作说明:↑8,↓2,←4,→6

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#define Map_long 20
#define Map_high 15
typedef struct snake{
    int goods_class;
    int direction;
}SNAKE;
SNAKE Snake[Map_high][Map_long] = { { 0 } };
unsigned die = 0;  
unsigned snake_move = 0;  
unsigned eat_food = 0;  
unsigned snake_long = 3;  
void Move(int ch);  
void HideCursor();  
int main(void)
{
    system("mode con:cols=43 lines=20"); 
    HideCursor();
    unsigned i, j;
    unsigned food = 0;  
    unsigned food_x, food_y;  
    unsigned score = 0;  
    char ch = '\0'; 
    srand((unsigned)time(NULL));
    for (i = 0; i < Map_long; i++)
    {
        Snake[0][i].goods_class = 1;
        Snake[Map_high - 1][i].goods_class = 1;
    }
    for (i = 0; i < Map_high; i++)
    {
        Snake[i][0].goods_class = 1;
        Snake[i][Map_long - 1].goods_class = 1;
    }
    for (i = 0; i < snake_long; i++)
        Snake[Map_high / 2][Map_long / 2 + i].goods_class = snake_long + i;

    while (1)
    {
        while (!_kbhit())
        {
            switch (ch)
            {
            case 72:Move(8); break;
            case 75:Move(4); break;
            case 77:Move(6); break;
            case 80:Move(2); break;
            default:break;
            }

            if (eat_food == 2)  
            {
                food = 0;  
                eat_food = 0;  
                snake_long++;  
                score++;  
            }
            do{
                if (food == 1)
                    break;
                food_x = rand() % Map_long;
                food_y = rand() % Map_high;
                if (Snake[food_y][food_x].goods_class == 0)
                {
                    Snake[food_y][food_x].goods_class = 2;
                    food++;
                    break;
                }
            } while (1);

            printf("Grade:%u\n", scor
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值