Advertisement
Jeffdecod

game.c

Dec 23rd, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. /*
  2. ** game.c for  in /home/death/ETNA/prog_C/Xmas Project/Snake/jnih_s
  3. **
  4. ** Made by Sami Jnih
  5. ** Login   <[email protected]>
  6. **
  7. ** Started on  Tue Dec 24 23:09:00 2013 Sami Jnih
  8. ** Last update Wed Jan  1 02:32:05 2014 Sami Jnih
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15.  
  16. #include <SDL/SDL.h>
  17.  
  18. #include <SDL/SDL_image.h>
  19. #include </usr/local/lib/fmod.h>
  20.  
  21. #include "snake.h"
  22.  
  23. void    game(SDL_Event event, SDL_Surface *screen, SDL_Surface *snake, SDL_Surface *background, SDL_Surface *gameover_continuer, SDL_Surface *gameover_quitter)
  24. {
  25.   SDL_Rect positionBackground, positionSnake;
  26.  
  27.   int game, snake_xval, snake_yval, tempsPrecedent, tempsActuel, pos;
  28.   pos = 0;
  29.   game = 1;
  30.   tempsPrecedent = 0;
  31.   tempsActuel = 0;
  32.   snake_xval = 0;
  33.   snake_yval = 0;
  34.  
  35.   positionBackground.x = 0;
  36.   positionBackground.y = 0;
  37.  
  38.   positionSnake.x = 400 - 25;
  39.   positionSnake.y = 320 - 25;
  40.  
  41.   background = IMG_Load("images/background.jpg");
  42.   snake = IMG_Load("images/curseur.png");
  43.  
  44.   SDL_BlitSurface(background, NULL, screen, &positionBackground);
  45.  
  46.   while (game)
  47.     {
  48.       SDL_PollEvent(&event);
  49.       //printf("positionSnake.x : %d\npositionSnake.y : %d\n", positionSnake.x, positionSnake.y);
  50.       if (check_pos(game, positionSnake) == 0)
  51.     {
  52.       game = 0;
  53.       gameover_menu(gameover_continuer, gameover_quitter, screen, event, snake, background);
  54.     }
  55.       tempsActuel = SDL_GetTicks();
  56.       switch(event.type)
  57.         {
  58.         case SDL_QUIT:
  59.           game = 0;
  60.           break;
  61.         case SDL_KEYDOWN:
  62.           if (event.key.keysym.sym == SDLK_ESCAPE)
  63.             {
  64.               game = 0;
  65.               printf("Vous quittez le jeu.\n");
  66.         }
  67.       else if (event.key.keysym.sym == SDLK_UP)
  68.         pos = 1;
  69.       else if (event.key.keysym.sym == SDLK_DOWN)
  70.         pos = 2;
  71.       else if (event.key.keysym.sym == SDLK_RIGHT)
  72.         pos = 3;
  73.       else if (event.key.keysym.sym == SDLK_LEFT)
  74.         pos = 4;
  75.       break;
  76.     }
  77.       if (tempsActuel - tempsPrecedent > 10)
  78.     {
  79.       switch (pos)
  80.         {
  81.         case 1:
  82.           positionSnake.y -= 3;
  83.           break;
  84.         case 2:
  85.           positionSnake.y += 3;
  86.           break;
  87.         case 3:
  88.           positionSnake.x += 3;
  89.           break;
  90.         case 4:
  91.           positionSnake.x -= 3;
  92.           break;
  93.         }
  94.       tempsPrecedent = tempsActuel;
  95.     }
  96.       else
  97.     SDL_Delay(10 - (tempsActuel - tempsPrecedent));
  98.       SDL_BlitSurface(background, NULL, screen, &positionBackground);
  99.       SDL_BlitSurface(snake, NULL, screen, &positionSnake);
  100.       SDL_Flip(screen);
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement