Event (Damage Pit, Healing Pool, Teleport Tile) - How to Make a 2D Game in Java #19

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

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

  • @tarnum113
    @tarnum113 3 года назад +23

    This is the best series. Doesn’t matter whether you want to have a career in gamedev or not. It’s perfect for learning how to program in general.

  • @GustavoHR115
    @GustavoHR115 3 года назад +6

    THE BEST Java Game-Dev on youtube, thanks for the videos man!

  • @Mypremiumacct
    @Mypremiumacct 6 месяцев назад +1

    Had little time to do anything today. Life has been hard but this series is the best thing to come home to

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

    Thank you! Please continue with lessons!

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

    Thank you for the amazing video.

  • @TerminallyUnique95
    @TerminallyUnique95 3 года назад +7

    Will you show how to add events to objects too? Also, when the player keeps walking through the pit they constantly take damage and get message, can we make it so event only happens if they leave and go back through the pit?

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

      We will add object reactions eventually (we've already done it in the first half but it will be handled in more object oriented way in this second half). For the event keeps happening when you are going to the same direction, there are two ways to fix. The one is creating some margin so the event won't happen again until player moves certain amount of pixels from where the event happened. This will be implemented when we add stairs in the future. The second one is making the event one time only so each event has boolean value and check if the event has already happened in the past or not. For the second method, you'll probably want to handle it by creating an event tile class. Maybe I will make a video for this.

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

      I created a method to take in a direction and move the player backwards if they step on a pit.
      You could also create some sort of timer where the player is invincible for a second or so after taking damage

  • @inuyasha4669
    @inuyasha4669 Год назад +2

    hello Mr. Ryisnow
    i follow your lesson.
    in part 19 I found an error. where the application runs but when it will "healing" in water. "enter" doesn't work.
    Please help

  • @masterred8073
    @masterred8073 10 месяцев назад

    Hi, can you help me with this moment at 11:35, when I try to walk, a window immediately appears that I fell into a hole - a dialog box, and each press of the w button opens a window, but in fact it should have worked on a certain place, but immediately after pressing w a window appears, how can I fix this, I didn’t write the code after 11:35

  • @abcd-pr6tw
    @abcd-pr6tw 3 года назад +2

    hi, is it normal if after falling into the pit (min 11:45 of the video), I can't move away like you did? Even if I press enter the message "You fall into a pit" doesn't disappear :/

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

      Can you close the window when you talk to an NPC?

    • @abcd-pr6tw
      @abcd-pr6tw 3 года назад

      @@RyiSnow Yes

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

      May I see your KeyHandler class?

    • @abcd-pr6tw
      @abcd-pr6tw 3 года назад

      @@RyiSnow Here: package main;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      public class KeyHandler implements KeyListener{
      public boolean enterPressed, upPressed, downPressed, leftPressed, rightPressed;
      GamePanel gp;


      public KeyHandler(GamePanel gp) {
      this.gp = gp;
      }
      @Override
      public void keyPressed(KeyEvent evt) {
      int code = evt.getKeyCode();

      //game state
      if (gp.gameState == gp.Play) {

      if(code == KeyEvent.VK_Z) {
      upPressed = true;
      }
      if(code == KeyEvent.VK_S) {
      downPressed = true;
      }
      if(code == KeyEvent.VK_Q) {
      leftPressed = true;
      }
      if(code == KeyEvent.VK_D) {
      rightPressed = true;
      }
      if(code == KeyEvent.VK_P) {
      gp.gameState = gp.pauseState;
      }
      }
      if(code == KeyEvent.VK_ENTER) {
      enterPressed = true;
      }

      else if (gp.ecranEtat == gp.pauseState) {
      if(code == KeyEvent.VK_P) {
      gp.gameState = gp.Play;
      }
      }
      else if (gp.gameState == gp.Dialogue) {
      if(code == KeyEvent.VK_ENTER) {
      gp.gameState = gp.Play;
      }
      }
      }

      @Override
      public void keyReleased(KeyEvent evt) {
      int code = evt.getKeyCode();

      if(code == KeyEvent.VK_Z) {
      upPressed = false;
      }
      if(code == KeyEvent.VK_S) {
      downPressed = false;
      }
      if(code == KeyEvent.VK_Q) {
      leftPressed = false;
      }
      if(code == KeyEvent.VK_D) {
      rightPressed = false;
      }
      }
      }

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

      Are you sure you can quit NPC dialogues by pressing Enter? I tested your code but NPC dialogue window doesn't close even if I press enter.

  • @the0wolf15
    @the0wolf15 Месяц назад +1

    my code somehow messed up so now every UI element goes super fast. Like for example, when I walk over the event trigger that takes away half a heart... mine takes away all three. It also skims every sentence with the NPC if I walk while interacting with it.
    Edit: I figured out what was wrong. In "GamePanel", the "gameState == dialogueState" was set to "player.update()" for some reason

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

    not sure if it would cause issues down the line in this tutorial but i put a release key check for enter in the keyhandler to avoid errors like that.

  • @gustavhenriksson867
    @gustavhenriksson867 2 года назад +2

    Love this im totaly hooked creating this game! Really fun and a lot of learning!
    now i have not watched all the episodes but i wonder, all these entity, player and object collition where you check the intersect isnt these possible to turn in to a method instead? or is it to many different values being handle to achieve this?

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

    I am having this key issue. Whenever I try to drink water, I have to press the W+Enter otherwise it doest work. Same problem with the npc, when i try to talk to the oldman, i have to press any direction key+enter to talk to him

    • @RyiSnow
      @RyiSnow  2 года назад +2

      Yes, that was a bug and has been addressed at the start of Part 23.

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

      @@RyiSnow oh thank you very much☺️ Your tutorials are very helpful. I prefer this more than using game engines 👌

  • @YasmineChar-d7h
    @YasmineChar-d7h 3 месяца назад

    My question might be trivial, but could you tell me which part of this script ensures that the tile event isn't triggered continuously?

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

    I think i must be missing something, but why doesnt the pit event trigger continously? Its being check every frame, and as long as youre in the event collision, it should still trigger.

  • @beardiegames014
    @beardiegames014 6 месяцев назад

    I’ve added another public void to not heal the player by checking if the player’s life is lower than its max life. If not then display a different dialogue saying that the player’s health is fine and don’t heal the player.

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

    I need your help @RyiSnow , when i comment out the EventHandler class the game works as soon as i load the Eventhandler class from the GamePanel. The game never starts, ive tried debugging it. Seems like its stuck in a loop when loading the EventHandler.

  • @cabircoskun5772
    @cabircoskun5772 4 месяца назад

    my healing function is not working. i think the reason is water is a collision = true tile. when the character tries to reach to a healing water tile the collision is not letting character to touch the healing rectangle. so the hit function returns false. if i put the healing tile in a collision = false tile, then it works fine. how can i fix that ?

    • @cabircoskun5772
      @cabircoskun5772 4 месяца назад

      okay i fixed that, im not sure if this solution brings more problem in the future :) i guess it is a problem if the hit function is called for an object but not for a tile.
      My solution is : when the hit function is working on a collision = true tile , i expanded the rectangle's area. So the collision is still happening but now the character can reach the healing area. here is the code : if anyone can see a bug here pls inform me..
      int tileNum = gp.tileM.mapTileNum[eventCol][eventRow];
      if(gp.tileM.tile[tileNum].collision == false) {
      gp.player.solidArea.x += gp.player.worldX;
      gp.player.solidArea.y += gp.player.worldY;
      eventRect.x += eventCol*gp.tileSize;
      eventRect.y += eventRow*gp.tileSize;
      }else {
      gp.player.solidArea.x += gp.player.worldX;
      gp.player.solidArea.y += gp.player.worldY;
      eventRect.x = -8 ;
      eventRect.x += eventCol*gp.tileSize;
      eventRect.y = -8;
      eventRect.y += eventRow*gp.tileSize;
      eventRect.width = 64;
      eventRect.height = 64;

      }

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

    Great videos!

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

    Hi, when I try to make the event happen, my hit is always true because I don't have time to move my character away from the event emplacement so the event is constantly happening. Do you have a solution ? I also think I have the same problem for dialogues.

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

      Maybe you forgot to change the enterPressed to false?

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

      @@RyiSnow I mean if I try to dialogue with the NPC just by hitting him (without using keyPressed) it works. But then I use the enter key to quit dialogue and the game instantly gets back to dialogueState because I'm still hitting the NPC and I don't have time to move away from him before the next loop. And it seems that I have the same kind of problem with events

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

      Yeah, so did you add the line: enterPressed = false after checking everything like we did in the video?

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

      Okay I'm sorry I'm going to check my code and the video again I probably made a mistake somewhere

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

      @@cedrichuynh9752 No worries!

  • @holmdev
    @holmdev 2 года назад +2

    i have done al the things i eventHandler, nothing happend (what can i have done wrong)

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

      same issue here idk

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

      @@Rebigex My fix was to set the rectangle to 96, imo 2 is way way too small

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

    Thank you!

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

    Can you make a multi-level system? Example: The Player collides with a specific Tile and hes taken into a different level and if he collides with another Tile he returns to the previous level

    • @RyiSnow
      @RyiSnow  3 года назад +5

      It will be featured when we implement a dungeon level in the later part of the series :)

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

    Also, where are the "right" and "up" being called from?
    Sorry this may be a silly q!

  • @freeballnut
    @freeballnut 9 месяцев назад +2

    Hi RyiSnow! Not sure if you are still replying to comments on these videos, but I want to thank you for creating a series that is in depth, very helpful and a great learning material. You claim to be a mediocre programmer but I think everyone here including myself would definitely beg to differ. Anyways, I was hoping to have some help with an issue or two i am having. I am having some issues with my event handler, specifically, the healing pool event isn't working at all (doesn't read e(i used e instead of enter) being pressed) and won't show any dialogue. My damage pit, however works TOO well. It will work as I walk into it, showing the dialogue, but then I will have to press e and move left multiple times to get out , as if i am stuck. This causes my player to lose 1 & a half hearts. I saw yours allows you to just press enter and walk out and then walk right back in, but mine won't do that. please help! I will send my code if need be. Although my EventHandler class I THINK is exactly the same😅

    • @Mypremiumacct
      @Mypremiumacct 6 месяцев назад

      If he doesnt respond, try asking chat gpt. its a cheap way to see if it can sniff out basic coding errors, if not there are tons of subs on reddit that can help. I coded a dodgeroll just for fun and learned how to do it with Chatgpt. its not the most reliable, but it can help tanslate some of the things and catch a missing { or capitalized letter where it neednt be. Hope you figured it out!

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

    I am having trouble with the events and can't seem to get them to work. The event rectangle does not seem to stay in the same place, rather it has a fixed location on the screen and moves whenever the character moves. Anyone know how to fix this?

    • @masterred8073
      @masterred8073 10 месяцев назад

      Do you fix it , if you can please tell me how ?

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

    when i press key for closing dialoguebubble, when i step on fallingPit, i cannot close it
    can anyone help?

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

    Amazing series.
    Could you please upload the entire and updated code on a drive pls?
    That could be awesome. Thank you so much for the series.

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

      blud what 💀💀💀💀💀 just type it out 😂😂😂😂😂😂

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

      @@BrimstoneMwG ok broski. i did almost the whole series last december then i lost motivation so i stopped then i started again a few months later then i stopped again then last week i accidentally cleared my entire drive so i kinda lost everything. i know you wont believe this but its true.

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

    Loving it so far. Do you think this would scale OK for something as big as a Dragon Quest NES game?

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

      Actually this has a higher resolution than the NES Dragon Quest which is 256x240 :P

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

      @@RyiSnow oh neat. Question: I’m getting black lines when I move up or left in proportion to the speed and can’t figure out why. Any thoughts?
      It’s fine when I don’t move or move down or to the right

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

    amazing amazing amazing amazing

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

    How can we do it for multiple directions not only right?
    Thanks in advance!

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

    Thanks!

  • @Klomerastickles-37onBloodbath-
    @Klomerastickles-37onBloodbath- 6 месяцев назад +1

    LOL I were teleported IN the npc so i couldn't escape from him XD

    • @RyiSnow
      @RyiSnow  6 месяцев назад +1

      Haha you can check the current NPC position and adjust the position to avoid that.

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

    Please help I can not get events to work.

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

      package main;
      import java.awt.Event;
      import java.awt.Rectangle;
      public class EventHandler {
      GamePanel gp;
      Rectangle eventRect;
      int eventRectDefaultX, eventRectDefaultY;
      public EventHandler(GamePanel gp) {
      this.gp = gp;
      eventRect = new Rectangle();
      eventRect.x = 8;
      eventRect.y = 8;
      eventRect.width = 8;
      eventRect.height = 8;
      eventRectDefaultX = eventRect.x;
      eventRectDefaultY = eventRect.y;
      }
      public void checkEvent() {
      if(hit(24,16,"right") == true) {
      damagePit(gp.dialougeState);
      }
      if(hit(24,16,"up") == true) {
      damagePit(gp.dialougeState);
      }
      }

      public boolean hit(int eventCol, int eventRow, String reqDirection) {
      boolean hit = false;
      gp.player.soildArea.x = gp.player.worldX + gp.player.soildArea.x;
      gp.player.soildArea.y = gp.player.worldY + gp.player.soildArea.y;
      eventRect.x = eventCol*gp.tileSize + eventRect.x;
      eventRect.y = eventRow*gp.tileSize + eventRect.y;
      if(gp.player.soildArea.intersects(eventRect)) {
      if(gp.player.direction.contentEquals(reqDirection) || reqDirection.contentEquals("any")) {
      hit = true;
      }
      }
      gp.player.soildArea.x = gp.player.soildAreaDefaultX;
      gp.player.soildArea.y = gp.player.soildAreaDefaultY;
      eventRect.x = eventRectDefaultX;
      eventRect.y = eventRectDefaultY;
      return hit;
      }
      public void damagePit(int gameState) {
      gp.gameState = gameState;
      gp.ui.currentDialouge = "You Fell Into A Pit, Damn It!!";
      gp.player.life -= 1;
      }
      }

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

      redo
      package main;
      import java.awt.Event;
      import java.awt.Rectangle;
      public class EventHandler {
      GamePanel gp;
      Rectangle eventRect;
      int eventRectDefaultX, eventRectDefaultY;
      public EventHandler(GamePanel gp) {
      this.gp = gp;
      eventRect = new Rectangle();
      eventRect.x = 23;
      eventRect.y = 23;
      eventRect.width = 4;
      eventRect.height = 4;
      eventRectDefaultX = eventRect.x;
      eventRectDefaultY = eventRect.y;
      }
      public void checkEvent() {
      if(hit(27,16,"right") == true) {damagePit(gp.dialougeState);}
      }
      public boolean hit(int eventCol, int eventRow, String reqDirection) {
      boolean hit = false;
      gp.player.soildArea.x = gp.player.worldX + gp.player.soildArea.x;
      gp.player.soildArea.x = gp.player.worldY + gp.player.soildArea.y;
      eventRect.x = eventCol*gp.tileSize + eventRect.x;
      eventRect.y = eventRow*gp.tileSize + eventRect.y;
      if(gp.player.soildArea.intersects(eventRect)) {
      if(gp.player.direction.contentEquals(reqDirection) || reqDirection.contentEquals("any")) {
      hit = true;
      }
      }
      gp.player.soildArea.x = gp.player.soildAreaDefaultX;
      gp.player.soildArea.y = gp.player.soildAreaDefaultY;
      eventRect.x = eventRectDefaultX;
      eventRect.y = eventRectDefaultY;
      return hit;
      }
      public void damagePit(int gameState) {
      gp.gameState = gameState;
      gp.ui.currentDialouge = "You fall into a pit!";
      gp.player.life -= 1;
      }

      }

    • @neighdoug6773
      @neighdoug6773 26 дней назад

      @@nicholasbrooks7349 holy it works now thanks man, you're a savior

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Год назад

    🥰🥰🥰

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

    Does anyone know why is this Event thing necessary? Wouldn't it be easier to just make an object with no collision that makes the same thing? Or even using tiles instead of objects? Couldnt you make a pit tile and check if the player is on it with the checkTile method we already did in the CollisionChecker class?

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

      We're basically handling it by making objects with no collision (EventRect) here. If what you mean is creating a class for each event, you'll need to create many classes as your game becomes larger (if the game has 100 events, you'll have 100 classes). It works but I thought handling it in a single class is more convenient so you don't need to search & open the class every time you adjust its coordinates or other parameters.
      As for handling it with the CollisionChecker, the tile basically becomes a collision tile so you cannot step on or walk through it (like hitting an invisible NPC).
      That said the method shown in this video is not the only way so feel free to try your own idea and improve it. Maybe you can come up with something better :)

  • @abd-elrahmanmahmoud3167
    @abd-elrahmanmahmoud3167 2 года назад

    hello sir, please i need your help in little thing in my college project

  • @ΚΥΡΙΑΚΟΣΓΕΩΡΓΑΝΤΗΣ-υ4δ

    This is good series but it would be better if you tried using more proper java. For example, not making every value public and having methods to either get or set some values. Or for GameStates and Directions instead of having Strings and Integers use Enums.

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

      This is not a Java tutorial. Its a game dev tutorial which happens to be done in Java. But if you think you can do better, by all means, show us your free tutorial videos. I'd love to learn some more proper Java as it were.

    • @ΚΥΡΙΑΚΟΣΓΕΩΡΓΑΝΤΗΣ-υ4δ
      @ΚΥΡΙΑΚΟΣΓΕΩΡΓΑΝΤΗΣ-υ4δ 8 месяцев назад +1

      @@AlexanderHL1919 As someone teaching in java code, no matter what that is, you MUST follow the SOLID principle, you MUST very well know design patterns and OOP very extensively. You saying "please provide us with your own tutorials" doesn't justify the fact that the tutorial is badly written. There shouldn't be any bad tutorials as there are loads nowadays and they are the reason new developers struggle to get better. Me making a bad tutorial wouldn't justify my first claim that this tutorial is badly written. Let people that ACTUALLY know how to write OOP do the tutorials. Let's not teach others bad practices just because you feel excited about making tutorials.

    • @AlexanderHL1919
      @AlexanderHL1919 8 месяцев назад

      @@ΚΥΡΙΑΚΟΣΓΕΩΡΓΑΝΤΗΣ-υ4δ Such cringe. Imagine being so entitled that you'd complain about a free service. You're seriously deranged.

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

      ​@ΚΥΡΙΑΚΟΣΓΕΩΡΓΑΝΤΗΣ-υ4δ his Java is fine. Also, It's for a game; I'm a SWE and work with Java every day, his practices are perfectly fine for his purpose. When you get better, you'll realize it's not about rigorous adherence to policies, and more about doing what you can to make things work 🙂

  • @deepborty_productions
    @deepborty_productions 3 месяца назад

    I am facing a problem where the code is not even entering if(gp.player.solidArea.intersects(eventRect)) part [I figured the the if statement was not being accessed by debugging] so do you have any idea why this could be happening?
    Update: So for some reason the event appears a tile diagonally bottom right to the mentioned coordinates, Why does this happen?

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 года назад

    Thanks!