使用工具
CLion 2022
前期准备
导入第三方库Easyx,详细教程 => 在 CLion、Dev-C++ 或 Code::Blocks 下面配置 EasyX(2023-7-23 更新) - CodeBus
运行效果
代码实现
/*
* 1. 定义蛇结构
* 2. 定义食物结构
* 3. 游戏初始化
* 4. 蛇行进
* 5. 按键控制(方向,暂停)
* 6. 吃食物
* 7. 死亡判定
*/
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#define LENGTH_MAX 100 // 蛇的最大长度
enum DIR{ // 方向
W, S, A, D
};
struct Snake{
// 蛇的长度,行进方向,行进速度,以及所在位置(坐标)
int length;
int direct;
int speed;
POINT coor[LENGTH_MAX];
}snake;
struct Food{
// 食物所在位置,大小,是否被吃
int x;
int y;
int r;
int grade;
bool flag;
}food;
void GameInit(){
// 设置游戏窗口大小
initgraph(648,475);
// 设置随机数种子
srand(GetTickCount());
// 设置蛇的参数
snake.length = 3;
snake.direct = D;
snake.speed = 10;
for(int i = 0; i < snake.length; i++){
snake.coor[i].x = 334-10*i;
snake.coor[i].y = 200;
}
// 设置食物的参数
food.x = rand()%638+10;
food.y = rand()%465+10;
food.r = rand()%8+8;
food.grade = 0;
food.flag = true;
}
void GameDraw(){
// 防闪烁
BeginBatchDraw();
// 设置背景颜色
setbkcolor(RGB(255,250,240));
cleardevice();
// 画蛇
setfillcolor(RGB(85,107,47));
for(int i = 0; i < snake.length; i++){
solidcircle(snake.coor[i].x,snake.coor[i].y,5);
}
// 画食物
setfillcolor(RGB(220,20,60));
if(food.flag){
solidcircle(food.x,food.y,food.r);
}
// 显示分数
char over[100];
setcolor(RGB(107,142,35));
sprintf(over, "Your score is: %d", food.grade);
outtextxy(10, 10, over);
EndBatchDraw();
}
void SnakeMove(){
// 从第二蛇身个开始都跟着上一个
for(int i = snake.length-1; i > 0; i--){
snake.coor[i] = snake.coor[i-1];
}
// 行进和穿墙设置
switch (snake.direct) {
case W:
snake.coor[0].y -= snake.speed;
if(snake.coor[0].y <= 0){
snake.coor[0].y = 475;
}
break;
case S:
snake.coor[0].y += snake.speed;
if(snake.coor[0].y >= 475){
snake.coor[0].y = 0;
}
break;
case A:
snake.coor[0].x -= snake.speed;
if(snake.coor[0].x <= 0){
snake.coor[0].x = 648;
}
break;
case D:
snake.coor[0].x += snake.speed;
if(snake.coor[0].x >= 648){
snake.coor[0].x = 0;
}
break;
}
}
void MoveControl(){
if(_kbhit()){
switch (_getch()) {
// 记得设置不能原地掉头
case 'w':
case 'W':
case 72:
if(snake.direct != S){
snake.direct = W;
}
break;
case 's':
case 'S':
case 80:
if(snake.direct != W){
snake.direct = S;
}
break;
case 'a':
case 'A':
case 75:
if(snake.direct != D){
snake.direct = A;
}
break;
case 'd':
case 'D':
case 77:
if(snake.direct != A){
snake.direct = D;
}
break;
// 设置按空格暂停
case ' ':
while(1){
if(_getch()==' '){
return;
}
break;
}
}
}
}
void EatFood(){
if(food.flag && abs(snake.coor[0].x-food.x) < food.r+1 && abs(snake.coor[0].y-food.y) < food.r+1 ){
food.flag = false;
food.grade++;
snake.length++;
}
// 食物刷新
if(!food.flag){
food.x = rand()%648;
food.y = rand()%475;
food.r = rand()%5+5;
food.flag = true;
}
}
bool snakeDie() {
for (int i = 1; i < snake.length; ++i) {
if (snake.coor[i].x == snake.coor[0].x && snake.coor[i].y == snake.coor[0].y) {
return true;
}
}
return false;
}
void ClearScore(){
food.grade = 0;
}
int main() {
int tmp;
do{
GameInit();
while(!snakeDie()){
GameDraw();
SnakeMove();
MoveControl();
EatFood();
Sleep(65); // 控制速度
}
// 按 0重新开始,按其他键退出游戏
char over[100] = "Game Over! Press '0' to restart, press other to quit.";
setcolor(RED);
outtextxy(180, 50, over);
setcolor(WHITE);
system("pause");
tmp = _getch();
}while(tmp != 0);
return 0;
}
标签:case,coor,food,direct,C语言,int,简易版,贪吃蛇,snake
From: https://blog.51cto.com/goku0623/8076677