Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include "SDL/SDL_ttf.h"
- #include "stdlib.h"
- #define SCREENWIDTH 640
- SDL_Surface *Screen = NULL;
- SDL_Surface *Head = NULL;
- SDL_Surface *Segment = NULL;
- SDL_Surface *Food = NULL;
- SDL_Color White = {255, 255, 255};
- TTF_Font *Font = NULL;
- SDL_Event Event;
- short unsigned int SegmentArray[3200];
- int Segments = 0;
- int Initiate();
- int Inbetween(int Min, int Value, int Max);
- int CheckSnakeCollision(int X, int Y);
- void RenderToWindow(int X, int Y, SDL_Surface *Char);
- int main(int argc, char * argv[])
- {
- if (Initiate() != 1) return 1;
- int XDir = 1;
- int YDir = 0;
- int HeadX = 20;
- int HeadY = 20;
- int FoodX = 10;
- int FoodY = 10;
- int SnakeIncrementer = 0;
- SDL_Rect Rect;
- Rect.x = 0;
- Rect.y = 0;
- Rect.w = SCREENWIDTH;
- Rect.h = SCREENWIDTH;
- while (!CheckSnakeCollision(HeadX, HeadY) && Inbetween(0, HeadX, 39) && Inbetween(0, HeadY, 39))
- {
- HeadX += XDir;
- HeadY += YDir;
- if (HeadX == FoodX && HeadY == FoodY)
- {
- SegmentArray[Segments * 2] = HeadX - XDir;
- SegmentArray[Segments * 2 + 1] = HeadY - YDir;
- Segments++;
- do
- {
- FoodX = rand() % 39 + 1;
- FoodY = rand() % 39 + 1;
- } while(CheckSnakeCollision(FoodX, FoodY));
- }
- if (Segments != 0)
- {
- SegmentArray[SnakeIncrementer] = HeadX - XDir;
- SegmentArray[SnakeIncrementer + 1] = HeadY - YDir;
- SnakeIncrementer += 2;
- if (SnakeIncrementer == Segments * 2) SnakeIncrementer = 0;
- }
- while(SDL_PollEvent(&Event))
- {
- if (Event.type == SDL_KEYDOWN)
- {
- if (Event.key.keysym.sym == SDLK_LEFT)
- {
- YDir = 0;
- XDir = -1;
- }
- else if (Event.key.keysym.sym == SDLK_RIGHT)
- {
- YDir = 0;
- XDir = 1;
- }
- if (Event.key.keysym.sym == SDLK_UP)
- {
- XDir = 0;
- YDir = -1;
- }
- else if (Event.key.keysym.sym == SDLK_DOWN)
- {
- XDir = 0;
- YDir = 1;
- }
- }
- else if (Event.type == SDL_QUIT) HeadX = 100;
- }
- SDL_FillRect(Screen, &Rect, 0x000000);
- RenderToWindow(HeadX, HeadY, Head);
- RenderToWindow(FoodX, FoodY, Food);
- int i;
- for (i = 0; i < Segments * 2; i += 2)
- RenderToWindow(SegmentArray[i], SegmentArray[i + 1], Segment);
- SDL_Flip(Screen);
- SDL_Delay(100);
- }
- printf("Score: %d\n", Segments);
- return 0;
- }
- int Inbetween(int Min, int Value, int Max)
- {
- return (Value > Min && Value < Max);
- }
- int Initiate()
- {
- if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
- {
- printf("Couldn't initialise SDL: SDL_GetError() returns %s\n", SDL_GetError());
- return 0;
- }
- Screen = SDL_SetVideoMode(SCREENWIDTH, SCREENWIDTH, 32, SDL_SWSURFACE);
- if (Screen == NULL) return 0;
- if (TTF_Init() == -1) return 0;
- SDL_WM_SetCaption("Snake", NULL);
- Font = TTF_OpenFont("DigitalDream.ttf", 16);
- if (Font == NULL) return 0;
- Head = TTF_RenderText_Solid(Font, "H", White);
- Segment = TTF_RenderText_Solid(Font, "S", White);
- Food = TTF_RenderText_Solid(Font, "F", White);
- return 1;
- }
- int CheckSnakeCollision(int X, int Y)
- {
- int i;
- for (i = 0; i < Segments * 2; i += 2)
- if (X == SegmentArray[i] && Y == SegmentArray[i + 1]) return 1;
- return 0;
- }
- void RenderToWindow(int X, int Y, SDL_Surface *Char)
- {
- SDL_Rect Rect;
- Rect.x = X * 16;
- Rect.y = Y * 16;
- Rect.w = Char->w;
- Rect.h = Char->h;
- SDL_BlitSurface(Char, NULL, Screen, &Rect);
- }
- /* Compiled using GCC
- abdul@computer [~/Programming/Snake]:
- make
- gcc -I /usr/include/SDL -o Snake main.c -Wall -g -lSDL -lSDL_ttf
- abdul@computer [~/Programming/Snake]:
- ./Snake
- Score: 8
- */
Advertisement
Add Comment
Please, Sign In to add comment