Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include "SDL/SDL.h"
- #include "surfaces.h"
- #include "classes.h"
- using namespace std;
- void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination);
- void restart();
- int main(int argc, char *args[]) {
- window.width = 640;
- window.height = 480;
- SDL_Surface *screen = SDL_SetVideoMode(window.width, window.height, 32, SDL_SWSURFACE);
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_WM_SetCaption("Kaktus - Pong", NULL);
- cPlayer.x = 50;
- cPlayer.y = (480 / 2) - 80;
- cPlayer2.x = 570;
- cPlayer2.y = (480 / 2) - 80;
- cBall.x = 320;
- cBall.y = 240;
- cBall.xspeed = 1;
- cBall.yspeed = 1;
- cBall.speed = 0;
- Uint8 *key;
- bool gameLoop = true;
- SDL_Event event;
- while(gameLoop == true) {
- SDL_PumpEvents();
- key = SDL_GetKeyState(NULL);
- if(key[SDLK_UP]) {
- cPlayer2.y-=4;
- } else if(key[SDLK_DOWN]) {
- cPlayer2.y+=4;
- }
- if(key[SDLK_w]) {
- cPlayer.y-=4;
- } else if(key[SDLK_s]) {
- cPlayer.y+=4;
- }
- if(key[SDLK_ESCAPE]) {
- gameLoop = false;
- } else if(key[SDLK_r]) {
- restart();
- }
- if(event.type == SDL_QUIT) {
- gameLoop = false;
- }
- if(cBall.x > cPlayer.x && cBall.x < cPlayer.x + 8)
- if(cBall.y + 16 > cPlayer.y && cBall.y + 16 < cPlayer.y + 80) {
- cBall.speed += 0.3;
- cBall.xspeed += 1 + cBall.speed;
- }
- if(cBall.x + 16 > cPlayer2.x && cBall.x < cPlayer2.x + 8)
- if(cBall.y + 16 > cPlayer2.y && cBall.y + 16 < cPlayer2.y + 80) {
- cBall.speed += 0.3;
- cBall.xspeed -= (1 + cBall.speed);
- }
- if(cBall.y+cBall.yspeed <=0 && cBall.yspeed<0) {
- cBall.yspeed =- cBall.yspeed;
- } else if(cBall.y + cBall.yspeed >= 480 - 16 && cBall.yspeed>0) {
- cBall.yspeed =- cBall.yspeed;
- }
- if(cBall.x+cBall.xspeed <= 0 - 16 && cBall.xspeed<0) {
- restart();
- } else if(cBall.x + cBall.xspeed >= 640 && cBall.xspeed>0) {
- restart();
- }
- cBall.x += cBall.xspeed;
- cBall.y += cBall.yspeed;
- apply_surface(0, 0, background, screen);
- apply_surface(cPlayer.x, cPlayer.y, player, screen);
- apply_surface(cPlayer2.x, cPlayer2.y, player2, screen);
- apply_surface(cBall.x, cBall.y, ball, screen);
- SDL_Flip(screen);
- }
- SDL_FreeSurface(background);
- SDL_FreeSurface(player);
- SDL_FreeSurface(player2);
- SDL_FreeSurface(ball);
- SDL_Quit();
- }
- void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
- SDL_Rect rect;
- rect.x = x;
- rect.y = y;
- SDL_BlitSurface(source, NULL, destination, &rect);
- }
- void restart() {
- cPlayer.x = 50;
- cPlayer.y = (480 / 2) - 80;
- cPlayer2.x = 570;
- cPlayer2.y = (480 / 2) - 80;
- cBall.x = 320;
- cBall.y = 240;
- cBall.xspeed = 1;
- cBall.yspeed = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment