CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Wei Cui
Level 39
Newark
  • 14.04.2020
  • 711views
  • 1comment

how to fix it

Question about the task Space (part 13)
Java Collections,  Level 3,  Lesson 15
Resolved

We now turn to the draw and move methods.
In the move() method, you need to:
a) increase x by dx;
b) check to see whether the ship has moved beyond the boundaries of outer space [0, Space.game.getWidth()]. Use the checkBorders method.
Bear in mind that the ship's width is equal to twice its radius.

I'll implement the draw method myself: just declare an empty method.

We also need a fire() method, since our ship can shoot.
This method is called when we need to fire a shot.

In this method, you need to:
a) create two rockets;
b) set their coordinates equal to the left and right edges of the ship (the guns are located a distance of 2 from the center of the ship);
c) add these rockets to the list of rockets in the game object.

You can get it as follows:
Space.game.getRockets()

Requirements:
  • In the Spaceship class, implement the move() method, which increases the x field by the value of dx and checks whether the ship has reached the limits of outer space.
  • Add an empty draw(Canvas canvas) method in the Spaceship class.
  • In the Spaceship class, implement the fire() method, which creates two rockets, whose coordinates are equal to the left and right edges of the ship, and adds them to the game object's rockets field.
package com.codegym.task.task25.task2515; import java.util.ArrayList; import java.util.List; public class Space { private int width, height; private Spaceship ship; private List<Ufo> ufos = new ArrayList<>(); private List<Rocket> rockets = new ArrayList<>(); private List<Bomb> bombs = new ArrayList<>(); public static Space game; public Space(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public int getHeight() { return height; } public Spaceship getShip() { return ship; } public List<Ufo> getUfos() { return ufos; } public List<Rocket> getRockets() { return rockets; } public List<Bomb> getBombs() { return bombs; } public void setShip(Spaceship ship) { this.ship = ship; } public void run() {} public void draw() {} public void sleep (int ms) {} public static void main(String[] args) {} }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Wei Cui
Level 39 , Newark, United States
15 April 2020, 16:17solution
checkBorders(radius * 2, Space.game.getWidth() - radius * 2, radius * 2, Space.game.getHeight() - radius * 2);
+6
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.