Java collision detection 💥

Поделиться
HTML-код
  • Опубликовано: 11 дек 2024

Комментарии • 68

  • @BroCodez
    @BroCodez  4 года назад +26

    //****************************************************************************************
    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
      @srinicnu5283 3 года назад

      could you please make a video on shooting bullets.

    • @markfeliciano
      @markfeliciano 3 года назад

      can teach render with collision for the next video?

  • @ZLCProductions
    @ZLCProductions Год назад +1

    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
    @girl6994 4 года назад +4

    I found one of the habit of Bro code. When he writes constructors, he doesn't like to write public keyword

  • @ZLCProductions
    @ZLCProductions Год назад +1

    Perfect video I just don’t understand graphics at all… breaks my spirit really to come this far and have to give up…

  • @arxivisdtp2941
    @arxivisdtp2941 Год назад

    Thanks for the video, I could get the idea of collisions now.

  • @edgy1076
    @edgy1076 4 года назад +1

    Thank you for the enlightment!

  • @markandrianescalante4888
    @markandrianescalante4888 2 месяца назад

    just watched this from 2024, why is my GUI still flickering? did something change in java from this vid to now??

  • @bitlong4669
    @bitlong4669 Год назад

    Liking commenting, and subscribing…. Rectangle class ftw. It has intersects() method.

  • @rionacko748
    @rionacko748 3 года назад

    my mornings with brocode videos

  • @mateusabdallah1172
    @mateusabdallah1172 Год назад

    Hey bro...I'm confused about the methods paint(). When you call it?

  • @SkandaT
    @SkandaT 3 года назад

    thx man your videos are soooo godlike

  • @turtleguy97
    @turtleguy97 2 года назад +1

    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

  • @ilyayasenev8118
    @ilyayasenev8118 3 года назад

    ты лучший!
    with love from Russia!

  • @ZainMacMaster
    @ZainMacMaster Год назад

    How could you the program stop when the two boxes collide or did I miss something in the video

  • @damiaotiktok023
    @damiaotiktok023 9 месяцев назад

    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

  • @rmdir
    @rmdir 4 года назад +3

    But how about some physics like they bounce off of each other with velocity?

    • @BroCodez
      @BroCodez  4 года назад +1

      I have a Pong video I'm working on rn

    • @rmdir
      @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
      @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
      @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
      @rmdir 4 года назад +1

      @@BroCodez I did get it to work using something along those lines. The main issue is I shouldnt even be using AWT lol.

  • @Mindinmatrix
    @Mindinmatrix 2 года назад

    Thanks bro!

  • @droidlycodes
    @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

  • @danielacosta119
    @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

  • @prexverdeflor01
    @prexverdeflor01 3 года назад

    How to change the characters into a image?

  • @noah77
    @noah77 4 года назад +1

    Cool

  • @sly2917
    @sly2917 4 года назад +1

    How would I make it so that it doesn't allow it to go any further?

    • @BroCodez
      @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
      @sly2917 4 года назад

      @@BroCodez I meant by further than the block like a grass block in minecraft
      thank you in advance

  • @chicogomu4382
    @chicogomu4382 7 месяцев назад

    thx bro:3

  • @GST-axelenandoh
    @GST-axelenandoh Год назад

    thanks broo

  • @asianman103
    @asianman103 3 года назад

    it keeps saying The constructor Box(int, int, int, int, Color) is undefined

  • @koei3920
    @koei3920 Год назад

    Bros console box covers like 90% of the screen.

  • @pratimameghashyamnaik7092
    @pratimameghashyamnaik7092 Год назад

    What is the app in which u did coding

  • @vamsikrishna-qn3gn
    @vamsikrishna-qn3gn 4 года назад +2

    bro can you please post a video on how to do a snake game in java /c++
    please!!!!!!!!!😭😭🤯🤯

    • @girl6994
      @girl6994 4 года назад

      I think it might be difficult one. But it would be easier to write with Javascript and jQuery .

    • @BroCodez
      @BroCodez  4 года назад +2

      I was thinking about that recently. If it's not difficult, I might

    • @vamsikrishna-qn3gn
      @vamsikrishna-qn3gn 4 года назад +1

      @@BroCodez thank you bro

  • @tomaszkarolak6179
    @tomaszkarolak6179 4 года назад

    Thx bro!

  • @augischadiegils.5109
    @augischadiegils.5109 4 года назад

    Nice bro:)

  • @atanki5682
    @atanki5682 4 года назад

    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?

  • @Acidicfly247
    @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
      @BroCodez  4 года назад

      I'm searching for a solution. Are all components (blue box, red box, "Game Over") flickering as well?

    • @Acidicfly247
      @Acidicfly247 4 года назад

      @@BroCodez Yes, both boxes and "Game Over" flicker

    • @BroCodez
      @BroCodez  4 года назад +3

      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
      @boletum6940 3 года назад +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

  • @AHSANALI-ev7sx
    @AHSANALI-ev7sx 4 года назад +1

    Bro....that was a little difficult ...🤔gotta watch with care....

    • @BroCodez
      @BroCodez  4 года назад

      this was a difficult topic

    • @atanki5682
      @atanki5682 4 года назад

      @@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
      @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

  • @nehalayaaz9406
    @nehalayaaz9406 4 года назад

    Thanks 🙏

  • @generalimperial1910
    @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
      @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

  • @ahmadabeer3523
    @ahmadabeer3523 3 года назад +1

    im a bro now :)

  • @tungkieu5457
    @tungkieu5457 3 года назад +2

    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
      @PurplePotatoBro 3 года назад

      i think its the graphics of the image example not the g2D of the frame itself but the image

    • @bitlong4669
      @bitlong4669 Год назад

      Yes, it’s,when you want to perform graphics operation on the image itself.

  • @girl6994
    @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.

    • @BroCodez
      @BroCodez  4 года назад +2

      it's not a game, just an example

  • @improvisthenics
    @improvisthenics Год назад

    lmfao intersex in youtube sections