public class AL extends KeyAdapter{ @Override public void keyPressed(KeyEvent e) { player.keyPressed(e); checkCollision(); repaint(); } } } //**************************************************************************************** import java.awt.*; import java.awt.event.*; public class Box extends Rectangle{ Color color;
Box(int x, int y, int width, int height, Color color){ this.x=x; this.y=y; this.width=width; this.height=height; this.color=color; }
There’s no way you made this video!!, I made a game up until I got to the collision detection and that’s where I’m stuck now and boom you have a video on it, I hope you know I love you man!! Thank you for being awesome
Simplesmente demais as suas aulas, que profissionalismo super 1000 essa é a nota que eu te daria. Mais só não entendi um coisa😮 a classe rectangle vc não fez bo vídeo, mais adicionou, como faço
@@BroCodez Nice, I have this game basically where enemies follow the player. But I want the enemies to bounce off the player, right now they just stick there.
@@BroCodez i thought so too, but i think their velocity is still going forward, so to reverse their velocity effectively makes it 0. Hence why they collide and stick there.
HOLA BUEN DIA PODRIA POR FAVOR AYUDARME ? PROGRAMA CUANDO CORRO EL PROGRAMA NO SE AFICHA NADA EN LA VENTANA APARECE VACIA ? CUAL PUEDE SER EL PROBLEMA GRACIAS
you could do that with an if statement before moving your square. Check to see if the location of your square is less than the width and height of the panel/frame.
The paint() method clears the screen and then paints a new one one within your frame. At times, the screen may get updated between the clearing phase and painting phase. Which may cause a flicker effect. A possible solution is to use Double Buffering for your image. Maybe try changing: Image image //replace with BufferedImage image & image = createImage(this.getWidth(),this.getHeight()) //replace with image = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
@@Acidicfly247 if you still haven't found a solution yet use this bufferstrategy method and run the render method in your public class AL extends KeyAdapter public void render() { BufferStrategy bs = getBufferStrategy(); if(bs == null) { createBufferStrategy(3); return; } } the flickering will stop
@@BroCodezyep, actually just a quick question, I followed your snakegame tutorial, and I was wondering if there was a way to set an image as the background instead of just black. Thanks! :)
It is good, but i am not sure i understand this game well. on the screen just 2 box and I can only use arrow key to control one of them. once they are touched each other, game is over. I don't quiet understand it.
//****************************************************************************************
public class Game {
public static void main (String[] args) {
//Collision detection = the computational problem of detecting the intersection
// of two or more objects.
MyFrame myFrame = new MyFrame();
}
}
//****************************************************************************************
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame{
Image image;
Graphics graphics;
Box player;
Box enemy;
boolean gameOver;
MyFrame(){
player = new Box(100,300,50,50,Color.blue);
enemy = new Box(400,300,50,50,Color.red);
gameOver = false;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,600);
this.setVisible(true);
this.addKeyListener(new AL());
}
public void paint(Graphics g) {
image = createImage(this.getWidth(),this.getHeight());
graphics = image.getGraphics();
g.drawImage(image,0,0,this);
player.draw(g);
enemy.draw(g);
if(gameOver) {
g.setColor(Color.RED);
g.setFont(new Font("MV Boli",Font.PLAIN,45));
g.drawString("GAME OVER!", 150, 100);
}
}
public void checkCollision() {
if(player.intersects(enemy)) {
gameOver = true;
System.out.println("GAME OVER!");
}
}
public class AL extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
checkCollision();
repaint();
}
}
}
//****************************************************************************************
import java.awt.*;
import java.awt.event.*;
public class Box extends Rectangle{
Color color;
Box(int x, int y, int width, int height, Color color){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.color=color;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
this.y=y-10;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
this.y=y+10;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
this.x=x-10;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
this.x=x+10;
}
}
public void draw(Graphics g) {
g.setColor(this.color);
g.fillRect(this.x, this.y, this.width, this.height);
}
}
//****************************************************************************************
could you please make a video on shooting bullets.
can teach render with collision for the next video?
There’s no way you made this video!!, I made a game up until I got to the collision detection and that’s where I’m stuck now and boom you have a video on it, I hope you know I love you man!! Thank you for being awesome
I found one of the habit of Bro code. When he writes constructors, he doesn't like to write public keyword
Welp
Acctualy yea-
Perfect video I just don’t understand graphics at all… breaks my spirit really to come this far and have to give up…
Thanks for the video, I could get the idea of collisions now.
Thank you for the enlightment!
just watched this from 2024, why is my GUI still flickering? did something change in java from this vid to now??
Liking commenting, and subscribing…. Rectangle class ftw. It has intersects() method.
my mornings with brocode videos
Hey bro...I'm confused about the methods paint(). When you call it?
thx man your videos are soooo godlike
bro this video is not added in java playlist and even in java swing playlist
pls add it and other if u missed any
thx for the vedio
ты лучший!
with love from Russia!
How could you the program stop when the two boxes collide or did I miss something in the video
Simplesmente demais as suas aulas, que profissionalismo super 1000 essa é a nota que eu te daria. Mais só não entendi um coisa😮 a classe rectangle vc não fez bo vídeo, mais adicionou, como faço
But how about some physics like they bounce off of each other with velocity?
I have a Pong video I'm working on rn
@@BroCodez Nice, I have this game basically where enemies follow the player. But I want the enemies to bounce off the player, right now they just stick there.
@@rmdir hmmm You could always reverse their velocity. To have my pong ball switch directions I just set -xVelocity when the objects touch or overlap
@@BroCodez i thought so too, but i think their velocity is still going forward, so to reverse their velocity effectively makes it 0. Hence why they collide and stick there.
@@BroCodez I did get it to work using something along those lines. The main issue is I shouldnt even be using AWT lol.
Thanks bro!
Can you help what if I place random squares on the board and they overlap collision equals true and square gets stuc...k
HOLA BUEN DIA PODRIA POR FAVOR AYUDARME ? PROGRAMA CUANDO CORRO EL PROGRAMA NO SE AFICHA NADA EN LA VENTANA APARECE VACIA ? CUAL PUEDE SER EL PROBLEMA GRACIAS
How to change the characters into a image?
Cool
How would I make it so that it doesn't allow it to go any further?
you could do that with an if statement before moving your square. Check to see if the location of your square is less than the width and height of the panel/frame.
@@BroCodez I meant by further than the block like a grass block in minecraft
thank you in advance
thx bro:3
thanks broo
it keeps saying The constructor Box(int, int, int, int, Color) is undefined
Bros console box covers like 90% of the screen.
What is the app in which u did coding
Eclipse
bro can you please post a video on how to do a snake game in java /c++
please!!!!!!!!!😭😭🤯🤯
I think it might be difficult one. But it would be easier to write with Javascript and jQuery .
I was thinking about that recently. If it's not difficult, I might
@@BroCodez thank you bro
Thx bro!
Nice bro:)
Well how do I make a method that just makes the enemy just unnacessable, as in, it just stops when you try to reach it?
thats the hard part
whenever a movement is inputted, the screen goes white for a split second unlike in the video where the animation is smooth
I'm searching for a solution. Are all components (blue box, red box, "Game Over") flickering as well?
@@BroCodez Yes, both boxes and "Game Over" flicker
The paint() method clears the screen and then paints a new one one within your frame. At times, the screen may get updated between the clearing phase and painting phase. Which may cause a flicker effect. A possible solution is to use Double Buffering for your image. Maybe try changing:
Image image
//replace with
BufferedImage image
&
image = createImage(this.getWidth(),this.getHeight())
//replace with
image = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
@@Acidicfly247 if you still haven't found a solution yet use this bufferstrategy method and run the render method in your public class AL extends KeyAdapter
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
}
the flickering will stop
Bro....that was a little difficult ...🤔gotta watch with care....
this was a difficult topic
@@BroCodezyep, actually just a quick question, I followed your snakegame tutorial, and I was wondering if there was a way to set an image as the background instead of just black. Thanks! :)
@@atanki5682
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class backgroundImage extends JFrame
{
JButton b1;
JLabel l1;
public backgroundImage() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("xyz.jpg"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
}
public static void main(String args[])
{
new backgroundImage();
}
}
use this code snippet
Thanks 🙏
If you were to make this into a JAR file to share it with people would it only save one class or all of them?
it would turn all java files to .class files which can be run on windows cmd line via bat file and yes all of them
im a bro now :)
Awesome lesson, thank you. Can I ask you a question? What does this line “graphics = image.getGraphics()” do? I found that it’s never used.
i think its the graphics of the image example not the g2D of the frame itself but the image
Yes, it’s,when you want to perform graphics operation on the image itself.
It is good, but i am not sure i understand this game well. on the screen just 2 box and I can only use arrow key to control one of them. once they are touched each other, game is over. I don't quiet understand it.
it's not a game, just an example
lmfao intersex in youtube sections