Kaktusen

PickItUp

Jan 25th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include <iostream>
  3.  
  4. #include "classes.h"
  5. #include "surfaces.h"
  6.  
  7. using namespace std;
  8.  
  9. int screen_width = 640;
  10. int screen_height = 480;
  11. int screen_bpp = 32;
  12.  
  13. SDL_Surface *screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);
  14.  
  15. bool gameLoop = true;
  16.  
  17. void loadVar();
  18. void game();
  19. void collision();
  20.  
  21. void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
  22.     SDL_Rect offset;
  23.  
  24.     offset.x = x;
  25.     offset.y = y;
  26.  
  27.     SDL_BlitSurface(source, NULL, destination, &offset);
  28. }
  29.  
  30. void load_sprite(int x, int y, int bx, int by, int width, int height, SDL_Surface *source, SDL_Surface *destination) {
  31.     SDL_Rect rect;
  32.     SDL_Rect offset;
  33.  
  34.     rect.x = bx;
  35.     rect.y = by;
  36.     rect.w = width;
  37.     rect.h = height;
  38.  
  39.     offset.x = x;
  40.     offset.y = y;
  41.  
  42.     SDL_BlitSurface(source, &rect, destination, &offset);
  43. }
  44.  
  45. int main(int argc, char *args[]) {
  46.     SDL_Init(SDL_INIT_EVERYTHING);
  47.     SDL_WM_SetCaption("Kaktus - PickItUp", NULL);
  48.  
  49.     Uint32 colorkey = SDL_MapRGB( tileset->format, 255, 0, 255);
  50.     SDL_SetColorKey( tileset, SDL_SRCCOLORKEY, colorkey );
  51.  
  52.     loadVar();
  53.     game();
  54.  
  55.     SDL_FreeSurface(tileset);
  56.     cout << "Program exit!" << endl;
  57.     SDL_Quit();
  58. }
  59.  
  60. void loadVar() {
  61.     cPlayer.pos = 1;
  62.     cPlayer.y = (480-32-10);
  63. }
  64.  
  65. void game() {
  66.     Uint8 *key;
  67.  
  68.     int x[3];
  69.     x[0] = 32 + 10;
  70.     x[1] = (640/2)-16;
  71.     x[2] = 640-32-10-32;
  72.  
  73.     double ballX[3];
  74.     ballX[0] = x[0];
  75.     ballX[1] = x[1];
  76.     ballX[2] = x[2];
  77.  
  78.     double ballY[3];
  79.     ballY[0] = 0;
  80.     ballY[1] = -32;
  81.     ballY[2] = -72;
  82.  
  83.     double ballYspeed[3];
  84.     for(int i=0; i<3; i++) {
  85.         ballYspeed[i] = 0.5;
  86.     }
  87.  
  88.     cPlayer.x = x[1];
  89.  
  90.     while(gameLoop) {
  91.         SDL_PumpEvents();
  92.         key = SDL_GetKeyState(NULL);
  93.  
  94.         if((key[SDLK_LEFT]) && cPlayer.pos == 1) {
  95.             cPlayer.pos = 0;
  96.             cPlayer.x = x[0];
  97.             SDL_Delay(100);
  98.         } else if((key[SDLK_LEFT]) && cPlayer.pos == 2) {
  99.             cPlayer.pos = 1;
  100.             cPlayer.x = x[1];
  101.             SDL_Delay(100);
  102.         }
  103.  
  104.         else if((key[SDLK_RIGHT]) && cPlayer.pos == 0) {
  105.             cPlayer.pos = 1;
  106.             cPlayer.x = x[1];
  107.             SDL_Delay(100);
  108.         } else if((key[SDLK_RIGHT]) && cPlayer.pos == 1) {
  109.             cPlayer.pos = 2;
  110.             cPlayer.x = x[2];
  111.             SDL_Delay(100);
  112.         }
  113.  
  114.  
  115.         for(int i = 0; i<3; i++) {
  116.             ballY[i] += ballYspeed[i];
  117.         }
  118.  
  119.         if(key[SDLK_ESCAPE]) {
  120.             gameLoop = false;
  121.         }
  122.  
  123.         for(int i = 0; i<3; i++) {
  124.             if(ballX[i] + 32 > cPlayer.x && ballX[i] < cPlayer.x + 32)
  125.                 if(ballY[i]+32 > cPlayer.y && ballY[i]+32 < cPlayer.y + 32) {
  126.                     ballY[i] = 0 - 32;
  127.                     ballYspeed[i] += 0.23;
  128.             }
  129.         }
  130.  
  131.         for(int i = 0; i<3; i++) {
  132.             if(ballY[i] > 480) {
  133.                 ballY[i] = 0 - 32;
  134.             }
  135.         }
  136.  
  137.         apply_surface(0, 0, background, screen);
  138.  
  139.         load_sprite(ballX[0], ballY[0], 32+2, 1, 32, 32, tileset, screen);
  140.         load_sprite(ballX[1], ballY[1], 32+2, 1, 32, 32, tileset, screen);
  141.         load_sprite(ballX[2], ballY[2], 32+2, 1, 32, 32, tileset, screen);
  142.  
  143.         load_sprite(cPlayer.x, cPlayer.y, 1, 1, 32, 32, tileset, screen);
  144.         SDL_Flip(screen);
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment