Programming C Problem Description: Your are to create a text base game to try an
ID: 3688720 • Letter: P
Question
Programming C
Problem Description:
Your are to create a text base game to try and hit a target in two dimensional plane (X,Y) using a cannon ball.
The program will randomly create 5 targets (5 sets of X,Y) and display the set on screen. These are the coordinates of the targets such that: 500 < X < 5000 (meter) and 500< Y < 5000(meter)
Player will have 3 chances to hit any of the 5 square targets which is 10 X 10 meters by entering the initial velocity and initial angle. The coordinate of the target is at the center of the target.
If the player hits a target, the program will display the name and the number of tries and message indicating that the player won and the time it took to hit the target (time of flight for the hit try), and the target number with beeps for indication that a target was hit. If the player did not hit any target in 3 tries, the program will display the initial angles and the initial velocities that were needed to hit each target and list them on the screen.
Regardless of win or lose, the program must record the target numbers and their coordinates in a file with the players name (a name from namepass.txt, below), for example leia.txt
The game can be played only if the user has the correct name and correct ID. The list of names and ID’s are given in a text file called namepass.txt, such as below
leia 12345
darth 23456
r2d2 34567
solo 45678
jabba 56789
yoda 67890
Game Requirements:
1.You must use array of structures for targets for each game. The members of the structure are: target # (1 to 5), coordinate of the target (X,Y)
2.The player must enter a correct name and ID (in namepass.txt file)to get access to the game
3.Player can choose to play again
4.Use random number generator for X and Y of each target
5.Must save the result in a file
More requirements, In addition to our standards:
The main function must do very little other than calling other functions and passing parameters to those functions.
Your program should be modularly designed with functions designed to do one task and one task well.
Use of pointers for arrays are encouraged
Do not use global variables. Pass data back and forth via parameters or as return values.
Make your functions as general as possible so that they can be called more than once if needed.
Document your main function as well as every function you write.
Use defined constants for ALL constants (if any) in your program.
Explanation / Answer
/*****************************************************/ #include #include #include #include #include #include /*****************************************************/ /***************** Gloabal Variables **********************/ float initial_velocity, angle_of_projection,g=9.8; /**/ int h=40,k=450,radius=10; /**/ float loc;/*Used to know the location of the tank*/ /**/ float x1,y1,x2,y2;/*line(x1,y1,x2,y2) for the cannon*/ /**/ /**/ float maximum;/*maximum velocity allowed*/ /**/ /*for a particular planet*/ /**/ int count;/*Lives*/ /**/ int loc_speed=0;/*speed bar location*/ /**/ int points;/*points*/ /**/ /**********************************************************/ /******************** FUNCTIONS ***************************/ void rotate(int,int,int,int,int,int,float); /**/ void wheel(int,int,int); /**/ void projectile(float,float,float,int,int); /**/ int enemy_tank(int turret_x,int turret_y); /**/ void battlefield(void); /**/ int get_the_initial_x(float); /**/ int get_the_initial_y(float); /**/ float detect_collision_for_tank(int turret_x, /**/ int turret_y,float x,float y); /**/ void blast(int x, int y,float r); /**/ void main_menu(void); /**/ void menu_selection(void); /**/ void startgame(void); /**/ void game_over(void); /**/ void select_planet(void); /**/ void speedbar(void); /**/ void remove_speedbar(void); /**/ void show_instructions(void); /**/ void credits(void); /**/ /**********************************************************/ int main(void) { int i=0; int gd=VGA,gm=2; initgraph(&gd,&gm,"c:\tc\bgi"); /*THE PATH ABOVE SHOULD BE CHANGED ACCORDINGLY*/ do { main_menu(); }while(i==0); return 0; } /*STARTS THE GAME*/ void startgame(void) { select_planet(); show_instructions(); count=10;/*NUMBER OF LIVES*/ points=0; randomize(); battlefield(); setlinestyle(0,0xFFFF,3); setcolor(15); line(h-radius*1.5,k,h+radius*2.5,k); rotate(h-radius*1.5,k,h+radius*2.5,k,h,k,radius); x1=y1=x2=y2=0; } /*DRAWS A WHEEL AT (h,k) OF RADIUS "radius"*/ void wheel (int h,int k, int radius) { int slice; circle(h,k,radius/2.5); setfillstyle(EMPTY_FILL,15); for (slice=0;slice=-initial_y && x=0;t=t+.01) { x=(initial_velocity*cos(a_rad)*t); y=(initial_velocity*sin(a_rad)*t)-(0.5*g*t*t); collision=detect_collision_for_tank(loc,k-20,x,y); if (collision 1 && points%25==0) /*INCREASES LIFE AFTER EVERY 25 SUCCESSFUL HITS*/ { sound(440); delay(1000); count++; nosound(); } if(angle_of_projection>=60) /*INCREASES LIFE IF ANGLE>=60 HITS*/ { count=count++; sound(640); delay(1000); nosound(); } battlefield(); break; } setcolor(14); circle(initial_x+x,initial_y-y,2); setfillstyle(SOLID_FILL,14); floodfill(initial_x+x,initial_y-y,14); delay(1); setcolor(0); circle(initial_x+x,initial_y-y,2); setfillstyle(SOLID_FILL,0); floodfill(initial_x+x,initial_y-y,0); } if (collision>0) /*NO COLLISION NO POINTS BUT LESS LIVES*/ count--; } /*DRAWS THE ENEMY TANK AT RANDOM POSITIONS*/ int enemy_tank(int turret_x,int turret_y) { int c=4; int i; setlinestyle(0,0xFFFF,1); setcolor(c); /*TURRET*/rectangle(turret_x,turret_y,turret_x+8,turret_y+2); /*HEAD*/ rectangle(turret_x+8,turret_y-3,turret_x+16,turret_y+6); /*BODY*/ rectangle(turret_x-8,turret_y+6,turret_x+32,turret_y+24); setfillstyle(HATCH_FILL,c); floodfill(turret_x+8+1,turret_y-3+1,c); setfillstyle(SOLID_FILL,c); floodfill(turret_x-8+1,turret_y+6+1,c); floodfill(turret_x+1,turret_y+1,c); for(i=0;i0) line(x1,y1,x2,y2); loc=enemy_tank(h+100+random(470),k-20); } /*RETURNS THE X-VALUE FROM WHERE THE PROJECTILE SHOULD BE SHOT*/ int get_the_initial_x(float a) { int initial_x; initial_x=h+cos(a*3.141592/180) *pow(((h-h+radius*2.5)*(h-h+radius*2.5)+(k-k)*(k-k)),.5); return initial_x+2.5; } /*RETURNS THE Y-VALUE FROM WHERE THE PROJECTILE SHOULD BE SHOT*/ int get_the_initial_y(float a) { int initial_y; initial_y=k-sin(a*3.141592/180) *pow(((h-h+radius*2.5)*(h-h+radius*2.5)+(k-k)*(k-k)),.5); return initial_y-2.5; } /*RETURNS 0 OR NEGATIVE VALUE IF A COLLISION IS DETECTED*/ float detect_collision_for_tank(int turret_x,int turret_y,float x,float y) { float a,b,r; float collision; a=((turret_x-8)+(turret_x+32))/2; b=((turret_y+6)+(turret_y+24))/2; r=20; //((turret_x+32)-(turret_x-8))/2 collision=pow((get_the_initial_x(angle_of_projection)+x-a),2)+ pow((get_the_initial_y(angle_of_projection)-y-b),2)- pow(r,2); return collision; } /*THE EXPLOSION*/ void blast(int x, int y,float r) { float i; for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.