Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

can some help with my loop in my main code.

OpenStudy (anonymous):

#include "ccc_win.h" #include <Windows.h> #include <iostream> #include <string> #include "ccc_time.cpp" /** Draw ships with set variable to call in a set point of origin **/ void drawship (double x, double y) { // Spaceship Wings // /* Points for spaceship ( x,y values) used to form lines to create a structure*/ Point starting_area (x,y); Point a(x-2,y+2); Point b(x-2,y-2); Point c(x+2,y-2); Point d(x+2,y+2); Point e(x-1,y+1); Point f(x-1,y-1); Point g(x+1,y-1); Point i(x+1,y+1); Point j(x+0,y+2); Point k(x+0,y-2); /* Lines to create wings using Points */ Line h(a,c); Line v(d,b); Line h1(b,a); Line v1(d,c); Line h2(e,j); Line v2(i,j); Line h3(f,k); Line v3(g,k); // Hub of spaceship // /* Types for circle (radius) */ Point p(x+0,y+0); /* variable to create circle */ Circle hub (p,1); //Spaceship as whole(looks kind of like Green Lanterns ring)// /* Output command to create Spaceship*/ cwin.clear(); cwin << h << v1 << h1 << v << h2 << v2 << h3 << v3 << hub; } void landscape() { // Landscape // /* Points for landscape */ Point l(-10,-7); Point m(10,-7); Point n(2,-2); Point o(-2,-2); Point q(3,-2); Point q1(7,-2); Point q2(-3,-2); Point q3(-7,-2); Point q4(-7,-7); Point q5(-3,-7); Point q6(-2,-7); Point q7(2,-7); Point q8(3, -7); Point q9(7,-7); /* Lines to bring landscape to life using Points provided */ Line h4(l,m); Line v4(n,o); Line h5(q,q1); Line v5(q2,q3); Line h6(q3,q4); Line v6(q2,q5); Line h7(o,q6); Line v7(n,q7); Line h8(q,q8); Line v8(q1,q9); // Shows Landscape drawn out // /* command to output landscape using lines created */ cwin << h4 << v4 << h5 << v5 << h6 << v6 << h7<< v7 << h8 << v8; } //gets new position for circle double New_Posistion(double& initial_pos, double& initial_vel, double t, double acceleration) { double new_pos = initial_pos + t * initial_vel + 0.5 * t * t * acceleration; return new_pos; } //gets new velocity for circle double New_velocity(double initial_vel, double acceleration, double t, double new_v) { new_v = initial_vel + acceleration * t; return new_v; } /* Vertical/Main Thruster: (l)ow = "l" = 0 (h)igh = "h" = 1 (n)one = "n" = -1 Horizontal, Right Thruster (on right side of circle, pushing circle left): (l)ow = "l" = -0.5 (h)igh = "h" = -1 (n)one = "n" = 0 Horizontal, Left Thruster (on left side of circle, pushing circle right): (l)ow = "l" = 0.5 (h)igh = "h" = 1 (n)one = "n" = 0 */ void value_HV(double & horizontal, double & vertical) { double horizontal_r; double horizontal_l; // Vertical Main Thruster string vertical_mainthrust = cwin.get_string("How Much thrust would you like? Enter l for low , h for high, n for none :") ; if(vertical_mainthrust == "l") { vertical = 0; } else if(vertical_mainthrust == "h") { vertical = 1; } else if(vertical_mainthrust =="n") { vertical = -1; } // Horizontal, Right Thruster string horizontal_right = cwin.get_string("How Much thrust to the right would you like? Enter l for low , h for high, n for none :") ; if(horizontal_right =="l") { horizontal_r = -0.5; } else if(horizontal_right =="h") { horizontal_r = -1; } else if(horizontal_right =="n") { horizontal_r = 0; } // Horizontal, Left Thruster string horizontal_left = cwin.get_string("How Much thrust to the left would you like? Enter l for low , h for high, n for none :") ; if(horizontal_left == "l") { horizontal_l = 0.5; } else if(horizontal_left =="h") { horizontal_l = 1; } else if(horizontal_left == "n") { horizontal_l = 0; } horizontal = horizontal_r + horizontal_l; } void draw_explosion() { Point r(1,0); Point s(-2,0); Point t(-2,-2); Point u(2,-2); Line h8(r,s); Line v8(s,t); Line h9(t,u); Line v9(u,r); cwin << h8 << v8<< h9 << v9; } void collision_detection(double x1, double y1) { if(x1 >= -3 && x1 <= -4 && y1 <= -6) { draw_explosion; } else if (x1 >= -3 && x1 <= 4 && y1 <= -6) { draw_explosion; } else if (x1 >= -3 && x1 <= 4 && y1 <= -6) { draw_explosion; } } int ccc_win_main() { landscape(); double x = 0; double y = 5; double horizontal; double vertical; value_HV(horizontal, vertical); double velocity_x = 0; double velocity_y = 0; double t; for (t = 0; t <= 5; t += 0.01) { landscape(); Sleep(75); drawship(x,y); velocity_x = New_velocity (velocity_x, horizontal,t,x); x = New_Posistion (x, velocity_x, t, y); velocity_y = New_velocity (velocity_y, vertical,t,y); y = New_Posistion (y, velocity_y, t, y); for (t = 0; t <= 3; t += 0.01) { landscape(); Sleep(75); drawship(x,y); velocity_x = New_velocity (velocity_x, horizontal,t,x); x = New_Posistion (x, velocity_x, t, y); velocity_y = New_velocity (velocity_y, vertical,t,y); y = New_Posistion (y, velocity_y, t, y); } } return 0; }

OpenStudy (anonymous):

For this assignment you will implement the final moon lander game! To do this, you will need to add code to, and modify your work from programming assignment 7. Specifically, you will add a function that will draw your ship instead of a circle, add some code to ccc_win_main, create a collision detection function, add a function that draws a simple landscape, and add a function that draws an explosion based on the position of the ship. I know, it seems like a lot, but many of the above additions are trivial (e.g. using a function to draw your ship--you already did that in assignment 6, the explosion drawing function works just like the draw ship function, etc...). The draw landscape function is passed nothing, and returns void, the draw explosion function is passed a point that represents the bottom center of your ship, and returns void. Keep your landscape simple, have no more than THREE places to land! Keeping your landscape simple will ensure our simple collision detection function will work! For the collision detection function, you will pass it a SINGLE Point object that represents the bottom center of the ship. The function will then check to see if that point is inside your landscape, if it is, the function will return true, otherwise it will return false. The easiest way to do this is to create a landscape using only horizontal & vertical lines (no diagonal), which allows you to divide your landscape into rectangles, the area under each horizontal line would constitute a rectangle. Then, all you need to do is check if your point lies within any of the rectangles. For example: if the upper left corner of the square was at point (0,3500), and the lower right point of the square was at point (2000,0), to check if the point representing your ship was inside the box you would use the statement: if(x >= 0 && x <= 2000 && y <= 3500) { //inside the square } else { //outside the square } This would need to be done for each square, so you will need to use a switch statement or an if-else if structure. Because this is a relatively simple way to check for collisions, it is OK if your ship *lands* on a wall (vertical line). Also, since the point representing your ship will be in the at the ship's center, it is OK if half your ship passes through a vertical line/wall before the collision is detected. You do not need to worry about collision with the window's edge. You will then need to add more code your ccc_win_main function. The program should continue until the ship either lands, or explodes. At the point in ccc_win_main where you were previously drawing the circle, you will now need to call your collision detection function to decide whether you will draw the ship, or draw an explosion. If there is no collision you can just draw the ship, otherwise you need to check the ship's current velocity. If the ship is over some acceptable negative velocity, it has landed safely, otherwise KABOOM! Using the code that was given for assignment 7, I found that any velocity greater than or equal to -17.5 was acceptable but difficult to land. For an easier game (and testing my code), a velocity greater than or equal to -35 was reasonable. After the ship lands safely or explodes, you will use a break statement to exit the main animation loop, this should cause the program to end. Lastly, you must provide in the header area of your code, a list of commands that will cause the ship to land safely on your landscape. Write the commands in the format below: /* other header info as specified above, name, etc... 1) Main: <value>, Right: <value>, Left: <value> 2) Main: <value>, Right: <value>, Left: <value> . . . n) Main: <value>, Right: <value>, Left: <value> */ To receive full credit, the ship must be able to land in greater than or equal to 5 commands, and less than or equal to 15 commands (5 <= n <= 15). Each set of three thruster values input by the user is considered one command.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!