Java Game Programming - Develop a Brick Breaker Game

preview_player
Показать описание
Subscribe to my other channel for updated videos on TECH and PROGRAMMING:

Practice Recursion Problems in JAVA:

Java game programming is really easy to learn. We will develop a brick breaker game with java. We will use JFrame and JPanel for drawing different graphics to make this game work perfectly.

Develop Snake game in java:
Рекомендации по теме
Комментарии
Автор

These are the kind of people that make the world so much easier to live on.

bloxshortsdaily
Автор

For beginners like me who don't understand the logic easily, your videos have always encouraged me and made it fun! looking forward to many more tutorials. thankyou :))

khyatimaddali
Автор

Just wanted to let you know that you just got me a 95% on my final computer science summative which brought my failing mark to a passing mark which allowed me to graduate highschool ! thank you!

mJiVAL
Автор

You can change the starting direction of the ball so you arent playing the same game over and over by changing the ballxdir to a random number within small range

Random random = new Random();
int n = random.nextInt(2+1-2) - 2;
private int ballXDir = n;
private int ballYDir = -2;

you can adjust the range of the random for +speed/+difficulty. Make sure to change it in the ENTER key event also.

jaffzzop
Автор

After 6 months of classes in text and math based Java, this was my first graphics based Java and I LOVED IT!! Thank you SO MUCH!!

InPassing
Автор

For people that have problems when its not drawing anything do this:
put your obj.setVisible(true); after obj.add(gamePlay); This will make it visible after the objects are drawn, I don't know why it matters though but it fixed it for me

RsFgLeB
Автор

okay, so I've completed this tutorial. glad to say this was my first game program. Here are some troubles I encountered:
1.Wrong signs - Mostly < & > were misplaced [Syntax Error]
2.Game restarts with any key press once the ball goes off-screen.

I'm new to coding and would really like to step forward. Thank you very much, Awais for this tutorial.

ayub
Автор

At 23:26, don't forget to add
obj.addKeyListener(gamePlay)
after
obj.add(gamePlay) in your Main class to get your paddles to move.

marlunyu
Автор

Thank you so much for taking the time and effort to produce and share such an informative, insightful, and helpful video!!!

austincraver
Автор

I have made this Brick Breaker game in just 2 hours and 15 minutes it's working properly like yours.This is my first game ever that is in java language and I learned a lot from your video.Thanks🙂

abdullaharif
Автор

Here is the code:


Main.java

package brickBreaker;

import javax.swing.JFrame;

public class Main {

public static void main(String[] args) {

JFrame obj = new JFrame();
Gameplay gameplay = new Gameplay();
obj.setBounds(10, 10, 700, 600);

obj.setTitle("Brick Breaker!");
obj.setResizable(false);
obj.setVisible(true);

obj.add(gameplay);

}

}

Gameplay.java

package brickBreaker;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;

import javax.swing.JPanel;

public class Gameplay extends JPanel implements KeyListener, ActionListener{

private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballPosX = 120;
private int ballPosY = 320;
private int ballDirX = -1;
private int ballDirY = -2;

private MapGenerator map;

public Gameplay(){
map = new MapGenerator(3, 7);
addKeyListener(this);
setFocusable(true);

timer = new Timer(delay, this);
timer.start();
}

public void paint(Graphics g) {
//background
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);

//drawing map
map.draw((Graphics2D)g);

//borders
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);

//Score
g.setColor(Color.white);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("Score: "+score, 300, 30);


//The paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);

//The ball
g.setColor(Color.yellow);
g.fillOval(ballPosX, ballPosY, 20, 20);

if(totalBricks<=0) {
play= false;
ballDirX = 0;
ballDirY = 0;
g.setColor(Color.red);
g.setFont(new Font("serif", Font.BOLD, 35));
g.drawString("You Won!", 250, 300);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press enter to restart", 200, 400);
}

if(ballPosY>570) {
play= false;
ballDirX = 0;
ballDirY = 0;
g.setColor(Color.red);
g.setFont(new Font("serif", Font.BOLD, 35));
g.drawString("Game over! Your score was: "+score, 100, 300);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press enter to restart", 200, 400);
}

g.dispose();

}

@Override
public void actionPerformed(ActionEvent e) {
timer.start();

if(play) {

if(new Rectangle(ballPosX, ballPosY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) {
ballDirY = -ballDirY;
}

A: for(int i = 0; i<map.map.length; i++) {
for(int j = 0; j<map.map[0].length; j++) {
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(ballPosX, ballPosY, 20, 20);
Rectangle brickRect = rect;

{
map.setBrickValue(0, i, j);
totalBricks--;
score+=5;

if(ballPosX +19 <= brickRect.x || ballPosX +1 >= brickRect.x + brickRect.width) {
ballDirX = -ballDirX;
}else {
ballDirY = -ballDirY;
}
break A;
}
}
}

}

ballPosX += ballDirX;
ballPosY += ballDirY;
if(ballPosX<0) {
ballDirX = -ballDirX;
}
if(ballPosY<0) {
ballDirY = -ballDirY;
}
if(ballPosX>670) {
ballDirX = -ballDirX;
}
}

repaint();


}

@Override
public void keyTyped(KeyEvent e) {


}

@Override
public void keyPressed(KeyEvent e) {
{
if(playerX >= 600) {
playerX = 600;
}else {
moveRight();
}
}
{
if(playerX <= 10) {
playerX = 10;
}else {
moveLeft();
}
}
{
if(!play) {
play=true;
ballPosX = 120;
ballPosY = 320;
ballDirX = -1;
ballDirY = -2;
playerX = 310;
score = 0;
totalBricks = 21;
map = new MapGenerator(3, 7);
repaint();


}
}
}
public void moveRight(){
play = true;
playerX += 40;
}
public void moveLeft(){
play = true;
playerX -= 40;
}

@Override
public void keyReleased(KeyEvent e) {


}
}

LeonidasSthlm
Автор

Thanks a lot for this tutorial. Everything runs great on my end and i changed some minor things on my end like making the bricks and paddle "3d" and adding a pic as custom background :)

adamh
Автор

It tooks 7 hours to make this game after your tutorial, but I learned very much from it. Thank you!

zsoldosbotond
Автор

I like how you explain, cause you go step by step. first time using swing and JFrame and totaly understood.

CarlosRamirez-jvnc
Автор

dear Awais Mirza, i am from Greece.thank you very much for this video.it really helped me a lot.but i built this game with netbeans and i must inform you that you must write the command:timer.start(); into all the functions you have created and implement the keylistener and the actionlistener otherwise the game will not be started!!!take care.thanks again!!!

panagiotiskoligas
Автор

Thanks, really helpful video, made me understand how timer, update and render works. Apart from the "physics", did all those changes between states and other things by myself. I think this video helped understand the basic game-makery and will lead me forwards to more advanced gaming stuff. Now I'm looking forward to upgrade the game and add tons of functions, levels, maps and so on. Thanks!

jumperlt
Автор

This took me almost 4 hours to finish but I love the end product. Thanks for this tutorial:)

khubinomhki
Автор

best Java channel. solve problems and videos to make games where u explain what u do. best man <3

mehmedcavas
Автор

Great Video for beginners😊.You have taught in a well mannered and easy way.My game ran succesfully.Thanks a lot.

_vasu
Автор

It took me 2days to completely understand and type it thank you. I wish i could show you how much i learned from this :)

neko_senpaianimeamvedits