Paddle Collision pong any advice please!!

Hi guys posted last time, making a simple pong game as a coursework and cant seem to get the ball to hit of the paddle I will post the code because I really am stumped with it. any help would be really appreciated.


#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
#include <cmath>
#include "gwin.h"


const int NUMBEROFBALLS = 1;

class Paddle
{
public:
float xPos;
float yPos;
GImage* image;


Paddle(float Xpos, float Ypos, GImage* Image)
{
xPos = Xpos;
yPos = Ypos;
image = Image;

}

void Draw(GWindow* gwin)
{
(*gwin).drawImage((int)xPos, (int)yPos, image);
}

void Limit(GWindow* gwin)
{
if (yPos < 0)
{
yPos = 0;
}

if(yPos > ((*gwin).getHeight() - (*image).getHeight()))
{
yPos = (*gwin).getHeight() - (*image).getHeight();
}
}
};
const float PaddleSpeed = 1.0f;

int main()
{
GWindow Gwin;
//objects info [1];
GImage blueball(GPath("pictures/BLUEBALL.png").transform());
GImage PaddleImg(GPath("pictures/Paddle.png").transform());
std::vector<GSprite *> ball_list;
typedef std::vector<GSprite *>::iterator ball_list_iter;



// 3. Variables for game objects


Paddle player1 = Paddle (0,PaddleImg.getWidth(), &PaddleImg);
Paddle player2 = Paddle(Gwin.getWidth() - PaddleImg.getWidth(), 100, &PaddleImg);


// Create a new sprite for every ball
//for (int i = 0 ; i < NUMBEROFBALLS ; i++)
{
GSprite *ball = new GSprite();
ball_list.push_back(ball);




int x = rand() % 1;

if (!x)

ball->attachImage(&blueball);

//Ball Directions


ball->x = rand() % 50; //+ 250;
ball->y = rand() % 50; //+ 250;

//Ball Speeds

ball->vx = 0;
while (fabs(ball->vx) < 0.3)
ball->vx = ((rand() % 400) - 200.0) / 200.0;

ball->vy = 0;
while (fabs(ball->vy) < 0.3)
ball->vy = ((rand() % 400) - 200.0) / 200.0;


}

int ball_width = blueball.getWidth();
int ball_height = blueball.getHeight();
int paddle_width = PaddleImg.getWidth();
int paddle_height = PaddleImg.getHeight();


// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );


bool isRequired = true;
//int x = 90;

// Loop until user presses Esc
while (isRequired)
{
Gwin.clear();

if (Keyboard.isPressed('w'))
{
player1.yPos -= PaddleSpeed;
}

if (Keyboard.isPressed('s'))
{
player1.yPos += PaddleSpeed;
}

player1.Limit(&Gwin);

if(Keyboard.isPressed(GwinKeyNames::CURSOR_UP))
{
player2.yPos -= PaddleSpeed;
}
if (Keyboard.isPressed(GwinKeyNames::CURSOR_DOWN))
{
player2.yPos += PaddleSpeed;
}
player2.Limit(&Gwin);



Gwin.clear();



int z = 0;



for (ball_list_iter i = ball_list.begin() ; i != ball_list.end() ; ++i)
{
(*i)->render();
(*i)->x += (*i)->vx;
(*i)->y += (*i)->vy;


//Check collisions with wall
//if ((((*i)->x + ball_width) > Gwin.getWidth()) || ((*i)->x <= 1))
//(*i)->vx *= -1;
if((((*i)->x - ball_width) > Gwin.getWidth())|| ((*i)->x <+ 1))
(*i)->vx *= -1;



if ((((*i)->y - ball_height)> Gwin.getHeight ()) || ((*i)->y <= 0))
(*i)->vy *= -1;


player1.Draw(&Gwin);
player2.Draw(&Gwin);



++z;
}
Gwin.refresh();

if (Keyboard.isKeyReady())
{// check if they want to exit
if (Keyboard.waitKey() == GwinKeyNames::ESCAPE)
{// let them exit
isRequired = false;
}
}
}
return 0;
}
Topic archived. No new replies allowed.