Wed. Dec 11th, 2019

Host your Website

Read and learn

FatCow Web Hosting $1.00/mo* Trust your web hosting to the #1 web host provider, GoDaddy!

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

1 min read



Best Javascript Book : http://amzn.to/2iQZpjN

2nd Javascript Book : http://amzn.to/2iaF9Kn

Best Java Book : http://amzn.to/2yxz28S

Best Angular JS Book: http://amzn.to/2iSzCHX

Best Angular 5 Book: http://amzn.to/2iQFMIk

Donate Us: http://paypal.me/tipawais

Udemy Courses

AutoCAD Course : https://www.udemy.com/autocad2017/?couponCode=AWAIS65

Revit Course : https://www.udemy.com/revit2017/?couponCode=AWAISYOU50

WordPress Course : https://www.udemy.com/wordpress-essentials-ecommerce/?couponCode=AWAIS501

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

Facebook Group:
https://www.facebook.com/groups/996305460498149/

Assets:
https://drive.google.com/drive/folders/0B8qiHDiC8qnbbkVibmZuSGd5TDg?usp=sharing

Solve Programming Practice Problems (Java):

Brick Breaker Game in JAVA:

29 thoughts on “Java Tutorial How to Develop Game In Java ( Snake Game )

  1. Hello,
    I´m totally new to the programming either Javaand i have come across a problem with the bounds. its showing differently on my side than on yours. I´m using the same digits in every g. "typecode", step by step as your tutorial. Also i had the same problem in the Brick Breaker Game tutorial. I´ve checked the code multiple times and i have no idea. Any advice on why does the pixels are drawn differently on my side then on yours ?

    I´m attaching image on how does the border looks at my side. The problem is that the left and right side of the window does not match.
    Screenshot – http://prntscr.com/q2xvqr

  2. It is very helpful so I would like to thank you for that. But there are a couple of bugs I encountered. First, there enemy can be drawn anywhere on the screen randomly. Including the snakes body. It's okay when the snake is small. But it can cause you trouble when the snake is longer. And second, The snake can't go left if it's going right and it can't go up if it's going down and so on, but if you very quickly press up and then left when it is going right, up will be true so the snake can now go left, so at the instant you press left, the snake will collide with it's body. It looks as if the snake directly went left from going right and collided with it's body. And third, After game over, if you press an arrow key instead of space, the snake continues to go (it doesn't restart). But I have already fixed that one in my code. So it will be appreciated if you can fix those bugs (if you have the time of course), but other than that, it was an amazing tutorial. Thank you again!

  3. My snake movement is not working, it is not moving at all. It is not affected what arrow key i press. i noticed that there was a cut between 44 and 45 minute mark, where an extra class is added, aclass.java and tester.java. Where are the code and explanation of that??
    Please help, as i am not that good in coding and i am excited to learn new things, and my snake is not moving.

  4. 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.

  5. 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.

  6. 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

    }

    }

Comments are closed.

Copyright © All rights reserved. | Newsphere by AF themes.