cmd版贪吃蛇

不知多少年前写的
发出来玩玩算了

/************************************************************************************/
// 方向键控制蛇方向
// q键退出
/************************************************************************************/

#include <Windows.h>
#include <cstdio>
#include <time.h>

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

#define width 78                //宽度
#define height 24               //高度
#define gamespeed 20            //游戏速度

struct gamestrcut
{
    enum res
    {
        null,                   //空
        snakebody,              //蛇身体
        wood,                   //木箱
        food,                   //食物
    };
    res type;                   //类型
    long time;                  //仅限蛇身用,用来计算蛇长
};

gamestrcut map[width][height];  //地图

char snakehead[4];  //x,y,direct,length //x轴位置,y轴位置,前进方向,蛇长

void main()
{
    printf("操作方式:\n");
    printf("方向键控制蛇方向,Q键退出\n");
    printf("回车开始游戏\n");
    getchar();
    srand(time(0));             //设置随机数
    ZeroMemory(map, sizeof(map));
    ZeroMemory(snakehead, sizeof(snakehead));
    //设置木箱
    for (unsigned long w = 0; w < width; ++w)
    {
        map[w][0].type = gamestrcut::wood;
        map[w][height-1].type = gamestrcut::wood;
    }
    for (unsigned long h = 0; h < height; ++h)
    {
        map[0][h].type = gamestrcut::wood;
        map[width-1][h].type = gamestrcut::wood;
    }
    //设置蛇数据
    snakehead[0] = width / 2;
    snakehead[1] = height / 2;
    snakehead[2] = 0;
    snakehead[3] = 2;
    map[snakehead[0]][snakehead[1]].type = gamestrcut::snakebody;
    map[snakehead[0]][snakehead[1]].time = snakehead[3];
    //提前生成一个食物
    {
        do
        {
            unsigned long x = ((float)rand() / RAND_MAX) * width;
            unsigned long y = ((float)rand() / RAND_MAX) * height;
            if (map[x][y].type == gamestrcut::null)
            {
                map[x][y].type = gamestrcut::food;
                break;
            }
        } while (true);
    }
    do
    {
        //取按下键状态,该循环是为了提高灵敏度
        for (unsigned long t = 0; t < gamespeed;++t)
        {
            Sleep(1);
            if (KEYDOWN(VK_UP))
            {
                if (snakehead[2] != 3)
                    snakehead[2] = 1;
            }
            else if (KEYDOWN(VK_RIGHT))
            {
                if (snakehead[2] != 4)
                    snakehead[2] = 2;
            }
            else if (KEYDOWN(VK_DOWN))
            {
                if (snakehead[2] != 1)
                    snakehead[2] = 3;
            }
            else if (KEYDOWN(VK_LEFT))
            {
                if (snakehead[2] != 2)
                    snakehead[2] = 4;
            }
            else if (KEYDOWN('Q'))
            {
                goto end;
            }
        }
        //控制蛇头
        switch (snakehead[2])
        {
            case 1:
                --snakehead[1];
                break;
            case 2:
                ++snakehead[0];
                break;
            case 3:
                ++snakehead[1];
                break;
            case 4:
                --snakehead[0];
                break;
            default:
                break;
        }
        //各种控制反应
        switch (map[snakehead[0]][snakehead[1]].type)
        {
            case gamestrcut::null:
                map[snakehead[0]][snakehead[1]].type = gamestrcut::snakebody;
                map[snakehead[0]][snakehead[1]].time = snakehead[3];
                break;
            case gamestrcut::snakebody:
                if (snakehead[2] != 0)
                    goto end;
                break;
            case gamestrcut::food:
                map[snakehead[0]][snakehead[1]].type = gamestrcut::snakebody;
                map[snakehead[0]][snakehead[1]].time = snakehead[3];
                ++snakehead[3];
                for (unsigned long s = 0; s < sizeof(map) / sizeof(map[0]); ++s)
                {
                    if (((gamestrcut*)map)[s].type == gamestrcut::snakebody)
                        ++((gamestrcut*)map)[s].time;
                }
                for (unsigned long i = 0; i < width; ++i)
                {
                    for (unsigned long j = 0; j < height; j++)
                    {
                        if (map[i][j].type == gamestrcut::snakebody)
                        {
                            ++map[i][j].time;
                        }
                    }
                }
                break;
            case gamestrcut::wood:
                goto end;
                break;
            default:
                break;
        }
        //刷新食物

        {
            static char foodfresh;
            ++foodfresh;
            do
            {
                if (foodfresh <= 10)
                    break;
                foodfresh = 0;
                unsigned long x = ((float)rand() / RAND_MAX) * width;
                unsigned long y = ((float)rand() / RAND_MAX) * height;
                if (map[x][y].type == gamestrcut::null)
                {
                    map[x][y].type = gamestrcut::food;
                    break;
                }
            } while (true);
        }

        //刷新蛇身
        for (unsigned long i = 0; i < width; ++i)
        {
            for (unsigned long j = 0; j < height; j++)
            {
                if (snakehead[2] == 0)
                    break;
                if (map[i][j].type == gamestrcut::snakebody)
                {
                    --map[i][j].time;
                    if (map[i][j].time <= 0)
                        map[i][j].type = gamestrcut::null;
                }
            }
        }
        //屏幕输出
        system("cls");
        for (unsigned long i = 0; i < height; ++i)
        {
            char line[width + 3];
            for (unsigned long j = 0; j < width; ++j)
            {
                switch (map[j][i].type)
                {
                    case gamestrcut::null:
                        line[j] = ' ';
                        break;
                    case gamestrcut::snakebody:
                        line[j] = 'S';
                        break;
                    case gamestrcut::food:
                        line[j] = 'F';
                        break;
                    case gamestrcut::wood:
                        line[j] = 'W';
                        break;
                    default:
                        line[j] = '!';
                        break;
                }
            }
            line[width] = 0x0D;
            line[width + 1] = 0x0A;
            line[width + 2] = 0;
            printf(line);
        }
    } while (true);
end:
    printf("你的分数是:%d\n",(long)snakehead[3] - 2);
    getchar();
    return;
}
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个使用turtlesim实现贪吃蛇的Python代码: ```python import rospy from geometry_msgs.msg import Twist from turtlesim.msg import Pose from math import pow, atan2, sqrt class TurtleBot: def __init__(self): rospy.init_node('turtlebot_controller', anonymous=True) self.velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) self.pose_subscriber = rospy.Subscriber('/turtle1/pose', Pose, self.update_pose) self.pose = Pose() self.rate = rospy.Rate(10) def update_pose(self, data): self.pose = data self.pose.theta = round(self.pose.theta, 4) def euclidean_distance(self, goal_pose): return sqrt(pow((goal_pose.x - self.pose.x), 2) + pow((goal_pose.y - self.pose.y), 2)) def linear_vel(self, goal_pose, constant=1.5): return constant * self.euclidean_distance(goal_pose) def steering_angle(self, goal_pose): return atan2(goal_pose.y - self.pose.y, goal_pose.x - self.pose.x) def angular_vel(self, goal_pose, constant=6): return constant * (self.steering_angle(goal_pose) - self.pose.theta) def move2goal(self): goal_pose = Pose() goal_pose.x = input("Set your x goal: ") goal_pose.y = input("Set your y goal: ") distance_tolerance = input("Set your tolerance: ") vel_msg = Twist() while self.euclidean_distance(goal_pose) >= distance_tolerance: vel_msg.linear.x = self.linear_vel(goal_pose) vel_msg.linear.y = 0 vel_msg.linear.z = 0 vel_msg.angular.x = 0 vel_msg.angular.y = 0 vel_msg.angular.z = self.angular_vel(goal_pose) self.velocity_publisher.publish(vel_msg) self.rate.sleep() vel_msg.linear.x = 0 vel_msg.angular.z = 0 self.velocity_publisher.publish(vel_msg) if __name__ == '__main__': try: x = TurtleBot() x.move2goal() except rospy.ROSInterruptException: pass ``` 希望能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值