#include <stdio.h>
#include <SDL.h>
#include <stdlib.h>
#include <SDL_image.h>
#undef main
// 定义屏幕宽度和高度及蛇的块大小
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SNAKE_BLOCK 20
// SDL窗口和渲染器全局变量
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
// 定义一个点结构体,用于表示蛇的身体部分和食物的位置
typedef struct {
int x;
int y;
} Point;
// 定义一个蛇结构体,包含蛇的身体部分数组、长度和方向
typedef struct {
Point* body; // 蛇的身体数组
int length; // 蛇的长度
int direction; // 蛇的方向(0=右, 1=下, 2=左, 3=上)
} Snake;
// 游戏中的蛇
Snake* gSnake;
// 食物的位置
Point gFood;
// 初始化蛇的函数
void init_snake(Snake* snake) {
snake->length = 3; // 蛇初始长度为3
snake->direction = 0; // 初始方向向右
snake->body = (Point*)malloc(snake->length * sizeof(Point)); // 分配内存给蛇的身体数组
for (int i = 0; i < snake->length; i++) {
snake->body[i].x = 200 - i * SNAKE_BLOCK; // 设置蛇的初始位置
snake->body[i].y = 200;
}
}
// 初始化食物位置的函数
void init_food(Point* food) {
food->x = rand() % (SCREEN_WIDTH / SNAKE_BLOCK) * SNAKE_BLOCK; // 随机生成食物的X坐标
food->y = rand() % (SCREEN_HEIGHT / SNAKE_BLOCK) * SNAKE_BLOCK; // 随机生成食物的Y坐标
}
// 绘制蛇的函数
void draw_snake(Snake* snake, SDL_Renderer* renderer) {
// 加载蛇的头、身体和尾巴图像
SDL_Texture* snakeHeadTexture = IMG_LoadTexture(renderer, "..//image//head2.png");
SDL_Texture* snakeBodyTexture = IMG_LoadTexture(renderer, "..//image//body2.png");
SDL_Texture* snakeTailTexture = IMG_LoadTexture(renderer, "..//image//tail2.png");
if (snakeHeadTexture == NULL || snakeBodyTexture == NULL || snakeTailTexture == NULL) {
printf("无法加载图像: %s\n", SDL_GetError()); // 错误处理
return;
}
SDL_Rect block;
for (int i = 0; i < snake->length; i++) {
block.x = snake->body[i].x;
block.y = snake->body[i].y;
block.w = SNAKE_BLOCK;
block.h = SNAKE_BLOCK;
// 绘制蛇的头部,并根据方向旋转图像
if (i == 0) {
SDL_RenderCopyEx(renderer, snakeHeadTexture, NULL, &block,
snake->direction * 90, NULL, SDL_FLIP_NONE);
}
// 绘制蛇的尾部,并根据前后方向调整角度
else if (i == snake->length - 1) {
int prevDirection = (snake->direction + 2) % 4; // 上一个方向
int angle = prevDirection * 90;
// 根据前后的方向调整角度
if (prevDirection == 0 && snake->direction == 3) { // 右转上
angle = 270;
}
else if (prevDirection == 3 && snake->direction == 0) { // 上转右
angle = 90;
}
else if (prevDirection == 1 && snake->direction == 2) { // 下转左
angle = 270;
}
else if (prevDirection == 2 && snake->direction == 1) { // 左转下
angle = 90;
}
SDL_RenderCopyEx(renderer, snakeTailTexture, NULL, &block, angle, NULL, SDL_FLIP_NONE);
}
// 绘制蛇的身体部分
else {
SDL_RenderCopy(renderer, snakeBodyTexture, NULL, &block);
}
}
// 释放图像纹理
SDL_DestroyTexture(snakeHeadTexture);
SDL_DestroyTexture(snakeBodyTexture);
SDL_DestroyTexture(snakeTailTexture);
}
// 绘制食物的函数
void draw_food(Point* food, SDL_Renderer* renderer) {
SDL_Rect block;
block.x = food->x;
block.y = food->y;
block.w = SNAKE_BLOCK;
block.h = SNAKE_BLOCK;
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // 设置颜色为红色
SDL_RenderFillRect(renderer, &block); // 在屏幕上绘制食物
}
// 移动蛇的函数
void move_snake(Snake* snake) {
Point newHead = snake->body[0]; // 获取蛇头的位置
switch (snake->direction) { // 根据方向更新蛇头的位置
case 0: newHead.x += SNAKE_BLOCK; break;
case 1: newHead.y += SNAKE_BLOCK; break;
case 2: newHead.x -= SNAKE_BLOCK; break;
case 3: newHead.y -= SNAKE_BLOCK; break;
}
// 将蛇的头部移到新的位置
for (int i = snake->length - 1; i > 0; i--) {
snake->body[i] = snake->body[i - 1];
}
snake->body[0] = newHead;
}
// 检查蛇是否吃到食物的函数
int check_collision(Snake* snake, Point* food) {
return (snake->body[0].x == food->x && snake->body[0].y == food->y); // 如果蛇头的位置等于食物的位置,则返回真
}
// 更新游戏状态的函数
void update_game(Snake* snake, Point* food) {
move_snake(snake); // 移动蛇
if (check_collision(snake, food)) { // 如果蛇吃到了食物
snake->length++; // 增加蛇的长度
snake->body = (Point*)realloc(snake->body, snake->length * sizeof(Point)); // 重新分配内存以适应新的长度
init_food(food); // 重新初始化食物的位置
}
}
// 主函数
int main(int argc, char* args[]) {
bool running = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("无法初始化SDL: %s\n", SDL_GetError()); // 错误处理
return -1;
}
gWindow = SDL_CreateWindow("Snake Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
printf("无法创建窗口: %s\n", SDL_GetError()); // 错误处理
return -1;
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL) {
printf("无法创建渲染器: %s\n", SDL_GetError()); // 错误处理
return -1;
}
gSnake = (Snake*)malloc(sizeof(Snake));
init_snake(gSnake);
init_food(&gFood);
SDL_Event e;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_UP:
if (gSnake->direction != 1) gSnake->direction = 3; // 避免反向移动
break;
case SDLK_DOWN:
if (gSnake->direction != 3) gSnake->direction = 1; // 避免反向移动
break;
case SDLK_LEFT:
if (gSnake->direction != 0) gSnake->direction = 2; // 避免反向移动
break;
case SDLK_RIGHT:
if (gSnake->direction != 2) gSnake->direction = 0; // 避免反向移动
break;
}
}
}
// 加载并绘制背景图像
SDL_Texture* bgTexture = IMG_LoadTexture(gRenderer, "..//image//background.bmp");
if (bgTexture == NULL) {
printf("无法加载图像: %s\n", SDL_GetError()); // 错误处理
return -1;
}
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL);
SDL_DestroyTexture(bgTexture); // 渲染完成后销毁纹理
draw_snake(gSnake, gRenderer);
draw_food(&gFood, gRenderer);
update_game(gSnake, &gFood);
SDL_RenderPresent(gRenderer); // 显示新帧
SDL_Delay(100); // 延迟以控制游戏速度
}
free(gSnake->body);
free(gSnake);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
IMG_Quit(); // 退出SDL_image
SDL_Quit();
return 0;
}