Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When you create your CN1 project, you should name the main class as Starter. Then you should modify the start() method of the Starter class

When you create your CN1 project, you should name the main class as Starter. Then you should modify the start() method of the Starter class so that it would construct an instance of the Game class. The other methods in Starter (i.e., init(), stop(), destroy()) should not be altered or deleted. The Game class must extend from the build-in Form class (which lives incom.codename1.ui package). The Game constructor instantiates a GameWorld, calls a GameWorld method init() to set the initial state of the game, and then starts the game by calling a Game method play(). The play() method then accepts keyboard commands from the player and invokes appropriate methods in GameWorld to manipulate and display the data and game state values in the game model. Since CN1 does not support getting keyboard input from command prompt (i.e., the standard input stream, System.in, supported in Java is not available in CN1) the commands will be entered via a text field added to the form (the Game class). Referto Appendix CN1 Notes for the code that accepts keyboard commands through the text fieldlocated on the form.

The following shows the pseudo-code implied by the above description. It is important for things that we will do later that your program follows this organization:

Game World Objects

For now, assume that the game world size is fixed and covers 1024(width) x 768(height)area (although we are going to change this later). The origin of the world (location (0,0)) is thelower left hand corner. The game world contains a collection which aggregates objects of abstract type GameObject. There are two kinds of abstract game objects: fixed objects with fixed locations (which are fixed in place) and moveable objects with changeable locations(which can move or be moved about the world). For this first version of the game there is just a single kind of fixed object: a space station; and there are three kinds of moveable objects:ships (Player Ship (PS) and Non-Player Ship (NPS), missiles (which are objects fired by ships), and asteroids. Later we will see that there are other kinds of game objects as well.

The various game objects have attributes (fields) and behaviors (functions or methods) as defined below. These definitions are requirements which must be properly implemented in your program.

speed of a player ship) command where a s command (add a player ship to the world) was not initiated before. The allowable input commands and their meanings are defined below (note that commands are case sensitive):

a

y b s

i d

l (ell) turn PS left by a small amount (change the heading of the ship by a small amount in the counter-clockwise direction). Note that determination of the actual new heading is not the responsibility of the game class; it simply instructs the game world that a change is to be made.

r turn PS right (change the ship heading by a small amount in the clockwise direction).

> - a user controls the direction of the PSs MissileLauncher by revolving it about the center of the player ship in a clockwise direction. This command turns the MissileLauncher by small angle. You decide a value.

f fire a missile out the front of the PS. If the ship has no more missiles, an error message should be printed; otherwise, add to the world a new missile with a location, speed, andlaunchers heading determined by the ship.

L Launch a missile out the front of the NPS. If the ship has no more missiles, an error message should be printed; otherwise, add to the world a new missile with a location, speed, and launchers heading determined by the ship.

j jump through hyperspace. This command causes the ship to instantly jump to the initial default position in the middle of the screen, regardless of its current position. (This makes it easy to regain control of the ship after it has left visible space; later we will see a different mechanism for viewing the ship when it is outside screen space.)

n load a new supply of missiles into the PS. This increases the ships missile supply tothe maximum. It is permissible to reload missiles before all missiles have been fired, but it is not allowed for a ship to carry more than the maximum number of missiles. Note that normally the ship would actually have to fly to a space station to reload; for this version of the game we will assume the station has transporters that can transfer new missiles to the ship regardless of its location.

k a PSs missile has struck and killed an asteroid; tell the game world to remove a missile and an asteroid and to increment the players score by some amount of your choosing.You may choose any missile and asteroid to be removed; later well worry about missiles needing to be close to their victims.

e a PSs missile has struck and eliminated a NPS; tell the game world to remove a missile and a NPS and to increment the players score by some amount of your choosing. You

E c

h x --

I --t

p

m q

For this assignment, all output will be in text on the console; no graphical output is required.The map (generated by the m command) will simply be a set of lines describing the objects currently in the world, similar to the following:

Player Ship: loc=130.0,565.5 color=[0,255,0] speed=8 dir=90 missiles=5 Missile launcher dir = 50PSs Missile: loc=180.3,100.0 color=[255,0,0] speed=10 dir=112 fuel=7 NPSs Missile: loc=50.9,540.2 color=[255,0,0] speed=5 dir=313 fuel=1 Non-Player Ship: loc=640.2,20.0 color=[128,128,128] speed=6 dir=20 size=10

Asteroid: loc=440.2,20.0 color=[128,128,128] speed=6 dir=0 size=6 Asteroid: loc=660.0,50.0 color=[128,128,128] speed=1 dir=90 size=26 Station: loc=210.2,800.0 color=[255,255,0] rate=4

Note that the appropriate mechanism for implementing this output is to override thetoString() method in each concrete game object class so that it returns a String describing itself. See the coding notes Appendix (see page 11 below) for additional details, including how to display single decimal value. Please be conformed to the format specified in above example.

  • Missiles are considered to have special radar that allows them to avoid hitting their own ships, other missiles, and space stations, so we will not worry about handling these collision conditions. Likewise, we will assume that space stations automatically avoid asteroids and NPS.

  • The program must handle any situation where the player enters an illegal command for example, a command to increase the ship speed when no ship has yet been added to the world by printing an appropriate condition-specific error message on the console (forexample, Cannot execute increase no ship has been created) and otherwise simplywaiting for a valid command.

  • The program is not required to have any code that actually checks for collisions betweenobjects; thats something well be adding later. For now, the program simply relies on the user to say when such events have occurred, using for example the k and c commands.

  • You must follow standard Java coding conventions:

    o class names always start with an upper case letter, o variable names always start with a lower case letter, o compound parts of compound names are capitalized (e.g., myExampleVariable),o Java interface names should start with the letter I (e.g., ISteerable).

  • Your program must be contained in a CN1 project called A1Prj. You must create your projectfollowing the instructions given at 2 Introduction to Mobile App Development and CN1lecture notes posted at Canvas (Steps for Eclipse: 1) File New Project CodenameOne Project. 2) Give a project name A1Prj and check Java 8 project 3) Hit Next. 4) Give a main class name Starter, package name com.mycompany.a1, and select a native theme, and Hello World(Bare Bones) template (for manual GUI building). 5) Hit Finish.).

Further, you must verify that your program works properly from the command prompt before submitting it to Canvas: First make sure that the A1Prj.jar file is up-to-date. If not, under eclipse, right click on the dist directory and say Refresh. Then get into the A1Prj directory and type (all in one line): java -cp dist\A1Prj.jar;JavaSE.jarcom.codename1.impl.javase.Simulator com.mycompany.a1.Starter for Windows machines(for the command line arguments of Mac OS/Linux machines please refer to the class notes). Alternatively, to prove it is working, you can also run the instructor provided programRunAssignment.jar (see class notes). Substantial penalties will be applied to submissions which do not work properly from the command prompt.

  • You are not required to use any particular data structure to store the game world objects, butall your game world objects must be stored in a single structure. In addition, your program must be able to handle changeable numbers of objects at runtime so you cant use a fixed- size array, and you cant use individual variables.

    Points will be deducted for poorly or incompletely documented programs. Use of JavaDoc- style comments is highly encouraged, but not required.

  • Students are encouraged to ask questions or solicit advice from the instructor outside of class. But have your UML diagram ready it is the first thing the instructor will ask to see.

package com.mycompany.a1;

import static com.codename1.ui.CN.*; import com.codename1.ui.Display; import com.codename1.ui.Form; import com.codename1.ui.Dialog; import com.codename1.ui.Label; import com.codename1.ui.plaf.UIManager; import com.codename1.ui.util.Resources; import com.codename1.io.Log; import com.codename1.ui.Toolbar; import java.io.IOException; import com.codename1.ui.layouts.BoxLayout; import com.codename1.io.NetworkEvent;

/** * This file was generated by Codename One for the purpose * of building native mobile applications using Java. */ public class Starter {

private Form current; private Resources theme;

public void init(Object context) { // use two network threads instead of one updateNetworkThreadCount(2);

theme = UIManager.initFirstTheme("/theme");

// Enable Toolbar on all Forms by default Toolbar.setGlobalToolbar(true);

// Pro only feature, uncomment if you have a pro subscription Log.bindCrashProtection(true);

addNetworkErrorListener(err -> { // prevent the event from propagating err.consume(); if(err.getError() != null) { Log.e(err.getError()); } Log.sendLogAsync(); Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null); }); } public void start() { if(current != null){ current.show(); return; } new Game(); //Form hi = new Form("Hi World", BoxLayout.y()); //hi.add(new Label("Hi World")); //hi.show(); }

public void stop() { current = getCurrentForm(); if(current instanceof Dialog) { ((Dialog)current).dispose(); current = getCurrentForm(); } } public void destroy() { }

}

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

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

What is quality of work life ?

Answered: 1 week ago

Question

What is meant by Career Planning and development ?

Answered: 1 week ago

Question

What are Fringe Benefits ? List out some.

Answered: 1 week ago