Question
Hi I've been trying to make the brick breaks game. The only thing I need help with is how to think when I make some
Hi I've been trying to make the brick breaks game. The only thing I need help with is how to think when I make some tiles disappear at the first hit and some 2 or 3. My code down here is a code that makes it require 3 hits but I want some of those tiles disappears at 1 or 2 hits
My cod:
class for Game:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.*;
import javax.swing.Timer;
public class Game {
ArrayList redBox;
Ball ball;
SquareCollection squarecollection;
Slagtra slagtra;
MapGenerator map;
private int score = 0;
private int lives = 3;
private Timer timer;
private boolean slowSpeed = false;
public int totalBricks = 63;
private boolean play = false;
public Game(GameBoard board) {
map = new MapGenerator(4, 10 );
ball = new Ball(20,20);
slagtra = new Slagtra(350,560);
}
public void update(Keyboard keyboard) {
ballslagtracollision();
ball.update(keyboard);
slagtra.update(keyboard);
}
public void draw(Graphics2D graphics) {
map.draw(graphics);
ball.draw(graphics);
slagtra.draw(graphics);
graphics.setColor(Color.red);
graphics.setFont(new Font("serif", 25,20));
graphics.drawString("Score: " + score, 700, 30);
graphics.drawString("Lives: " + lives, 10, 30);
if (totalBricks =>
play = false;
ball.ballXdir = -2;
ball.ballYdir = -4;
graphics.setColor(Color.green);
graphics.setFont(new Font("serif", 1, 30));
graphics.drawString("You won: ", 350, 300);
graphics.drawString("Score: " + score, 350, 330);
graphics.drawString("Lives left: " + lives, 350, 360);
graphics.setFont(new Font("serif", 1, 20));
//graphics.drawString("Press Enter to Restart", 230, 350);
}
if(ball.getY() > 570) {
graphics.setColor(Color.RED);
if (lives == 0) {
graphics.setFont(new Font("serif", 30, 30));
graphics.drawString("Score: " + score, 350, 350);
graphics.drawString("Game over", 350, 300);
// graphics.drawString("Press Enter to Try again", 230, 350);
//play = false;
//ball.ballXdir = 0;
//ball.ballYdir = 0;
} else {
if (!slowSpeed) {
slowSpeed = true;
ball.ballXdir = - ball.ballXdir ;
ball.ballYdir = - ball.ballYdir ;
}
graphics.setFont(new Font("serif", 30, 30));
graphics.drawString("You lost a life :(", 220, 300);
lives--;
ball.setX(350);
ball.setY(330);
}
}
graphics.dispose();
}
public void ballslagtracollision() {
if(!play) {
if(new Rectangle(ball.getX(),ball.getY(),20, 20).intersects(new Rectangle(slagtra.getX(), 550, 100, 8))) {
ball.ballYdir = -ball.ballYdir;
}
B:
for(int i = 0; i
for(int j = 0; j
if (map.map[i][j] > 0) {
int brickX = j * map.width + 80;
int brickY = i * map.height + 50;
int brickWidth = map.width;
int brickHeight = map.height;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ball.getX(), ball.getY(), 20, 20);
if (ballRect.intersects(rect)) {
int v=map.map[i][j];
v= v-1;
map.setBrickValue(v, i, j);
--totalBricks;
score +=1 + (int) (Math.random() * 5);
if (ball.getX() + 1 >= rect.x && ball.getX() + 10 =>
ball.ballYdir = -ball.ballYdir;
break B;
}
ball.ballXdir = -ball.ballXdir;
break B;
}
}
}
}
}
}
}
class for MapGenerator:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
public class MapGenerator{
public int map[][];
public int map2[][];
public int width;
public int height;
public int totalBricks = 21;
public MapGenerator(int rad, int col) {
map = new int[rad] [col];
for (int i = 0; i
// two dimensional array table, two tables, first table size is set by row variable the second by col.
for (int j=0; j
map[i][j] = 3; // we get the element i and set its value to 3
}
}
width= 630/col;
height =100/rad;
}
public void setBrickValue(int value, int row, int col) {
map[row][col] = value;
}
public void draw(Graphics2D graphics) {
for (int i = 0; i
for (int j =0; j
if(map[i][j] == 3) {
graphics.setColor(Color.white);
graphics.fillRect(j * width + 80, i * height + 50, width, height);
graphics.setStroke(new BasicStroke(3));
graphics.setColor(Color.black);
graphics.drawRect(j * width + 80, i * height + 50, width, height);
}
else if(map[i][j] == 2){
graphics.setColor(Color.green);
graphics.fillRect(j * width + 80, i * height + 50, width, height);
graphics.setStroke(new BasicStroke(3));
graphics.setColor(Color.black);
graphics.drawRect(j * width + 80, i * height + 50, width, height);
}
else if(map[i][j] == 1){
graphics.setColor(Color.yellow);
graphics.fillRect(j * width + 80, i * height + 50, width, height);
graphics.setStroke(new BasicStroke(3));
graphics.setColor(Color.black);
graphics.drawRect(j * width + 80, i * height + 50, width, height);
}
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started