Question

In this code I have 3 racers running, dog, cat, and tigger. Dog and tigger are...

In this code I have 3 racers runnin

In this code I have 3 racers running, dog, cat, and tigger. Dog and tigger are at random speeds and the cat controlled by pressing a and s to move. Please just edit the code to include part E which is when one of the three racers reaches the finish line announce the winner.


import java.awt.Color;
import java.util.Random;


public class RaceTrack{


   public static void main(String[] args) {

       //
       EZ.initialize(680,680);

       //
       int screenWidth = 340;
       EZImage trackPicture = EZ.addImage("track1.png", screenWidth,340);


       //add pictures of three pictures
       EZImage dogPicture = EZ.addImage("dog.png", 0,0);
       EZImage tiggerPicture = EZ.addImage("tigger.png", 0,0);
       EZImage catPicture = EZ.addImage("cat.png", 0,0);

      
       EZSound dogSound = EZ.addSound("dog.wav");
      
      
       // initialize position of all three runners
       int dogPosX = 0;
       int posY = 300;
       int tiggerPosX =0;
       int catPosX= 0;
       int lastPressed = -1 ; // lastPressed = -1 -> no key was pressed last
       // lastPressed == 0 -> a was pressed last
       // lastPressed == 1 -> s was pressed last

       int lastInLead = -1; //0 - dog
                           //1 - tigger
                           //2 - cat
              
       //WHILE loop that will loop forever
       while (true) {

           dogPicture.translateTo(dogPosX, posY); // Set x position of dog.
           tiggerPicture.translateTo(tiggerPosX, 130); // set position of tigger
           catPicture.translateTo (catPosX, 460); //set position of cat

           //  
           Random randomGenerator;
           randomGenerator = new Random(); // make a random number generator and store it in the variable called randomGenerator
           int randomInteger = randomGenerator.nextInt(6); // make a random number from the variable randomInteger
           dogPosX= dogPosX + randomInteger; // make dog move along the x axis with the speed of the initialized random integer
           randomInteger = randomGenerator.nextInt(3); //generate a random integer >=12 or <1
           tiggerPosX= tiggerPosX +randomInteger;// make tigger move within the random integer
           //randomInteger = randomGenerator.nextInt(12); //generate a random integer >=12 or <1
           //catPosX= catPosX+randomInteger; //make cat move within the random integer
           if(lastPressed == -1 && EZInteraction.wasKeyPressed('a')
                   || lastPressed == 1 && EZInteraction.wasKeyPressed('a'))
           {
               catPosX = catPosX + 15;
               lastPressed = 0;
           }

           if ( lastPressed == -1 && EZInteraction.wasKeyPressed('a') ||
                   lastPressed == 0 && EZInteraction.wasKeyPressed('s') )
           {
               catPosX = catPosX + 15;
               lastPressed = 1;
           }


           if(dogPosX - tiggerPosX >= (1/16)*screenWidth && lastInLead != 0 && dogPosX - catPosX >= (1/16)*screenWidth)
           {
               System.out.println("dog in lead");
               lastInLead = 0;
           dogSound.play();
           }
           if(tiggerPosX - catPosX >= (1/16)*screenWidth && lastInLead != 1 && tiggerPosX - dogPosX >= (1/16)*screenWidth)
           {
               System.out.println("tigger in lead");
               lastInLead = 1;
          
           }

           if(catPosX - dogPosX >= (1/16)*screenWidth && lastInLead != 2 && catPosX - tiggerPosX >= (1/16)*screenWidth)
           {
               System.out.println("cat in lead");
               lastInLead = 2;
              
           }
           // continue to refresh the screen
           EZ.refreshScreen();

       }

   }

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
In this code I have 3 racers running, dog, cat, and tigger. Dog and tigger are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...

    In this problem you'll get to use one of Java's random number generators, java.util.Random (see the docs for Random). This class generates a random double precision number uniformly distributed in the range [0.0 ... 1.0). That is, the numbers go from 0 to 1, including 0 but excluding 1. It is a common need in Java programming to generate a random integer number that's uniformly distributed in the range of [0 ... n), such as we saw in the dice...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 1. What is the output? System.out.print(3 + 3 * 3); a. 18 b. 12 c. 9 d. 0 e. 10             2.   What is output by the code below? System.out.print("\\dog\\cat&#...

    1. What is the output? System.out.print(3 + 3 * 3); a. 18 b. 12 c. 9 d. 0 e. 10             2.   What is output by the code below? System.out.print("\\dog\\cat"); a. dog b. dogcat c. \\dog\\cat d. \dog\cat e. catdog\\\\             3.   What is returned by the call     getIt(9) ? public static int getIt(int num){ int ans = 0; if( num >=2 ) {      if( num >= 7)         ans += 2;      else         ans += 3; } ans += 4; return ans; }...

  • I have this code done in two methods, but it has to be done in one,...

    I have this code done in two methods, but it has to be done in one, and I can not figure out how to have it work and run correctly in one method. Chess Board: You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it; i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc. The user and the computer can take any number of pieces from one lane. The...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • I am confused about how to code this. I have tried multiple ways of coding this...

    I am confused about how to code this. I have tried multiple ways of coding this and nothing has worked for me. If someone could help that would be great thanks. /*Chess Board: You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it; i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc. The user and the computer can take any number of pieces from one lane. The object is...

  • package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput;...

    package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT