Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Ball.cpp
- *
- * Created on: Nov 21, 2014
- * Author: Austin
- *
- * Creates ball texture, when goes out of bounds, restarts back at center, launches in a random direction, collides with
- * bottom and top of window with the opposite direction of velocity.
- */
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- #include <SDL_image.h>
- using namespace std;
- #include "Ball.h"
- Ball::Ball(SDL_Renderer *renderBall, string filePath, int x, int y)
- {
- SDL_Surface *surface = IMG_Load(filePath.c_str());
- if(surface == NULL)
- cout << "Error ball surface" << endl;
- else
- {
- texture = SDL_CreateTextureFromSurface(renderBall, surface);
- if(texture == NULL)
- cout << "Error ball texture" << endl;
- }
- SDL_FreeSurface(surface);
- ballRect.x = x;
- ballRect.y = y;
- posRect.w = 14;
- posRect.h = 14;
- ballRect.w = posRect.w;
- ballRect.h = posRect.h;
- xVel = -1;
- yVel = 3;
- SDL_QueryTexture(texture, NULL, NULL, &posRect.w, &posRect.h);
- }
- void Ball::UpdateBall(float delta)
- {
- //Ball::RandomNumber = srand(time(NULL));
- //Collision detection
- ballRect.x += xVel;
- ballRect.y += yVel;
- if(ballRect.y < 0)
- yVel = -yVel;
- if(ballRect.y > 586)
- yVel = -yVel;
- if(ballRect.x + ballRect.w > posRect.x && ballRect.x < posRect.x + posRect.w)
- {
- if(ballRect.y + ballRect.h > posRect.y && ballRect.y < posRect.y + posRect.w)
- {
- xVel = -xVel;
- }
- }
- }
- Ball::~Ball()
- {
- SDL_DestroyTexture(texture);
- }
- int GetRandomNumber(int high, int low)
- {
- return rand() % high + low;
- }
- void Ball::Draw(SDL_Renderer *renderBall)
- {
- SDL_RenderCopy(renderBall, texture, NULL, &ballRect);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement