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. I have collision with the ball and the walls however want to change the collision on the sides to react off the paddles instead. Urgent help and really would appreciate any help. Thanks guys.
if (Keyboard.isKeyReady())
{// check if they want to exit
if (Keyboard.waitKey() == GwinKeyNames::ESCAPE)
{// let them exit
isRequired = false;
}
}
}
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
#include <cmath>
#include "gwin.h"
constint 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();
}
}
};
constfloat 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();
float paddle_width = PaddleImg.getWidth();
float 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 + paddle_width) > Gwin.getWidth() - PaddleImg.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;
}
(Thanks to indentcode.net)
Now my suggestion to fix collisions:
You're overcomplicating things. Make a class Rectangle which has an x and y coordinate (probably of the upper-left corner) and a width and height. Assign a rectangle to the ball and both paddles. You can easily check if two of these rectangles are overlapping; I'll let you figure that out. If the two rectangles touch, you need to move the ball back along its path to the front edge of the paddle and modify its velocity somehow.
I have created the class for rectangle, and that all seems to be fine, I keep getting errors when trying to assign the rectangle to the paddle and ball... any more advice? would be really be appreciated.
if (Keyboard.isKeyReady())
{// check if they want to exit
if (Keyboard.waitKey() == GwinKeyNames::ESCAPE)
{// let them exit
isRequired = false;
}
}
}
return 0;
}