#pragma execution_character_set("utf-8")
#include <math.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_messagebox.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define rectsize 20
#define rectinit 300
#define window_width 1080
#define window_height 720
SDL_Rect* rect = NULL;
SDL_Rect zsjc = {0,0,0,0};
SDL_Rect apple = { 40,60,20,20};
int nowlength = 0;
int newlength = 0;
int dx = rectsize;
int dy = 0;
bool quit = true;
void message() // 显示窗口
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "游戏结束","你撞死了", NULL);
}
void snake(int length)
{
rect = (SDL_Rect*)malloc(length * sizeof(SDL_Rect));
for (int i = 0;i < length;i++)
{
rect[i].x = rectinit + i * rectsize;
rect[i].y = rectinit;
rect[i].w = rectsize;
rect[i].h = rectsize;
}
nowlength = length;
}
void appleshow()
{
apple.x = 40 + (rand() % (window_width - 40) / 20) * 20;
apple.y = 40 + (rand() % (window_height - 40) / 20) * 20;
}
void pzjc() // 碰撞检测
{
if (rect[nowlength - 1].x > 1080 || rect[nowlength -1].x < 0 || rect[nowlength - 1].y > window_height || rect[nowlength - 1].y < 0)
{
quit = false;
message();
}
else if (rect[nowlength - 1].x == apple.x && rect[nowlength - 1].y == apple.y)
{
appleshow();
newlength = nowlength + 1;
rect = (SDL_Rect*)realloc(rect,newlength * sizeof(SDL_Rect));
rect[newlength - 1] = rect[newlength - 2];
nowlength = newlength;
}
else
{
for (int i = 0; i < nowlength - 2; i++)
{
zsjc = rect[i];
if (rect[nowlength - 1].x == zsjc.x && rect[nowlength - 1].y == zsjc.y)
{
quit = false;
message();
}
}
}
}
void update() // 矩形更新
{
for (int i = 0; i < nowlength -1; i++)
{
rect[i] = rect[i + 1];
}
rect[nowlength - 1].x += dx;
rect[nowlength - 1].y += dy;
}
void control() // 方向控制
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = false;
}
else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_UP:
dx = 0;
dy = -rectsize;
break;
case SDLK_DOWN:
dx = 0;
dy = rectsize;
break;
case SDLK_LEFT:
dx = -rectsize;
dy = 0;
break;
case SDLK_RIGHT:
dx = rectsize;
dy = 0;
break;
}
}
}
}
int main(int argc, char** argv)
{
srand(time(NULL));
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("贪吃蛇", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width, window_height, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
snake(3);
appleshow();
while (quit) {
pzjc();
control();
update();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 1);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 1);
for (int i = 0; i < nowlength; i++) {
SDL_RenderFillRect(renderer, &rect[i]);
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 1);
SDL_RenderFillRect(renderer, &apple);
for (int i = 0; i <= window_height; i += 20)
{
SDL_RenderDrawLine(renderer, 0, i, 1080, i);
for (int i = 0; i <= window_width; i += 20)
{
SDL_RenderDrawLine(renderer, i, 0, i, 720);
}
}
SDL_RenderPresent(renderer);
SDL_Delay(200);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

783

被折叠的 条评论
为什么被折叠?



