Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help getting this code to work together. I am trying to make a basic frog

ID: 3816886 • Letter: I

Question

I need help getting this code to work together. I am trying to make a basic frogger game.

package controller;

import java.awt.*;

import java.awt.event.*;

public class HandlingEvents extends Canvas {

     // myX and myY initializes the starting point for the image the user can control

    int myX = 250;

    int myY = 470;

    int speed = 5; // controls the speed of the image movement

    // Boundary variables make a limit for the movement of an image in a window

    int xBoudaryL = 0; // the left x axis boundary

    int xBoudaryR = 470; // the right x-axis boundary

    int yBoudaryU = 0; // the upper limit y-axis boundary

    int YBoudaryD = 470; // the lower limit y-axis boundary

    /**

     * set the dimensions of the window

     * and intializes and sets up the keylistener

     */

    public HandlingEvents() {

        setSize(new Dimension(500, 500));

        addKeyListener(new KeyAdapter() {

            @Override

            public void keyPressed(KeyEvent evt) {

                moveIt(evt);

            }

        });

    }

    /**

     * sets up the circle image location and size of it.

     */

    public void paint(Graphics g) {

        g.fillOval(myX, myY, 30, 30);

    }

    /**

     * this method sets up the buttons that are used and sets the speed to the buttons along with the boundry

     * @param evt

     */

    public void moveIt(KeyEvent evt) {

     switch (evt.getKeyCode()) {

     case KeyEvent.VK_DOWN:

          if(myY>=YBoudaryD){ // If y value reaches 480, it holds

               myY=YBoudaryD;

          }else{

               myY += speed;

          }

          break;

     case KeyEvent.VK_UP:

    if(myY<=yBoudaryU){ //If Y value reaches 0, it holds

          myY=yBoudaryU;

    }else{

          myY -= speed;

    }

    break;

     case KeyEvent.VK_LEFT:

    if(myX<=xBoudaryL){ //If X Value reaches 0, it holds

          myX=xBoudaryL;

    }else{

          myX -= speed;

    }

    break;

     case KeyEvent.VK_RIGHT:

    if(myX>=xBoudaryR){ //If X Value reaches 470, it holds, since ball size is 30 i.e 500-30 = 470 or 480

         myX=xBoudaryR;

    }else{

          myX += speed;

    }

    break;

     }

    

        repaint();

    }

}

package controller;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import View.FroggerView;

import model.Player;

import model.World;

/**

* Controller that runs on a timer to update all the objects

* in the game world

* @author cru103

*

*/

public class RepaintController implements ActionListener {

     private World world;

     private Player player;

     private FroggerView view;

    

     public RepaintController(World world, Player player, FroggerView view) {

          this.world = world;

          this.view = view;

          this.player = player;

     }

    

     @Override

     public void actionPerformed(ActionEvent e) {

          // TODO Auto-generated method stub

          world.updateObjects();

          player.updateScore();

          if (world.hasCollision()) {

               // kill the frog

          }

          view.repaint();

         

     }

}

package View;

import java.awt.*;

import java.awt.Menu;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

//import java.awk.*;

public class FroggerView extends JFrame implements ActionListener{

            JFrame view = new JFrame("Frogger");

            JMenuBar menuBar;

            public FroggerView() {

                        super();

           

                        JPanel panelNorth = new JPanel();

                        add(panelNorth, BorderLayout.NORTH);

                        JMenu Menu;

                        menuBar = new JMenuBar();

                        Menu = new JMenu("Menu");

                        menuBar.add(Menu);

                        JMenuItem Controls = new JMenuItem("Controls");

                        Menu.add(Controls);

                       

                        JMenuItem Help = new JMenuItem("Help");

                        Menu.add(Help);

                        JMenuItem exitButton = new JMenuItem("Exit");

                        Menu.add(exitButton);

                        panelNorth.setLayout(new GridLayout(1,1));

                        panelNorth.add(menuBar);

                        panelNorth.repaint();

                        view.add(panelNorth,BorderLayout.NORTH);                   //view.

                        //(panelNorth.getComponents(),BorderLayout.NORTH);

            }

            public void registerListener(FroggerView controller) {

                        Component[] components = view.getComponents();

            for (Component component : components) {

                        if (component instanceof AbstractButton) {

                                    AbstractButton button = (AbstractButton) component;

                                    button.addActionListener(controller);

                        }

            }

}

            public void actionPerformed(ActionEvent e) {

                        String command = e.getActionCommand();

                        System.out.println(command);

                        if (command.equals("Exit")) {

                                    //System.exit(0);

                                    view.dispose();

                        } else if (command.equals("Help")) {

                                    System.out.printf("Help");

                        } else if (command.equals("Controls")) {

                                    System.out.printf("Controls");

                        } else {

                                   

                        }

            }

            public void load() {

                        view.setSize(600, 600);

                        view.getContentPane().setBackground(Color.WHITE);

                        view.setVisible(true);

            }

}

package model;

public class Player {

     private int lives;

     private int score;

    

     public Player() {

          // TODO Auto-generated constructor stub

          lives = 3;

          score = 0;

     }

    

     /**

     * kill player, subtract a life, all proceeds to charity

     * @return remaining lives

     */

     public int die() {

          lives--;

          return lives;

     }

    

     /**

     * set player score

     * @param newscore

     */

     public void setScore(int newscore) {

          score = newscore;

     }

    

     /**

     * get playerscore

     * @return

     */

     public int getScore() {

          return score;

     }

     /**

     * Update score. I don't know

     */

     public void updateScore() {

          // TODO Auto-generated method stub

         

     }

}

package model;

import objects.*;

/**

* World class. This maintains all the objects in the world,

* the dimensions, and checks for collisions

*

* It looks something like this

* 13 x 19 grid

*

*

* -------------------------------

*     0 1 2 3 4 5 6 7 8 9 10 11 12

* 0 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] menu zone

* 1 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] info zone

* 2 [ ][P][ ][P][ ][P][ ][P][ ][P][ ][P][ ] lillypad zone

* 3 [l][l][l][l][l][l][l][l][l][l][l][l][l] log zone

* 4 [l][l][l][l][l][l][l][l][l][l][l][l][l]

* 5 [l][l][l][l][l][l][l][l][l][l][l][l][l]

* 6 [l][l][l][l][l][l][l][l][l][l][l][l][l]

* 7 [l][l][l][l][l][l][l][l][l][l][l][l][l]

* 9 [y][y][y][y][y][y][y][y][y][y][y][y][y] safe zone

* 10 [c][c][c][c][c][c][c][c][c][c][c][c] car zone

* 11 [c][c][c][c][c][c][c][c][c][c][c][c][c]

* 12 [c][c][c][c][c][c][c][c][c][c][c][c][c]

* 13 [c][c][c][c][c][c][c][c][c][c][c][c][c]

* 14 [c][c][c][c][c][c][c][c][c][c][c][c][c]

* 15 [c][c][c][c][c][c][c][c][c][c][c][c][c]

* 16 [x][x][x][x][x][x][x][x][x][x][x][x][x] start zone

* 17 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] score and

* 18 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] info zone

*

*

*

* @author cru103

*

*/

public class World {

     public static final int LENGTH = 13;

     public static final int HEIGHT = 19;

     public static final int START_X = 0;

     public static final int START_Y = 0;

     public static final int TIME = 25;

    

     private static final int CAR_START = 11;

     private static final int LOG_START = 3;

    

    

     private AbstractFroggerObject[] objects;

     private Frog frog;

     public World() {

          // TODO Auto-generated constructor stub

          objects = new AbstractFroggerObject[10];

          for (int i = 0; i < 5; i++) {

               objects[i] = new Car(i + CAR_START, 1, 1, 1);

          }

          for (int i = 0; i < 5; i++) {

               objects[i + 5] = new Log(i + LOG_START, 3, 1, 1);

          }

         

          frog = new Frog();

         

         

         

     }

     /**

     * Updates the position of all ingame objects

     * if they go out of range and aren't frogs, they

     * reset on the other side

     */

     public void updateObjects() {

          // TODO Auto-generated method stub

          for (AbstractFroggerObject o: objects) {

               o.move();

          }

          if (!frog.hasMoved()) {

               frog.move();

               frog.resetMove();

          }

         

     }

     public boolean hasCollision() {

          // TODO Auto-generated method stub

          // loop through all the game objects and

          // check for collision with the frog

          for (AbstractFroggerObject o : objects) {

               if (o.doesCollide(frog)) {

                     return true;

               }

          }

          return false;

     }

}

package main;

import javax.swing.*;

import View.FroggerView;

import controller.*;

import model.*;

public class Main {

            public static void main(String[] args) {

                        /*

        JFrame frame = new JFrame("Basic Game");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        HandlingEvents ex = new HandlingEvents();

        frame.getContentPane().add(ex);

        frame.pack();

        frame.setResizable(false);

        frame.setVisible(true);

        ex.requestFocus();

        */

       

        World world = new World();

        Player player = new Player();

        FroggerView view = new FroggerView();

        view.load();

        RepaintController repainter = new RepaintController(world, player, view);

       

        // for the repaint controller

        new Timer(World.TIME, repainter).start();

    }

}

Explanation / Answer

import ch.aplu.jgamegrid.*;
import java.awt.event.KeyEvent;
import java.awt.*;

public class Frogger extends GameGrid
{
public Frogger()
{
super(800, 600, 1, null, "sprites/lane.gif", false);
setSimulationPeriod(80);
Frog frog = new Frog();
addActor(frog, new Location(400, 560), Location.NORTH);
playSound(GGSound.DUMMY);
frog.setCollisionRectangle(new Point(0, 0), 30, 30);

Car[] cars = new Car[20];
for (int i = 0; i < 10; i++)
{
cars[i] = new Car("sprites/car" + i + ".gif");
cars[i].setHorzMirror(true);
frog.addCollisionActor(cars[i]);
}
for (int i = 0; i < 10; i++)
{
cars[10 + i] = new Car("sprites/car" + i + ".gif");
frog.addCollisionActor(cars[10 + i]);
}

for (int i = 0; i < 5; i++)
addActor(cars[i], new Location(350 * i, 90), Location.WEST);
for (int i = 5; i < 10; i++)
addActor(cars[i], new Location(350 * (i - 5), 350), Location.WEST);
for (int i = 10; i < 15; i++)
addActor(cars[i], new Location(350 * (i - 10), 220), Location.EAST);
for (int i = 15; i < 20; i++)
addActor(cars[i], new Location(350 * (i - 15), 480), Location.EAST);

addKeyRepeatListener(frog);
setTitle("Frogger -- Use 4 cursor keys to move the frog");
show();
doRun();
}

public static void main(String[] args)
{
new Frogger();
}
}


class Car extends Actor
{
public Car(String imagePath)
{
super(imagePath);
}

public void act()
{
move();
if (getLocation().x < -100)
setLocation(new Location(1650, getLocation().y));
if (getLocation().x > 1650)
setLocation(new Location(-100, getLocation().y));
}
}


class Frog extends Actor implements GGKeyRepeatListener
{
private boolean isFinished = false;

public Frog()
{
super("sprites/frog.gif");
}

public void act()
{
if (getLocation().y < 25)
{
if (!isFinished)
{
isFinished = true;
gameGrid.playSound(GGSound.FROG);
}
}
else
isFinished = false;
}

public void keyRepeated(int keyCode)
{
switch (keyCode)
{
case KeyEvent.VK_UP:
if (!isFinished)
setLocation(new Location(getLocation().x, getLocation().y - 5));
break;
case KeyEvent.VK_DOWN:
setLocation(new Location(getLocation().x, getLocation().y + 5));
break;
case KeyEvent.VK_LEFT:
setLocation(new Location(getLocation().x - 5, getLocation().y));
break;
case KeyEvent.VK_RIGHT:
setLocation(new Location(getLocation().x + 5, getLocation().y));
break;
}
}

public int collide(Actor actor1, Actor actor2)
{
gameGrid.playSound(GGSound.BOING);
setLocation(new Location(400, 560));
setDirection(Location.NORTH);
return 0;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote