当前位置:   article > 正文

用C语言编写贪吃蛇小游戏_编写一个贪吃蛇游戏 满足:1.键盘wsad和up down left right控制上下左右 2.吃

编写一个贪吃蛇游戏 满足:1.键盘wsad和up down left right控制上下左右 2.吃到食

实现功能

蛇最开始三节,向右移动。用户可以通过按上下左右来控制蛇的移动,食物随机产生,蛇吃到食物后蛇的身体会变长。蛇撞墙或者撞到自己身体后,游戏结束。
在这里插入图片描述

怎么实现

要实现一个贪吃蛇小游戏,首先要想清楚游戏里有什么,怎样实现功能。

很明显游戏中只有两样东西,蛇和食物。
所以要建立蛇和食物信息,然后将蛇和食物进行初始化,在将蛇和食物画出来。
实现的功能有:
 1. 蛇的移动
 2. 按键控制蛇的移动
 3. 食物的产生
 4. 蛇吃食物后蛇身体变长
 5. 游戏的结束
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

用结构体建立蛇和食物的信息

struct COOR{//位置,x,y坐标
	int x;
	int y;
};

struct SNAKE{//蛇的基础信息
	int size;//节数
	int speed;//运动速度
	char dir;//运动方向
	struct COOR xy[MAX];//位置
}snakes;

struct FOOD{//食物信息
	struct COOR fooddir;//食物位置
	int flag;//判断食物是否被吃掉,1未被吃掉,0被吃掉
}food;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

实现功能的函数:
蛇:

void snakeInit(){//初始化蛇的信息
void drawSnake(){//画蛇
void moveSnake(){//蛇的移动
void coorSnake(){//按键控制蛇的运动方向
  • 1
  • 2
  • 3
  • 4

食物:

void initFood(){//初始化食物的信息
void drawFood(){//画食物
  • 1
  • 2

其它:

int gameOver(){//游戏结束情况
void gameInit(){//初始化窗口范围
  • 1
  • 2

代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<graphics.h>
#include<conio.h>
#include<Windows.h>

#define MAX 200
HWND hwnd = NULL;

enum DIR{//枚举移动方向
	UP,
	DOWN,
	LEFT,
	RIGHT,
};

struct COOR{//位置,x,y坐标
	int x;
	int y;
};

struct SNAKE{//蛇的基础信息
	int size;//节数
	int speed;//运动速度
	char dir;//运动方向
	struct COOR xy[MAX];//位置
}snakes;

struct FOOD{//食物信息
	struct COOR fooddir;//食物位置
	int flag;//判断食物是否被吃掉,1未被吃掉,0被吃掉
}food;

void snakeInit(){//初始化蛇的信息
	snakes.size = 3;//开始节数
	snakes.dir = RIGHT;//开始运动方向
	snakes.speed = 10;
	int i = 0;
	for (; i < snakes.size; i++){//每一节书的位置,注意将第一节作为头
		snakes.xy[i].x = 40 - 10 * i;
		snakes.xy[i].y = 10;
	}

}

void drawSnake(){//画蛇
	int i = 0;
	for (; i < snakes.size; i++){
	setlinecolor(BLACK);//画线的颜色
	setfillcolor(RED);//填充色
	//fillrectangle(snakes.xy[i].x, snakes.xy[i].y, snakes.xy[i].x + 10, snakes.xy[i].y+10);//矩形
	fillcircle(snakes.xy[i].x, snakes.xy[i].y, 5);//圆形
	}


}

void moveSnake(){//蛇的移动
	//snakes.xy[0].x++;

	int i = 0;
	for (i = snakes.size-1; i >0; i--){//蛇身跟着舌头运动
		snakes.xy[i].x = snakes.xy[i-1].x;
		snakes.xy[i].y = snakes.xy[i-1].y;
	}
	switch (snakes.dir){
	case UP:
		snakes.xy[0].y-=snakes.speed;
		break;
	case DOWN:
			snakes.xy[0].y+=snakes.speed;
		break;
	case LEFT:
			snakes.xy[0].x-=snakes.speed;
		break;
	case RIGHT:
			snakes.xy[0].x+=snakes.speed;
		break;
	default:
		break;
	}


}

void coorSnake(){//按键控制蛇的运动方向
	if (_kbhit()){ //等待获取按键
		char c = _getch();//获得按键
		switch (c){
		case 72:
		case'w':
			if (snakes.dir != DOWN){
				snakes.dir = UP;
			}
			break;
		case 80:
		case's':
			if (snakes.dir != UP){
				snakes.dir = DOWN;
			}
			break;
		case 75:
		case'a':
			if (snakes.dir != RIGHT){
				snakes.dir = LEFT;
			}
			break;
		case 77:
		case'd':
			if (snakes.dir != LEFT){
				snakes.dir = RIGHT;
			}
			break;
		default:
			break;
		}
	}
}
void initFood(){//初始化食物的信息
	food.flag = 1;
	while (1){
START:
		food.fooddir.x = rand() % 63 * 10;//食物位置随机
		food.fooddir.y = rand() % 47 * 10;
		for (int i = 0; i < snakes.size; i++){//防止食物生成在蛇身上。
			if (food.fooddir.x == snakes.xy[i].x&&food.fooddir.y == snakes.xy[i].y){
				goto START;
			}
			else{
				break;
			}
		}
		break;
	}
}

void drawFood(){//画食物
	//food.fooddir.x = 100;
	//food.fooddir.y = 200;
	setlinecolor(BLACK);
	setfillcolor(RED);
	fillcircle(food.fooddir.x, food.fooddir.y, 5);

}
void eatFood(){//蛇吃食物
	if (snakes.xy[0].x - food.fooddir.x <= 5 && snakes.xy[0].y - food.fooddir.y <= 5 \
		&& food.fooddir.x - snakes.xy[0].x <= 5 && food.fooddir.y - snakes.xy[0].y <= 5 && food.flag == 1){
		food.flag = 0;
		snakes.size++;
	}

}

int gameOver(){//游戏结束情况
	if (snakes.xy[0].x < 5 || snakes.xy[0].y <= 0 || snakes.xy[0].x > 635 || snakes.xy[0].y > 478){
		MessageBox(hwnd, "GAME OVER!","你撞墙了!", MB_OK);
		return 1;
	}
	for (int i = 1; i < snakes.size; i++){
		if (snakes.xy[0].x == snakes.xy[i].x&&snakes.xy[0].y == snakes.xy[i].y){
			MessageBox(hwnd, "GAME OVER!", "你撞了自己",MB_OK);
			return 1;
		}
	}
	return 0;
}



void gameInit(){
	hwnd=initgraph(640, 480);//设置窗口大小
	setbkcolor(GREEN);//设置窗口颜色
}


int main(){
	srand((unsigned long)time(NULL));//生成随机数
	gameInit();
	cleardevice();//刷新窗口
	snakeInit();
	initFood();
	while (1){
		cleardevice();
		if (food.flag == 0){
			initFood();
		}
		drawFood();
		drawSnake();
		coorSnake();
		eatFood();
		moveSnake();
		//eatFood();
		if (gameOver()){
			break;
		}
		//stopGame();
		Sleep(100);
	}
	getchar();//防止闪屏
	closegraph();
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/750215
推荐阅读
相关标签
  

闽ICP备14008679号