THIS IS A JAVA program and i really need help finishing it. please my question i
ID: 3834719 • Letter: T
Question
THIS IS A JAVA program and i really need help finishing it. please
my question is this..... i have to create a brick braker game in java and im almost finished with it i just need help on putting lives of the user, speed of the ball and the paddle and those things i dont know how to do them.., and I NEED HELP PLEASE. I Wrote the code down so you can help me with it
I have 7 classes... i have This classes.
-Sprite.java
import java.awt.Image;
public class Sprite extends Settings {
public int x;
public int y;
public int height;
public int width;
Image image;
}
-Brick.java
import javax.swing.ImageIcon;
public class Brick extends Sprite{
public int is_destroyed = 0;
public Brick(int _x, int _y) {
x = _x;
y = _y;
ImageIcon icn = new ImageIcon("brick.png");
image = icn.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
}
}
-Ball.java
import javax.swing.ImageIcon;
public class Ball extends Sprite{
int moveX, moveY;
public Ball(int _x, int _y) {
x = _x;
y = _y;
ImageIcon icn = new ImageIcon("ball.png");
image = icn.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
}
public void setSpeed(int x, int y) {
moveX = x;
moveY = y;
}
public void move() {
x = x + moveX;
y = y + moveY;
}
}
-Paddle.java
-Board.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel {
private Brick bricks[];
private Ball ball;
private Timer timer;
public Board() {
bricks = new Brick[Settings.TOTAL_BRICKS];
//create a bunch of bricks
for (int i =0;i<Settings.TOTAL_BRICKS;i++) {
int row = (int) Math.floor(i / Settings.BRICK_COLUMNS);
int col = i % Settings.BRICK_COLUMNS;
int x = 10 + 70 * col;
int y = 10 + 70 * row;
bricks[i] = new Brick(x,y);
}
ball = new Ball(100,100);
ball.setSpeed(2,2);
addKeyListener(new KeyboardManager());
setFocusable(true);
timer = new Timer(20, new GameTick());
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < Settings.TOTAL_BRICKS;i++) {
g2d.drawImage(bricks[i].image,bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height,this);
}
g2d.drawImage(ball.image,ball.x,ball.y,ball.width,ball.height,this);
}
private class KeyboardManager extends KeyAdapter {
public void keyRelease(KeyEvent e) {
//paddle.setMovement(0);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
//user pressed down left arrow
//paddle.setMovement(-1);
}
}
}
private class GameTick implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
ball.move();
repaint();
}
}
}
-Settings.java
public abstract class Settings {
final static int TOTAL_BRICKS = 30;
final static int BRICK_COLUMNS = 10;
final static int WINDOW_HEIGHT = 375;
final static int WINDOW_WIDTH = 300;
}
-Game.java
import java.awt.Component;
import javax.swing.JFrame;
public class Game extends JFrame{
public static void main(String[] args) {
Game g = new Game();
g.initializeGame();
}
public void initializeGame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setResizable(false);
setVisible(true);
add(new Board());
setSize(Settings.WINDOW_WIDTH, Settings.WINDOW_HEIGHT);
}
}
Explanation / Answer
import java.awt.Image;
public class Sprite extends Settings {
public int x;
public int y;
public int height;
public int width;
Image image;
}
-Brick.java
import javax.swing.ImageIcon;
public class Brick extends Sprite{
public int is_destroyed = 0;
public Brick(int _x, int _y) {
x = _x;
y = _y;
ImageIcon icn = new ImageIcon("brick.png");
image = icn.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
}
}
-Ball.java
import javax.swing.ImageIcon;
public class Ball extends Sprite{
int moveX, moveY;
public Ball(int _x, int _y) {
x = _x;
y = _y;
ImageIcon icn = new ImageIcon("ball.png");
image = icn.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
}
public void setSpeed(int x, int y) {
moveX = x;
moveY = y;
}
public void move() {
x = x + moveX;
y = y + moveY;
}
}
-Paddle.java
-Board.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel {
private Brick bricks[];
private Ball ball;
private Timer timer;
public Board() {
bricks = new Brick[Settings.TOTAL_BRICKS];
//create a bunch of bricks
for (int i =0;i<Settings.TOTAL_BRICKS;i++) {
int row = (int) Math.floor(i / Settings.BRICK_COLUMNS);
int col = i % Settings.BRICK_COLUMNS;
int x = 10 + 70 * col;
int y = 10 + 70 * row;
bricks[i] = new Brick(x,y);
}
ball = new Ball(100,100);
ball.setSpeed(2,2);
addKeyListener(new KeyboardManager());
setFocusable(true);
timer = new Timer(20, new GameTick());
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < Settings.TOTAL_BRICKS;i++) {
g2d.drawImage(bricks[i].image,bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height,this);
}
g2d.drawImage(ball.image,ball.x,ball.y,ball.width,ball.height,this);
}
private class KeyboardManager extends KeyAdapter {
public void keyRelease(KeyEvent e) {
//paddle.setMovement(0);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
//user pressed down left arrow
//paddle.setMovement(-1);
}
}
}
private class GameTick implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
ball.move();
repaint();
}
}
}
-Settings.java
public abstract class Settings {
final static int TOTAL_BRICKS = 30;
final static int BRICK_COLUMNS = 10;
final static int WINDOW_HEIGHT = 375;
final static int WINDOW_WIDTH = 300;
}
-Game.java
import java.awt.Component;
import javax.swing.JFrame;
public class Game extends JFrame{
public static void main(String[] args) {
Game g = new Game();
g.initializeGame();
}
public void initializeGame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setResizable(false);
setVisible(true);
add(new Board());
setSize(Settings.WINDOW_WIDTH, Settings.WINDOW_HEIGHT);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.