IN JAVA! Expand part A, except add a dog at point (200, 200). Every time the cat
ID: 3575695 • Letter: I
Question
IN JAVA! Expand part A, except add a dog at point (200, 200). Every time the cat moves (with the arrow keys) the dog moves but only 25 units rather than the cats 50. If the dog is within 25 units in both x and y, then the cat should be caught by the dog and you lose the game (print “Dog bites you!”). For example, if the cat is at (300,250) and the dog is at (250, 200) and after the cat moves it is at (300, 300) and the dog is at (250, 250). The dog is within 50 units in both x and y and your program should stop. The dog should always move towards the cat, in whichever axis is greater.
For example, if the dog is 200 x-units away from the cat and 100 y-units away, the dog should move closer in the x-direction. If there is a tie, move in the y-direction. This movement should be determined after the cat has moved, so first the cat moves, then the dog tries to move closer. Given the setup of the game, the only way for the cat to get past the dog is to move up or left from the current position 6 times, then go towards the food. (Once the cat passes the dog, it can take detours and still win.)
Here's part A code:
Could I have a main class created?????
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/*
*
*/
public class Cat extends Applet implements KeyListener {
int x=350,y=350;
public void init()
{
setSize(400,400);
addKeyListener(this);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.drawOval(x, y, 50, 50);
g.setColor(Color.YELLOW);
g.drawRect(50, 50, 60, 40);
if(x==0 ||x==400 || y==0 ||y==40)
{
g.setColor(Color.RED);
g.drawString("Cat fell off the world", 200, 200);
try{
Thread.sleep(300);
}catch(Exception e1){}
System.exit(0);
}
if(x==50 && y==50)
{
g.setColor(Color.RED);
g.drawString("You Have won", 200, 200);
try{
Thread.sleep(3000);
}catch(Exception e1){}
System.exit(0);
}
}
public void keyPressed(KeyEvent k){
if(k.getKeyCode()==KeyEvent.VK_LEFT)
{
x=x-50;
repaint();
}
else if(k.getKeyCode()==KeyEvent.VK_RIGHT)
{
x=x+50;
repaint();
}
else if(k.getKeyCode()==KeyEvent.VK_UP)
{
y=y-50;
repaint();
}
else if(k.getKeyCode()==KeyEvent.VK_DOWN)
{
y=y+50;
repaint();
}
}
public void keyReleased(KeyEvent k){
}
public void keyTyped(KeyEvent k){
}
}
Example 1 (user input is underlined, bold is descriptive not verbatim)
Example 1 (user input is underlined, bold is descriptive not verbatim):
x MC chegg study IGuided x VM ksci11031Hvi gB mist x MM welcome to Myu x MM course. cs.i 1103 Intr x D hwog.pdf C O www-users cselabs umn ed all 2016/csci1103/assignme pdf u/classes E: Apps M Welrn tn Myll Md MVU FMAll M Hamr university of m httpsl E The gir Roni, fil R How to Think Straight Courser PSY 2901 W 2016's States That H tranter koutstaal agir a Type here to search Emily Other 11:28 PM 12/1/2016Explanation / Answer
main method is not required for this code.
Applet does not require main method to run.
just copy this code into your IDE, save and run the code.You'll get your output.
if you are running using command prompt then,
compile using
javac Cat.java
run using
appletviewer Cat.java
provided:You have set Path and CLASSPATH system variables.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//don't delete this comment. it's read by appletviewer to run the code.
/*
* <applet code="Cat.class" height="400" width="400"></applet>
*/
public class Cat extends Applet implements KeyListener {
int xc=350,yc=350;//co-ordinates for cat
int xd=200,yd=200;//co-ordinates for dog
public void init() //initialize the applet
{
setVisible(true);
setSize(400,400); //set size of window frame.
addKeyListener(this); //add KeyListener to this window.
setLayout(null);
}
public void paint(Graphics g){
/*
* draw cat with black color at position x,y
*/
g.setColor(Color.BLACK);
g.drawOval(xc, yc, 50, 50);
/*
* draw food with yellow rectangle at (50,50)
*/
g.setColor(Color.BLUE);
g.drawRect(xd, yd, 60, 40);
g.setColor(Color.YELLOW);
g.drawRect(50, 50, 50, 30);
/*
* if cat reaches border of the window
*/
if(xc==0 ||xc==400 || yc==0 ||yc==40)
{
g.setColor(Color.RED);
g.drawString("Cat fell off the world", 200, 200); //print message
try{
Thread.sleep(3000); //wait for 3 seconds.
}catch(Exception e1){}
System.exit(0); //stop the program and close the window
}
/*
* if cat reaches to the food successfully.
*/
if(xc==50 && yc==50)
{
g.setColor(Color.RED);
g.drawString("Cat has won!", 10, 10);//print message
try{
Thread.sleep(3000); //wait for 3 seconds
}catch(Exception e1){}
System.exit(0);//stop the program and close the window.
}
/*
* if dog comes within 25 units from cat in both (x,y) direction
*/
if(Math.abs(xc-xd)<=25 && Math.abs(yc-yd)<=25)
{
g.setColor(Color.RED);
g.drawString("Dog Bites You!", 10, 10);//print message
try{
Thread.sleep(3000); //wait for 3 seconds
}catch(Exception e1){}
System.exit(0);//stop the program and close the window.
}
}
/*
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
* implement keyPresed method
*/
public void keyPressed(KeyEvent k){
int xdiff=Math.abs(xc-xd);
int ydiff=Math.abs(yc-yd);
//if left arrow key is pressed.
if(k.getKeyCode()==KeyEvent.VK_LEFT)
{
xc=xc-50; //reduce x coordinate of cat by 50
if(xdiff>ydiff)
{
xd=xd+25; //increase 25 in dog's co-ordinate
if(Math.abs(xc-xd)>xdiff) //if dog went far from cat
{
xd=xd-50; //revert the changes in dog's co-ordinate and move
//25 in opposite direction
}
}
else
{
yd=yd+25;
if(Math.abs(yc-yd)>ydiff)
{
yd=yd-50;
}
}
repaint();//draw cat and dogs at new position
}
//if right arrow key is pressed.
else if(k.getKeyCode()==KeyEvent.VK_RIGHT)
{
xc=xc+50;
if(xdiff>ydiff)
{
xd=xd-25;
if(Math.abs(xc-xd)>xdiff)
{
xd=xd+50;
}
}
else
{
yd=yd+25;
if(Math.abs(yc-yd)>ydiff)
{
yd=yd-50;
}
}
repaint();
}
//if up arrow key is pressed.
else if(k.getKeyCode()==KeyEvent.VK_UP)
{
yc=yc-50;
if(xdiff>ydiff)
{
xd=xd+25;
if(Math.abs(xc-xd)>xdiff)
{
xd=xd-50;
}
}
else
{
yd=yd+25;
if(Math.abs(yc-yd)>ydiff)
{
yd=yd-50;
}
}
repaint();
}
//if down arrow key is pressed.
else if(k.getKeyCode()==KeyEvent.VK_DOWN)
{
yc=yc+50;
if(xdiff>ydiff)
{
xd=xd+25;
if(Math.abs(xc-xd)>xdiff)
{
xd=xd-50;
}
}
else
{
yd=yd-25;
if(Math.abs(yc-yd)>ydiff)
{
yd=yd+50;
}
}
repaint();
}
}
public void keyReleased(KeyEvent k){
}
public void keyTyped(KeyEvent k){
}
}