给我一个贪吃蛇c语言代码前进按数字8后退按数字2向左按数字4向右按数字6
以下是一个简单的贪吃蛇游戏的C语言代码,其中使用了Windows API实现游戏界面。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#define WIDTH 60 // 游戏界面宽度
#define HEIGHT 20 // 游戏界面高度
#define INIT_LENGTH 5 // 贪吃蛇初始长度
int score; // 得分
int direction; // 移动方向
int speed; // 移动速度
int snake[WIDTH * HEIGHT][2]; // 贪吃蛇的身体坐标
int food[2]; // 食物的坐标
void initGame(); // 初始化游戏界面和贪吃蛇
void drawGame(); // 绘制游戏界面和贪吃蛇
void moveSnake(); // 移动贪吃蛇
void generateFood(); // 随机生成食物
void gameOver(); // 游戏结束
int main() {
initGame(); // 初始化游戏
while (1) {
drawGame(); // 绘制游戏界面和贪吃蛇
moveSnake(); // 移动贪吃蛇
Sleep(speed); // 等待一段时间,控制移动速度
}
return 0;
}
void initGame() {
// 初始化游戏界面
system("mode con cols=60 lines=20"); // 设置控制台窗口大小
system("title 贪吃蛇"); // 设置控制台窗口标题
srand(time(NULL)); // 随机数种子
// 初始化贪吃蛇
direction = 6; // 初始向右移动
speed = 150; // 初始移动速度为150毫秒
score = 0; // 初始得分为0
for (int i = 0; i < INIT_LENGTH; i++) {
snake[i][0] = i + 1; // 贪吃蛇的x坐标
snake[i][1] = 1; // 贪吃蛇的y坐标
}
generateFood(); // 随机生成食物
}
void drawGame() {
system("cls"); // 清空屏幕
// 绘制游戏界面
for (int i = 0; i < WIDTH; i++) {
printf("#");
}
printf("\n");
for (int j = 1; j < HEIGHT - 1; j++) {
printf("#");
for (int i = 1; i < WIDTH - 1; i++) {
int flag = 0;
for (int k = 0; k < INIT_LENGTH; k++) {
if (snake[k][0] == i && snake[k][1] == j) {
printf("O"); // 绘制贪吃蛇的身体
flag = 1;
break;
}
}
if (!flag) {
if (food[0] == i && food[1] == j) {
printf("*"); // 绘制食物
}
else {
printf(" ");
}
}
}
printf("#\n");
}
for (int i = 0; i < WIDTH; i++) {
printf("#");
}
printf("\n");
// 绘制得分和速度
printf("Score: %d\tSpeed: %d\n", score, speed);
}
void moveSnake() {
// 移动贪吃蛇
int tail[2] = { snake[INIT_LENGTH - 1][0], snake[INIT_LENGTH - 1][1] }; // 记录尾巴的位置
for (int i = INIT_LENGTH - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
switch (direction) {
case 4: // 左移
snake[0][0]--;
break;
case 6: // 右移
snake[0][0]++;
break;
case 8: // 上移
snake[0][1]--;
break;
case 2: // 下移
snake[0][1]++;
break;
}
// 判断是否吃到食物
if (snake[0][0] == food[0] && snake[0][1] == food[1]) {
score++; // 得分加1
if (speed > 50) {
speed -= 5; // 移动速度加快
}
INIT_LENGTH++; // 贪吃蛇长度加1
snake[INIT_LENGTH - 1][0] = tail[0];
snake[INIT_LENGTH - 1][1] = tail[1];
generateFood(); // 随机生成食物
}
// 判断是否撞墙或撞到自己
if (snake[0][0] <= 0 || snake[0][0] >= WIDTH - 1 || snake[0][1] <= 0 || snake[0][1] >= HEIGHT - 1) {
gameOver(); // 游戏结束
}
for (int i = 1; i < INIT_LENGTH; i++) {
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
gameOver(); // 游戏结束
}
}
}
void generateFood() {
// 随机生成食物
int flag = 0;
while (!flag) {
food[0] = rand() % (WIDTH - 2) + 1; // 食物的x坐标
food[1] = rand() % (HEIGHT - 2) + 1; // 食物的y坐标
flag = 1;
for (int i = 0; i < INIT_LENGTH; i++) {
if (snake[i][0] == food[0] && snake[i][1] == food[1]) {
flag = 0;
break;
}
}
}
}
void gameOver() {
// 游戏结束
system("cls");
printf("Game Over!\n");
printf("Your score is %d.\n", score);
printf("Press any key to exit.\n");
getch();
exit(0);
}
// 监听键盘事件
void listenKeyboard() {
if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
if (direction != 6) {
direction = 4; // 左移
}
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
if (direction != 4) {
direction = 6; // 右移
}
}
if (GetAsyncKeyState(VK_UP) & 0x8000) {
if (direction != 2) {
direction = 8; // 上移
}
}
if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
if (direction != 8) {
direction = 2; // 下移
}
}
}
// 调用listenKeyboard函数,监听键盘事件
void callListenKeyboard() {
HANDLE hThread = GetCurrentThread();
DWORD oldThreadPriority = GetThreadPriority(hThread);
SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
while (1) {
listenKeyboard();
Sleep(10);
}
SetThreadPriority(hThread, oldThreadPriority);
}
原文地址: https://www.cveoy.top/t/topic/E6o 著作权归作者所有。请勿转载和采集!