-
take a look
Hey, I'm a junior in high school and in a Computer Science class. It has to be the easiest thing in the world. the extend of what will be cover goes up to structs, and thats more than 25 weeks from now. So what i did is I read the book and did my own arkanoid type program; the thing is, I don't have anyone to tell me what I could do better in it cause the teacher only knows whats in the book. It did it in borland 5.something or other and i used graphics.h and a dos project.. can someone give me tips? Here's what I have, it compiles and runs just fine, i didnt document it at all so i guess ill do so in the post.. sigh
Code:
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <dos.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct ballobject //The variables the ball needs to function
{
int x1;
int x2;
int y1; //coordinates
int y2;
int xmag;
int ymag; //magnitude of direction; -1 for back and 1 for forward
};
struct paddleobject //the paddle on the bottom's vars
{
int x1;
int x2; //coords
int y1;
int y2;
};
struct targetobject //the targets
{
int x1;
int x2;
int y1; //coords
int y2;
int gone; //so i know when its been hit
};
ballobject ball; //makes a ball
paddleobject paddle; //makes a paddle
targetobject target1[12]; //makes 12 targets
int num_of_targ = 12; //assign to a var so that i only have to change it here if
//i want to change it in the whole prog
void drawball();//the backbone of user input and screen output
void moveball(); //functions i'll need; ill explain them below
void eraseball();
void getmag();
int gameend = 0; //if the user presses q
int loss = 0; //if the balls falls past bottom
int loop; //my cheesy way of doing speed, see below
int main()
{
time_t seconds; //for srand
time(&seconds); //etc
srand((unsigned int) seconds); //for srand
int gameyes=1; //the game is in progress
do
{
cout<<"enter ball speed (recommended speed: 1 - 10): ";
cin >> loop; //this is actually the number of times the ball is drawn in place
//to produce a slower speed
loop+=num_of_targ; //the speed increases every time a target is hit, so add numebr of targets to
//loop for i dont get negatives
int graphicsdriver = DETECT, graphicsmode;
initgraph(&graphicsdriver, &graphicsmode, "c:\\bc5\\bgi"); //set up graphics
int ErrorCode = graphresult(); //display when **** happens
if( ErrorCode != grOk ){
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
}
outtext("Arkanoid by SaRGaS W-UP S-DOWN A-LEFT D-RIGHT"); //header in fullscreen
moveto(1,9);
lineto(639,9); //underline header
ball.x1 = 318;
ball.x2 = 322;
ball.y1 = 238;
ball.y2 = 242;
ball.ymag = 1;
ball.xmag = -1;
paddle.x1 = 290;
paddle.x2 = 340;
paddle.y1 = 470;
paddle.y2 = 471; //all of the above are beginning coords in pixels
int temp;
int temp2; //for below
for(int first = 0; first < num_of_targ; first++) //do this for all targets
{
temp = ((rand()%570)+20); //store a random place on the screen in x axis
temp2 = ((rand()%400)+10); //do the same for y
target1[first].x1 = temp; //the random x, fits snugly in screen
target1[first].y1 = temp2; //same but with y
target1[first].x2 = temp + 50; //add 50 so that the target has a width
target1[first].y2 = temp2 + 3; //add the height of the target
target1[first].gone = 0; //it isnt dead yet
}
moveto(ball.x1, ball.y1); //lets start where the ball starts
drawball(); //this does more than just draw it, see below
sleep(3); //wait for the user to see whats up
while(1==1) //do this untill a break
{
getmag(); //find where the ball is supposed to go
eraseball(); //erase,
moveball(); //move,
drawball(); //and redraw it
if(gameend == 1) //if the user pressed q,
{
closegraph(); //then stop the game
break;
}
if(loss == 1) //if he lost
{
closegraph(); //stop the game
cout<<"Better luck next time! Again? 1 for yes 0 for no:";
//ask him if he wants to go again
loss = 0; //reset loss
break;//go out of loop
}
}
cin>>gameyes; //should the game still be in progress? (was prompted a couple lines up)
}while(gameyes==1); //repeat from that do way up there while the game is in progress
return 0;//bye bye
}
void drawball() //the backbone of user input and screen output
{
for(int x = 0; x<loop; x++)//the number of times to repeat this before the ball is moved
{ //it draws the ball in the same place so humans have enough time to respond (loop times)
rectangle(ball.x1, ball.y1, ball.x2, ball.y2); //draw the ball
rectangle(paddle.x1, paddle.y1, paddle.x2, paddle.y2); //draw the paddle
for(int y = 0; y < num_of_targ; y++)//draw the targets
{
if(target1[y].gone !=1)//draw unless they are gone
{
rectangle(target1[y].x1, target1[y].y1, target1[y].x2, target1[y].y2);
}
}
if(kbhit())//if the user presses a key
{
char x;
x = getch();//what key did he press?
if(x=='a')//if he pressed a
{
setcolor(BLACK);//these 3 lines erase the previous paddle
rectangle(paddle.x1, paddle.y1, paddle.x2, paddle.y2);
setcolor(WHITE);
paddle.x1 = paddle.x1 - 25; //this moves the paddle, its drawn on the next loop
paddle.x2 = paddle.x2 - 25;
}
if(x=='d') //same as above
{
setcolor(BLACK);
rectangle(paddle.x1, paddle.y1, paddle.x2, paddle.y2);
setcolor(WHITE);
paddle.x1 = paddle.x1 + 25;
paddle.x2 = paddle.x2 + 25;
}
if(x=='w') //same as above
{
setcolor(BLACK);
rectangle(paddle.x1, paddle.y1, paddle.x2, paddle.y2);
setcolor(WHITE);
paddle.y1 = paddle.y1 - 15; //yes, you can move up and down!
paddle.y2 = paddle.y2 - 15;
}
if(x=='s') //same as above
{
setcolor(BLACK);
rectangle(paddle.x1, paddle.y1, paddle.x2, paddle.y2);
setcolor(WHITE);
paddle.y1 = paddle.y1 + 15;
paddle.y2 = paddle.y2 + 15;
}
if(x=='q') //if he hits q then go away
{
gameend = 1;
}
}
}
}
void moveball() //makes the ball move by adding the magnitude of the ball to the coord
{
ball.x1 = ball.x1 + ball.xmag;
ball.x2 = ball.x2 + ball.xmag;
ball.y1 = ball.y1 + ball.ymag;
ball.y2 = ball.y2 + ball.ymag;
}
void eraseball() //gets rid of a ball before moving it
{
setcolor(BLACK);
rectangle(ball.x1, ball.y1, ball.x2, ball.y2);
setcolor(WHITE);
}
void getmag() //gets the magnitude of the ball, the ifs get tricky here
{
if(ball.x1 == 1) //if it hits the left wall
{
ball.xmag = ball.xmag * -1;//switch the magnitude
}
if(ball.x2 == 639)//if it hits the right wall
{
ball.xmag = ball.xmag * -1; //switch the magnitude
}
if(ball.y1 == 10)//if it hits the line below the header
{
ball.ymag = ball.ymag * -1;//switch the magnitude
}
if(ball.y2 == 479) //if it hits the ground
{
loss = 1;//you lose
//ball.ymag = ball.ymag * -1; <--- i used that for testing, it would bounce off ground
}
if(ball.y2 == paddle.y1 && (ball.x2 < paddle.x2 && ball.x1 > paddle.x1))
{ //yipes! if the bottom of the ball hits the top of the paddle and it is between the paddle
ball.ymag = ball.ymag * -1; //reverse the magnitude
}
//HITTING A TARGET (i cry)
for(int check = 0; check < num_of_targ; check++) //check all the targets
{
if(ball.y2 == target1[check].y1 && ( ball.x2 < target1[check].x2 && ball.x1 > target1[check].x1) && target1[check].gone!=1)
{//if it hits the top of a target with the bottom of the ball, and it is between the target
target1[check].gone = 1; //the target is gone
ball.ymag = ball.ymag * -1; //reverse the magnitude
setcolor(BLACK);//the next 3 erase the target
rectangle(target1[check].x1, target1[check].y1, target1[check].x2, target1[check].y2);
setcolor(WHITE);
--loop; //makes the ball faster
}
if(ball.y1 == target1[check].y2 && ( ball.x2 < target1[check].x2 && ball.x1 > target1[check].x1) && target1[check].gone!=1)
{ //does the same as above but this time if it hits the bottom of a target with the top of the ball
target1[check].gone = 1;
ball.ymag = ball.ymag * -1;
setcolor(BLACK);
rectangle(target1[check].x1, target1[check].y1, target1[check].x2, target1[check].y2);
setcolor(WHITE);
--loop;
}
}
}
//WOOT! done
-
-
Ok I have 2 suggestions, but if your program works fine I'd leave it and start something new.
(1): Instead of using square brackets [] declare a pointer and use that.
(2): Instead of using ball.y1 = ball.y1 + ball.ymag
use ball.y1 += ball.ymag.
The 2 above should improve the efficency of your program, especially number (2), plus it's less to type.
But as I said if your program works fine leave it.