Java Tutorial How to Develop Game In Java ( Snake Game )

///Java Tutorial How to Develop Game In Java ( Snake Game )

Java Tutorial How to Develop Game In Java ( Snake Game )

FavoriteLoadingAdd to favorites

Best Javascript Book :

Java468x60

2nd Javascript Book :

Best Java Book :

Best Angular JS Book:

Best Angular 5 Book:

Donate Us:

Udemy Courses

AutoCAD Course :

Revit Course :

WordPress Course :

Java Game Development Tutorial. In this Video You will learn step by step how to Develop Snake Game in Java.

Facebook Group:

Assets:

Solve Programming Practice Problems (Java):

Brick Breaker Game in JAVA:

source

By |2019-10-28T23:35:14+00:00October 28th, 2019|Java Video Tutorials|29 Comments

29 Comments

  1. Mihai Vlasceanu October 28, 2019 at 11:35 pm - Reply

    How can I make the assets?

  2. Sujeen Lucky October 28, 2019 at 11:35 pm - Reply

    where to get those png images

  3. Binh Duong Pham October 28, 2019 at 11:35 pm - Reply

    i have little comment on improvement that when you code function press space to restart you need to set all the value of 2 arrays snakeXLength and snakeYLength to 0 otherwise when you restart the game if you go to the area you die the previous round you will trigger game over again. Anywway great video man, i made my day, keep up the good work and thank you very much for this.

  4. treentje October 28, 2019 at 11:35 pm - Reply

    For everyone who encounters the same problem as me: between part 2 and 3 some code has been added above the class which wasn't explained but you do need to import those items.

  5. Nestor Alexander Castañeda Padrón October 28, 2019 at 11:35 pm - Reply

    There's a lot of Indian guys in YouTube! Nothing wrong with that, I am just surprised of the amount of Indian YouTubers

  6. sonali deshpande October 28, 2019 at 11:35 pm - Reply

    There is error occurred that method start is undifined for method timer . How should I correct it

  7. Omkar Rao October 28, 2019 at 11:35 pm - Reply

    thanks for helping me with my java project

  8. multi techno October 28, 2019 at 11:35 pm - Reply

    Send me the this game's source code

  9. Shanebagel October 28, 2019 at 11:35 pm - Reply

    My snake doesn't move how do I fix that? 🙁

  10. Pragya Singh October 28, 2019 at 11:35 pm - Reply

    The snake isn't moving after pressing any key.. It throws unsupported operation exception . Can anyone fix this bug?

  11. Fraternal House October 28, 2019 at 11:35 pm - Reply

    Which IDE are you using?

  12. asim sheikh October 28, 2019 at 11:35 pm - Reply

    hi bro where is the link of the images used in this game

  13. shubham mittal October 28, 2019 at 11:35 pm - Reply

    unable to pick enemy

  14. shubham mittal October 28, 2019 at 11:35 pm - Reply

    i m getting error when i m using timer

  15. ashok rathore October 28, 2019 at 11:35 pm - Reply

    Sir i am getting error in these lines

    timer=new Timer(delay,this);
    timer.start();

  16. pessimistic 557 October 28, 2019 at 11:35 pm - Reply

    33:17 you dont need to add the other directions as false, since they will just remain unchanged, theres no need to say theyre false if thats already initiated

  17. para sakthi October 28, 2019 at 11:35 pm - Reply

    Snake just pass through the enemy and enemy position didn't change?!

  18. LeonidasSthlm October 28, 2019 at 11:35 pm - Reply

    import java.awt.*;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.awt.event.KeyEvent;

    import java.awt.event.KeyListener;

    import java.util.Random;

    import javax.swing.*;

    public class Gameplay extends JPanel implements KeyListener, ActionListener{

    private int[] snakeXLength = new int[750];

    private int[] snakeYLength = new int[750];

    private boolean left = false;

    private boolean right = false;

    private boolean up = false;

    private boolean down = false;

    private ImageIcon leftMouth;

    private ImageIcon rightMouth;

    private ImageIcon upMouth;

    private ImageIcon downMouth;

    private int lengthOfSnake = 3;

    private Timer timer;

    private int delay = 100;

    private ImageIcon snakeImage;

    private int[] enemyXPos = {25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500,525,550,575,600,625,650,675,700,725,750,775,800,850};

    private int[] enemyYPos = {75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500,525,550,575,600,625};

    private ImageIcon enemyImage;

    private Random random = new Random();

    private int xPos = random.nextInt(34);

    private int yPos = random.nextInt(23);

    private int score = 0;

    private int moves = 0;

    private ImageIcon titleImage;

    public Gameplay() {

    addKeyListener(this);

    setFocusable(true);

    setFocusTraversalKeysEnabled(false);

    timer = new Timer(delay, this);

    timer.start();

    }

    public void paint (Graphics g) {

    if(moves==0) {

    snakeXLength[2] = 50;

    snakeXLength[1] = 75;

    snakeXLength[0] = 100;

    snakeYLength[2] = 100;

    snakeYLength[1] = 100;

    snakeYLength[0] = 100;

    }

    //draw title image border

    g.setColor(Color.WHITE);

    g.drawRect(24, 10, 851, 55);

    //draw the title image

    titleImage = new ImageIcon("snaketitle.jpg");

    titleImage.paintIcon(this, g, 25, 11);

    //draw border for gameplay;

    g.setColor(Color.WHITE);

    g.drawRect(24, 74, 851, 577);

    //draw backgorund for the gameplay

    g.setColor(Color.black);

    g.fillRect(25, 75, 850, 575);

    //draw the score

    g.setColor(Color.WHITE);

    g.setFont(new Font("arial", Font.PLAIN, 14));

    g.drawString("Score: "+score, 780, 30);

    //draw the length of the snake

    g.setColor(Color.WHITE);

    g.setFont(new Font("arial", Font.PLAIN, 14));

    g.drawString("Length: "+lengthOfSnake, 780, 50);

    rightMouth = new ImageIcon("rightmouth.png");

    rightMouth.paintIcon(this, g, snakeXLength[0], snakeYLength[0]);

    for(int a = 0; a<lengthOfSnake; a++) {

    if(a==0 && left) {

    leftMouth = new ImageIcon("leftmouth.png");

    leftMouth.paintIcon(this, g, snakeXLength[a], snakeYLength[a]);

    }

    if(a==0 && right) {

    rightMouth = new ImageIcon("rightmouth.png");

    rightMouth.paintIcon(this, g, snakeXLength[a], snakeYLength[a]);

    }

    if(a==0 && up) {

    upMouth = new ImageIcon("upmouth.png");

    upMouth.paintIcon(this, g, snakeXLength[a], snakeYLength[a]);

    }

    if(a==0 && down) {

    downMouth = new ImageIcon("downmouth.png");

    downMouth.paintIcon(this, g, snakeXLength[a], snakeYLength[a]);

    }

    if(a!=0) {

    snakeImage = new ImageIcon("snakeimage.png");

    snakeImage.paintIcon(this, g, snakeXLength[a], snakeYLength[a]);

    }

    }

    enemyImage = new ImageIcon("enemy.png");

    if((enemyXPos[xPos] == snakeXLength[0]) &&(enemyYPos[yPos] == snakeYLength[0])) {

    score++;

    lengthOfSnake++;

    xPos = random.nextInt(34);

    yPos = random.nextInt(23);

    }

    enemyImage.paintIcon(this, g, enemyXPos[xPos], enemyYPos[yPos]);

    for(int b = 1; b<lengthOfSnake; b++) {

    if((snakeXLength[b]==snakeXLength[0])&&(snakeYLength[b]==snakeYLength[0])) {

    right= false;

    left = false;

    up = false;

    down = false;

    g.setColor(Color.WHITE);

    g.setFont(new Font("arial", Font.BOLD, 50));

    g.drawString("Game Over", 340, 300);

    g.setFont(new Font("arial", Font.BOLD, 20));

    g.drawString("Press the Spacebar to restart", 340, 340);

    }

    }

    g.dispose();

    }

    @Override

    public void actionPerformed(ActionEvent e) {

    timer.start();

    if(left) {

    for(int r = lengthOfSnake-1; r>=0; r–) {

    snakeYLength[r+1] = snakeYLength[r];

    }

    for(int r = lengthOfSnake; r>=0; r–) {

    if(r==0) {

    snakeXLength[r] = snakeXLength[r]-25;

    }else {

    snakeXLength[r] = snakeXLength[r-1];

    }

    if(snakeXLength[r] < 25) {

    snakeXLength[r] = 850;

    }

    }

    repaint();

    }

    if(right) {

    for(int r = lengthOfSnake-1; r>=0; r–) {

    snakeYLength[r+1] = snakeYLength[r];

    }

    for(int r = lengthOfSnake; r>=0; r–) {

    if(r==0) {

    snakeXLength[r] = snakeXLength[r]+25;

    }else {

    snakeXLength[r] = snakeXLength[r-1];

    }

    if(snakeXLength[r] > 850) {

    snakeXLength[r] = 25;

    }

    }

    repaint();

    }

    if(up) {

    for(int r = lengthOfSnake-1; r>=0; r–) {

    snakeXLength[r+1] = snakeXLength[r];

    }

    for(int r = lengthOfSnake; r>=0; r–) {

    if(r==0) {

    snakeYLength[r] = snakeYLength[r]-25;

    }else {

    snakeYLength[r] = snakeYLength[r-1];

    }

    if(snakeYLength[r] < 75) {

    snakeYLength[r] = 625;

    }

    }

    repaint();

    }

    if(down) {

    for(int r = lengthOfSnake-1; r>=0; r–) {

    snakeXLength[r+1] = snakeXLength[r];

    }

    for(int r = lengthOfSnake; r>=0; r–) {

    if(r==0) {

    snakeYLength[r] = snakeYLength[r]+25;

    }else {

    snakeYLength[r] = snakeYLength[r-1];

    }

    if(snakeYLength[r] > 625) {

    snakeYLength[r] = 75;

    }

    }

    repaint();

    }

    }

    @Override

    public void keyTyped(KeyEvent e) {

    // TODO Auto-generated method stub

    }

    @Override

    public void keyPressed(KeyEvent e) {

    if(e.getKeyCode() == KeyEvent.VK_SPACE) {

    moves = 0;

    score = 0;

    lengthOfSnake = 3;

    repaint();

    }

    if(e.getKeyCode() == KeyEvent.VK_LEFT) {

    moves++;

    left = true;

    if(!right) {

    left = true;

    }else {

    left = false;

    right = true;

    }

    up = false;

    down = false;

    }

    if(e.getKeyCode() == KeyEvent.VK_RIGHT) {

    moves++;

    right = true;

    if(!left) {

    right = true;

    }else {

    right = false;

    left = true;

    }

    up = false;

    down = false;

    }

    if(e.getKeyCode() == KeyEvent.VK_UP) {

    moves++;

    up = true;

    if(!down) {

    up = true;

    }else {

    up = false;

    down = true;

    }

    left = false;

    right = false;

    }

    if(e.getKeyCode() == KeyEvent.VK_DOWN) {

    moves++;

    down = true;

    if(!up) {

    down = true;

    }else {

    down = false;

    up = true;

    }

    left = false;

    right = false;

    }

    }

    @Override

    public void keyReleased(KeyEvent e) {

    // TODO Auto-generated method stub

    }

    }

  19. Arif Ahmed October 28, 2019 at 11:35 pm - Reply

    why doesn't the right arrow key move???

  20. Main Malik Zeeshan October 28, 2019 at 11:35 pm - Reply

    In step 2 ur extending gameplay form jpanel but there is no such class like jpanel class?? Can any one tell??

  21. abhishek sahoo October 28, 2019 at 11:35 pm - Reply

    My snake is moving left pressing right any idea why?

  22. Manthan Desai October 28, 2019 at 11:35 pm - Reply

    very nice

  23. Ajay Botchu October 28, 2019 at 11:35 pm - Reply

    I compiled a game blue j environment but i dont know how to play it

  24. Ajaxsied October 28, 2019 at 11:35 pm - Reply

    25:58, wtf are those 2 thingies thata he is typing.. help pls

  25. Technical Jugad October 28, 2019 at 11:35 pm - Reply

    सर मुझे एक एप्लीकेशन को मॉडिफाई करके आप दे सकते हैं क्या बदले में मैं 50% आपको दूंगा जो भी इनकम होगा

  26. Tapasvi Patel October 28, 2019 at 11:35 pm - Reply

    Hi this is a great tutorial bt i am having problems in moving the snake

  27. raheel hussain October 28, 2019 at 11:35 pm - Reply

    Thanks for such an excellent tutorial. Will you please tell me how i can make a . jar or .exe file of this game??????

  28. Techno Pi October 28, 2019 at 11:35 pm - Reply

Leave A Comment

*