Kaktusen

Kaktus_pong main.cpp

Feb 7th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include "SDL/SDL.h"
  4.  
  5. #include "surfaces.h"
  6. #include "classes.h"
  7.  
  8. using namespace std;
  9.  
  10. void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination);
  11. void restart();
  12.  
  13. int main(int argc, char *args[]) {
  14.  
  15.     window.width = 640;
  16.     window.height = 480;
  17.  
  18.     SDL_Surface *screen = SDL_SetVideoMode(window.width, window.height, 32, SDL_SWSURFACE);
  19.  
  20.     SDL_Init(SDL_INIT_EVERYTHING);
  21.     SDL_WM_SetCaption("Kaktus - Pong", NULL);
  22.  
  23.     cPlayer.x = 50;
  24.     cPlayer.y = (480 / 2) - 80;
  25.  
  26.     cPlayer2.x = 570;
  27.     cPlayer2.y = (480 / 2) - 80;
  28.  
  29.  
  30.     cBall.x = 320;
  31.     cBall.y = 240;
  32.     cBall.xspeed = 1;
  33.     cBall.yspeed = 1;
  34.     cBall.speed = 0;
  35.  
  36.     Uint8 *key;
  37.     bool gameLoop = true;
  38.     SDL_Event event;
  39.  
  40.     while(gameLoop == true) {
  41.  
  42.         SDL_PumpEvents();
  43.         key = SDL_GetKeyState(NULL);
  44.  
  45.         if(key[SDLK_UP]) {
  46.             cPlayer2.y-=4;
  47.         } else if(key[SDLK_DOWN]) {
  48.             cPlayer2.y+=4;
  49.         }
  50.  
  51.         if(key[SDLK_w]) {
  52.             cPlayer.y-=4;
  53.         } else if(key[SDLK_s]) {
  54.             cPlayer.y+=4;
  55.         }
  56.  
  57.         if(key[SDLK_ESCAPE]) {
  58.             gameLoop = false;
  59.         } else if(key[SDLK_r]) {
  60.             restart();
  61.         }
  62.  
  63.         if(event.type == SDL_QUIT) {
  64.             gameLoop = false;
  65.         }
  66.  
  67.         if(cBall.x > cPlayer.x && cBall.x < cPlayer.x + 8)
  68.             if(cBall.y + 16 > cPlayer.y && cBall.y + 16 < cPlayer.y + 80) {
  69.                 cBall.speed += 0.3;
  70.                 cBall.xspeed += 1 + cBall.speed;
  71.         }
  72.  
  73.         if(cBall.x + 16 > cPlayer2.x && cBall.x < cPlayer2.x + 8)
  74.             if(cBall.y + 16 > cPlayer2.y && cBall.y + 16 < cPlayer2.y + 80) {
  75.                 cBall.speed += 0.3;
  76.                 cBall.xspeed -= (1 + cBall.speed);
  77.         }
  78.  
  79.         if(cBall.y+cBall.yspeed <=0 && cBall.yspeed<0) {
  80.             cBall.yspeed =- cBall.yspeed;
  81.         } else if(cBall.y + cBall.yspeed >= 480 - 16 && cBall.yspeed>0) {
  82.             cBall.yspeed =- cBall.yspeed;
  83.         }
  84.  
  85.         if(cBall.x+cBall.xspeed <= 0 - 16 && cBall.xspeed<0) {
  86.             restart();
  87.         } else if(cBall.x + cBall.xspeed >= 640 && cBall.xspeed>0) {
  88.             restart();
  89.         }
  90.  
  91.         cBall.x += cBall.xspeed;
  92.         cBall.y += cBall.yspeed;
  93.  
  94.         apply_surface(0, 0, background, screen);
  95.         apply_surface(cPlayer.x, cPlayer.y, player, screen);
  96.         apply_surface(cPlayer2.x, cPlayer2.y, player2, screen);
  97.         apply_surface(cBall.x, cBall.y, ball, screen);
  98.  
  99.         SDL_Flip(screen);
  100.  
  101.     }
  102.  
  103.     SDL_FreeSurface(background);
  104.     SDL_FreeSurface(player);
  105.     SDL_FreeSurface(player2);
  106.     SDL_FreeSurface(ball);
  107.     SDL_Quit();
  108. }
  109.  
  110. void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
  111.     SDL_Rect rect;
  112.  
  113.     rect.x = x;
  114.     rect.y = y;
  115.  
  116.     SDL_BlitSurface(source, NULL, destination, &rect);
  117. }
  118.  
  119. void restart() {
  120.     cPlayer.x = 50;
  121.     cPlayer.y = (480 / 2) - 80;
  122.  
  123.     cPlayer2.x = 570;
  124.     cPlayer2.y = (480 / 2) - 80;
  125.  
  126.  
  127.     cBall.x = 320;
  128.     cBall.y = 240;
  129.     cBall.xspeed = 1;
  130.     cBall.yspeed = 1;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment