//**************************************************************************************** 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); } } //****************************************************************************************
@srinicnu5283
3 жыл бұрын
could you please make a video on shooting bullets.
@markfeliciano
3 жыл бұрын
can teach render with collision for the next video?
@ZLCProductions
Жыл бұрын
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
@girl6994
4 жыл бұрын
I found one of the habit of Bro code. When he writes constructors, he doesn't like to write public keyword
@atanki5682
3 жыл бұрын
Welp
@-Lexi_Bexi-
Жыл бұрын
Acctualy yea-
@arxivisdtp2941
Жыл бұрын
Thanks for the video, I could get the idea of collisions now.
@edgy1076
4 жыл бұрын
Thank you for the enlightment!
@damiaotiktok023
6 ай бұрын
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
@markandrianescalante4888
12 күн бұрын
just watched this from 2024, why is my GUI still flickering? did something change in java from this vid to now??
@rionacko748
3 жыл бұрын
my mornings with brocode videos
@bitlong4669
Жыл бұрын
Liking commenting, and subscribing…. Rectangle class ftw. It has intersects() method.
@ZLCProductions
Жыл бұрын
Perfect video I just don’t understand graphics at all… breaks my spirit really to come this far and have to give up…
@turtleguy97
2 жыл бұрын
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
@SkandaT
3 жыл бұрын
thx man your videos are soooo godlike
@ilyayasenev8118
3 жыл бұрын
ты лучший! with love from Russia!
@vamsikrishna-qn3gn
4 жыл бұрын
bro can you please post a video on how to do a snake game in java /c++ please!!!!!!!!!😭😭🤯🤯
@girl6994
4 жыл бұрын
I think it might be difficult one. But it would be easier to write with Javascript and jQuery .
@BroCodez
4 жыл бұрын
I was thinking about that recently. If it's not difficult, I might
@vamsikrishna-qn3gn
4 жыл бұрын
@@BroCodez thank you bro
@noah77
4 жыл бұрын
Cool
@mateusabdallah1172
Жыл бұрын
Hey bro...I'm confused about the methods paint(). When you call it?
@Mindinmatrix
2 жыл бұрын
Thanks bro!
@ZainMacMaster
Жыл бұрын
How could you the program stop when the two boxes collide or did I miss something in the video
@danielacosta119
2 жыл бұрын
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
@koei3920
Жыл бұрын
Bros console box covers like 90% of the screen.
@droidlycodes
2 жыл бұрын
Can you help what if I place random squares on the board and they overlap collision equals true and square gets stuc...k
@asianman103
3 жыл бұрын
it keeps saying The constructor Box(int, int, int, int, Color) is undefined
@ahmadabeer3523
3 жыл бұрын
im a bro now :)
@tungkieu5457
3 жыл бұрын
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.
@PurplePotatoBro
3 жыл бұрын
i think its the graphics of the image example not the g2D of the frame itself but the image
@bitlong4669
Жыл бұрын
Yes, it’s,when you want to perform graphics operation on the image itself.
@GST-axelenandoh
Жыл бұрын
thanks broo
@chicogomu4382
5 ай бұрын
thx bro:3
@AHSANALI-ev7sx
4 жыл бұрын
Bro....that was a little difficult ...🤔gotta watch with care....
@BroCodez
4 жыл бұрын
this was a difficult topic
@atanki5682
3 жыл бұрын
@@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! :)
@DreamDropsTV
3 жыл бұрын
@@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
@tomaszkarolak6179
3 жыл бұрын
Thx bro!
@nehalayaaz9406
4 жыл бұрын
Thanks 🙏
@augischadiegils.5109
3 жыл бұрын
Nice bro:)
@rmdir
4 жыл бұрын
But how about some physics like they bounce off of each other with velocity?
@BroCodez
4 жыл бұрын
I have a Pong video I'm working on rn
@rmdir
4 жыл бұрын
@@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
4 жыл бұрын
@@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
@rmdir
4 жыл бұрын
@@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.
@rmdir
4 жыл бұрын
@@BroCodez I did get it to work using something along those lines. The main issue is I shouldnt even be using AWT lol.
@improvisthenics
Жыл бұрын
lmfao intersex in youtube sections
@prexverdeflor01
2 жыл бұрын
How to change the characters into a image?
@sly2917
4 жыл бұрын
How would I make it so that it doesn't allow it to go any further?
@BroCodez
4 жыл бұрын
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.
@sly2917
4 жыл бұрын
@@BroCodez I meant by further than the block like a grass block in minecraft thank you in advance
@atanki5682
3 жыл бұрын
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?
@shadmilkgod7115
3 жыл бұрын
thats the hard part
@generalimperial1910
3 жыл бұрын
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?
@peacefuldeityspath
2 жыл бұрын
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
@Acidicfly247
4 жыл бұрын
whenever a movement is inputted, the screen goes white for a split second unlike in the video where the animation is smooth
@BroCodez
4 жыл бұрын
I'm searching for a solution. Are all components (blue box, red box, "Game Over") flickering as well?
@Acidicfly247
4 жыл бұрын
@@BroCodez Yes, both boxes and "Game Over" flicker
@BroCodez
4 жыл бұрын
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);
@boletum6940
3 жыл бұрын
@@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
@girl6994
4 жыл бұрын
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.
Пікірлер: 67