Sprites and Animation - How to Make a 2D Game in Java #3

Sdílet
Vložit
  • čas přidán 23. 08. 2024
  • In this video, we will import player sprites and display them on the screen. We also create a simple animation system so the player character looks like it's walking.
    For IntelliJ users:
    • Reading Resource Files...
    If you want to use the player sprites in the video, here's the link:
    drive.google.c...
    Guidelines for using Blue Boy Adventure's code and assets:
    docs.google.co...
    The website (Piskel) that I used to create the sprites:
    www.piskelapp....
    If you have any questions, please leave a comment!
    #javatutorial #javagamedevelopment #java2d #javaretrogame

Komentáře • 540

  • @RyiSnow
    @RyiSnow  Před 2 lety +90

    To see how to read images in IntelliJ, please check this video:
    czcams.com/video/Q9jtg0ocGgo/video.html
    It seems quite a few people are struggling with "java.lang.IllegalArgumentException: input == null!" error.
    In case of this video, the error happens when the program fails to read sprite images.
    First, *make sure to check if your res folder has been registered as a source folder*
    Especially if you are not using Eclipse since the registration/resource reading process in other IDEs can be different from what I did in the video.
    Also, check if your file names/paths are identical to what you typed in the getPlayerImage method. Check for any unnecessary space, underscore, or other characters. *The file names and paths need to be completely identical.* If you cannot find any discrepancy, try using the same sprites in the video (the link is in the description). If you could display the images, the issue was caused by your modification so you can narrow it down.
    You can check if image loading is done properly or not by inserting these lines in the getPlayerImage method:
    System.out.println("Image loading started");
    // load sprite images....
    // load sprite images....
    System.out.println("Image loading ended");
    The loading was done properly if the second text was printed on the console. If only the first text was printed out, something went wrong during the loading and the program stopped there.

    • @unity_with_timoteo
      @unity_with_timoteo Před 2 lety +2

      In my case i am doing it on vs code. This error occurred. I solve it by changing the path, in my case the path is: "./../src/player/boy_right_2.png", for example.

    • @unity_with_timoteo
      @unity_with_timoteo Před 2 lety

      @Jackson Hello man. Look my answer below, it could help you!

    • @arelyezra6339
      @arelyezra6339 Před 2 lety

      @@unity_with_timoteo did you type this "./../src/player/boy_right_2.png" only or not

    • @arelyezra6339
      @arelyezra6339 Před 2 lety +1

      @Jackson did you can compile it?

    • @shinyspy850
      @shinyspy850 Před 2 lety +2

      you should pin this comment, it was very helpfull.

  • @organista03
    @organista03 Před 2 lety +183

    for IntelliJ users:
    1) create "res" folder:
    - right mouse button on main project folder -> New -> Directory -> "res"
    2) mark "res" folder as Sourses Root:
    - right mouse button on "res" folder -> mark directory as -> Sources Root
    3) create "player" folder inside "res" folder:
    - right mouse button on "res" folder -> new -> Package -> "player"
    4) paste images in "player" folder
    5) you have to add .getClassLoader() between getClass() and .getResourceAsStream()
    OLD:
    up1 = ImageIO.read(getClass().getResourceAsStream("player/boy_up_1.png"));
    NEW:
    up1 = ImageIO.read(getClass().getClassLoader().getResourceAsStream("player/boy_up_1.png"));

    • @darmoforeelz5857
      @darmoforeelz5857 Před 2 lety +12

      This comment right here is pure gold, thank you so much ❤❤

    • @ogaz_
      @ogaz_ Před rokem

      I had to add a try catch to getPlayerImage()
      try {
      getPlayerImage();
      }catch (Exception e){
      System.out.println("getPlayerImage() is not working");
      }

    • @seamus9898
      @seamus9898 Před rokem +3

      there's several solutions posted for IntelliJ, but somehow NOTHING works for me... always get NullPointerException
      I give up

    • @seamus9898
      @seamus9898 Před rokem +5

      omg I just forgot to add
      setDefaultValues();
      getPlayerImage();"
      I'm so annoyed at myself :((((

    • @landon9889
      @landon9889 Před rokem

      Thank you!

  • @michaelharrington5860
    @michaelharrington5860 Před rokem +22

    My son and I finished episode 3 of your series tonight. You do an excellent job explaining everything and teach at the perfect pace. We were both so excited when the little blue haired boy started walking around. Will continue the series tomorrow with episode 4. Thank you!

    • @RyiSnow
      @RyiSnow  Před rokem +8

      It warms my heart to hear that! I hope you and your son will keep enjoying coding :)

  • @yasthatsme
    @yasthatsme Před 2 lety +114

    You deserve more viewers! I have learned so much from you

    • @RyiSnow
      @RyiSnow  Před 2 lety +14

      Glad it helped! Thank you for the comment.

    • @thenolife1957
      @thenolife1957 Před 2 lety +2

      Well most ppls don't play java games no more except for me

    • @faced2404
      @faced2404 Před 2 lety +1

      @@thenolife1957 minecraft is originally made in java

    • @thenolife1957
      @thenolife1957 Před 2 lety +1

      @@faced2404 except from that

    • @thenolife1957
      @thenolife1957 Před 2 lety

      Heheeee

  • @snarpking
    @snarpking Před 2 lety +20

    This series is so good. I've been interested in making actual games for a long time, but I've had a lot of struggle learnign unity, and I was about ready to give up, but now I've this AMAZING series to help me use the language I'm really familiar with to make real games.
    I've made some pretty cool console based games before, like this whole tower defense game which was displayed to the player exclusively in text, but games like that have obviously an incredibly niche appeal. That's why I'm so excited to really branch out like this.
    Please keep making these amazing videos.

    • @Ashokkumar31082
      @Ashokkumar31082 Před 2 lety +1

      wut this aint unity lol

    • @kira890
      @kira890 Před 10 měsíci +1

      @@Ashokkumar31082 I am a little late but he probably meant that he struggled with Unity, but now he found this tutorial to NOT use Unity to create his games, so that he doesn't struggle so much with making games. I hope I could enlighten you.

  • @okay1296
    @okay1296 Před 2 lety +16

    I now have ca. 3 or 4 years of programming experience in Java and Python. But sometimes I simply dont know how to implement somthing clean. Sure I could do it, but it will look like shit. You helped me a lot, how to implement all the stuff objectoriented. And a lot of times I think to abstract.
    Also I like your tutorials because you write all the stuff from scratch, only with the default java libarys. There are a lot of youtubers out there that would simply download Unity for 2D game development - im ok with that. But its hard to find good tutorials how to programm a 2D game engine from scratch. Also a lot of people is hating on Java, I cant understand that.
    Im intrested where you learned java.
    Keep it up, really good work and videos.

    • @RyiSnow
      @RyiSnow  Před 2 lety +13

      Yes, I also feel nothing against using engines such as Unity. If making a game is your sole purpose, using engine or library is a lot easier. That said developing it from scratch gives you deeper understanding on how game programs work and programming itself. Also, creating from scratch is fun! I'm glad you liked the videos. Always good to meet a fellow Java developer!

  • @marrokyoung4159
    @marrokyoung4159 Před 2 lety +11

    I came across your tutorials just a few days ago, but they are helping tremendously with making a space invaders esque game. Thank you so much Ive been learning a LOT

  • @lvdv4j
    @lvdv4j Před 2 lety +30

    If anyone is having issues where they are getting no errors but nothing is drawn be sure to add the getPlayerImage(); method to the player constructor in the Player class. (Just under setDefaultValues ();)

  • @cripz1436
    @cripz1436 Před 2 lety +13

    I'm just happy I got my sprite character to move, since started this tutorial 2 days ago its fun learning game dev again 😁 Thanks RyiSnow

    • @RyiSnow
      @RyiSnow  Před 2 lety +1

      Glad to hear that and welcome back to game dev :D

  • @5nsou
    @5nsou Před 2 lety +5

    Man. you are actually a legend. You deserve like more than 1 mil subscribers. Youre the greatest!

  • @gopnik7234
    @gopnik7234 Před 2 lety +6

    Great Tutorial! Ive been loving it so far! Please keep uploading!
    For anyone who uses another IDE than RyiSnow and can't fix the "java.lang.IllegalArgumentException: input == null!" error here are solutions for VS Code and Intellij that worked for me:
    For Intellij:
    1. Create the "res" folder as a new package in the src folder/section
    2. Create the player folder in res as player.res so that it is a package inside of the "res" package
    3. Add /res to the file path in the getPlayerImage-method so that it becomes "/res/player/boy....."
    4. enjoy
    For VS Studio:
    1. Create the "res" folder in the src folder/section
    2. Create the "player" folder in the "res" folder
    3. Add /res to the file path in the getPlayerImage-method so that it becomes "/res/player/boy....."
    4. enjoy
    The error most likely occurs because only the "src" folder is saved in the project source folder

    • @kalebwilliams4681
      @kalebwilliams4681 Před rokem

      THANKYOU SO MUCH

    • @gCoder_
      @gCoder_ Před 6 měsíci

      it happens to me in eclipse, i don't know why, could you please help me?

  • @crackrokmccaib
    @crackrokmccaib Před 2 lety +12

    You're so incredibly talented at what you do. I hope you have a job that appreciates your skill and pays you generously for what you can do. I hope to someday be even 1/4 the developer that you are.

    • @RyiSnow
      @RyiSnow  Před 2 lety +4

      I'm just a mediocre programmer. I'm sure you can catch up :)

  • @Alex-chesspenguin
    @Alex-chesspenguin Před 5 měsíci +3

    I’m an elementary student, this is very easy to understand, thanks!

  • @108mmbb
    @108mmbb Před 2 lety +7

    Thank you for this tutorial. This has been very easy to follow and learn from! I'm a beginner and didn't know a thing before I started this series.

    • @RyiSnow
      @RyiSnow  Před 2 lety +1

      Glad it was helpful!

  • @lamagiadiele8879
    @lamagiadiele8879 Před rokem +2

    Can i say it? YOU ARE A THE BEST TEACHER THAT I EVER HAD! Thank you very much, fantastic tutorial.

  • @Asdpire
    @Asdpire Před rokem +2

    If your thinking led to the same question as mine : "Wait a minute, we already tested the upPressed, downPressed etc. Why test it again just to update the sprite?"
    You can also just update the spritecounter in the if slot of the update method, like so :
    public void update(){
    if(keyH.upPressed) {
    direction = "up";
    y -= speed;
    spriteCounter++;}
    I just think it looks cleaner as a whole. Now it accomplishes the same thing with less nesting, see which you like better.
    I sincerely hope I'm not offending the author with my suggestions. Thank you for the instructive series!

  • @coldblade8795
    @coldblade8795 Před 11 měsíci +1

    Thank you sir, I have always understood concepts of java but never really understood when to use them. Using your tutorial i was able to get a basic hold of 2d programming I hope you become a more popular teacher cause your teaching techniques are excellent and as always Thank You!

  • @moonlightmacky9055
    @moonlightmacky9055 Před 2 lety +6

    Your tutorials are amazing man, keep it up!!!

  • @misterdneh
    @misterdneh Před 2 lety +2

    Your art skills are great, spent a few hours last night messing around with converting your guide to Android so far I have an un animated sprite that can move around the screen smoothly. I'm going to see what I can learn tonight, I feel so new lol.

    • @misterdneh
      @misterdneh Před 2 lety

      Also I enjoy that you explain things easily, plus everything makes sense from someone that does have java experience! With your permission I may do guides on this while I'm following along to convert to Android 😁

  • @benn125m
    @benn125m Před 2 lety +1

    Hello there. I just started learning a few days ago the beautiful language "java"..I am very glad I found your channel, a few video and already helped me understand more than just reading 📚.
    You deserve more views for sure.
    Keep up the good work. Wishing you a lot of success 🙌🏼

    • @RyiSnow
      @RyiSnow  Před 2 lety +2

      Thank you for your kind words!

  • @Talight-db9hj
    @Talight-db9hj Před 2 měsíci

    Бро, ты лучший❤
    Ты единственный на кого я подписался не из вежливости, а из уважения к твоему труду и твоим знаниям!

  • @kanhchanaly6445
    @kanhchanaly6445 Před 2 lety +2

    Thank you so much for making time to share this tutorial with us! I’m really grateful!

    • @RyiSnow
      @RyiSnow  Před 2 lety

      No problem :) I'm glad you liked it!

  • @daviddashen4070
    @daviddashen4070 Před 2 lety +36

    I have the solution for those of you, who are struggling with the error pinned in the comments and using VS Code (but you can give it a try in your IDE). I put the pictures in a single folder called player and then I moved the folder in the src folder. And then wrote this: File f1 = new File("./src/player/ninja_up_1.png");
    File f2 = new File("./src/player/ninja_up_2.png");
    File f3 = new File("./src/player/ninja_down_1.png");
    File f4 = new File("./src/player/ninja_down_2.png");
    File f5 = new File("./src/player/ninja_left_1.png");
    File f6 = new File("./src/player/ninja_left_2.png");
    File f7 = new File("./src/player/ninja_right_1.png");
    File f8 = new File("./src/player/ninja_right_2.png");
    up1 = ImageIO.read(f1);
    up2 = ImageIO.read(f2);
    down1 = ImageIO.read(f3);
    down2 = ImageIO.read(f4);
    left1 = ImageIO.read(f5);
    left2 = ImageIO.read(f6);
    right1 = ImageIO.read(f7);
    right2 = ImageIO.read(f8);

    • @AverageLowElo69
      @AverageLowElo69 Před 2 lety

      I swear you are god in disguise. Thank you so much! I was going to give up since I didn’t know what to do.

    • @pabloa1312
      @pabloa1312 Před rokem

      Thank you so much! omg I literally spent HOURS looking for a solution the other way. LIFE SAVER!

    • @matheusguedes8432
      @matheusguedes8432 Před rokem

      it worked very well in intellij, thank you very much!

    • @killian9843
      @killian9843 Před rokem

      I use VSCode and this worked hella good. thank u very much

    • @brendacchebet
      @brendacchebet Před rokem

      I've been trying to fix this error for like days. Thankkssss

  • @flow2035
    @flow2035 Před 2 měsíci

    Thank you mate! This is the best series in CZcams...keep going!!

  • @001archimedes
    @001archimedes Před 2 lety

    I'm following along your tutorial, and I can say I'm having a lot of fun creating this game. Thank you for putting this together and for posting the resources needed to create the game. Your explanations are very easy to follow and does not take too long.

  • @alitahboub493
    @alitahboub493 Před rokem +1

    Your explanations are very clear and straight forward! Great video! (I subscribed).

  • @ghostek7792
    @ghostek7792 Před 2 lety +1

    I love that you did this. you're really great at teaching it srsly dude don't underestimate your talent. I greatly appreciate your videos, I'm doing this as a summer project and I make edm so I'm going to be making full tracks for different settings and looking forward to drawing little characters

  • @jarrodmcevoy
    @jarrodmcevoy Před 2 lety +2

    Can't wait for the background tiles :)

  • @K-GamingSkyblock
    @K-GamingSkyblock Před rokem +3

    For any of those who is only getting one sprite character, make sure to add break at the end of each case in the switch statement

  • @themasterrogerdelgado
    @themasterrogerdelgado Před 8 měsíci

    Enjoying this series a lot. Thank you.

  • @rokasvidziunas4396
    @rokasvidziunas4396 Před rokem

    thank you so much for the amazing help

  • @mamapuci
    @mamapuci Před 2 lety +1

    Im here bro. Ehehe. Really, lot learning from you. Thanks again bro

  • @ncthatsme
    @ncthatsme Před rokem +1

    Thank you so much. This has been really fun and very informative.

  • @teodorairina6729
    @teodorairina6729 Před 2 lety +1

    You are doing a great job ! I will tell my friends about you !!!

  • @hizokadarkwolf
    @hizokadarkwolf Před rokem +4

    Incredible. What about storing the sprites inside an array?
    And better, having one single sprite sheet file and changing the position? For performance/memory. I think I will add these if it's not done on the videos to my version on my repository.
    Thanks a lot. Very fun tutorial.

  • @SlothfulSage285
    @SlothfulSage285 Před 2 lety +2

    I was trying to limit myself to one video a night, but I could not help it and decided to do another! :)

  • @playLaF
    @playLaF Před 5 dny +1

    Hi @RyiSnow
    I have a question, is it possible for the sprites to move diagonally? Thanks!

  • @RedlikMusic
    @RedlikMusic Před 2 lety +6

    you can also use arrays to storage the images and then iterate through the values c:

    • @Team6OWG
      @Team6OWG Před rokem +1

      How????? I am confused becasue we store BufferedImage objects, so we can create one 1 arrayList called images, that has all of the, but are we still creating up1, up2, down1, down2? or dont and just do image.add(ImageIO.read(getClass....etc ? But how do you get the string to change for "/player/boy_left1... or /player/boy_right2"????????
      Can you show what you did?

    • @r4c_h3ll
      @r4c_h3ll Před rokem

      @@Team6OWG @REDLIK share dude!! ((:

  • @mikemazanetz4183
    @mikemazanetz4183 Před 6 měsíci

    thank you very much for these wonderful video tutorials. An excellent resource !

  • @BigManBand22
    @BigManBand22 Před 2 lety +2

    You are a great teacher man! Arigato!

  • @brinklebros7136
    @brinklebros7136 Před 6 měsíci

    I LOVE YOUR VIDEOS!. I made a few tweaks to this code, there I made use of arrays for my animation frames since i had 4 frames per walk cycle. I also made it so I could tweak the timing per frame more easily, and a few other things. its functionally the same but i love this engine your making and im learning a ton.

  • @NLogSpace
    @NLogSpace Před rokem +1

    Nice video, I really enjoy your content! I also like to develop 2D tile-based games in Java, this series seems to be a good resource for me!
    Do you also use the entity component system (ECS) pattern for any of your games? I recently found out about it and I think it is a great way to structure code for this kind of games.

  • @silviuzlota6458
    @silviuzlota6458 Před 2 lety +1

    Cool video , I learned a lot from you.Thanks

  • @kylellsuperman
    @kylellsuperman Před 4 měsíci

    I'm really enjoying this series so far. it's getting me back on my feet with a lot of my coding knowledge. i just noticed that you're putting a lot of your boolleans into your if statements with a equivalency check when boolleans are automatically true or false values. I just wanted to point out you could write it as "if(keyH.upPressed)" instead of "if(keyH.upPressed == true)" to save some space. I hope this can help someone out there. (Again, this only works for boollean variables.)

  • @IseOnCrack
    @IseOnCrack Před 2 lety +5

    Wow, that's a really good and in depth tutorial, good job! I'm following the tutorial but im trying to do some things in "my way", for exemple, why dont you use entity as an abstract class?
    Instead of the 4 if you used for sprite number, my spriteNum grows every 10 spriteCounter (like yours), but for select the image in the switch i use if(spriteNum%2==0) , i find it a bit more elegant.
    Pls dont use keyHandler==true XD.
    Good job again!

  • @saschapolaschek2715
    @saschapolaschek2715 Před 2 lety +1

    Nice video man!

  • @anicepineapple9067
    @anicepineapple9067 Před rokem

    For anyone wondering how to resolve the error in VS code. Just right click on the res folder and select "add folder to java source path." this worked for me.

  • @LouShunt
    @LouShunt Před 2 lety +5

    Hey I love your videos. But i got a question about the animation. The update method is based on the number of FPS. Which means that with the system you made, a person with 30 fps on the game will have 2x slower animation and a person with 120 fps 2x faster. Is there a way to have the same animation speed regardless of fps?

    • @RyiSnow
      @RyiSnow  Před 2 lety +6

      Yes, I think you can set the speed based on fps.
      Example:
      int speed = 240/FPS;
      This returns 4 if fps is 60 and returns 8 if fps is 30 so the player speed stays the same.
      That said if you set fps 241 or more, the speed becomes 0 (since int doesn't accept decimal) so player won't move at all in that case. So probably you'll need to change it to double and arrange the code a little bit. Like counting decimal behind the scene and when it hits 1, apply the movement or something.

    • @LouShunt
      @LouShunt Před 2 lety

      @@RyiSnow Great idea! Thank you I will try it

    • @RyiSnow
      @RyiSnow  Před 2 lety +3

      Hi, I just realized that you were asking about the animation, not the player's movement speed. Maybe you've already figured it out but you can also set the sprite counter based on fps so the interval between sprite 1 and 2 stays the same.

  • @mistiical3033
    @mistiical3033 Před 2 lety +3

    Hello, in class Player when you import Gamepanel and Keyhandler you get option to do main.Gamepanl etc i do not get these options when i type it in myself I get an error saying the import main cannot be resolved. any solutions, Thanks.

  • @alex_tremo255
    @alex_tremo255 Před 4 měsíci

    3k like. Thanks for your tutorials man

  • @NikitaBomba112
    @NikitaBomba112 Před rokem +1

    Thanks you so much for this video!

  • @thehandleisuseless
    @thehandleisuseless Před rokem +2

    crazy to think that Minecraft was made like this, wow

    • @juandebenedetti2884
      @juandebenedetti2884 Před rokem

      What do you mean that MC was made like that? Minecraft was made with LWJGL to be able to draw using OpenGL, this being a hardware representation. This tutorial uses Swing, and Swing uses software rendering. The difference between the two is that hardware rendering has countless benefits. But I'm not saying that learning Swing to make video games is bad, don't get me wrong. RyiSnow teaches us the basics of 2D game development, which is essential if you want to get into something deeper like OpenGL.

    • @apro8723
      @apro8723 Před 8 měsíci

      bro u mean the og legend of zelda bro.... dont get ur games mixed up.

  • @weptashka
    @weptashka Před 2 lety

    You make awesome tutorials! Thank you!

  • @LEALCUTSS
    @LEALCUTSS Před rokem +1

    Thanks, very informative video!

  • @Sviskebisk
    @Sviskebisk Před rokem +2

    Thank you Ryi

    • @RyiSnow
      @RyiSnow  Před rokem

      Thank you for your support!!! I hope you'll enjoy the series :)

  • @shiroganieee1150
    @shiroganieee1150 Před 11 měsíci +1

    how can you add another source folder? There's no option for it on my Netbeans and it only adds another folder or package inside the "source package" folder.

  • @S1mplector
    @S1mplector Před 23 dny

    Hey, RyiSnow! I have been following your tutorial and this has been amazing so far. I am excited to go through all of the videos on this series, thank you so much for the effort! I wanted to add something to the character walking animation. I updated the Player class slightly so it switches between walking sprites when a movement key is held and displays a static sprite when the key is released, by using a boolean.
    package entity;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import main.GamePanel;
    import main.KeyHandler;
    public class Player extends Entity {
    GamePanel gp;
    KeyHandler keyH;
    public Player(GamePanel gp, KeyHandler keyH) {
    this.gp = gp;
    this.keyH = keyH;
    setDefaultValues();
    getPlayerImage();
    }
    public void setDefaultValues() {
    x = 100;
    y = 100;
    speed = 4;
    direction = "down";
    }
    public void getPlayerImage() {
    try {
    // static images
    up1 = ImageIO.read(getClass().getResourceAsStream("/player/up.png"));
    down1 = ImageIO.read(getClass().getResourceAsStream("/player/down.png"));
    left1 = ImageIO.read(getClass().getResourceAsStream("/player/left.png"));
    right1 = ImageIO.read(getClass().getResourceAsStream("/player/right.png"));
    // animations
    up2 = ImageIO.read(getClass().getResourceAsStream("/player/up2.png"));
    up3 = ImageIO.read(getClass().getResourceAsStream("/player/up3.png"));
    down2 = ImageIO.read(getClass().getResourceAsStream("/player/down2.png"));
    down3 = ImageIO.read(getClass().getResourceAsStream("/player/down3.png"));
    left2 = ImageIO.read(getClass().getResourceAsStream("/player/left2.png"));
    left3 = ImageIO.read(getClass().getResourceAsStream("/player/left3.png"));
    right2 = ImageIO.read(getClass().getResourceAsStream("/player/right2.png"));
    right3 = ImageIO.read(getClass().getResourceAsStream("/player/right3.png"));
    } catch(IOException e) {
    e.printStackTrace();
    }
    }
    public void update() {
    boolean moving = false;
    if (keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) {
    moving = true;
    if (keyH.upPressed) {
    direction = "up";
    y -= speed;
    } else if (keyH.downPressed) {
    direction = "down";
    y += speed;
    } else if (keyH.leftPressed) {
    direction = "left";
    x -= speed;
    } else if (keyH.rightPressed) {
    direction = "right";
    x += speed;
    }
    spriteCounter++;
    if (spriteCounter > 12) {
    if (spriteNum == 1) {
    spriteNum = 2;
    } else if (spriteNum == 2) {
    spriteNum = 1;
    }
    spriteCounter = 0;
    }
    }
    if (!moving) {
    spriteNum = 1; // Reset to the static sprite when not moving
    }
    }
    public void draw(Graphics2D g2) {
    BufferedImage image = null;
    switch (direction) {
    case "up":
    if (keyH.upPressed) {
    image = (spriteNum == 1) ? up2 : up3;
    } else {
    image = up1;
    }
    break;
    case "down":
    if (keyH.downPressed) {
    image = (spriteNum == 1) ? down2 : down3;
    } else {
    image = down1;
    }
    break;
    case "left":
    if (keyH.leftPressed) {
    image = (spriteNum == 1) ? left2 : left3;
    } else {
    image = left1;
    }
    break;
    case "right":
    if (keyH.rightPressed) {
    image = (spriteNum == 1) ? right2 : right3;
    } else {
    image = right1;
    }
    break;
    }
    g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null);
    }
    }
    I have already learned so much, thank you so much for it again!

  • @ochkoochkeevich9123
    @ochkoochkeevich9123 Před rokem

    Hello from Russia!!! You deserve more viewers)

  • @dovydasbalciunas5002
    @dovydasbalciunas5002 Před 10 měsíci

    Hey man! I'm trying to master java right now and I got a question. Were you just like messing around or is creating games your passion? Are you a game developer?

  • @smartestpotato2127
    @smartestpotato2127 Před rokem +2

    I’ve been following your tutorials to the t so far, but when after shifting the key input stuff to the player class and after clicking run, the square doesn’t move at all, do you have any clue what the problem could be? no errors are popping up, it’s just not getting my key inputs for some reason
    edit - never mind, I forgot to put the player update inside the game panel update

  • @pauldhartley
    @pauldhartley Před 21 hodinou

    If you are going to use the keyPress data in the braw method, why have a direction variable at all!
    I did enjoy your video very much and is helping me to understand sprites etc. Thank you

  • @ivangamer2888
    @ivangamer2888 Před 6 měsíci +1

    I'm making a game as a final project (the game is Tron the one of the movie, the motorcycles that leave a wall of light behind them), the thing is that I don't know how to make that wall and I've been searching a lot and I can't find how to do it. I don't know if you or someone from the community could help me, thank you very much in advance.

  • @kyletran6160
    @kyletran6160 Před rokem

    bro's good at drawing, i cant

  • @neon-mask5171
    @neon-mask5171 Před rokem +2

    i dont know what is happening my player sprite is not showing its showing the white rectangle only even though i have cut out the code for it. please help.

  • @doxuantruong04
    @doxuantruong04 Před 2 měsíci +1

    i use netbeans IDE and i don't play program, can use help me?

  • @suiz137
    @suiz137 Před rokem +1

    5:14 can tell me why my line 'Player player = new Player(this.keyH);' is error constructor undefined ?

    • @solareclipsedudefinale9026
      @solareclipsedudefinale9026 Před rokem

      Instead of "this.keyH", do "this, keyH" because we want to pass 2 arguments to the constructor

  • @bluestreak711
    @bluestreak711 Před rokem +2

    God is good and I am getting a little better in troubleshooting. the mistake I made that should be avoided on this video was that I did not include the line of code, "import java.awt.image.BufferedImage; " right after the " package entity;" in the Entity class.
    To all my peers, I would encourage you to make sure all your imports are added. This will solve a lot of your problems.
    To our teacher @RyiSnow, for future tutorials, please call more attention to when you import something or click to import something. Sometimes you click so fast and if I blink, I miss where you click to have Eclipse automatically import something. Sometimes, I do not ever see it, but I know it is there. So far, I have been able to use Eclipse to track it down.
    Other Input: MSpaint and Gimp both are good alternatives to make Pixel art. The only problem with MSpaint is that it is difficult to make it 16x16px, but if you make your draw area close to square and draw your images, you can use Gimp to both resize your image to 16x16px and a few simple clicks will make the white background transparent.
    You should be able to do everything inside of Gimp, but I am not yet well versed with Gimp as I am MsPaint as of yet. Gimp is almost as powerful as Photoshop and is completely free and opensource, so it is worth learning.
    Fun Thought: I have considered taking photos of myself, and other people, in all of the same poses as your little boy sprite and inserting that into the game. Yes, I could probably do animated pixel art also, but I am talking about just unedited pictures except for the 16x16px resizing and making the background transparent.

    • @megha9307
      @megha9307 Před rokem

      Hey man I keep getting this error and I think it has something to do with imports you think you can take a look
      ----------------------------------
      package entity;
      import java.awt.Color;
      import java.awt.Graphics2D;
      import java.awt.image.BufferedImage;
      import java.io.IOException;
      import javax.imageio.ImageIO;
      import main.GamePanel;
      import main.KeyHandler;
      public class Player extends Entity{

      GamePanel gp;
      KeyHandler keyH;

      public Player(GamePanel gp, KeyHandler keyH) {

      this.gp = gp;
      this.keyH = keyH;

      setDefaultValues();
      getPlayerImage();
      }

      public void setDefaultValues() {
      x = 100;
      y = 100;
      speed = 4;
      direction = "down";
      }

      public void getPlayerImage() {
      try {

      down1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_1.png"));
      down2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_down_2.png"));
      up1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_1.png"));
      up2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_up_2.png"));
      right1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_1.png"));
      right2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_right_2.png"));
      left1 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_1.png"));
      left2 = ImageIO.read(getClass().getResourceAsStream("/player/boy_left_2.png"));


      }catch (IOException e) {

      e.printStackTrace();
      }
      }


      public void update() {
      if (keyH.upPressed == true) {
      direction = "up";
      y -= speed;

      }
      else if(keyH.downPressed == true) {
      direction = "down";
      y += speed;

      }
      else if(keyH.leftPressed == true) {
      direction = "left";
      x -= speed;
      }
      else if(keyH.rightPressed == true) {
      direction = "right";
      x += speed;
      }
      }

      public void draw(Graphics2D g2) {

      BufferedImage image = null;
      switch(direction) {
      case"up":
      image = up1;
      break;
      case"down":
      image = down1;
      break;
      case"right":
      image = right1;
      break;
      case"left":
      image = left1;
      break;

      }
      g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null);

      }

      }

    • @megha9307
      @megha9307 Před rokem

      The error that im getting is The type java.io.IOException cannot be resolved. It is indirectly referenced from required type java.awt.Font

    • @bluestreak711
      @bluestreak711 Před rokem

      ​@@megha9307 I am going to take a wild guess at what your problem is, but without seeing all of your code and not just your player class, it is hard to tell, but I am going to give you a few things to look for and there is a good chance you will find it on your own and be a better programmer for it.
      First, see if it helps to add
      import javax.imageio.ImageIO;
      to your entity class for reasons I explain below.
      If that doesn't work and if you are using eclipse like Ryisnow and me, read the error line and see exactly what it reads. It will give you a lot of clues.
      Remember that your player class extends the entity class. You pasted your code for the Player class. If you happen to forget an import in your Entity class, it can cause an error on your Player class and cause it not to work.
      Be watchful of each time Ryi uses a variable or attribute from another class. You might be working in the Player class and think you should import something there when you should import something to the Entity class because that is where that item in question originates. The same goes for making variables public when you call them from a different class than where they were declared. In a video coming up there is an instance where Ryi forgets to make a certain variable public and has to go back and make it public, so the program will work.
      I do not remember if this applies to both folders and packages, but this definitely applies to packages. Each time Ryi has you create a new package or a new folder, you have to import your main class or some other class to that new class you just created or vice versa, so it is not isolated and can talk to the rest of your program. Eclipse will often do this with a couple of clicks and Ryisnow will click so fast to generate that import automatically that if you blink, I assure you will miss it.
      I have also had a few instances where Eclipse offered to generate that import line of code for Ryisnow, but it wouldn't do it for me, so I had to read the error details very carefully and figure out which import line of code I needed to add and add it manually.
      Good luck and comment back when you figure it out because these comments help other people with their code also!

    • @bluestreak711
      @bluestreak711 Před rokem

      @@megha9307 Libbania Reyes had some good suggestions of a few things to check in a comment below also.

  • @CanePazzoHD
    @CanePazzoHD Před 2 lety +1

    Dear Ryisnow, thank you for the immense work you are doing. I have recently started to follow your tutorials step by step. I’m having a great time and you’re very clear in your explanation.
    I have a problem I can’t solve, can you help me?
    When I press start, the character moves in the direction I have indicated. At some point, however, it freezes and does not move anymore despite me pressing the command on the keyboard. Shortly after it starts moving again and so on it stops again.
    The FPS are fixed at 60, it seems to me that everything I marked on the KeyHandler is correct... what can it be? Thank you very much for your availability!

  • @davidbijaksantoso-085
    @davidbijaksantoso-085 Před rokem +1

    is there a way to make another image animation (I want to add an idle animation when not pressing any keys)?

  • @henrikcederlof8444
    @henrikcederlof8444 Před rokem +1

    Took me a day only to find out i missed .png that solved all the problems i faced xD

  • @yajie7756
    @yajie7756 Před 2 lety

    You are just so amazing !

  • @selimsmajlovic7871
    @selimsmajlovic7871 Před rokem +1

    I require help. When I try to import "main.GamePanel" and "main.KeyHandler" nothing shows up. When I write the imports manualy, it is still an error, like it does not recognise the imports or something. Does anyone have an answer to this?

    • @idkwhoami9986
      @idkwhoami9986 Před rokem +1

      not sure if you still need help but.. i had a similar problem, turns out it was because my GamePanel and KeyHandler files were not in a "main" folder. when I added them to a folder called "main", it worked as it did in the video

  • @Zuko0847
    @Zuko0847 Před rokem +2

    1:39
    for some reason it doesn't show me the import option, any idea why?
    btw thanks a lot for the video, you're doing an amazing job!

  • @flow2035
    @flow2035 Před 2 měsíci

    Awesome!!!

  • @luandkg
    @luandkg Před 2 lety +1

    Amazing 👏🏻👏🏻👏🏻👏🏻👏🏻

  • @lordkuuba3517
    @lordkuuba3517 Před 2 lety +2

    Hi! I have some problem with animation how to add more frames (4) ?

    • @maximhanzely1988
      @maximhanzely1988 Před 2 lety

      18:57 u can try adding something like this :
      else if (spriteNum == 2) {
      spriteNum = 3;
      }
      else if (spriteNum == 3) {
      spriteNum = 4;
      }
      before he adds this:
      else if (spriteNum == 8) {
      spriteNum = 1;
      }
      it worked for me idk if its good but hey it works

  • @kenanahmed
    @kenanahmed Před rokem +1

    subscribed immediately

  • @Lazullien
    @Lazullien Před 2 lety +1

    this might be a little late but if i switch to bufferedimage, not even drawing the images, just assigning the pngs, my key binding seems to break, whenever i stop calling the method that assigns the pngs it goes back to normal, if i add any other functions with the images on they work just fine, but the key binding breaks, i'm not sure how keybinding focus works but it still seems quite awful

    • @Lazullien
      @Lazullien Před 2 lety +1

      nevermind i'm leaving this comment here for people with the same problem, key binding's default focus is just as crappy as keylistener's, put JComponent.WHEN_IN_FOCUSED_WINDOW inside the getInputMap()'s parameter to fix this

  • @jamesschraffl6820
    @jamesschraffl6820 Před 2 lety +1

    Thank you so much ryi

    • @RyiSnow
      @RyiSnow  Před 2 lety

      Not at all. It's been a while since I uploaded those 2D demo videos and you commented on them. They say being able to understand it and being able to explain it to others are different thing and that was so true haha. It took longer than I had expected and I appreciate your patience. I hope you're enjoying this series!

  • @tito-kz1fm
    @tito-kz1fm Před 2 lety +1

    Hey, the character only appearing when going D key, WAS keys won't show character? can you help me with that?

    • @JCIgaming
      @JCIgaming Před 2 lety

      In case you haven't figured it out yet, you are probably missing some breaks in switch statement. It's such a stupid mistake but it slipped by me and hopefully its the same solution for you.

  • @noahbarger1
    @noahbarger1 Před 2 lety +7

    If the sprite you made has more than 2 images, and when it stops animating you want it to go back to the first image, then after the "if" statement, put:
    if (keyH.upPressed || keyH...
    else
    {
    spriteNum = 1;
    }

  • @RamdomOfInternet
    @RamdomOfInternet Před 9 měsíci

    I change language because English is more comfortable for me.
    Day1: I got to this part of the video today and the truth is it took me a bit... At least I'm understanding it.

  • @zitrix7848
    @zitrix7848 Před 2 lety +1

    *my sprites are not drawing to the screen, i used the same sprites from video description everything loads in without any error but the sprites won't load to screen please help*

    • @lvdv4j
      @lvdv4j Před 2 lety +1

      Did you add the getPlayerImage(); method to the player constructor in the Player class? I had the same problem and this was my solution

  • @krishmangat3224
    @krishmangat3224 Před 4 měsíci

    I am having trouble with the movement(I have followed all the directions properly but I think that the movement logic is wrong in the tutorial)
    -When I press right then while right is still pressed, I press up or down, the sprite will move up or down and when I let go of up or down (remember right is still pressed), the sprite will continue to go right.
    - The problem is that this does not work for when I am going up and then I want to go left or right.
    - There is something wrong with the if statements, we check for up before down,left and right so anything below in the if statement is overruled by everything above.
    Is is there any way I can fix this bug?

  • @SJAMS_
    @SJAMS_ Před 2 lety +1

    I love these tutorials/walkthroughs so much! I just have one question though when using Netbeans I can't get the walking sprites to load and I am not getting any errors in my code. The normal standing in place sprites show but when I do the same code as shown in the video only the eight sprite will load in and everything else is a blank and it isn't even "walking"? I have no clue how to fix this issue. Any other Netbean users running into the same thing? Thank you!

    • @Ramej
      @Ramej Před rokem

      solution: delete netbeans 👍😁 (like i did) cuz theres no source folder in it and switched to eclipse

  • @cedricsahaghian1605
    @cedricsahaghian1605 Před rokem

    Thank you RyiSnow!!

  • @pietrosica5265
    @pietrosica5265 Před 2 lety +2

    I have this problem : i can move the rectangle so i guess in the key listener class and update method there s no error but when i import the images (i just copied like in the video) and run the program i can see my player in the defaultValues position and direction so the sprites are loaded but i can’t move it
    (i tried to comment the getPlayerImage(); and the g2.drawImage(x,y,gp.tileSize,gp.tileSize,null) and i wrote again g2.fillRect(x,y,gp.tileSize, ,gp.tileSize) , so i can move again the rectangle , but with te image loaded i can’t move the player and the program runs with no error

    • @natesannatesan2220
      @natesannatesan2220 Před rokem

      For me too

    • @samirunlu9900
      @samirunlu9900 Před rokem

      @@natesannatesan2220 I used a different key instead (VK_W,A,D,X OR SOMETHING 😊) , the arrow up, down, left, right (VK_UP,DOWN,LEFT,RIGHT) and it worked 😊.
      had the same problem hopefully it helped.

    • @miguelmalini2635
      @miguelmalini2635 Před rokem

      same

  • @jamford3054
    @jamford3054 Před 2 lety

    Thank youuu for the tutorial i really want to try making a game

  • @brayanavilacaravaca4964

    i have a problem, the class KeyHander no works for some reason.. And I did it the same of the video... Any sugestions?

  • @TamLe-yh3eg
    @TamLe-yh3eg Před 8 měsíci

    for anyone want the character to move diagonally, just replace all else if conditions in update method to all if conditions

  • @Gytimaru
    @Gytimaru Před rokem

    I was listening while coding and didn't realize that when you said "player *dot* boy_up_1.png" you meant "player/boy_up_1.png"... I was so confused when nothing worked in eclipse, even changing the folder multiple times, I had to rewatch a third time to realize it!

  • @Omoqim
    @Omoqim Před rokem

    Hello~ I know this vid's kinda old, but I want to ask something. I was following exactly step-after-steps and everything worked perfectly. But right after the 14:50 step, it says "Error: Could not find or load main class main.Main
    Caused by: java.lang.ClassNotFoundException: main.Main". I don't understand what's wrong because I didn't change anything related to the main package nor the Main class... I hope you'll read this and reply. Thank you for these tutorials and great work btw!! Your vids really easy to understand~ Keep up the great work~!

  • @Noogai
    @Noogai Před 10 měsíci

    Hi! So I've been following your videos almost line by line but when I had to instantiate the Player class in the GamePanel class, it gives me an error that says "The constructor Player(KeyHandler) is undefined". I manually imported "entity.Player" because my quick fix suggestion didn't have one and I've double checked the codes and I think everything is the same with your code.
    (edit: okay I'm sorry, I didn't check hard enough, I saw it immediately after checking once again. Turns out that what you put was (this,keyH) using a comma, and not (this.keyH) using a period. Just changed it and it's all good now. Also thanks for the tutorials! Great vids and very helpful explanations)

  • @contrasena5243
    @contrasena5243 Před 2 lety +1

    I accidentally made my character sprites 32x32 pixels and its smaller when I draw it. Shouldnt it be bigger since it has more pixels? Also I have tried changing the "oiriginalTileSize" variable from 16 to 32 but it doesnt work very good.. I think i'll just start over and make a 16x16 character

    • @RyiSnow
      @RyiSnow  Před 2 lety +1

      Your character was rendered as 16x16 because we used the tileSize as their width and height when we draw images.
      (this line)
      g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null);
      If you change the width and the height, the sprites will be rendered with the specified size.

  • @JZSNooB
    @JZSNooB Před 2 lety +1

    Ah, so I see you are using 8 different images for the player sprite. Would you ever optimize the animation system to support reflections, such that you only use 4 unique sprites? Like "Boy_down_2" is just "Boy_down_1" but flipped on the y-axis, and left_1 and left_2 are just right_1 and right_2, but reflected over y, and so on.

    • @RyiSnow
      @RyiSnow  Před 2 lety +3

      That sounds like an interesting idea (never thought about it) but probably won't fit this game because not every character is symmetrical. For example, if a character is holding a sword on their right hand, flipping the sprite will produce a strange result (the character will be holding the sword with his left hand). So I think I will stick to the current system for the time being. Thank you for the suggestion though :)

    • @JZSNooB
      @JZSNooB Před 2 lety +2

      @@RyiSnow Understandable. In that sword senario, that definitely wouldn't work, but I just figured that if you're going for a retro style graphics, that working within some artistical limitations like a limited color palette (or in this case a limited number of keyframes) might spark some creativity or just be a fun extra challenge. Ofcourse though it is your project, and I'll watch the series regardless!

  • @luklukfamous175
    @luklukfamous175 Před 6 měsíci

    i have question
    do you recomend some kind of books?

  • @guilhermerezende7654
    @guilhermerezende7654 Před 2 lety +2

    could you do a video teaching how to do character jump? it would really be cool

    • @apro8723
      @apro8723 Před 8 měsíci

      idk why u would u need that... its a 2D game

  • @badkinson5459
    @badkinson5459 Před 2 měsíci +1

    at 14:50 my sprite has a white background how do you get yours to have a black background?

    • @badkinson5459
      @badkinson5459 Před 2 měsíci +1

      I see now i just didn't commit out the two lines that's why my character had a white background in the square

    • @ganesh9952
      @ganesh9952 Před 19 hodinami

      Bro I did comment out the two lines but it's still Showing the white box 😭 any fix for this ?

  • @sugarycashew3035
    @sugarycashew3035 Před 10 měsíci

    I get to the 7:10 test and the rectangle no longer moves :( I left the old if loop location updater in the GamePanel class and that box still moves. But the new box will not budge. Advice? I've got the new loop in the Player class, as well as updated all the x's and y's.

  • @wen2790
    @wen2790 Před 7 měsíci

    Hello, I have an error at 2:23 when you type -Player player = new Player(this.KeyH){}, here Player is at the beginning and Player after new I have an error and it says like this - Player cannot be resolved to a type , help me how can I fix it?