贪吃蛇代码100行c/c++

贪吃蛇

说明: 本人想在网上 找一篇VS或者dev可以运行的贪吃蛇玩一下并且看看怎么实现的 ,奈何网上的c/c++版本不是报错就是运行不了, ~一气之下连夜写了一个~~

基础知识 :结构体、数组 、srand()随机函数 以及#include <conio.h> 头文件下面的控制台输入
简单易懂没什么难的内容

运行环境 VScode2019 dev好像运行的更稳定 晕

要玩这个游戏的话 按键盘的上下左右哦

代码这么短值得说的是
运用了 head 和tail 吧 ,呃好像队列 (自己也不知道为啥想起来用这个)
贪吃蛇吃掉食物后tail就减减, 然后变为蛇的长度就增加啦
没吃的话一直走蛇身体的长度不变(害 我擦 自己也不会解释了 。。。 代码说话吧)
运行界面这样子吧

人狠话不多 代码:

在这里插入代码片
```#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>     // 随机函数的头文件
#include<iostream>  //输入输出流吧      c也可以 cout  换成printf 就好了
#include<conio.h>
#include<ctime>
using namespace std;
char s[1000][1000];
int N = 28 , M = 80 ,  direction  = 75 , k =77  ,grade = 0;      //n, m是地图的大小 ,dir和是方向  , grade是成绩     
int head = 4, tail = 1, leng, x, y, z = 1000 , game = 1; 
//head ,tail 是蛇头蛇尾的位置
struct note    
{
   
	int x, y;
}snake[1000000];     //蛇的结构体

void random()            //地图上随机出现蛇的食物
{
   
	srand((unsigned int)time(NULL));
   
	x = rand() % N;
	y = rand() % M;
   
	while (x == snake[head].x && y == snake[head].y) //判断食物是否会被随机到蛇身上   如果是重新随机
	{
   
  • 18
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个基于C++贪吃蛇代码,共计大约500代码中使用了Windows API库进图形界面的实现,使用了链表数据结构来存储蛇的身体,同时使用了多线程来处理游戏逻辑和图形渲染。 ```c++ #include <iostream> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 60 #define HEIGHT 20 #define SNAKE_HEAD '*' #define SNAKE_BODY 'O' #define FOOD '$' using namespace std; // 食物结构体 struct Food { int x, y; }; // 蛇身结构体 struct SnakeBody { int x, y; SnakeBody* next; }; // 蛇结构体 struct Snake { SnakeBody* head; SnakeBody* tail; int length; char direction; }; // 游戏状态 enum GameState { RUNNING, PAUSED, OVER }; // 全局变量 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); HWND hWnd = GetConsoleWindow(); DWORD dwThreadId, dwThreadPriority; Snake snake; Food food; GameState gameState = PAUSED; bool isEaten = false; int score = 0; // 初始化游戏 void InitGame() { // 初始化蛇 SnakeBody* body = new SnakeBody{ WIDTH / 2, HEIGHT / 2, nullptr }; snake.head = body; snake.tail = body; snake.length = 1; snake.direction = 'D'; // 初始化食物 srand((unsigned)time(NULL)); food.x = rand() % WIDTH + 1; food.y = rand() % HEIGHT + 1; // 初始化游戏状态 gameState = RUNNING; isEaten = false; score = 0; } // 绘制地图 void DrawMap() { system("cls"); for (int i = 0; i <= HEIGHT + 1; i++) { for (int j = 0; j <= WIDTH + 1; j++) { if (i == 0 || i == HEIGHT + 1 || j == 0 || j == WIDTH + 1) { cout << "#"; } else { if (i == food.y && j == food.x) { cout << FOOD; } else { SnakeBody* p = snake.head; bool isBody = false; while (p != nullptr) { if (p->x == j && p->y == i) { cout << (isBody ? SNAKE_BODY : SNAKE_HEAD); isBody = true; } p = p->next; } if (!isBody) { cout << " "; } } } } cout << endl; } cout << "Score: " << score << endl; } // 更新蛇的位置 void UpdateSnake() { SnakeBody* newHead = new SnakeBody{ 0, 0, nullptr }; newHead->next = snake.head; switch (snake.direction) { case 'W': newHead->y = snake.head->y - 1; newHead->x = snake.head->x; break; case 'A': newHead->y = snake.head->y; newHead->x = snake.head->x - 1; break; case 'S': newHead->y = snake.head->y + 1; newHead->x = snake.head->x; break; case 'D': newHead->y = snake.head->y; newHead->x = snake.head->x + 1; break; } snake.head = newHead; // 判断是否吃到食物 if (newHead->x == food.x && newHead->y == food.y) { isEaten = true; score++; food.x = rand() % WIDTH + 1; food.y = rand() % HEIGHT + 1; } // 判断是否撞墙或撞到自己 SnakeBody* p = snake.head->next; while (p != nullptr) { if (p->x == newHead->x && p->y == newHead->y) { gameState = OVER; return; } p = p->next; } if (newHead->x == 0 || newHead->x == WIDTH + 1 || newHead->y == 0 || newHead->y == HEIGHT + 1) { gameState = OVER; return; } // 更新蛇的尾部 if (!isEaten) { snake.tail = snake.tail->next; } else { snake.length++; isEaten = false; } } // 处理键盘输入 void HandleInput() { if (_kbhit()) { char c = _getch(); switch (c) { case 'W': case 'w': if (snake.direction != 'S') { snake.direction = 'W'; } break; case 'A': case 'a': if (snake.direction != 'D') { snake.direction = 'A'; } break; case 'S': case 's': if (snake.direction != 'W') { snake.direction = 'S'; } break; case 'D': case 'd': if (snake.direction != 'A') { snake.direction = 'D'; } break; case 'P': case 'p': gameState = PAUSED; break; case 'Q': case 'q': gameState = OVER; break; } } } // 游戏线程 DWORD WINAPI GameThread(LPVOID lpParam) { while (gameState == RUNNING) { UpdateSnake(); DrawMap(); Sleep(100); } return 0; } // 消息循环线程 DWORD WINAPI MessageLoopThread(LPVOID lpParam) { MSG msg; while (GetMessage(&msg, hWnd, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } // 主函数 int main() { InitGame(); DrawMap(); // 创建游戏线程和消息循环线程 CreateThread(NULL, 0, GameThread, NULL, 0, &dwThreadId); CreateThread(NULL, 0, MessageLoopThread, NULL, 0, &dwThreadId); // 消息循环 while (gameState != OVER) { HandleInput(); Sleep(10); } // 结束游戏 system("cls"); cout << "Game over!" << endl; cout << "Your score: " << score << endl; system("pause"); return 0; } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值