How to create Snake as a Java Applet

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ต.ค. 2024
  • For additional java game programming projects with sample code provided, go to: www.krohneducat...
    Explanation of how to create the game "Snake" as a Java Applet.

ความคิดเห็น • 97

  • @PunchingStuff
    @PunchingStuff 8 ปีที่แล้ว +5

    I cant thank you enough I learned so much from this tutorial and everything was explained so well great job :)

  • @Qornv
    @Qornv 5 ปีที่แล้ว

    Picture by picture explanation of run function is fantastic

  • @johnshoemaker4989
    @johnshoemaker4989 7 ปีที่แล้ว

    I understand all the syntax and pretty much everything except the try - catch portion. I haven't really studied threads or exceptions so that's my fault. Good video!

  • @Prepaidgeecko
    @Prepaidgeecko 7 ปีที่แล้ว

    Thanks for the great videos. They've definitely helped me take the next step in programming.

  • @NSGenius
    @NSGenius 8 ปีที่แล้ว +11

    *Stops recording* "MOM TURN OFF THE HAIR DRYER!!!" *Starts recording again* 13:22

    • @krohn-education3906
      @krohn-education3906  8 ปีที่แล้ว +14

      lol. Was my fiance, but close.

    • @NSGenius
      @NSGenius 8 ปีที่แล้ว

      +Krohn - Education 👍🏽 Good tutorial btw.

    • @kyulangk
      @kyulangk 7 ปีที่แล้ว +1

      Krohn - Education what is your keyboard's brand i liked its sound :D

  • @marclefer1619
    @marclefer1619 8 ปีที่แล้ว +1

    Hey, just saying, you can use ctrl + shift + o to import instead of clicking it and yeah..
    Great vid btw!

    • @krohn-education3906
      @krohn-education3906  8 ปีที่แล้ว +4

      +MarcLefer Thanks for sharing that shortcut. Definitely a good time saver.

    • @marclefer1619
      @marclefer1619 8 ปีที่แล้ว

      +Krohn - Education np :)

  • @niiiko_gn
    @niiiko_gn 7 ปีที่แล้ว +2

    what are the differences between JPanel and Applet, is it better to choose Applet for games?

  • @fahimshahriar5928
    @fahimshahriar5928 4 ปีที่แล้ว +1

    Where is the KeyReleased part? Is that needed or not?

  • @mayukhdas1617
    @mayukhdas1617 4 ปีที่แล้ว

    Awesome tutorial

  • @harleydelcastillo7426
    @harleydelcastillo7426 4 ปีที่แล้ว +1

    I don't have any option to run the SnakeGame file? I followed step by step up until the point you created the Snake class, but I don't want to move on cause I can't even run the Snakegame file, does it have something to do with Applet being deprecated?

    • @MisterWealth
      @MisterWealth 3 ปีที่แล้ว

      Did you ever figure out it out

  • @poisedstool5136
    @poisedstool5136 5 ปีที่แล้ว +2

    Great Vid! Can you atleast tell how to restart the game by pressing SPACE or ESC?

  • @ashtongaupp5876
    @ashtongaupp5876 6 ปีที่แล้ว +2

    It says I have an error when I run it. Can you please explain to me if you can. Basically my snake won't move at all and sends this error message:
    "Exception in thread "Thread-1" java.lang.NullPointerException
    at SnakeGame.run(SnakeGame.java:58)
    at java.lang.Thread.run(Thread.java:748)"
    Please help!

  • @suranjanaindra6872
    @suranjanaindra6872 6 ปีที่แล้ว +5

    when I am trying to import java.applet.Applet it is sowing error that the type applet is deprecated..How to resolve this??

    • @INVADE.00
      @INVADE.00 4 ปีที่แล้ว

      Applet is deprecated in java 9.

    • @INVADE.00
      @INVADE.00 3 ปีที่แล้ว

      @David Assio use JPanel

    • @vent5594
      @vent5594 2 ปีที่แล้ว +1

      Applet is deprecated since Java 9. Use Java 8 instead.

  • @ameyashetty1441
    @ameyashetty1441 4 ปีที่แล้ว

    i have a question do you control the snake when you run it or does it run on its own

  • @beeiskey9225
    @beeiskey9225 7 ปีที่แล้ว +1

    When typing
    public void draw(Graphics g){
    g.setColor(Color.white);
    for(Point p : snakePoints);
    g.fillRect(p.getX(), p.getY(), 4, 4);
    I get a error saying p needs its own variable..
    idk what to do p is underlined red /

  • @luckytribhakti3234
    @luckytribhakti3234 6 ปีที่แล้ว

    Can you help me? Why my snake after i run , it still get long? Not get normal when i run before done

  • @HackersVillaCybersecurity
    @HackersVillaCybersecurity 5 ปีที่แล้ว

    I am getting error at
    img=createImage(400,400);
    createImage can't be resolved to a type.. Please help me every other thing is error free for me..

  • @DuhBoiNick
    @DuhBoiNick 7 ปีที่แล้ว

    only one of the rectangles of the snake is being drawn, not sure why, but when i move it, it moves diagonally and then i can see the other square blocks

    • @DuhBoiNick
      @DuhBoiNick 7 ปีที่แล้ว

      nevermind, im just a goofball lmaoo

  • @berkannur7919
    @berkannur7919 6 ปีที่แล้ว

    Why would you add a repaint() function if you just can call this.paint() again?

  • @stefanjentoft8107
    @stefanjentoft8107 6 ปีที่แล้ว

    The line "List snakePoint;" keeps giving me an error. It keeps saying "The type List is not generic; it cannot be parameterized with arguments ." I have double and triple checked that my syntax is the same as yours. Can you please help?

    • @stefanjentoft8107
      @stefanjentoft8107 6 ปีที่แล้ว +1

      Er, never mind. Eclipse decided to automatically import java.awt.List instead of java.util.List

  • @annscherbyna6130
    @annscherbyna6130 5 ปีที่แล้ว +1

    please text the code ready, very necessary

  • @AgentDoubleA96
    @AgentDoubleA96 8 ปีที่แล้ว

    Mine keeps flickering between black and white. Why is this?

  • @marclefer1619
    @marclefer1619 8 ปีที่แล้ว +1

    Now I have another problem.. it moves perfectly, but when I run ver the token it works the first 1 or few times but then after I run over it and it doesn't work anymore :( please oeone help

    • @HauroKyo
      @HauroKyo 8 ปีที่แล้ว +1

      Same problem here

    • @michieb1231
      @michieb1231 8 ปีที่แล้ว

      maybe a problem in your game loop

  • @marclefer1619
    @marclefer1619 8 ปีที่แล้ว

    uhh... I did everything on the part where we were actually working towards MOVING the snake but when I run it it and i press up, the snake just shrinks :(
    My Code in Snake.java:
    package snake;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.ArrayList;
    import java.util.List;
    public class Snake {
    List snakePoints;
    int xDir, yDir;
    boolean isMoving, elongate;
    final int STARTSIZE = 20, STARTX = 150, STARTY = 150;

    public Snake(){
    snakePoints = new ArrayList();
    xDir = 0;
    yDir = 0;
    isMoving = false;
    elongate = false;
    snakePoints.add(new Point(STARTX, STARTY));
    for(int i = 1; i < STARTSIZE; i++){
    snakePoints.add(new Point(STARTX - i * 4, STARTY));
    }

    }
    public boolean isMoving(){
    return isMoving;
    }
    public void setIsMoving(boolean b){
    isMoving = b;
    }
    public void draw(Graphics g){
    g.setColor(Color.white);
    for(Point p : snakePoints){
    g.fillRect(p.getX(), p.getY(), 4, 4);
    }
    }
    public void move(){
    if(isMoving){
    Point temp = snakePoints .get(0);
    Point last = snakePoints.get(snakePoints.size() - 1);
    Point newStart = new Point(temp.getX() + xDir * 4, temp.getY() + yDir * 4);
    for(int i = snakePoints.size() - 1; i >= 1; i--){
    snakePoints.set(i, snakePoints.get(i - 1));
    }
    snakePoints.set(0, newStart);
    }

    }
    public int getXDir(){
    return xDir;
    }
    public int getYDir(){
    return yDir;
    }
    public void setXDir(){
    xDir = xDir;
    }
    public void setYDir(){
    yDir = yDir;
    }
    //X position of head of snake
    public int getX(){
    return snakePoints.get(0).getX();
    }
    //Y position of head of snake
    public int getY(){
    return snakePoints.get(0).getX();
    }

    }
    My code in SnakeGame.java:
    package snake;
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    public class SnakeGame extends Applet implements Runnable, KeyListener{
    Graphics gfx;
    Image img;
    Thread thread;
    Snake snake;
    public void init(){
    this.resize(400, 400);
    img = createImage(400, 400);
    gfx = img.getGraphics();
    this.addKeyListener(this);
    snake = new Snake();
    thread = new Thread(this);
    thread.start();
    }
    public void paint(Graphics g){
    gfx.setColor(Color.black);
    gfx.fillRect(0, 0, 400, 400);
    snake.draw(gfx);

    g.drawImage(img, 0, 0, null);
    }
    public void update(Graphics g){
    paint(g);
    }
    public void repaint(Graphics g){
    paint(g);
    }
    public void run() {
    for(;;){

    snake.move();
    this.repaint();
    try {
    Thread.sleep(40);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    public void keyPressed(KeyEvent e) {
    if(!snake.isMoving()){
    if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_RIGHT ||
    e.getKeyCode() == KeyEvent.VK_DOWN){
    snake.setIsMoving(true);
    }
    }
    if(e.getKeyCode() == KeyEvent.VK_UP){
    if(snake.getYDir() != 1){
    snake.setYDir();
    snake.setXDir();
    }
    }
    if(e.getKeyCode() == KeyEvent.VK_DOWN){
    if(snake.getYDir() != -1){
    snake.setYDir();
    snake.setXDir();
    }
    }
    if(e.getKeyCode() == KeyEvent.VK_LEFT){
    if(snake.getXDir() != 1){
    snake.setXDir();
    snake.setYDir();
    }
    }
    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    if(snake.getXDir() != -1){
    snake.setXDir();
    snake.setYDir();
    }
    }
    }
    public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub
    }
    public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub
    }
    }

    • @marclefer1619
      @marclefer1619 8 ปีที่แล้ว

      +MarcLefer actually im going to delete everything and rewrite the code :I

  • @naveenjoshy6737
    @naveenjoshy6737 8 ปีที่แล้ว

    have u used a data structure for the coding part???

  • @marclefer1619
    @marclefer1619 8 ปีที่แล้ว +1

    24:18 how did you move all that like that

    • @krohn-education3906
      @krohn-education3906  8 ปีที่แล้ว +5

      +MarcLefer Highlight the lines that you want to move. Then, hold down Alt and press Up or Down. You can do this with single lines as well as several highlighted lines.

    • @marclefer1619
      @marclefer1619 8 ปีที่แล้ว

      +Krohn - Education thanks!

  • @onlineaccadmy9659
    @onlineaccadmy9659 4 ปีที่แล้ว +1

    Plz give code of this game

  • @marclefer1619
    @marclefer1619 8 ปีที่แล้ว

    Alright so I re-coded it and it moves! But.. when I press the Left arrow key it moves right and vice versa.. Not sure what I did wrong.

  • @yashkhangura7842
    @yashkhangura7842 6 ปีที่แล้ว

    Mine keeps saying that there is no main method

  • @TopPodClips
    @TopPodClips 7 ปีที่แล้ว

    how would you change the speed of the snake?

    • @themongru
      @themongru 6 ปีที่แล้ว

      He shows how to do this at the end of the video, by modifying the 'sleep' time period in the run() method

  • @Mintsel
    @Mintsel 6 ปีที่แล้ว

    Hi, do you have other java applet codes for me?

  • @mrmm2511
    @mrmm2511 7 ปีที่แล้ว

    my snake pops up in the top left corner and only as one pixel can anyone help?

  • @1NinjaSamurai
    @1NinjaSamurai 6 ปีที่แล้ว +2

    How did he make it run without a main. I tried doing this on netbeans, but it said no main found.

    • @jeffreyhuang3814
      @jeffreyhuang3814 4 ปีที่แล้ว

      In this video he uses a java applet. Java applets do not need a main method.

  • @alotoflag
    @alotoflag 6 ปีที่แล้ว

    When I do the snakePoints.set(0,newStart); it gives me an error telling me to just remove it.
    When i do the.getY and .getX it for some reason is a double. Does anyone know why this it?

    • @alotoflag
      @alotoflag 6 ปีที่แล้ว

      I also have to cast the getY and X as an int which i think might be screwing something up to

  • @dylanweisner8719
    @dylanweisner8719 7 ปีที่แล้ว

    public class Snake {
    List snakePoints;
    int xDir, yDir;
    public Snake(){
    snakePoints = new ArrayList();
    }
    keeps saying there is an error
    saying that the list dont take paramerters

    • @jessiekeijoke6918
      @jessiekeijoke6918 7 ปีที่แล้ว

      me too :/

    • @laxminath6739
      @laxminath6739 7 ปีที่แล้ว

      u have to import the list of util pacakge not the list of applet package

  • @lifestyle_leap-n8x
    @lifestyle_leap-n8x 7 ปีที่แล้ว

    my snake is dies randomly please rply me...

  • @priyankasundar3497
    @priyankasundar3497 7 ปีที่แล้ว

    superbbbbbbbb

  • @fajarsatyasanjaya8947
    @fajarsatyasanjaya8947 6 ปีที่แล้ว

    nice

  • @MrFalcos
    @MrFalcos 8 ปีที่แล้ว

    Every time i press restart in the applet after the game is over, my snake gets faster. After the next restart it gets even more faster. If i close the Applet completly and start it completly new, it will start at his default speed.
    Does anyone have an idea ? :/

    • @daveshn
      @daveshn 7 ปีที่แล้ว

      I had that same problem. You just have to close it and rerun the program.

    • @TenMinuteTraditionalLax
      @TenMinuteTraditionalLax 6 ปีที่แล้ว

      Hey guys this a Thread problem. It is adding the delay to itself everytime. I added a "Hit R for Reload" string after the game under score then do this in keyPressed:
      if(gameOver){
      if(e.getKeyCode() == KeyEvent.VK_R){
      reload();
      }
      }
      then make a reload function that reloads and stops the thread Fixed:
      public void reload(){
      this.destroy();
      this.init();
      thread.stop();
      }

    • @aqeelzol7857
      @aqeelzol7857 2 ปีที่แล้ว

      @@TenMinuteTraditionalLax it doesn't work for me perhaps u have other solution?

  • @williamcuaton3080
    @williamcuaton3080 5 ปีที่แล้ว

    How do i make Jar file

  • @sachingupta3736
    @sachingupta3736 7 ปีที่แล้ว

    bro, i am not getting the placement of your Key Listener
    it is placed in init()
    which in executed only fiirst time by compiler how it listen to Keyaction all time with thread please explain

  • @craigwhite4068
    @craigwhite4068 7 ปีที่แล้ว

    How to make this an application? (not an applet)

    • @ashiinsane90
      @ashiinsane90 6 ปีที่แล้ว

      Search on how to make it executable .exe

  • @anirudhmalik5107
    @anirudhmalik5107 5 ปีที่แล้ว

    How to study all this stuff?
    Express specific book plzz

  • @ilsyflorian8454
    @ilsyflorian8454 6 ปีที่แล้ว

    my snake is not moving and I dont know why u.u

  • @jairroy222
    @jairroy222 7 ปีที่แล้ว

    Where is your main method?

    • @krohn-education3906
      @krohn-education3906  7 ปีที่แล้ว +1

      Applets do not have a "main" method the way that typical Java programs do. Specific methods are initiated by a web browser. I would say that the closest thing to a "main" method in an Applet would be "init". When an applet is placed in a web browser, the "init" method is called first, followed by "start", and then "paint".

  • @RogueMinato
    @RogueMinato 5 ปีที่แล้ว

    Snake is not moving please help I did everything up to 27:42

    • @hernanadrielmartinezmazari676
      @hernanadrielmartinezmazari676 5 ปีที่แล้ว

      Check your keyPressed event. Make sure you don't have VK_KP_LEFT instead of VK_LEFT, I had the same problem

    • @RogueMinato
      @RogueMinato 5 ปีที่แล้ว

      @@hernanadrielmartinezmazari676 thank you very much ill check today in class and get back to you

    • @RogueMinato
      @RogueMinato 5 ปีที่แล้ว

      @@hernanadrielmartinezmazari676 okay after looking at it with my teacher my keyListener method isnt even running

  • @roboto0101
    @roboto0101 2 ปีที่แล้ว

    16:08 - I stayed there

  • @pythonturtle363
    @pythonturtle363 4 ปีที่แล้ว

    go to: 16:54

  • @bephrem
    @bephrem 7 ปีที่แล้ว

    My snake dies randomly

  • @vmixelas7555
    @vmixelas7555 6 ปีที่แล้ว

    I dnt understand

  • @h4ze19
    @h4ze19 8 ปีที่แล้ว

    Can u Upload the Source?

  • @michieb1231
    @michieb1231 8 ปีที่แล้ว +3

    too fast
    but good

    • @krohn-education3906
      @krohn-education3906  6 ปีที่แล้ว

      If you go to settings, you can slow the video down to 0.75x or 0.5x speed.

  • @Jh1lls
    @Jh1lls 2 ปีที่แล้ว

    11:21

  • @yashkhangura7842
    @yashkhangura7842 6 ปีที่แล้ว

    No clue what to do

  • @paladinbutters2
    @paladinbutters2 8 ปีที่แล้ว

    I haven't learn anything. It's way too fast.

    • @finalfantasy7820
      @finalfantasy7820 8 ปีที่แล้ว +3

      Don't stick with the details, just try to understand the concept. Next time everything makes more sense.