贪吃蛇攻击功能C语言代码
时间: 2023-08-18 14:42:29 浏览: 124
color-snake.zip_snake_贪吃蛇
以下是一个简单的贪吃蛇攻击功能的 C 语言代码示例,其中使用了图形界面库 `graphics.h` :
```c
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
int score = 0;
int snake[200][2];
int len = 3;
int food_x, food_y;
int direction = RIGHT;
int attack_range = 50; // 攻击范围半径
void draw_map();
void generate_food();
void draw_snake();
void move();
void check_collision();
int distance(int x1, int y1, int x2, int y2); // 计算两点间距离
void attack();
int main() {
int gd=0, gm;
initgraph(&gd, &gm, "");
draw_map();
generate_food();
draw_snake();
while (1) {
if (kbhit()) {
int key = getch();
if (key == 27) { // ESC
break;
}
switch (key) {
case LEFT:
if (direction != RIGHT)
direction = LEFT;
break;
case RIGHT:
if (direction != LEFT)
direction = RIGHT;
break;
case UP:
if (direction != DOWN)
direction = UP;
break;
case DOWN:
if (direction != UP)
direction = DOWN;
break;
case ' ':
attack(); // 空格键触发攻击
break;
}
}
move();
check_collision();
}
closegraph();
return 0;
}
void draw_map() {
setfillstyle(SOLID_FILL, GREEN);
bar(0, 0, 640, 480);
}
void generate_food() {
srand((unsigned) time(NULL));
food_x = rand() % (640 - 10);
food_y = rand() % (480 - 10);
setfillstyle(SOLID_FILL, RED);
bar(food_x, food_y, food_x + 10, food_y + 10);
}
void draw_snake() {
int i;
for (i = 0; i < len; i++) {
snake[i][0] = 320 - i*10;
snake[i][1] = 240;
setfillstyle(SOLID_FILL, WHITE);
bar(snake[i][0], snake[i][1], snake[i][0] + 10, snake[i][1] + 10);
}
}
void move() {
int i;
setfillstyle(SOLID_FILL, GREEN);
bar(snake[len-1][0], snake[len-1][1], snake[len-1][0] + 10, snake[len-1][1] + 10);
for (i = len-1; i > 0; i--) {
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
}
switch (direction) {
case LEFT:
snake[0][0] -= 10;
break;
case RIGHT:
snake[0][0] += 10;
break;
case UP:
snake[0][1] -= 10;
break;
case DOWN:
snake[0][1] += 10;
break;
}
setfillstyle(SOLID_FILL, WHITE);
bar(snake[0][0], snake[0][1], snake[0][0] + 10, snake[0][1] + 10);
}
void check_collision() {
if (snake[0][0] < 0 || snake[0][0] > 630 || snake[0][1] < 0 || snake[0][1] > 470) {
printf("Game over!\n");
exit(0);
}
int i;
for (i = 1; i < len; i++) {
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
printf("Game over!\n");
exit(0);
}
}
if (distance(snake[0][0], snake[0][1], food_x, food_y) < 10) {
len++;
score++;
generate_food();
}
}
int distance(int x1, int y1, int x2, int y2) {
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
void attack() {
int i;
for (i = 1; i < len; i++) {
if (distance(snake[0][0], snake[0][1], snake[i][0], snake[i][1]) < attack_range) {
setfillstyle(SOLID_FILL, YELLOW);
bar(snake[i][0], snake[i][1], snake[i][0] + 10, snake[i][1] + 10);
delay(100);
setfillstyle(SOLID_FILL, WHITE);
bar(snake[i][0], snake[i][1], snake[i][0] + 10, snake[i][1] + 10);
}
}
}
```
该代码实现了一个简单的贪吃蛇游戏,按下空格键可以触发攻击。攻击范围的半径为 50,攻击时敌人被标记为黄色方块,持续 100 毫秒,然后恢复为白色方块。该代码仅作为示例,具体实现方式可以根据需要进行调整和改进。
阅读全文