Create a graphical JAVA program that simulates a simple arcade game (like Frogger) using the ACM graphical package. an example is written below. import acm.program.*; import java.awt.Color; import acm.graphics.*; import java.util.Random; import java.awt.event.KeyEvent; public class Frogger extends GraphicsProgram {
Create a graphical JAVA program that simulates a simple arcade game (like Frogger) using the ACM graphical package.
an example is written below.
import acm.program.*;
import java.awt.Color;
import acm.graphics.*;
import java.util.Random;
import java.awt.event.KeyEvent;
public class Frogger extends GraphicsProgram {
GLabel gover = new GLabel("GAME OVER ", 50, 60);
GImage[] car = { new GImage("car.png", 0, 145), new GImage("car.png", 200, 145), new GImage("car.png", 400, 145) };
GImage frog = new GImage("frogsmall.png", 200, 325);
public static void main(String[] args) {
Frogger game = new Frogger();
game.start();
}
public void drawImage() {
for (int i = 0; i < car.length; i++) {
add(car[i]);
}
add(frog);
}
public void run() {
setBackground(Color.GREEN);
// tell program to listen for keyboard events
addKeyListeners();
//set up game over label
gover.setColor(Color.RED);
gover.setFont("Arial-30");
drawRoad();
drawRoadMarkers();
drawImage();
while (true) {
// for every car in the array
for (GImage c : car) {
// move car
c.move(50, 0);
// if car goes off right side of screen,
// place back on left
if (c.getX() > getWidth()) {
c.setLocation(0, 145);
}
// if car runs over frog
if (c.getBounds().intersects(frog.getBounds())) {
// make frog invisible
frog.setVisible(false);
//stop car from moving
c.move(0, 0);
// add Game Over label
add(gover);
}
// pause so human eye can see
pause(50);
}
}
}
public void drawRoad() {
GRect road = new GRect(0, 140, 900, 180);
road.setFilled(true);
road.setColor(Color.BLACK);
add(road);
}
public void drawRoadMarkers() {
for (int x = 10; x < getWidth(); x += 50) {
GRect line = new GRect(x, 225, 25, 5);
line.setFilled(true);
line.setColor(Color.YELLOW);
add(line);
}
}
// if up down right or left arrow is pressed move frog
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_UP)
frog.move(0, -10);
else if (event.getKeyCode() == KeyEvent.VK_DOWN)
frog.move(0, 10);
else if (event.getKeyCode() == KeyEvent.VK_LEFT)
frog.move(-10, 0);
else if (event.getKeyCode() == KeyEvent.VK_RIGHT)
frog.move(10, 0);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps