Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//Create a ball, using helper method to make sure no balls overlap private void createBall(int index){ Ball newBall; while (!validLocation(newBall = randomBall())) {} // TODO:

image text in transcribed image text in transcribed //Create a ball, using helper method to make sure no balls overlap private void createBall(int index){ Ball newBall; while (!validLocation(newBall = randomBall())) {} // TODO: add newBall to the sequence. } private boolean validLocation(Ball newBall){ /* TODO: Implement this pseudo-code: for every Ball b in balls { if (b.isColliding(newBall)) return false; } */ return true; } @Override public void run(){ Timer timer = new Timer(1000/FPS, (ae)->update()); SwingUtilities.invokeLater(()->timer.start()); } //Update all game elements private void update(){ if (!alive) return; updateTime(); player.update(getAdjustedMouseLoc()); //update balls /* TODO: Implement this pseudo-code for every Ball b in balls { b.step(); } */ if (!suspended){ checkCollisions(); checkGameOver();} } //Update times, queue start of rounds and transitions between rounds private void updateTime(){ if (Instant.now().isBefore(startTime)) displayClock.setText(""+(Duration.between(Instant.now(), startTime).getSeconds()+1)); else if (Instant.now().isBefore(stopTime)){ if (suspended){ suspended = false; /* TODO: Implement this pseudo-code for every Ball b in balls { b.launch(); } */ } displayClock.setText(""+(Duration.between(Instant.now(), stopTime).getSeconds()+1));} else{ makeHarder(); nextLevel();} } private void makeHarder(){ switch ((int) (Math.random() * 2)){ case 0: {ballCount++;System.out.println("Added ball.");break;} default: {player.grow();System.out.println("Increased player size.");break;} } } private Point getAdjustedMouseLoc(){ return new Point((int) MouseInfo.getPointerInfo().getLocation().getX()-frame.getX()-PANEL_OFFSET.width, (int) MouseInfo.getPointerInfo().getLocation().getY()-frame.getY()-PANEL_OFFSET.height);} //Check ball-on-ball and ball-on-wall collisions private void checkCollisions(){ /* TODO: Implement using Sequence: // NB: You can't use nested loops on the same sequence for every Ball a in balls { for every Ball b in balls, except a { if a collides with b { a.bounce(b); } } a.bounceWalls(DodgeBall.BOUNDS); } */ } //Check if the player was hit by any balls private void checkGameOver(){ /* TODO: Implement this pseudo-code for every Ball b in balls { if player collides with b { remember that deathBall is b; call gameover(); } } */ } private void gameover(){ alive=false; suspended=true; player.setImg(sadImg); panel.repaint(); } @SuppressWarnings("serial") private class DodgeBallPanel extends JPanel{ public DodgeBallPanel(){ setDoubleBuffered(true); addMouseListener(new MouseListener(){ @Override public void mouseClicked(MouseEvent arg0) {} @Override public void mouseEntered(MouseEvent arg0) {} @Override public void mouseExited(MouseEvent arg0) {} @Override public void mousePressed(MouseEvent arg0) {if (alive)player.setImg(winkImg);} @Override public void mouseReleased(MouseEvent arg0) {if (alive)player.setImg(happyImg);}}); } /* * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) */ public void paintComponent(Graphics g) { super.repaint(); g.setColor(Color.WHITE); g.fillRect(0, 0, BOUNDS.width, BOUNDS.height); //draw game entities // TODO: draw every ball in balls player.draw(g); if (suspended){ g.setColor(new Color(0,0,0,100)); g.fillRect(0, 0, BOUNDS.width+PANEL_OFFSET.width, BOUNDS.height+PANEL_OFFSET.height); g.setFont(new Font("Verdana", Font.BOLD, 24)); if (alive){ g.setColor(Color.WHITE); g.drawString("Round "+level + "..", BOUNDS.width/2 - 62, BOUNDS.height/2);} else{ g.setColor(new Color(255,255,255,50)); g.fillOval(deathBall.getLoc().getX()-50, deathBall.getLoc().getY()-50, 100, 100); deathBall.draw(g); player.draw(g); g.setColor(Color.WHITE); g.drawString("Game Over!", BOUNDS.width/2 - 70, BOUNDS.height/2); g.drawString("You made it", BOUNDS.width/2 - 72, BOUNDS.height/2+50); g.drawString(level+" rounds.", BOUNDS.width/2 - 52, BOUNDS.height/2+80);} } } } private static BufferedImage happyImg, winkImg, sadImg; static{ try {happyImg = ImageIO.read(new File("images/happy_face.png")); winkImg = ImageIO.read(new File("images/wink_face.png")); sadImg = ImageIO.read(new File("images/sad_face.png"));} catch (Exception e){System.out.println("Error loading player image.");} } public static void main(String[] args){ new DodgeBall().run();} }

Can someone help me with this java class please? There are some //TODO parts for some of the methods. Thank you! import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt. Font; import java.awt.Graphics; import java.awt.MouseInfo; import java.awt.event.ActionEvent; import java.awt.event. ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image. BufferedImage; import java.io.File; import java.time.Duration; import java.time.Instant; import javax.imageio. ImageI0; import javax.swing. JFrame; import javax.swing.JLabel; import javax.swing. JMenuBar; import javax.swing. JMenuItem; import javax.swing. JPanel; import javax.swing. SwingConstants; import javax.swing. Swingutilities; import javax.swing.Timer; public class DodgeBall implements Runnable\{ static final int FPS =60; static final Dimension PANEL_OFFSET = new Dimension (5,51); static final int CLOCK_HEIGHT =22; static final int COUNTDOWN =5, LEVEL_DURATION =12; static final Dimension BOUNDS = new Dimension (600,600); static final int PLAYER_RADIUS =15; static final int INITIAL_BALL_COUNT =5; static final double MIN_SPEED =4.5, MAX_SPEED =6.5; static final boolean MORTAL = true; private JFrame frame; private DodgeBallPanel panel; private JLabel displayClock; private Player player; private Ballseq balls; private Ball deathBall; private int level; private int ballCount; private boolean suspended, alive; private Instant startTime, stopTime

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the role of supervisors ?

Answered: 1 week ago