Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by doles ( 17 years ago )
#include "../include/Ball.h"

Ball::Ball(SDL_Surface *ball, SDL_Surface *screen)
{
  this->ball = ball;
  this->screen = screen;
  box.x = SCREEN_WIDTH / 2 - BALL_WIDTH / 2;
  box.y = SCREEN_HEIGHT / 2 - BALL_HEIGHT / 2;

  box.w = BALL_WIDTH - 5;
  box.h = BALL_HEIGHT - 5;

  xVel = 0;
  yVel = 0;
}

Ball::~Ball()
{
    SDL_FreeSurface(ball);
}

void Ball::reset()
{
  box.x = SCREEN_WIDTH / 2 - BALL_WIDTH / 2;
  box.y = SCREEN_HEIGHT / 2 - BALL_HEIGHT / 2;

  xVel = 0;
  yVel = 0;
}

void Ball::handleInput(SDL_Event event)
{
  int leftOrRight;

  if(event.type == SDL_KEYDOWN)
  {
    if(event.key.keysym.sym == SDLK_SPACE)
    {
	leftOrRight = rand() % 2;
	if(leftOrRight == 0)
	{
	    xVel = rand() % 5 + 10;
	    yVel = rand() % 15;
	}
	else
	{
	    xVel = -1 * (rand() % 5 + 10);
	    yVel = -1 * (rand() % 15);
	}
    }
  }
}

void Ball::move()
{
  box.x += xVel;
  box.y += yVel;

    if( (box.y < 0) || (box.y + BALL_HEIGHT > SCREEN_HEIGHT))
      yVel = -1 * yVel;
}


void Ball::show()
{
    SDL_Rect offset;
    offset.x = box.x;
    offset.y = box.y;
    //apply_surface(box.x, box.y, ball, screen);
    SDL_BlitSurface(ball, NULL, screen, &offset);
}

int Ball::getXV()
{
  return xVel;
}

int Ball::getYV()
{
  return yVel;
}

void Ball::setXV(int x)
{
  xVel = x;
}

void Ball::setYV(int y)
{
  yVel = y;
}

void Ball::setBallSurface(SDL_Surface* ball)
{
  this->ball = ball;
}

 

Revise this Paste

Your Name: Code Language: