Hi guys. :) I'm a game design student and we're working on this simple pong game in c++, opengl... I wouldn't be asking here but our professor can really be... "unclear" at times. Apparently he wants us to make the points of our paddles (wall1P1x, etc) into data structures. Can anyone help me here? :) I'd really appreciate it. Don't worry, I NEVER copy and paste. I study the content first and do it by myself. I wanna learn.All I know is that there's supposed to the the word 'struct', nothing else.
void GameScene()
{
//_____________________________
// para gumalaw ang ball :D
ballX =ballX+moveX;
ballY =ballY+moveY;
////Diagonally
//_____________________________
//raidus ng bounding box (0.5/-0.5) para sakto sa ball
ballULx=ballX-ballRadius;
ballULy=ballY+ballRadius;
ballLRx=ballX+ballRadius;
ballLRy=ballY-ballRadius;
//____________________________
//ballULx,
//ballULy,
//ballLRx,
//ballLRy;
//_____________________________________________________________________
// these are for the times when the ball bounces at the sides of the paddles
if ((ballULx<=wall2P1x) && (ballULy<=wall2P2y) && (ballLRy>=wall2P3y))
moveX*=-1;
if ((ballLRx>=wall1P1x) && (ballULy<=wall1P1y) && (ballLRy>=wall1P3y))
moveX*=-1;
void Keys(unsigned char key, int x, int y)
{
switch(key)
{
case 27 : exit(0); break;
//______________________________________
// these are for the controls (up and down)
case 'q': if (wall2P1y>=wall4P2y)
{
wall2P1y=wall2P1y+0;
}
Why use time.h instead of ctime . time.h is the c version, ctime is the c++ version.
A struct is a pretty simple thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//this is like defining your own data type. It is not an instance of that type.
struct Vec2 {
float x;
float y;
};
int main() {
//creating an instance of the type you have defined
Vec2 point;
//access the fields of the struct with <instance>.<field>
point.x = .1;
std::cout << point.x;
}
Probably he wants you to make it into a struct, and then create the individual paddles seperately, to prevent data duplication. Here is one way of how to do it, using nested structs:
struct Point {
float x;
float y;
float z;
};
struct Paddle {
Point tr; // Top Right
Point tl; // Top Left
Point bl; // Bottom Left
Point br; // Bottom Right
};
// Declaring instances
Paddle p1 =
{
{13.5, 2.5, 0.0},
{15.0, 2.5, 0.0},
{15.0, -2.5, 0.0},
{13.5, -2.5, 0.0}
}
You should probably try to avoid the ridiculous number of global variables you have, that can get messy (especially in game development, where multithreading is the norm). Also, on a side note, just pointing out that that version of OpenGL has been deprecated for a long time... :)