C语言贪吃蛇使用线程定时器实现源代码
时间: 2023-07-30 18:04:41 浏览: 106
以下是使用线程定时器实现的 C语言贪吃蛇源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define WIDTH 50
#define HEIGHT 20
#define SNAKE_LEN 5
#define FOOD_SCORE 10
#define SPEED 100
int score = 0;
int rx = 0;
int ry = 0;
int over = 0;
int map[HEIGHT][WIDTH] = {0};
int snake[SNAKE_LEN][2] = {0};
int dir = 1; // 0:up 1:right 2:down 3:left
void init_map();
void init_snake();
void init_food();
void display();
void move();
void update_snake(int x, int y);
void update_food();
void game_over();
void set_timer();
void init_map() {
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
map[i][j] = -1;
}
else {
map[i][j] = 0;
}
}
}
}
void init_snake() {
int i;
for (i = 0; i < SNAKE_LEN; i++) {
snake[i][0] = 5;
snake[i][1] = 5 + i;
map[snake[i][0]][snake[i][1]] = i == SNAKE_LEN - 1 ? 1 : 2;
}
}
void init_food() {
srand((unsigned)time(NULL));
rx = rand() % (HEIGHT - 2) + 1;
ry = rand() % (WIDTH - 2) + 1;
if (map[rx][ry] != 0) {
init_food();
}
else {
map[rx][ry] = -2;
}
}
void display() {
int i, j;
system("cls");
printf("Score: %d\n", score);
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
switch (map[i][j]) {
case 0:
printf(" ");
break;
case -1:
printf("#");
break;
case -2:
printf("*");
break;
default:
printf("o");
break;
}
}
printf("\n");
}
}
void move() {
int i, j;
int x = snake[SNAKE_LEN - 1][0];
int y = snake[SNAKE_LEN - 1][1];
switch (dir) {
case 0:
x--;
break;
case 1:
y++;
break;
case 2:
x++;
break;
case 3:
y--;
break;
}
if (map[x][y] == -1 || map[x][y] >= 1) {
game_over();
return;
}
update_snake(x, y);
if (map[x][y] == -2) {
score += FOOD_SCORE;
update_food();
}
else {
map[snake[0][0]][snake[0][1]] = 0;
for (i = 0; i < SNAKE_LEN - 1; i++) {
snake[i][0] = snake[i + 1][0];
snake[i][1] = snake[i + 1][1];
}
snake[SNAKE_LEN - 1][0] = x;
snake[SNAKE_LEN - 1][1] = y;
map[x][y] = SNAKE_LEN;
}
}
void update_snake(int x, int y) {
int i;
map[snake[0][0]][snake[0][1]] = 0;
for (i = 0; i < SNAKE_LEN - 1; i++) {
snake[i][0] = snake[i + 1][0];
snake[i][1] = snake[i + 1][1];
}
snake[SNAKE_LEN - 1][0] = x;
snake[SNAKE_LEN - 1][1] = y;
map[x][y] = SNAKE_LEN;
}
void update_food() {
int i, j;
rx = rand() % (HEIGHT - 2) + 1;
ry = rand() % (WIDTH - 2) + 1;
if (map[rx][ry] != 0) {
update_food();
}
else {
map[rx][ry] = -2;
}
}
void game_over() {
over = 1;
printf("Game Over!\n");
}
void set_timer() {
time_t t;
t = time(NULL);
while (1) {
if (over == 1) {
break;
}
if (time(NULL) - t >= SPEED / 1000) {
move();
display();
t = time(NULL);
}
}
}
int main() {
init_map();
init_snake();
init_food();
display();
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
liDueTime.QuadPart = 0;
hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(hTimer, &liDueTime, SPEED, NULL, NULL, 0);
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)set_timer, NULL, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
return 0;
}
```
该代码使用了线程定时器来控制蛇的移动速度,通过 CreateWaitableTimer 和 SetWaitableTimer 函数来创建和设置定时器,使用 CreateThread 函数来创建一个新线程并将 set_timer 函数作为入口点,通过 WaitForSingleObject 函数来等待线程执行完毕。
该代码实现了贪吃蛇的基本功能,包括蛇的移动、食物的随机生成、分数的计算、游戏结束等,可以在 Windows 环境下编译运行。
阅读全文