Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This exercise will involve a game that simulates a landing craft descending to the surface of the moon.You may have played this game before, we'll

This exercise will involve a game that simulates a landing craft descending to the surface of the moon.You may have played this game before, we'll implement a simple version of it.In our version, you are in control of a lander that is descending toward the moon.Gravity steadily accelerates your craft towards the moon's surface.You have two controls: thrust, which accelerates the craft in the direction it's currently facing, and turning, which adjusts the orientation of the craft to the left or right.Your goal is to get your lander to be moving at a slow enough speed that it doesn't crash upon impact.However, you have a limited amount of fuel, and when that runs out, you can no longer apply thrust or turn.You will also need to avoid falling meteors that threaten to destroy your ship on impact.

Problem A. (10points)Simulating Gravity

To get you started, we'll give some pretty detailed instructions for the first few steps of the project: hopefully you can extrapolate from those steps to figure out how to proceed in parts B and C as well.

Our first task is to set up classes to represent the entities that need to be manipulated for the game.One reasonable division of functionality is to have the class for the player controlled lander, and another overarching class for the game as a whole (we'll talk about how to deal with the meteors later).We're going to have the lander class derive from Turtle, since a lot of the functionality we want (a movable image on a screen) is already provided by Turtle.

First, importturtle,math, andrandomat the top of the file.Then, define a class calledLanderto represent an object falling towards the moon's surface, and have it inherit from theturtle.Turtleclass.

The__init__method ofLandershould take in five parameters:

  • self
  • the starting x position for the lander
  • the starting y position
  • the starting x velocity
  • the starting y velocity

The last four values should all be numerical types: integers or floating point numbers.The first thing __init__ should do is call the constructor for its base class (turtle.Turtle).You can do by just using

turtle.Turtle.__init__(self) 

sinceturtle.Turtle's constructor does not take any arguments other than self.

At this point, you should be able to test your code to make sure that you have properly inherited fromturtle.Turtle.For example, if you run the line

>>> Lander(100, -50, 0.5, -0.3) 

then you should get a turtle window to pop up with the lander (i.e. the turtle) in the middle of the screen.This will happen regardless of what you put in for the arguments to the constructor, since you're not doing anything with those values just yet.

Next, we want to set up the modified turtle, including a few instance variables that don't appear in the originalturtle.Turtleclass.Do the following inLander.__init__:

  • Turtles don't normally have a velocity, so create two instance variables (self.vxandself.vy) to represent the lander's velocity in the x and y directions, and initialize them to the appropriate parameters.
  • We'll also need an instance variable to represent the fuel remaining for our lander.define an instance variable to track the fuel remaining and set it equal to 50.
  • We want the lander to be initially facing upwards, rather than to the right.Remember, the Lander class inherits fromturtle.Turtle, so that means that Lander hasevery method that turtle.Turtle has(Links to an external site.)
  • .So we can use the.left(Links to an external site.)
  • method on our Lander object (self) to turn it 90 degrees to the left, with the lineself.left(90).
  • Use a similar strategy to apply the.penup(Links to an external site.)
  • ,.speed(Links to an external site.)
  • , and.setpos(Links to an external site.)
  • Turtle methods in order to cause our Lander object (self) to stop drawing, change its speed to 0 (which is weirdly the fastest possible speed), and move to the initial x and y coordinates that were passed in as parameters.

At this point, if you re-run the line from earlier, then you should now see that the lander is pointing upwards, and should be located at (100, -50) rather than the middle of the screen at (0, 0).

>>> Lander(100, -50, 0.5, -0.3) 

Next, define a new classGame: this will be responsible for keeping the game running, keeping track of the meteors, and so on.Gameshould not inherit from anything.Game.__init__should only take in self as a parameter, and should do the following:

  • turtle.setworldcoordinates(0, 0, 1000, 1000)(Links to an external site.)
  • : this will ensure that regardless of what platform you're running the program on, the lower left corner of the screen will be (0, 0), and the upper right will be (1000, 1000).
  • turtle.delay(0)(Links to an external site.)
  • to keep the animation moving quickly
  • define aLanderobject to be controlled by the player and set it as an instance variable calledself.player.You should pass the following values into theLanderconstructor:
  • The initial x coordinate should be a random float between 100 and 900.You can userandom.uniform(Links to an external site.)
  • to get a random floating point number.
  • The initial y coordinate should be a random float between 500 and 900.
  • The initial x velocity should be a random float between -5 and 5.
  • The initial y velocity should be a random float between -5 and 0.
  • Note that you are creating aLanderobject within theGameobject (Gamehas-aLander), not using inheritance (Gameis-aLander), so you should be calling theLanderconstructor directly usingLander(...), not usingLander.__init__(...).
  • Depending on the size of your screen, you may want to double the size of the lander to make it easier to see.You can do it with the.turtlesize(Links to an external site.)
  • Turtle method.Remember, you can use any Turtle method on your Lander object because Lander inherits from turtle.Turtle, so you'd useself.player.turtlesize(2).

We'll need to add more to this method later, but for now, put a call toGame()at the bottom of yourhw13.pyfile and run it.Each time you run the file, you should see the lander appear in a random location on the top half of the screen.

Next, we need to add animation.This requires three steps.

  • First, define a methodmove(self)inLanderthat moves the Lander to a new position each step, based on its current velocity.This should consist of:
  • Subtract 0.0486 from the y velocity (this represents the downward acceleration due to gravity on the moon).
  • Compute the new x position by getting previous x position using thexcor()(Links to an external site.)
  • Turtle method, and then adding the x velocity.
  • Do the same for the new y position usingycor()(Links to an external site.)
  • Usesetpos(Links to an external site.)
  • to move the lander to the new (x, y) position.
  • Second, define a methodgameloop(self)in Game that does two things:
  • Calls themovemethod for the player controlled Lander (self.player)
  • Calls itself again 30 milliseconds from now.You can do it usingturtle.Screen().ontimer(self.gameloop, 30).
  • Finally, add a call toself.gameloop()at the end ofGame.__init__, so that the animation cycle is started immediately.

If you completed these steps correctly, then you should see the your Lander shoot off in a random initial direction, but then start to slowly accelerate downwards.If you want to be sure the acceleration component is working correctly, temporarily set your initial x and y velocity to 0 and see if the Lander accelerates downwards.

Finally, let's implement the thrust function: we want it so that pressing the 'Up' arrow will cause the Lander to increase velocity in whatever direction its currently pointing.First, deifine a function calledthrust(self)in theLanderclass.For now, just have this method print out "Up button pressed".Then, inGame.__init__, add the following three lines to the end of the method:

turtle.onkeypress(self.player.thrust, 'Up') turtle.listen() turtle.mainloop() 

These lines cause turtle to call the thrust method on your player Lander every time the Up arrow is pressed.Test your program to ensure that it's currently printing "Up button pressed" whenever you press the up key.You may need to click on the turtle graphics window first to get it to register, and this may fail to work entirely in repl.it under some conditions.

Assuming that worked, alter your thrust function to do the following:

  • If there is greater than 0 fuel remaining, this should:
  • Reduce the amount of fuel by 1
  • Get the angle the ship is pointing at in radians - usemath.radians(Links to an external site.)
  • (self.heading()(Links to an external site.)
  • )
  • Get the cosine of that angle, and add it to the x velocity (usemath.cos(Links to an external site.)
  • ).Think about why cosine of the angle tells us what proportion of the unit of thrust to add to the x velocity.
  • Get the sine of that angle, and add it to the y velocity (usemath.sin(Links to an external site.)
  • )
  • Print out the number of fuel units remaining.
  • If there is no fuel remaining, the method should instead just print out "Out of fuel".

Now each time you press the up button, the ship should either slow its fall, or start accelerating upwards if you hit it enough times (unless you press it 50 times, in which case you'll run out of fuel and it shouldn't cause the ship to change speed).Test this further by changing the initial turn angle to something other than 90 degrees, and ensure that the acceleration is always in the direction the Lander is pointing, even when that direction isn't up.

Problem B. (5points)Turning

Add additional methods so that the Lander turns left 10 degrees whenever the left arrow key is pressed, and right 10 degrees whenever the right arrow key is pressed.This requires one fuel, so similar to the thrust method, nothing should happen if there is no fuel remaining, and if there is fuel left then one fuel should be removed each time a turn occurs.Be sure to print out the remaining fuel each time a turn happens.

Problem C. (5points)Game Over

Change your gameloop method in the Game class so that it stops calling itself in the case that the Lander reaches the bottom of the screen (say, the y coordinate is less than 10), causing the game to stop running.When this happens, if the Lander's speed in either the x or the y direction is greater than 3, then print out "You crashed!".Otherwise, print out "Successful landing!".If you wish, you can useturtle.write(Links to an external site.)

to output the message to the turtle window rather than print it out.

Problem D. (10points)Meteors

Add red circular meteors to your game.The implementation details are left up to you, but these should continually appear at the top of the screen and accelerate downward due to gravity.If the Lander crashes into any Meteor, then the game should end and result in a "You crashed!" message, just as if you had landed at too great a speed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions