Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <SDL.h>
- int main(int argc, char **argv){
- SDL_Rect b0 = {10, 103, 10, 50}; //x, y, w, h
- SDL_Rect b1 = {246, 103, -10, 50};
- SDL_Rect ball = {123, 123, 10, 10};
- int xv = 2;
- int yv = 2;
- int p1 = 0;
- int p2 = 0;
- SDL_Window *window; // Declaring window
- SDL_Renderer *renderer; // Declaring renderer
- SDL_Init(SDL_INIT_VIDEO); // Initialize SDL video
- window = SDL_CreateWindow("G A M E", //windown name Initializing window
- SDL_WINDOWPOS_UNDEFINED, //window position
- SDL_WINDOWPOS_UNDEFINED, //window position
- 256, //window width
- 256, //window height
- 0); //flags
- renderer = SDL_CreateRenderer(window, // pointer to window
- -1, // index
- SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); // flags
- SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); // Set renderers draw colour to blue
- SDL_RenderClear(renderer); // Clear renderer to blue screen
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Set renderers draw colour to white
- int done = 0;
- SDL_Event event;
- while(done == 0){
- while(SDL_PollEvent(&event)){
- switch(event.type){
- case SDL_WINDOWEVENT_CLOSE:
- if (window){
- SDL_DestroyWindow(window);
- window = NULL;
- done = 1;
- }
- break;
- case SDL_QUIT:
- done = 1;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym){
- case SDLK_ESCAPE:
- done = 1;
- break;
- case SDLK_DELETE:
- done = 1;
- break;
- }
- break;
- }
- }
- const Uint8 *keyboard = SDL_GetKeyboardState(NULL);
- if (keyboard[SDL_SCANCODE_W]) b0.y -= (((unsigned) b0.y - 5) > 256) ? 0 : 5;
- if (keyboard[SDL_SCANCODE_S]) b0.y += ((b0.y + 55) > 256) ? 0 : 5;
- if (keyboard[SDL_SCANCODE_UP]) b1.y -= (((unsigned) b1.y - 5) > 256) ? 0 : 5;
- if (keyboard[SDL_SCANCODE_DOWN]) b1.y += ((b1.y + 55) > 256) ? 0 : 5;
- //code for ball
- if((((unsigned) ball.y + yv) > 256) || (((unsigned) ball.y + 10 + yv) > 256)) yv *= -1; //bouncing off of top/bottom wall
- else if(((unsigned) ball.x + xv) > 256) {p2++; ball.x = 123; ball.y = 123; xv *= -1;}
- else if(((unsigned) ball.x + 10 + xv) > 256) {p1++; ball.x = 123; ball.y = 123; xv *= -1;}
- else if(((ball.y > b0.y && ball.y < b0.y + b0.h) || (ball.y + ball.h > b0.y && ball.y + ball.h < b0.y + b0.h)) && ((ball.x > b0.x && ball.x + ball.w < b0.x + b0.w) || (ball.x > b0.x && ball.x < b0.x + b0.w))) {xv *= -1; puts("b0\n");} //b0 hit detection
- else if((((ball.y > b1.y) && (ball.y < b1.y + b1.h)) || ((ball.y + ball.h > b1.y) && (ball.y + ball.h < b1.y + b1.h))) && (((ball.x > b1.x + b1.w) && (ball.x < b1.x)) || ((ball.x + ball.w > b1.x + b1.w) && (ball.x + ball.w < b1.x)))) {xv *= -1; puts("b1\n");} //b1 hit detection
- ball.x += xv;
- ball.y += yv;
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set renderers draw colour to black
- SDL_RenderClear(renderer); // Clear renderer to black screen
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Set renderers draw colour to white
- SDL_RenderFillRect(renderer, &b0); // Fill rectangle with white
- SDL_RenderFillRect(renderer, &b1); // Fill rectangle with white
- SDL_RenderFillRect(renderer, &ball); // Fill rectangle with white
- SDL_RenderPresent(renderer); // Present
- printf("P1: %d\nP2: %d\n", p1, p2);
- SDL_Delay(1000 / 60000); // Wait 1/60 seconds
- }
- // Destroy window/renderer
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- // Clean up
- SDL_Quit();
- // Return
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement