Created
April 25, 2014 00:36
-
-
Save warlok900/11274283 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <SDL.h> | |
| #include <SDL_mixer.h> | |
| #include <SDL_image.h> | |
| using namespace std; | |
| SDL_Texture* LoadTexture(SDL_Renderer* render, const char* name); | |
| bool collision(SDL_Rect* rect1, SDL_Rect* rect2); | |
| bool processInput(bool b[4]); | |
| SDL_Rect ball_rect; | |
| int main (int argc, char* args[]) { | |
| const int screen_width = 1920; | |
| const int screen_height = 1200; | |
| const int FPS = 40; | |
| bool b[5] = {0,0,0,0,0}; | |
| float paddle_speed = 1; | |
| Uint32 start_time = SDL_GetTicks(); | |
| SDL_Init(SDL_INIT_EVERYTHING); | |
| SDL_Window* window = NULL; | |
| window = SDL_CreateWindow("Pong", 100, 100, screen_width, screen_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE ); | |
| if (window == NULL) { | |
| cout << "Window load Error!" << SDL_GetError()<< endl; | |
| exit(1); | |
| } | |
| SDL_Renderer* render = NULL; | |
| render = SDL_CreateRenderer(window, -1, 0); | |
| if(render == NULL) { | |
| cout << "Rendering Error!" << SDL_GetError() << endl; | |
| exit(1); | |
| } | |
| if(1000/FPS > SDL_GetTicks() - start_time) { | |
| SDL_Delay(1000/FPS - (SDL_GetTicks() - start_time)); | |
| } | |
| SDL_Texture* stage1_image = NULL; | |
| stage1_image = LoadTexture(render,"stage_02.png"); | |
| SDL_Rect stage1_rect; | |
| stage1_rect.x = 0; | |
| stage1_rect.y = 0; | |
| stage1_rect.w = screen_width; | |
| stage1_rect.h = screen_height; | |
| SDL_Texture* red_bar_image = NULL; | |
| red_bar_image = LoadTexture(render, "paddle_01.png"); | |
| SDL_Rect red_bar_rect; | |
| red_bar_rect.x = 10; | |
| red_bar_rect.y = 600; | |
| red_bar_rect.w = 40; | |
| red_bar_rect.h = 125; | |
| SDL_Texture* blue_bar_image = NULL; | |
| blue_bar_image = LoadTexture(render, "paddle_01.png"); | |
| SDL_Rect blue_bar_rect; | |
| blue_bar_rect.x = 1870; | |
| blue_bar_rect.y = 600; | |
| blue_bar_rect.w = 40; | |
| blue_bar_rect.h = 120; | |
| SDL_Texture* ball_image = NULL; | |
| ball_image = LoadTexture(render, "ball_03.png"); | |
| SDL_Rect ball_rect; | |
| ball_rect.x = 960; | |
| ball_rect.y = 600; | |
| ball_rect.w = 40; | |
| ball_rect.h = 40; | |
| SDL_Texture* border = NULL; | |
| border = LoadTexture(render,"border_01.png"); | |
| SDL_Rect border_rect; | |
| border_rect.x = -600; | |
| border_rect.y = -10; | |
| border_rect.w = 3000; | |
| border_rect.h = 70; | |
| //SDL_Texture* | |
| //MainEvent is to poll event in wile loop | |
| while(processInput(b)) { | |
| start_time; | |
| if(b[0]) { | |
| red_bar_rect.y -= paddle_speed; | |
| } | |
| if (b[1]) { | |
| red_bar_rect.y += paddle_speed; | |
| } | |
| if(b[2]) { | |
| blue_bar_rect.y -= paddle_speed; | |
| } | |
| if(b[3]) { | |
| blue_bar_rect.y += paddle_speed; | |
| } | |
| if (b[4]){ | |
| ball_rect.x++; | |
| } | |
| if (collision(&ball_rect,&red_bar_rect)){ | |
| ball_rect.x ++; | |
| ball_rect.y --; | |
| } | |
| if (collision(&ball_rect,&blue_bar_rect)){ | |
| ball_rect.x --; | |
| ball_rect.y ++; | |
| } | |
| SDL_RenderClear(render); | |
| SDL_RenderCopy(render, stage1_image, NULL, &stage1_rect); | |
| SDL_RenderCopy(render, border, NULL, &border_rect); | |
| SDL_RenderCopy(render, red_bar_image, NULL, &red_bar_rect); | |
| SDL_RenderCopy(render, blue_bar_image, NULL, &blue_bar_rect); | |
| SDL_RenderCopy(render, ball_image, NULL, &ball_rect); | |
| SDL_RenderPresent(render); | |
| if(1000/FPS > SDL_GetTicks() - start_time) { | |
| SDL_Delay(1000/FPS - (SDL_GetTicks() - start_time)); | |
| } | |
| } | |
| SDL_DestroyWindow(window); | |
| SDL_DestroyRenderer(render); | |
| SDL_DestroyTexture(stage1_image); | |
| SDL_DestroyTexture(border); | |
| SDL_DestroyTexture(red_bar_image); | |
| SDL_DestroyTexture(blue_bar_image); | |
| SDL_DestroyTexture(ball_image); | |
| } | |
| SDL_Texture* LoadTexture(SDL_Renderer* render, const char* name) { | |
| SDL_Texture* img = IMG_LoadTexture(render, name); | |
| if (img == NULL) { | |
| cout << "Error Loading Texture: " << SDL_GetError() << endl; | |
| exit(1); | |
| } | |
| return img; | |
| } | |
| bool processInput(bool b[5]) { | |
| static SDL_Event* mainevent = new SDL_Event(); | |
| while (SDL_PollEvent(mainevent)) { | |
| switch (mainevent->type) { | |
| case SDL_QUIT: | |
| return false; | |
| break; | |
| case SDL_KEYDOWN: | |
| switch(mainevent->key.keysym.sym) { | |
| case SDLK_w: | |
| b[0] = 1; | |
| break; | |
| case SDLK_s: | |
| b[1] = 1; | |
| break; | |
| case SDLK_o: | |
| b[2] = 1; | |
| break; | |
| case SDLK_l: | |
| b[3] = 1; | |
| break; | |
| case SDLK_ESCAPE: | |
| return false; | |
| break; | |
| case SDLK_SPACE: | |
| b[4] = 1; | |
| break; | |
| } | |
| break; | |
| case SDL_KEYUP: | |
| switch(mainevent->key.keysym.sym) { | |
| case SDLK_w: | |
| b[0] = 0; | |
| break; | |
| case SDLK_s: | |
| b[1] = 0; | |
| break; | |
| case SDLK_o: | |
| b[2] = 0; | |
| break; | |
| case SDLK_l: | |
| b[3] = 0; | |
| break; | |
| case SDLK_SPACE: | |
| b[4] = 0; | |
| } | |
| break; | |
| } | |
| } | |
| return true; | |
| } | |
| bool collision(SDL_Rect* rect1, SDL_Rect* rect2) | |
| { | |
| if (rect1->y >= rect2->y + rect2->h) | |
| return 0; | |
| if (rect1->x >= rect2->x + rect2->w) | |
| return 0; | |
| if (rect1->y + rect1->h <= rect2->y) | |
| return 0; | |
| if (rect1->x + rect1->w <= rect2->x) | |
| return 0; | |
| return 1; | |
| } | |
Author
I did it. i got it before i read this. it came to me too. I am fine tuning it right now.i am having problems with how fluid the movements are.
i put SDL_RENDERER_PRESENTVSYNC in and it slows the game down, however, it allows to use the values i have set. it makes it more manageable. At the same time the movements are a little lagging, but not choppy,
is there a way to fix that?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, so this is one of the times where I had an aha moment in high school. Think about it, you have a property (the way the ball moves) and you want to be able to change it based on some condition (collision). This is what variables are for! So the idea is instead of
ball_rect.x--;You have something like
ball_rect.x += ballXMovement;That way you can set ballXMovement to whatever you want and the ball will move in a different way. You'd also want to do that for the Y direction, obviously.