Skip to content
Snippets Groups Projects
Commit 80adfca5 authored by Samita Adhikari's avatar Samita Adhikari
Browse files

Basic Game logic Implementation

parent ec1bd200
No related branches found
No related tags found
No related merge requests found
...@@ -7,11 +7,13 @@ import javax.swing.JFrame; ...@@ -7,11 +7,13 @@ import javax.swing.JFrame;
public class App { public class App {
public static void main(String[] args) { public static void main(String[] args) {
LoginFrame frame=new LoginFrame(); JFrame obj = new JFrame();
frame.setTitle("Register || Brick Breaker"); GamePanel gamePanel = new GamePanel();
frame.setVisible(true); obj.setBounds(10, 10, 700, 600);
frame.setBounds(10,10,370,600); obj.setTitle("Brick Breaker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); obj.setResizable(false);
frame.setResizable(false); obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gamePanel);
} }
} }
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package brickbreaker;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
/**
*
* @author Suman
*/
class GameMapBuilder {
public int map [][];
public int brickWidth;
public int brickHeight;
// this creates the brick of size 4x8
public GameMapBuilder(int row, int col) {
map = new int [row][col];
for (int i = 0; i < map.length; i++) {
for (int j=0; j< map[0].length;j++) {
map[i][j] = 1;
}
}
brickWidth = 540/col;
brickHeight = 150/row;
}
// this draws the bricks
public void draw(Graphics2D g) {
for (int i = 0; i < map.length; i++) {
for (int j=0; j< map[0].length;j++) {
if(map[i][j] > 0) {
g.setColor(new Color(0XFF8787)); // brick color
g.fillRect(j*brickWidth + 80, i*brickHeight + 50, brickWidth, brickHeight);
g.setStroke(new BasicStroke(4));
g.setColor(Color.BLACK);
g.drawRect(j*brickWidth + 80, i*brickHeight + 50, brickWidth, brickHeight);
}
}
}
}
// this sets the value of brick to 0 if it is hit by the ball
public void setBrickValue(int value, int row, int col) {
map[row][col] = value;
}
}
\ No newline at end of file
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package brickbreaker; package brickbreaker;
/** import javax.swing.*;
* import java.awt.*;
* @author Suman import java.awt.event.ActionEvent;
*/ import java.awt.event.ActionListener;
public class GamePanel { import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
private boolean play = true;
private int score = 0;
private int totalBricks = 21;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private Vector ballPosition;
private Vector ballDirection;
private String font = "MV Boli";
private GameMapBuilder map;
public GamePanel() {
this.ballPosition = new Vector(120, 350);
this.ballDirection = new Vector(-1, -2);
map = new GameMapBuilder(4, 8);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
// background color
graphics.setColor(Color.green);
graphics.fillRect(1, 1, 692, 592);
map.draw((Graphics2D) graphics);
graphics.fillRect(0, 0, 3, 592);
graphics.fillRect(0, 0, 692, 3);
graphics.fillRect(691, 0, 3, 592);
graphics.setColor(Color.blue);
graphics.fillRect(playerX, 550, 100, 12);
graphics.setColor(Color.RED); // ball color
graphics.fillOval(ballPosition.x, ballPosition.y, 20, 20);
graphics.setColor(Color.black);
graphics.setFont(new Font("MV Boli", Font.BOLD, 25));
graphics.drawString("Score: " + score, 520, 30);
if (totalBricks <= 0) { // if all bricks are destroyed then you win
Color color = new Color(0XFF6464);
gameOver(graphics, "You Won", color);
}
if (ballPosition.y > 570) { // if ball goes below the paddle then you lose
gameOver(graphics, "Game Over", Color.BLACK);
}
}
public void gameOver(Graphics graphics, String gameOverText,Color color) {
play = false;
ballDirection.x = 0;
ballDirection.y = 0;
graphics.setColor(color);
graphics.setFont(new Font(font, Font.BOLD, 30));
graphics.drawString(gameOverText + ", Your Score: " + score, 190, 300);
graphics.setFont(new Font(font, Font.BOLD, 20));
graphics.drawString("Press Enter to Restart", 230, 350);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (play) {
// Ball - Pedal interaction
if (new Rectangle(ballPosition.x, ballPosition.y, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) {
ballDirection.y = -ballDirection.y;
}
for (int i = 0; i < map.map.length; i++) { // Ball - Brick interaction
for (int j = 0; j < map.map[0].length; j++) { // map.map[0].length is the number of columns
if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballPosition.x, ballPosition.y, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
if (ballPosition.x + 19 <= brickRect.x || ballPosition.x + 1 >= brickRect.x + brickRect.width)
ballDirection.x = -ballDirection.x;
else {
ballDirection.y = -ballDirection.y;
}
}
}
}
}
ballPosition.x += ballDirection.x;
ballPosition.y += ballDirection.y;
if (ballPosition.x < 0) { // if ball hits the left wall then it bounces back
ballDirection.x = -ballDirection.x;
}
if (ballPosition.y < 0) { // if ball hits the top wall then it bounces back
ballDirection.y = -ballDirection.y;
}
if (ballPosition.x > 670) { // if ball hits the right wall then it bounces back
ballDirection.x = -ballDirection.x;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent arg0) {
}
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) { // if right arrow key is pressed then paddle moves right
if (playerX >= 600) {
playerX = 600;
} else {
moveRight();
}
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) { // if left arrow key is pressed then paddle moves left
if (playerX < 10) {
playerX = 10;
} else {
moveLeft();
}
}
if (arg0.getKeyCode() == KeyEvent.VK_ENTER) { // if enter key is pressed then game restarts
if (!play) {
play = true;
ballPosition.x = 120;
ballPosition.y = 350;
ballDirection.x = -1;
ballDirection.y = -2;
score = 0;
totalBricks = 21;
map = new GameMapBuilder(7, 8);
repaint();
}
}
}
public void moveRight() { // paddle moves right by 50 pixels
play = true;
playerX += 50;
}
public void moveLeft() { // paddle moves left by 50 pixels
play = true;
playerX -= 50;
}
@Override
public void keyReleased(KeyEvent arg0) {
}
} }
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package brickbreaker; package brickbreaker;
/** /**
......
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package brickbreaker;
/**
*
* @author Suman
*/
public class Vector {
public int x;
public int y;
public Vector(int x, int y) {
this.x = x;
this.y = y;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment