📄 snake.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <unistd.h>#include <SDL/SDL.h>#define SNAKE_MAX_SIZE 200#define SNAKE_HEAD 20#define SNAKE_SPEED 60#define SCREEN_X 380#define SCREEN_Y 380#define xmacro_opposite(x) \ ((x) == SDLK_UP ? SDLK_DOWN : \ (x) == SDLK_DOWN ? SDLK_UP : \ (x) == SDLK_RIGHT ? SDLK_LEFT : \ (x) == SDLK_LEFT ? SDLK_RIGHT : \ SDLK_UNKNOWN)void put_square(SDL_Surface *, SDL_Rect, Uint8);struct Snake { SDL_Rect body[SNAKE_MAX_SIZE]; SDL_Rect position; size_t size; size_t startsize; Uint8 colour; unsigned direction;};SDL_Rect newmouse(struct Snake *, SDL_Surface *);SDL_Rect pop(struct Snake *);void push(struct Snake *);size_t len;int main(int argc, char **argv) { SDL_Surface *screen; SDL_Event event; SDL_Rect mouse; struct Snake snake; size_t counter; unsigned char mousecolours[] = { 10, 40, 50, 67, 182, 133, 242 }; int walls = 0; /* 0 = no teleport 1 = teleport */ if(SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "%s\n", SDL_GetError()); return -1; } if(atexit(SDL_Quit) == -1) { perror("atexit"); SDL_Quit(); return -1; } screen = SDL_SetVideoMode(SCREEN_X, SCREEN_Y, 8, SDL_SWSURFACE);//Implementamos la pantalla... if(screen == NULL) { fprintf(stderr, "%s\n", SDL_GetError()); return -1; } if(argc > 1) { //Evalua el Nivel 0 (Muere al chocar en las paredes) o 1 (Se transporta de un la a otro)... if(argv[1][0] == '0') walls = 0; else walls = 1; } srand(time(NULL)); SDL_WM_SetCaption("Snake Parte 1", NULL); memset(screen->pixels, 0, SCREEN_X * SCREEN_Y); memset(&snake, 0, sizeof snake); snake.colour = 253; snake.direction = SDLK_RIGHT; snake.startsize = 1; //aqui pone el pixel en la pantalla. for(counter = 0; counter < snake.startsize; counter++) push(&snake); put_square(screen, snake.position, snake.colour); mouse = newmouse(&snake, screen); put_square(screen, mouse, mousecolours[rand() % sizeof mousecolours]); SDL_Flip(screen); for(counter = 0; 1; counter++) { /* if(SDL_PollEvent(&event) == 0) continue; if(event.type != SDL_KEYDOWN) continue; */ if(SDL_PollEvent(&event)) switch(event.key.keysym.sym) { case SDLK_SPACE: push(&snake); break; case SDLK_BACKSPACE: pop(&snake); break; case SDLK_ESCAPE: SDL_FreeSurface(screen); return 0; break; case SDLK_w: case SDLK_UP: if(snake.direction != xmacro_opposite(SDLK_UP)) snake.direction = SDLK_UP; break; case SDLK_a: case SDLK_LEFT: if(snake.direction != xmacro_opposite(SDLK_LEFT)) snake.direction = SDLK_LEFT; break; case SDLK_s: case SDLK_DOWN: if(snake.direction != xmacro_opposite(SDLK_DOWN)) snake.direction = SDLK_DOWN; break; case SDLK_d: case SDLK_RIGHT: if(snake.direction != xmacro_opposite(SDLK_RIGHT)) snake.direction = SDLK_RIGHT; break; default: break; }#if 0 if(counter != 10) { SDL_Delay(SNAKE_SPEED); continue; } counter = 0;#endif if(snake.direction == SDLK_UP) { if(snake.position.y == 0) { if(walls == 0) { SDL_FreeSurface(screen); printf("You lost! points: %zu\n", snake.size - snake.startsize); return 0; } else snake.position.y = screen->h; } snake.position.y -= SNAKE_HEAD; } else if(snake.direction == SDLK_DOWN) { if(snake.position.y == screen->h - SNAKE_HEAD) { if(walls == 0) { SDL_FreeSurface(screen); printf("You lost! points: %zu\n", snake.size - snake.startsize); return 0; } else snake.position.y = -SNAKE_HEAD; } snake.position.y += SNAKE_HEAD; } else if(snake.direction == SDLK_RIGHT) { if(snake.position.x == screen->w - SNAKE_HEAD) { if(walls == 0) { SDL_FreeSurface(screen); printf("You lost! points: %zu\n", snake.size - snake.startsize); return 0; } else snake.position.x = -SNAKE_HEAD; } snake.position.x += SNAKE_HEAD; } else if(snake.direction == SDLK_LEFT) { if(snake.position.x == 0) { if(walls == 0) { SDL_FreeSurface(screen); printf("You lost! points: %zu\n", snake.size - snake.startsize); return 0; } else snake.position.x = screen->w; } snake.position.x -= SNAKE_HEAD; } for(counter = 0; counter < snake.size; counter++) if(snake.position.x == snake.body[counter].x && snake.position.y == snake.body[counter].y) { SDL_FreeSurface(screen); printf("You lost! points: %zu\n", snake.size - snake.startsize); return 0; } put_square(screen, snake.position, snake.colour); if(snake.position.x == mouse.x && snake.position.y == mouse.y) { mouse = newmouse(&snake, screen); put_square(screen, mouse, mousecolours[rand() % sizeof mousecolours]); } else put_square(screen, pop(&snake), 0); push(&snake); SDL_Flip(screen); SDL_Delay(SNAKE_SPEED); } SDL_FreeSurface(screen); return 0;}SDL_Rect newmouse(struct Snake *ptr, SDL_Surface *screen) { SDL_Rect ret = { 0, 0, 0, 0}; size_t n; int x, y; int ok = 1; x = screen->w / SNAKE_HEAD; y = screen->h / SNAKE_HEAD; while(ok != 0) { ok = 0; ret.x = (rand() % x) * SNAKE_HEAD; for(n = 0; n < ptr->size; n++) if(ret.x == ptr->body[n].x) { ok = 1; break; } if(ok == 0) { ret.y = (rand() % y) * SNAKE_HEAD; for(n = 0; n < ptr->size; n++) if(ret.y == ptr->body[n].y) { ok = 1; break; } } } return ret;}void push(struct Snake *ptr) { if(ptr->size != SNAKE_MAX_SIZE) { ptr->body[ptr->size] = ptr->position; ptr->size++; }}SDL_Rect pop(struct Snake *ptr) { SDL_Rect ret = { 0, 0, 0, 0 }; if(ptr->size != 0) { ret = ptr->body[0]; if(ptr->size-- > 1) memmove(ptr->body, &ptr->body[1], ptr->size * sizeof ptr->body[0]); } return ret;}void put_square(SDL_Surface *screen, SDL_Rect foo, Uint8 color) { int i, n; Uint8 *p = screen->pixels; p += foo.x + foo.y * screen->w; for(n = 0; n < SNAKE_HEAD; n++) { for(i = 0; i < SNAKE_HEAD; i++) *p++ = color; p += screen->w - SNAKE_HEAD; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -