CSUS 1 of 13 CSUS COLLEGE OF ENGINEERING AND COMPUTER SCIENCE Department of Computer Science CSc 133 – Object-Oriented Computer Graphics Programming Fall 2021 Dr. Muyan Assignment #1: Class...

1 answer below »
everything is in file


CSUS 1 of 13 CSUS COLLEGE OF ENGINEERING AND COMPUTER SCIENCE Department of Computer Science CSc 133 – Object-Oriented Computer Graphics Programming Fall 2021 Dr. Muyan Assignment #1: Class Associations & Interfaces Due Date: Monday, September 27th (11:59 PM) Introduction This semester we will study object-oriented graphics programming and design by developing a simple video game we’ll call OnTarget. In this game, you (the player) will be controlling an ant walking around a path while trying to avoid collisions with spiders and keeping your ant fed. The goal of this first assignment is to develop a good initial class hierarchy and control structure by designing the program in UML and then implementing it in Codename One (CN1). This version will use keyboard input commands to control and display the contents of a “game world” containing the set of objects in the game. In future assignments, many of the keyboard commands will be replaced by interactive GUI operations, and we will add graphics, animation, and sound. For now we will simply simulate the game in “text mode” with user input coming from the keyboard and “output” being lines of text on the console. Program Structure Because you will be working on the same project all semester, it is extremely important to organize it correctly from the beginning. Pay careful attention to the class structure described below and make sure your program follows this structure accurately. The primary class in the program encapsulates the notion of a Game. A game in turn contains several components, including (1) a GameWorld which holds a collection of game objects and other state variables, and (2) a play() method to accept and execute user commands. Later, we will learn that a component such as GameWorld that holds the program’s data is often called a model. The top-level Game class also manages the flow of control in the game (such a class is therefore sometimes called a controller). The controller enforces rules such as what actions a player may take and what happens as a result. This class accepts input in the form of keyboard commands from the human player and invokes appropriate methods in the game world to perform the requested commands – that is, to manipulate data in the game model. In this first version of the program the top-level Game class will also be responsible for displaying information about the state of the game. In future assignments, we will learn about a separate kind of component called a view which will assume that responsibility. 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 built-in Form class (which lives in com.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 2 of 13 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). Refer to “Appendix – CN1 Notes” for the code that accepts keyboard commands through the text field located 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 OnTarget game world size is fixed and covers 1000 (width) x 1000 (height) area (although we are going to change this later). The origin of the “world” (location (0,0)) is the lower left hand corner. The game world contains a collection which aggregates objects of abstract type GameObject. There are two abstract kinds of game objects called: “fixed objects” of type Fixed with fixed locations (which are fixed in place) and “moveable objects” of type Movable with changeable locations (which can move or be moved about the world). For this first version of the game there are two concrete types that fixed objects are instantiated from which are called: Flag and FoodStation; and there are two concrete types that moveable objects are instantiated from which are called: Ant and Spider. Later we may add other kinds of game objects (both Fixed kinds and Moveable kinds) as well. The various game objects have attributes (fields) and behaviors (methods) as defined below. These definitions are requirements which must be properly implemented in your program. • All game objects have an integer attribute size. All game objects provide the ability for external code to obtain their size. However, they do not provide the ability to have their size changed once it is created. As will be specified in the later assignments, each type of game class Starter { //other methods public void start() { if(current != null){ current.show(); return; } new Game(); } //other methods } public class GameWorld { public void init(){ //code here to create the //initial game objects/setup } // additional methods here to // manipulate world objects and // related game state data } import com.codename1.ui.Form; public class Game extends Form{ private GameWorld gw; public Game() { gw = new GameWorld(); gw.init(); play(); } private void play() { // code here to accept and // execute user commands that // operate on the game world //(refer to “Appendix - CN1 //Notes” for accepting //keyboard commands via a text //field located on the form) } } 3 of 13 object has a different shape which can be bounded by a square. The size attribute provides the length of this bounding square. All flags have the same size (chosen by you), assigned when they are created (e.g., size of all flags can be 10). You should also assign a size value to the ant. Sizes of the rest of the game objects are chosen randomly when created, and constrained to a reasonable positive integer value (e.g., between 10 to 50). For instance, size of one of the food stations may be 15 while size of another food station may be 20. • All game objects have a location, defined by Point built-in class of CN1 (which resides under com.codename1.charts.models and uses float values), which is constituted from non-negative X and Y values that initially should be in the range 0.0 to 1000.0 and 0.0 to 1000.0, respectively. The point (X,Y) is the center of the object. Hence, initial locations of all game objects should always be set to values such that the objects’ centers are contained in the world. All game objects provide the ability for external code to obtain their location. By default, game objects provide the ability to have their location changed, unless it is explicitly stated that a certain type of game object has a location which cannot be changed once it is created. Except flags and the ant, initial locations of all the game objects should be assigned randomly when created. • All game objects have a color, defined by a int value (use static rgb() method of CN1’s built-in ColorUtil class to generate colors). Initially, all objects of the same class have the same color (chosen by you), assigned when the object is created (e.g., flags could be blue, the ant could be red, food stations can be green, spiders can be black). All game objects provide the ability for external code to obtain their color. By default, game objects provide the ability to have their color changed, unless it is explicitly stated that a certain type of game object has a color which cannot be changed once it is created. • Flags are fixed game objects that have attribute sequenceNumber. Each flag is a numbered marker that acts as a “waypoint” on the path; following the path is accomplished by walking over on top of flags in sequential order (the flags mark the course of the path on the ground, see the diagram below). Flags are not allowed to change color once they are created. All flags should be assigned to locations chosen by you, when they are created. • FoodStations are fixed game objects that have an attribute capacity (amount of food a food station contains). The initial capacity is proportional to the size of the food station. When the ant is hungry (i.e., running low on its food level), it must walk to a food station that is not empty before its food level becomes zero; otherwise it cannot move. • All fixed game objects are not allowed to change location once they are created. • Moveable game objects have integer attributes heading and speed. Telling a moveable object to move() causes the object to update its location based on its current heading and speed. The movable game objects move simultaneously according to their individual speed and heading. Heading is specified by a compass angle in degrees: 0 means heading north (upwards on the screen), 90 means heading east (rightward on the screen), etc. See below for details on updating an movable object’s position when its move() method is invoked.
Answered 19 days AfterSep 12, 2021

Answer To: CSUS 1 of 13 CSUS COLLEGE OF ENGINEERING AND COMPUTER SCIENCE Department of Computer Science CSc 133...

Swapnil answered on Sep 19 2021
127 Votes
91016/A1/com/mycompany/a1/Ant.java
91016/A1/com/mycompany/a1/Ant.java
package com.mycompany.a1;
import com.codename1.charts.util.ColorUtil;
public class Ant extends MoveableGameObject implements IMoveable, ISteerable
{
    private int maximumSpeed = 50;
    private int steeringDirection = 5;
    private int foodLevel = 25;
    private int healthLevel = 10;
    private int lastFlagReached = 1;
    private int foodConsumptionRate = 2;
    private boolean isDead = false;
    //constructor for Ant, sets location, size, speed, and color
    public Ant(float x, float y)
    {
        super(0);
        this.setLocation(x,y);
        this.setSize(40);
        this.setSpeed(0);
        this.setColor(ColorUtil.BLACK);
    }
    //change direction ant is heading in
    //@param newDirection
    public void changeDirection(int newDirection)
    {
        this.setDirection(this.getDirection() + newDirection);
    }
    //change ant's speed, making s
ure it's less than max speed
    //@param speed
    public void setAntSpeed(int speed)
    {
        if (speed < this.maximumSpeed)
        {
            this.speed = speed;
        }
        else
        {
            System.out.println("Ant cannot go beyond maximum speed!");
        }
    }
    //ant speed is updated after colliding with a spider or having low food level
    public void updateAntSpeed()
    {
        if (this.getSpeed() < this.getMaximumSpeed() *(10-this.getHealthLevel()))
        {
 
        }
        else
        {
            this.setSpeed(this.getMaximumSpeed() * (this.getHealthLevel()/2));
        } 
        this.checkHealthLevel();
    }
    //Getters and Setters
    //@return steeringDirection
    public int getSteeringDirection() 
    { 
        return steeringDirection; 
    }
    //@param newSteeringDirection
    public void setSteeringDirection(int newSteeringDirection) 
    { 
        this.steeringDirection = newSteeringDirection; 
    }
    //@return maximumSpeed
    public int getMaximumSpeed() 
    { 
        return maximumSpeed; 
    }
    //@param maximumSpeed
    public void setMaximumSpeed(int maxSpeed) 
    { 
        this.maximumSpeed = maxSpeed; 
    }
    //@return foodLevel
    public int getFoodLevel() 
    { 
        return foodLevel; 
    }
    //@param foodLevel
    public void setFoodLevel(int foodLevel) 
    { 
        this.foodLevel = foodLevel; 
    }
    //@return foodConsumptionRate
    public int getFoodConsumptionRate() 
    { 
        return foodConsumptionRate; 
    }
    //@param foodConsumptionRate
    public void setFoodConsumptionRate(int rate) 
    { 
        this.foodConsumptionRate = rate; 
    }
    //@return healthLevel
    public int getHealthLevel() 
    { 
        return healthLevel; 
    }
    //@param healthLevel
    public void setHealthLevel(int healthLevel) 
    { 
        this.healthLevel = healthLevel; 
    }
    //@return lastFlagReached
    public int getLastFlagReached() 
    { 
        return lastFlagReached; 
    }
    //@param lastFlagReached
    public void setLastFlagReached(int lastFlag) 
    { 
        this.lastFlagReached = lastFlag; 
    }
    //@return isDead
    public boolean getIsDead() 
    { 
        return isDead; 
    }
    //@param isDead
    public void setIsDead(boolean isDead) 
    { 
        this.isDead = isDead; 
    }
    //ant's speed reduces depending on healthLevel and foodLevel
    //collisions with spiders reduce healthLevel
    //if ant's foodLevel or healthLevel is zero it cannot move
    public void checkHealthLevel()
    {
        //this.updateAntSpeed();
        if (this.getHealthLevel() == 0 ) 
        {
            this.setSpeed(0);
        }
        if (this.getFoodLevel() <= 0 ) 
        {
            this.setSpeed(0);
        }
        //if ant's food level is half or less, it's speed is reduced by 1/2
        if (this.getFoodLevel() <= 12) 
        {
            this.setFoodLevel(this.getFoodLevel()/2);
        }
        //if ant's speed is 0, that means it's health and food is 0
        //means game is over
        if (this.getSpeed() == 0) 
        {
            this.isDead = true;
        } 
    }
    //health and speed of ant are reduced after collision
    public void collisionWithSpider()
    {
        this.healthLevel -=1;
        this.speed -=5;
        //this.updateAntSpeed();
    }
    //method to reduce ant's food level by foodConsumptionRate
    public void reduceFoodLevel()
    {
        this.foodLevel = this.foodLevel - this.foodConsumptionRate;
        if(this.foodLevel <= 0)
        {
            this.setAntSpeed(0);
            this.isDead = true;
        }
    }
    //after the ant's health level reaches 0, ant is reset
    public void reset()
    {
        this.setColor(ColorUtil.LTGRAY);
        this.setDirection(0);
        this.setSpeed(0);
        this.setLastFlagReached(1);
        this.setLocation(0,0);
        this.setFoodLevel(25);
        this.setHealthLevel(10);
        this.setIsDead(false);
    }
    public String toString()
    {
        String objDesc = super.toString();
        String localDesc = " maxSpeed=" + maximumSpeed + " steeringDirection=" + getSteeringDirection() + " foodLevel=" + foodLevel  + " healthLevel=" + healthLevel;
        return "Ant:" + objDesc + localDesc;
    }
}
91016/A1/com/mycompany/a1/FixedGameObject.java
91016/A1/com/mycompany/a1/FixedGameObject.java
package com.mycompany.a1;
public abstract class FixedGameObject extends GameObjects
{
    //this is for fixed in place GameObjects(Flag, FoodStation)
    public FixedGameObject(float x, float y)
    {
        super.setLocation(x,y);
    }
}
91016/A1/com/mycompany/a1/Flag.java
91016/A1/com/mycompany/a1/Flag.java
package com.mycompany.a1;
import com.codename1.charts.util.ColorUtil;
public class Flag extends FixedGameObject
{
    private int sequenceNumber = 1;
    public Flag(float x, float y, int sequenceNumber)
    {
        super(x, y);
        this.setSize(10);
        this.sequenceNumber = sequenceNumber;
        this.setColor(ColorUtil.BLUE);
    }
    //@return sequenceNumber
    public int getSequenceNumber()
    {
        return sequenceNumber;
    }
    @Override
    public void setColor(int color) {}
    public String toString()
    {
        String objDesc = super.toString();
        String localDesc = " seqNum=" + sequenceNumber;
        return "Flag:" + objDesc + localDesc;
    }
}
91016/A1/com/mycompany/a1/FoodStation.java
91016/A1/com/mycompany/a1/FoodStation.java
package com.mycompany.a1;
import com.codename1.charts.util.ColorUtil;
public class FoodStation extends FixedGameObject
{
    private int capacity;
    //constructor for FoodStation, sets location, size, capacity, and color
    public FoodStation(float x, float y, int size)
    {
        super(x,y);
        this.setSize(size);
        this.setCapacity(size);
        this.setColor(ColorUtil.GREEN);
    }
    //@param capacity
    public void setCapacity(int c)
    {
        this.capacity = c;
    }
    //@return capacity
    public int getCapacity()
    {
        return this.capacity;
    }
    //toString method to return the food station coordinates & capacity
    public String toString()
    {
        String objDesc = super.toString();
        String localDesc = "Capacity = " + capacity;
        return "FoodStation: " + objDesc + localDesc;
    }
}
91016/A1/com/mycompany/a1/Game.java
91016/A1/com/mycompany/a1/Game.java
package com.mycompany.a1;
import static com.codename1.ui.CN.*;
import com.codename1.ui.*;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BoxLayout;
public class Game extends Form
{
    private Form current;
    private GameWorld gw;
    private boolean check = false;
    public Game()
    {
        gw = new GameWorld();
        gw.init();
        play();
    }
    private void play()
    {
        //code here to accept and
        //execute user commands that
        //operate on the game world
        Label myLabel=new Label("Enter a Command:");
        this.addComponent(myLabel);
        final TextField myTextField=new TextField();
        this.addComponent(myTextField);
        this.show();
        myTextField.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent evt) 
            {
                String sCommand=myTextField.getText().toString();
                myTextField.clear();
                if(sCommand.length() != 0)
                {
                    if(check==true) //checks if user wants to quit
                    {
                        switch(sCommand.charAt(0))
                        {
                            case 'y':
                                System.exit(0);
                                break;
                            case 'n':
                                check = false;
                                break;
                                default: 
                                    System.out.println("Enter y or n");
                        }
                    }
                }
                if(sCommand.length() != 0) 
                switch (sCommand.charAt(0)) 
                {
                    case 'a':
                        gw.accelerate();
                        break;
                    case 'b':
                        gw.brake();
                        break;
                    case 'l':
                        gw.left();
  ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Submit New Assignment

Copy and Paste Your Assignment Here