Question: I am completing a java game of flying machine shooting meteorites, please help me complete some part of the java code as required below, thank
I am completing a java game of flying machine shooting meteorites, please help me complete some part of the java code as required below, thank you very much
Task Adding a Bullet Actor Class
Adding a Moving Bullet
To shoot a bullet you need to add a bullet image as anActorclass in the scenario. The bullet image is already given in the starting scenario, inside theimagesfolder The image looks like this:
In your starting scenario you need add a newActorsubclass using the above image. Let's call this classBullet
After adding the bullet class you will need to make it move. Even though your bullet is supposed to move along the direction of the spaceship, you only need to make it move to the right at this stage. This is similar to what we have done for theRockclass in the lesson. When you move the bullet you need to make it travel faster than the spaceship and the rock, say, using a distance value of when you move the bullet.
Removing the Bullet
Once your bullet moves out of the game area, you need to remove the bullet from the world. You have to add the code in theactmethod of the class. First you use an if statement to check whether the location of the bullet is not within the game area. If the bullet is out of the world you will then need to use theremoveObjectmethod to remove it
Task Shooting the Bullet from the Spaceship
Shooting a Bullet When the Spacebar Key Is Pressed
You can shoot a bullet by pressing the spacebar key. In theactmethod of the spaceship, you see the code to turn the spaceship left the left arrow key turn right the right arrow key and move forward the up arrow key You will add an additional key for controlling the shooting of the bullet. The key you need to use is the spacebar key. For example, the following code checks if the spacebar key, using the textspace has been pressed:
if GreenfootisKeyDownspace
shoot the bullet...
If the spacebar key has been pressed you can shoot the bullet by creating a new instance of theBulletclass and adding it to the world using theaddObjectmethod To do that, you can use the following code:
Bullet bullet new Bullet;
getWorldaddObjectbullet getX getY;
Checking for the Existing Bullet in the World
To solve the first problem you don't shoot the bullet every time the player presses the spacebar. You only shoot the bullet when there is no bullet currently on the screen.You can use thegetObjectsmethod of the world object to do that. For example, the following if statement checks whether there is any bullet in the world:
if getWorldgetObjectsBulletclassisEmpty
Turning the Bullet to the Direction of the Spaceship
You need to turn the bullet so that it travels in the direction of the spaceship.
There are two helpful methods in this task. The first method is thegetRotationmethod which allows you to get the current direction of an actor. Another method is thesetRotationmethod which lets you change the direction of an actor to a specific angle.You need to do this when you add the bullet to the world.
Task Updating the Score
The final task is to update the score of the game.
To do that, you simply get the score object from the world and then use theincreasemethod on the score. To get the score object, you again make use of thegetObjectsmethod from the world object. This time you want to get the score object and thus you need to give the value ofScoreclassto the method, like this:
Score score Score getWorldgetObjectsScoreclassget;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
