Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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);

}

}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education