I am getting confused with classes, so I made it a struct.
here's what I came up with so far:
PHP Code:
#include <stdio.h>
#include <conio.h>
struct gamecomponent
{
int bufx, bufy;
int oldx, oldy;
int newx, newy;
int speed;
void resetcoords();
} ball, paddle;
void gamecomponent :: resetcoords()
{
oldx = bufx;
oldy = bufy;
bufx = newx;
bufy = newy;
}
void changecoordball()
{
int xadd, yadd;
if (ball.newx >= 80)
{
xadd = -ball.speed;
}
if (ball.newx <= 0)
{
xadd = ball.speed;
}
if (ball.newy >= 25)
{
yadd = -ball.speed;
}
if (ball.newy <= 0)
{
yadd = ball.speed;
}
ball.newx = ball.newx + xadd;
ball.newy = ball.newy + yadd;
}
void initialize()
{
ball.bufx = 1;
ball.bufy = 1;
ball.newx = 1;
ball.newy = 1;
ball.oldx = 1;
ball.oldy = 1;
ball.speed = 1;
paddle.bufx = 1;
paddle.bufy = 25;
paddle.newx = 1;
paddle.newy = 25;
paddle.oldx = 1;
paddle.oldy = 25;
paddle.speed = 1;
}
void placeball()
{
gotoxy(ball.oldx, ball.oldy);
printf(" ");
gotoxy(ball.newx, ball.newy);
printf("*");
}
void placepaddle()
{
gotoxy(paddle.oldx, paddle.oldy)
printf(" ");
gotoxy(paddle.newx, paddle.newy)
printf("=======");
}
main()
{
clrscr();
initialize();
placeball();
while (!kbhit())
{
}
}
SUMMARY :
1) Defined structs for ball and paddle which contain all the related variables for each "gamecomponent"
2) Write gamecomponent :: resetcoords ... I am not sure if this works. Now that I wrote the code like this, can I now say
ball.resetcoords
? Does it reset the coordinates ?
3) Place ball will place the ball at the newx and newy coordinates of the ball and puts a blank space " " at the previous coordinates.
QUESTIONS
1) Can I still keep these things public and use it ? Because I want
to write different functions that can access these variables.
2) I want to write a function that keeps running with time (say every 1/100th of a second or something like that) and keeps changing the ballcoordinates ... i.e. in other words, runs the changecoordball() function.
At the same time, this function should get input from user,
(i.e. left arrow, or right arrow) and change the coordinates of the paddle
It should also check if the ball fell down (I have an idea of how this one works)
So how do I write this function which takes arrow keys and changes the coordinates of the paddle (with input) and of the
ball (with time) and of also checks if it has fallen down ?