Java Programming: Let’s Build a Game #5

Produce your own Java game from scratch with this very simple, yet very powerful course available right now

In this video we take care of full on collision within our game and also a particle trail effect. If you have any questions then be sure to leave a comment or contact me on twitter; realtutsgml. If you learned something then be sure to leave a like, comment, and favorite.

Have you ever wanted to create games? Have you ever gotten fed up with it being to difficult? Well now is the time to thank me and possibly subscribe because you have just found the channel for you! Game Maker Tutorials, Java Game Programming, Unity3D Tutorials, Batch, C++ and much much more! This is the channel for you, the one stop shop for an exploration of your hidden talent as a game developer. Unleash your potential and go wild with imagination when you finally figure out how to make any game you want!

Visit CodingMadeSimple for more exclusive tutorials and get the help you need to succeed as your very own indie game developer!

Follow me on twitter for exclusive content and interaction with me!

Follow me on Google+ to keep updated with all of my tutorials

Game Maker Studio: Programming
Game Maker Studio: Tutorial
Java Programming
Game Programming
Game Tutorial
Programming Tutorial






42 responses to “Java Programming: Let’s Build a Game #5”

  1. skuul88 Avatar

    ok so im pretty new to this whole programming thing, and I managed to set the ID of the trail to BasicEnemy. This caused a collision issue where the trail was colliding with the enemy square due to the nature of the collision method and how it loops through the whole object list. I was able to locate and remedy the issue after about 10 minutes, and it felt so damn good.

  2. Kyle Pereira Avatar

    How are you not getting ConcurrentModificationExceptions rn?

  3. Scarlet Chiira Avatar

    I did the exact same stuff as in the video but somehow I botched it and its not working. I was having real fun with this but sadly I can't continue because there's no source code. And if there is then if you have to buy it. I'm not going to, because that is a poor way to actually educate someone on how to build a game. If you want to educate people at least lend the source code off to a repository on GitHub so that way people can write the code at their own pace and see where they made the mistake.

  4. swishy _ fishy Avatar

    lol i thought he said in our sex method at: 1:58

  5. Your Average Joe Avatar

    For anyone having trouble with key inputs, (pressing multiple keys at once causes the player to stop moving in the correct direction) here are the correct functions for the KeyInput class

    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    for(int i = 0; i < handler.object.size(); i++) {
    GameObject tempObject = handler.object.get(i);

    if(tempObject.getId() == ID.Player) {
    //key events for player

    if(key == KeyEvent.VK_W) tempObject.setVelY(tempObject.getVelY()-5);
    if(key == KeyEvent.VK_A) tempObject.setVelX(tempObject.getVelX()-5);
    if(key == KeyEvent.VK_S) tempObject.setVelY(tempObject.getVelY()+5);
    if(key == KeyEvent.VK_D) tempObject.setVelX(tempObject.getVelX()+5);

    if(key == KeyEvent.VK_ESCAPE) System.exit(1);

    public void keyReleased(KeyEvent e){
    int key = e.getKeyCode();

    for(int i = 0; i < handler.object.size(); i++) {
    GameObject tempObject = handler.object.get(i);

    if(tempObject.getId() == ID.Player) {
    //key events for player

    if(key == KeyEvent.VK_W) tempObject.setVelY(tempObject.getVelY()+5);
    if(key == KeyEvent.VK_A) tempObject.setVelX(tempObject.getVelX()+5);
    if(key == KeyEvent.VK_S) tempObject.setVelY(tempObject.getVelY()-5);
    if(key == KeyEvent.VK_D) tempObject.setVelX(tempObject.getVelX()-5);


  6. Zachary Young Avatar

    in the method public Rectangle getBounds() in the BasicEnemy class, if you use: return new Rectange(velX, velY, 16, 16); instead of x and y, it is much smoother than using x & y.

  7. Karex_ Avatar

    i don't know why but my health doesn't go down for some reason. Can someone please help me? 11:03

  8. Vitex Avatar

    This is actually really fun, I've never had so much fun on a 2D game before, this is great! Thank you <3

  9. Robert Balassan Avatar

    anyone got any suggestion on how to differentiate player 1 from player 2 in the getBounds method? (i kept the game 2 players) the problem is when any player collides both health bars decrease

  10. Marcus Case Avatar

    Just wondering, in that for loop, u make a temporary object. Would it be faster memory wise to have tempObject as a local variable, and set it to the handler.object.get(i), and then set it to null afterwards?

  11. SauceMaster2K Avatar

    Why does it say Exception in thread "Thread-2" java.lang.IllegalArgumentException: alpha value out of range

  12. Ephraim Bane Avatar


    No trail shows up, as far as I know it's not creating the object.
    In BasicEnemy:
    public void tick() {
    x += velX;
    y += velY;

    if (y <= 0 || y >= Game.HEIGHT – 45) velY *= -1;
    if (x <= 0 || x >= Game.WIDTH – 22) velX *= -1;

    handler.addObject(new Trail(x, y, ID.Trail,, 16, 16, 0.01f, handler));

  13. Rens Hulsebosch Avatar

    I fucked up, but don't know where… And you didn't post source code. Shame, I was enjoying this…

  14. TheBrohman Avatar

    I have an issue. In my player class It can't get "handler.object.size" because "object" is not public. Do I need to change it to public or have I missed something? I do have "private Handler handler;" and "this.handler = handler" etc.

  15. Or Dadush Avatar

    hi I'm having a problem about min 23I run the game and after a second the Player and the BasicEnemy disappears (the HUD not) I don't get any error the game is still running I have no idea what do to someone has any answer

  16. Teofilo Francisco Avatar

    Hi, I was having an error: java.util.ConcurrentModificationException, because I used an iterator to loop over the GameObject list, then I used a CopyOnWriteArrayList and every thing goes well. Thanks for your tutorial.

  17. Remag Avatar

    Whenever I run it the trails are synced but they don't line up which causes them to move at the same time but when I press w the trail moves left and the player move up. PLZ Help

  18. SkViX Avatar

    If your getting an error "Thread-2". Its because u haven't written Handler code in player class 8:11

  19. Smily Clown Avatar

    handler.addObject(new Trail(x + 4, y + 4, ID.Trail, Color.WHITE, 24, 24, 0.02f, handler));

    for better effect on player's trail.
    If trails is equal to player, when you move horizontal or vertical it looks like a line.
    So i make trail smaller and now you can see where is head and where is trail.

  20. Nicolas Bohnenberger Avatar

    As soon as I run the code with the collision method I get a NullPointerExpection coming from the loop. Just throws the error:

    Exception in thread "Thread-2" java.lang.NullPointerException
    at com.frogger.Player.tick(
    at com.frogger.Handler.tick(
    at com.frogger.Game.tick(
    at Source)

    +RealTutsGML any idea why?

  21. TRESCHK Avatar

    I'm getting an error:

    Exception in thread "Thread-2" java.lang.NullPointerException
    at java.awt.Rectangle.intersects(Unknown Source)
    at de.cryptodev.game2.main.Player.collision(
    at de.cryptodev.game2.main.Player.tick(
    at de.cryptodev.game2.main.Handler.tick(
    at de.cryptodev.game2.main.Game.tick(
    at Source)

  22. Adam Hussain Avatar

    My player is laggy anyone know how to fix it.It has something to do with the buffer strategy

  23. Mahdi Avatar

    This is quite old but I have the code perfectly fine but I keep getting lag spikes every 5-10 seconds and it gets worse after every spike does anyone know how to fix that?

  24. Roy Westerhof Avatar

    the line "else handler.removeObject(this);" makes the game crash for me… why?

  25. Sasha Omerasvic Avatar

    for the if(getBounds().intersects(tempObject.getBounds())){
    I keep getting an error under the second getBounds. Any idea why?

  26. Kimera / キメラ Avatar

    For those getting errors with the collision function in the "player class", make sure you added this line of code in the "public Player": this.handler = handler;
    I did that and it cleared the problem right up and now it works as intended

  27. MrBearcat82 Avatar

    Totally been using these videos for an FBLA State competition learning tool!

  28. PumpkiNot Avatar

    Is the whole video about trails?

  29. Manfred Orse Avatar

    7:20 is it colliding if its fully inside?

  30. Chocolate Storm Avatar

    my game starts stuttering when i add trails… any ideas?

  31. Cosme Tejeda Avatar

    Is there a way to edit the code so the BasicEnemy bounces off of the Player when hit instead of going through?

    What I did was make another collision method for the BasicEnemy and if it collides with the player I changed the BasicEnemy's velX *= -1 and velY *= -1 but sometimes the player will keep moving towards the enemy and a weird glitch will happen. Is there a more efficient way to do this?

  32. Sam Mader Avatar

    Can't fix a bug where my health drops to 0 after one hit. Everything is the same as in his code.

  33. The Gaming Koala Avatar

    im making a tower defense type of game, so im having the towers attacking the soldiers, i have them all placed and moving how id like(besides the fact id like the hitbox to be a circle) . this is what id like to know. how would i go about making the towers hitting one enemy at a time, and hitting the closest one. and how would i make the hitbox being a circle


    Trying to keep up with the furious typing is actually really fun to do

  35. Bananasfan Avatar

    there is something wrong with the controlls when ever im using it for a little while the controlls stop working

  36. Pop Club Avatar

    Anyone's Game Freezing after adding the Trail? Here's a fix!

    Check your Handler Class and see if the addObject and removeObject methods are exactly like this:

    public void addObject(GameObject object){

    public void removeObject(GameObject object){

  37. Tim Studio Avatar

    Health bar is not getting low need help. Please do something.

  38. Raxx Scar Avatar

    i love your desktop picture!

  39. Raxx Scar Avatar


  40. Rhoncus Vitae Avatar

    My player trail refuses to be any size other than 16, 16. I copied
    handler.addObject(new Trail(x, y, ID.Trail,, 16, 16, 0.05f, handler));
    from BasicEnemy class into Player class and changed it to
    handler.addObject(new Trail(x, y, ID.Trail, Color.white, 32, 32, 0.05f, handler));

    The color changed to white but the size of the trail didn't.

  41. LGsmoesie Avatar

    I'm trying to follow along with the series so I'm doing everything the same as you (apparently not) and there's no error in my code. Still, when I'm launching the game there is no trail. Can anyone tell what is wrong or what could be wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *