Kaktusen

Kaktus_slot_machine

Jul 15th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include "SDL/SDL.h"
  3. #include <ctime>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void exit();
  9.  
  10. //surfaces
  11. SDL_Surface *screen = NULL;
  12. SDL_Surface *cpp = NULL;
  13. SDL_Surface *java = NULL;
  14. SDL_Surface *background = NULL;
  15. //slotsakerna
  16. SDL_Surface *lisp = NULL;
  17. SDL_Surface *gml = NULL;
  18. SDL_Surface *c = NULL;
  19.  
  20. //variables
  21. int random[3];
  22. Uint8 *key;
  23.  
  24. SDL_Event event;
  25.  
  26. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  27.     SDL_Rect offset;
  28.  
  29.     offset.x = x;
  30.     offset.y = y;
  31.  
  32.     SDL_BlitSurface(source, NULL, destination, &offset);
  33. }
  34.  
  35. void slot();
  36. void keyinput();
  37.  
  38. int main(int argc, char *args []) {
  39.  
  40.     srand(time(NULL));
  41.     SDL_Init(SDL_INIT_EVERYTHING);
  42.  
  43.     cpp = SDL_LoadBMP("image/cpp.bmp");
  44.     java = SDL_LoadBMP("image/java.bmp");
  45.     background = SDL_LoadBMP("image/background.bmp");
  46.     lisp = SDL_LoadBMP("image/lisp.bmp");
  47.     c = SDL_LoadBMP("image/c.bmp");
  48.     gml = SDL_LoadBMP("image/gml.bmp");
  49.  
  50.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  51.     apply_surface(0, 0, background, screen);
  52.  
  53.     slot();
  54.     keyinput();
  55.  
  56.     SDL_Flip(screen);
  57.  
  58.     SDL_Delay(2000);
  59.     exit();
  60.     SDL_Quit();
  61.     return 0;
  62. }
  63.  
  64. void exit() {
  65.     SDL_FreeSurface(cpp);
  66.     SDL_FreeSurface(java);
  67.     SDL_FreeSurface(background);
  68.     SDL_FreeSurface(lisp);
  69.     SDL_FreeSurface(gml);
  70.     SDL_FreeSurface(c);
  71. }
  72.  
  73. void slot() {
  74.     int place[3];
  75.     place[0] = 90;
  76.     place[1] = 250;
  77.     place[2] = 400;
  78.  
  79.     int y = 220;
  80.  
  81.     for(int i=0; i<3; i++) {
  82.         random[i] = rand() % 5 + 1;
  83.  
  84.         if(random[i] == 1) {
  85.             apply_surface(place[i], y, cpp, screen);
  86.         } else if(random[i] == 2) {
  87.             apply_surface(place[i], y, java, screen);
  88.         } else if(random[i] == 3) {
  89.             apply_surface(place[i], y, lisp , screen);
  90.         } else if(random[i] == 4) {
  91.             apply_surface(place[i], y, gml, screen);
  92.         } else if(random[i] == 5) {
  93.             apply_surface(place[i], y, c, screen);
  94.         }
  95.     }
  96.  
  97.     SDL_Flip(screen);
  98. }
  99.  
  100. void keyinput() {
  101.     while(true) {
  102.         if( SDL_PollEvent (&event)) {
  103.  
  104.                 if(event.type == SDL_KEYDOWN) {
  105.  
  106.                     switch(event.key.keysym.sym) {
  107.  
  108.                         case SDLK_SPACE:
  109.                             slot();
  110.                             break;
  111.  
  112.                         case SDLK_ESCAPE:
  113.                             exit();
  114.                             break;
  115.                     }
  116.                 }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment