JAVA PROGRAM: Make simple pac-man game in JAVA that a sweeper removes all trashe
ID: 3873931 • Letter: J
Question
JAVA PROGRAM:
Make simple pac-man game in JAVA that a sweeper removes all trashes in a room. The game runs on console but not GUI. Use * and @ to express the trash and sweeper in the console. Once the game starts, the sweeper randomly moves until all trashes are removed. You can assume the room is fully filled with the trashes. Please make the room size row=10 and col =20. Hint: Use array and while loop with Thread.sleep(1000). Only use import java.io.*, and import java.util.*, use simple and basic statements, put whole program in main.
Explanation / Answer
import java.applet.*; import java.awt.*; import java.awt.event.*; public class PacMan extends Applet { int m = 100; int n = 100; int r = 50; int cct = 0; int total = 0; OneCircle[] C = new OneCircle[500]; public void init () { resize (10,20); setBackground (Color.black); addKeyListener (new KeyAdapter () { public void keyPressed (KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_UP) ChangeCircles (0, -10); if (e.getKeyCode()==KeyEvent.VK_DOWN) ChangeCircles (0, 10); if (e.getKeyCode()==KeyEvent.VK_LEFT) ChangeCircles (-10, 0); if (e.getKeyCode()==KeyEvent.VK_RIGHT) ChangeCircles (10, 0); cct++; repaint (); } } ); addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { C[total] = new OneCircle(); C[total].x = e.getX(); C[total].y = e.getY(); C[total].r = 10; total++; repaint(); //clear the screen and run the paint method again // if (Math.sqrt( (C[total].x-m)*(C[total].x-m) + (C[total].y-n)*(C[total].y-n)) <= 60) { // C[total].x=0; C[total].y=0; //} } } ); } public void paint (Graphics g) { g.setColor (Color.yellow); if (cct%2!=0){ g.fillOval(m-r,n-r,2*r,2*r); } if (cct%2==0) { g.fillArc (m-r, n-r, 2*r, 2*r, 45, 290); } for (int i=0; i < total; i++) { g.setColor (Color.white); if (NoOverlap (C[i].x, C[i].y)) { g.fillOval(C[i].x-C[i].r,C[i].y-C[i].r,2*C[i].r,2*C[i].r); } else { C[i].x = 0; C[i].y = 0; } } } public void ChangeCircles (int x, int y) { m+=x; n+=y; } public boolean NoOverlap (double x, double y) { if (Math.sqrt( (C[total].x-m)*(C[total].x-m) + (C[total].y-n)*(C[total].y-n)) <= 60) return true; return false; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.