SlideShare a Scribd company logo
Snake report ROHIT MALAV
Snake report ROHIT MALAV
ACKNOWLEDGEMENT
So, with gratitude we acknowledge all those who guided and encouraged has
served a beacon of light and crowned the effect with success.
We would like to thank SURESH GYAN VIHAR UNIVERSITY for providing an
opportunity to carry out the project successfully.
We would then like to thank Miss. Tanuja Rajput
Mam who guided and provided the technical and moral supportthroughout the
project. Next we would also to thank the entire faculty for their full co-operation.
REQUIREMENTS :-
Hardware Requirements:-
Processor : 2 MHZ
RAM : 2 GB
Hard Disk : 10 GB (Free space on HDD)
Software Requirements:-
Operating System :- Windows 10 pro
Softwear :- NetBeans IDE 8.0.1
Snake report ROHIT MALAV
Snake report ROHIT MALAV
SOURCECODE:-
snake.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Snake extends JFrame implements KeyListener, Runnable {
JPanel p1, p2;
JButton[] lb = new JButton[200];
JButton bonusfood; JTextArea t; int x = 500, y = 250, gu = 2, directionx = 1, directiony = 0, speed =
50, difference = 0, oldx, oldy, score = 0; int[] lbx = new int[300]; int[] lby = new int[300];
Point[] lbp = new Point[300];
Point bfp = new Point(); Thread myt; boolean food = false, runl = false, runr =
true, runu = true, rund = true, bonusflag = true; Random r = new Random();
JMenuBar mymbar;
JMenu game, help, level;
public void initializeValues()
{ gu = 3; lbx[0] = 100;
lby[0] = 150; directionx =
10; directiony = 0;
difference = 0;
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
score = 0;
food = false;
runl = false; runr
= true; runu =
true; rund = true;
bonusflag = true;
}
Snake() {
super("Snake");
setSize(500, 330);
//Create Menue bar with functions
creatbar();
//initialize all variables
initializeValues(); // Start of UI design
p1 = new JPanel(); p2 = new JPanel();
// t will view the score t = new
JTextArea("Score ==>" + score);
t.setEnabled(false);
t.setBackground(Color.BLACK);
// snake have to eat bonousfood to
growup bonusfood =new JButton();
bonusfood.setEnabled(false); // will make
first snake createFirstSnake();
p1.setLayout(null);
p2.setLayout(new GridLayout(0, 1));
p1.setBounds(0, 0, x, y);
p1.setBackground(Color.blue);
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
p2.setBounds(0, y, x, 30);
p2.setBackground(Color.RED);
p2.add(t); // will contain score board
// end of UI design
getContentPane().setLayout(null);
getContentPane().add(p1);
getContentPane().add(p2);
show();
setDefaultCloseOperation(EXIT_ON_CLOSE);
addKeyListener(this);
// start thread myt = new
Thread(this); myt.start(); // go to
run() method
}
public void createFirstSnake() {
// Initially the snake has small length 3
for (int i = 0; i < 3; i++) { lb[i] = new
JButton("lb" + i);
lb[i].setEnabled(false); p1.add(lb[i]);
lb[i].setBounds(lbx[i], lby[i], 10, 10);
lbx[i + 1] = lbx[i] - 10; lby[i + 1] =
lby[i];
}
}
public void creatbar() {
mymbar = new JMenuBar();
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
game = new JMenu("Game");
JMenuItem newgame = new JMenuItem("New Game");
JMenuItem exit = new JMenuItem("Exit");
newgame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
game.add(newgame);
game.addSeparator(); game.add(exit);
mymbar.add(game);
level = new JMenu("Level");
mymbar.add(level);
help = new JMenu("Help");
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
JMenuItem creator = new JMenuItem("Creator");
JMenuItem instruction = new JMenuItem("Instraction");
creator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(p2, "Name :Abdullah al hasbnRollno :2006331093nSub
:cseninstitute :sust");
}
});
help.add(creator);
help.add(instruction);
mymbar.add(help);
setJMenuBar(mymbar);
}
void reset() {
initializeValues();
p1.removeAll();
myt.stop();
createFirstSnake();
t.setText("Score==>" + score);
myt = new Thread(this);
myt.start();
}
void growup() {
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
lb[gu] = new JButton();
lb[gu].setEnabled(false);
p1.add(lb[gu]);
int a = 10 + (10 * r.nextInt(48));
int b = 10 + (10 * r.nextInt(23));
lbx[gu] = a;
lby[gu] = b;
lb[gu].setBounds(a,b, 10, 10);
gu++;
}
// this method contains the logic to move the snake. player will define the derection
// this method just forward the snake to the right derection which deriction is pressed
// by the player.
void moveForward() {
for (int i = 0; i < gu; i++) {
lbp[i] = lb[i].getLocation();
}
lbx[0] += directionx;
lby[0] += directiony;
lb[0].setBounds(lbx[0], lby[0], 10, 10);
for (int i = 1; i < gu; i++) {
lb[i].setLocation(lbp[i - 1]);
}
if (lbx[0] == x) {
lbx[0] = 10;
} else if (lbx[0] == 0) {
lbx[0] = x - 10;
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
} else if (lby[0] == y) {
lby[0] = 10;
} else if (lby[0] == 0) {
lby[0] = y - 10;
}
if (lbx[0] == lbx[gu - 1] && lby[0] == lby[gu - 1]) {
food = false; score += 5;
t.setText("Score==>" + score);
if (score % 50 == 0 && bonusflag == true) {
p1.add(bonusfood);
bonusfood.setBounds((10* r.nextInt(50)), (10 * r.nextInt(25)), 15, 15);
bfp = bonusfood.getLocation();
bonusflag = false;
}
}
if (bonusflag == false) {
if (bfp.x <= lbx[0] && bfp.y <= lby[0] && bfp.x + 10 >= lbx[0] && bfp.y + 10 >= lby[0]) {
p1.remove(bonusfood); score += 100;
t.setText("Score ==>" + score);
bonusflag = true;
}
}
if (food == false) {
growup();
food = true;
} else {
lb[gu - 1].setBounds(lbx[gu - 1], lby[gu - 1], 10, 10);
}
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
for (int i = 1; i < gu; i++) {
if (lbp[0] == lbp[i]) {
t.setText("GAME OVER " + score);
try {
myt.join();
} catch (InterruptedException ie) {
}
break;
}
}
p1.repaint();
show();
}
public void keyPressed(KeyEvent e) {
// snake move to left when player pressed left arrow
if (runl == true && e.getKeyCode() == 37) {
directionx = -10; // means snake move right to left by 10pixels
directiony = 0;
runr = false; // run right(runr) means snake cant move from left to right
runu = true; // run up (runu) means snake can move from down to up rund =
true; // run down (run down) means snake can move from up to down
}
// snake move to up when player pressed up arrow
if (runu == true && e.getKeyCode() == 38) {
directionx = 0;
directiony = -10; // means snake move from down to up by 10 pixel
rund = false; // run down (run down) means snake can move from up to down
runr = true; // run right(runr) means snake can move from left to right
runl = true; // run left (runl) means snake can move from right to left
Snake report ROHIT MALAV
Snake report ROHIT MALAV
Snake report ROHIT MALAV
}
// snake move to right when player pressed right arrow
if (runr == true && e.getKeyCode() == 39) {
directionx = +10; // means snake move from left to right by 10 pixel
directiony = 0; runl = false; runu = true; rund = true;
}
// snake move to down when player pressed down arrow
if (rund == true && e.getKeyCode() == 40) {
directionx = 0;
directiony = +10; // means snake move from left to right by 10 pixel
runu = false;
runr = true;
runl = true;
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void run() {
for (;;) {
// Move the snake move forword. In the start of the game snake move left to right,
// if player press up, down, right or left arrow snake change its direction according to
// pressed arrow
moveForward();
try {
Thread.sleep(speed);
Snake report ROHIT MALAV
Snake report ROHIT MALAV
} catch (InterruptedException ie) {
}
}
}
}
OUTPUT:-
Snake report ROHIT MALAV
Conclusion:-
The snake game in
java is
implemented and
executed
Successfully.
I hereby thanks to
to tanuja Rajput
mam for her
supportand guidance which helped us to complete this project.

More Related Content

PDF
The Ring programming language version 1.8 book - Part 57 of 202
PDF
PDF
The Ring programming language version 1.5.1 book - Part 49 of 180
PDF
The Ring programming language version 1.2 book - Part 38 of 84
PDF
The Future of JavaScript (SXSW '07)
PDF
The Ring programming language version 1.5.4 book - Part 51 of 185
PDF
The Ring programming language version 1.3 book - Part 42 of 88
PDF
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.8 book - Part 57 of 202
The Ring programming language version 1.5.1 book - Part 49 of 180
The Ring programming language version 1.2 book - Part 38 of 84
The Future of JavaScript (SXSW '07)
The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.3 book - Part 42 of 88
The Ring programming language version 1.5 book - Part 9 of 31

What's hot (20)

PDF
The Ring programming language version 1.5.2 book - Part 50 of 181
KEY
Introduction to Game Programming Tutorial
PDF
The Ring programming language version 1.3 book - Part 41 of 88
PDF
The Ring programming language version 1.6 book - Part 53 of 189
PDF
The Ring programming language version 1.9 book - Part 62 of 210
PDF
The Ring programming language version 1.4.1 book - Part 15 of 31
PDF
The Ring programming language version 1.9 book - Part 61 of 210
DOCX
New microsoft office word document
PDF
The Ring programming language version 1.5.3 book - Part 61 of 184
PDF
The Ring programming language version 1.2 book - Part 37 of 84
PDF
The Ring programming language version 1.5.1 book - Part 51 of 180
PDF
The Ring programming language version 1.6 book - Part 55 of 189
PDF
The Ring programming language version 1.8 book - Part 56 of 202
PDF
The Ring programming language version 1.5.3 book - Part 62 of 184
PDF
The Ring programming language version 1.7 book - Part 55 of 196
KEY
Intro to Game Programming
PDF
The Ring programming language version 1.3 book - Part 40 of 88
PDF
Node meetup feb_20_12
PDF
The Ring programming language version 1.5.1 book - Part 48 of 180
PDF
The Ring programming language version 1.10 book - Part 61 of 212
The Ring programming language version 1.5.2 book - Part 50 of 181
Introduction to Game Programming Tutorial
The Ring programming language version 1.3 book - Part 41 of 88
The Ring programming language version 1.6 book - Part 53 of 189
The Ring programming language version 1.9 book - Part 62 of 210
The Ring programming language version 1.4.1 book - Part 15 of 31
The Ring programming language version 1.9 book - Part 61 of 210
New microsoft office word document
The Ring programming language version 1.5.3 book - Part 61 of 184
The Ring programming language version 1.2 book - Part 37 of 84
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.5.3 book - Part 62 of 184
The Ring programming language version 1.7 book - Part 55 of 196
Intro to Game Programming
The Ring programming language version 1.3 book - Part 40 of 88
Node meetup feb_20_12
The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.10 book - Part 61 of 212
Ad

Similar to Snake report ROHIT MALAV (20)

DOCX
A simple snake game project
PPTX
PowerPoint Presentation For Snake Game Project In Java
PPTX
484478584-Presentation-on-Snake-game-pptx.pptx
PDF
create file name as board.javawrite below codeimport java.aw.pdf
PPTX
Snake_game.pptx introduction to snake game
PDF
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
PDF
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
PDF
MineSweeper.java public class MS { public static void main(Strin.pdf
DOC
Snake project report
PDF
This is Java,I am currently stumped on how to add a scoreboard for.pdf
PPTX
Snake game with c++ and the tools are codeblocks(IDE) and windows operating s...
PPTX
Snake Game using computer graphics in c and c++
PPT
snake game using java it help you to play the game easilly
PDF
TASK #1In the domain class you will create a loop that will prompt.pdf
DOCX
TilePUzzle class Anderson, Franceschi public class TilePu.docx
PPTX
Most asked JAVA Interview Questions & Answers.
PDF
import java.util.Scanner; import java.util.Random; public clas.pdf
PDF
java code please Add event handlers to the buttons in your TicTacToe.pdf
PDF
Description For this part of the assignment, you will create a Grid .pdf
PDF
Matching Game In Java
A simple snake game project
PowerPoint Presentation For Snake Game Project In Java
484478584-Presentation-on-Snake-game-pptx.pptx
create file name as board.javawrite below codeimport java.aw.pdf
Snake_game.pptx introduction to snake game
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
MineSweeper.java public class MS { public static void main(Strin.pdf
Snake project report
This is Java,I am currently stumped on how to add a scoreboard for.pdf
Snake game with c++ and the tools are codeblocks(IDE) and windows operating s...
Snake Game using computer graphics in c and c++
snake game using java it help you to play the game easilly
TASK #1In the domain class you will create a loop that will prompt.pdf
TilePUzzle class Anderson, Franceschi public class TilePu.docx
Most asked JAVA Interview Questions & Answers.
import java.util.Scanner; import java.util.Random; public clas.pdf
java code please Add event handlers to the buttons in your TicTacToe.pdf
Description For this part of the assignment, you will create a Grid .pdf
Matching Game In Java
Ad

More from Rohit malav (20)

PPTX
Aca lab project (rohit malav)
PPTX
operating system calls input and output by (rohit malav)
PPTX
Python pandas liberary
PPTX
Presentation by purshotam verma
PPTX
Deep learning in python by purshottam verma
PPTX
Atm Security System Using Steganography Nss ptt by (rohit malav)
DOCX
Samba server Pts report pdf by Rohit malav
PPTX
System calls operating system ppt by rohit malav
PPTX
A project on spring framework by rohit malav
PPTX
android text encryption Network security lab by rohit malav
PPTX
samba server setup Pts ppt (rohit malav)
PPTX
Spring frame work by rohit malav(detailed)
PPTX
spring framework ppt by Rohit malav
PPTX
Samba server linux (SMB) BY ROHIT MALAV
PPTX
Payroll system ppt1 (rohit malav)
PPTX
Payroll system ppt2 (rohit malav) version point 2
PPTX
ONLINE STUDENT MANAGEMENT SYSTEM
PPTX
digital unlock power point slide
PDF
Rohit android lab projects in suresh gyan vihar
PPTX
Gyan vihar app
Aca lab project (rohit malav)
operating system calls input and output by (rohit malav)
Python pandas liberary
Presentation by purshotam verma
Deep learning in python by purshottam verma
Atm Security System Using Steganography Nss ptt by (rohit malav)
Samba server Pts report pdf by Rohit malav
System calls operating system ppt by rohit malav
A project on spring framework by rohit malav
android text encryption Network security lab by rohit malav
samba server setup Pts ppt (rohit malav)
Spring frame work by rohit malav(detailed)
spring framework ppt by Rohit malav
Samba server linux (SMB) BY ROHIT MALAV
Payroll system ppt1 (rohit malav)
Payroll system ppt2 (rohit malav) version point 2
ONLINE STUDENT MANAGEMENT SYSTEM
digital unlock power point slide
Rohit android lab projects in suresh gyan vihar
Gyan vihar app

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
PPTX
Lecture Notes Electrical Wiring System Components
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Digital Logic Computer Design lecture notes
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
OOP with Java - Java Introduction (Basics)
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Construction Project Organization Group 2.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
Well-logging-methods_new................
Lecture Notes Electrical Wiring System Components
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Digital Logic Computer Design lecture notes
additive manufacturing of ss316l using mig welding
Internet of Things (IOT) - A guide to understanding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
OOP with Java - Java Introduction (Basics)
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Snake report ROHIT MALAV

  • 3. ACKNOWLEDGEMENT So, with gratitude we acknowledge all those who guided and encouraged has served a beacon of light and crowned the effect with success. We would like to thank SURESH GYAN VIHAR UNIVERSITY for providing an opportunity to carry out the project successfully. We would then like to thank Miss. Tanuja Rajput Mam who guided and provided the technical and moral supportthroughout the project. Next we would also to thank the entire faculty for their full co-operation. REQUIREMENTS :- Hardware Requirements:- Processor : 2 MHZ RAM : 2 GB Hard Disk : 10 GB (Free space on HDD) Software Requirements:- Operating System :- Windows 10 pro Softwear :- NetBeans IDE 8.0.1
  • 7. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; class Snake extends JFrame implements KeyListener, Runnable { JPanel p1, p2; JButton[] lb = new JButton[200]; JButton bonusfood; JTextArea t; int x = 500, y = 250, gu = 2, directionx = 1, directiony = 0, speed = 50, difference = 0, oldx, oldy, score = 0; int[] lbx = new int[300]; int[] lby = new int[300]; Point[] lbp = new Point[300]; Point bfp = new Point(); Thread myt; boolean food = false, runl = false, runr = true, runu = true, rund = true, bonusflag = true; Random r = new Random(); JMenuBar mymbar; JMenu game, help, level; public void initializeValues() { gu = 3; lbx[0] = 100; lby[0] = 150; directionx = 10; directiony = 0; difference = 0;
  • 11. score = 0; food = false; runl = false; runr = true; runu = true; rund = true; bonusflag = true; } Snake() { super("Snake"); setSize(500, 330); //Create Menue bar with functions creatbar(); //initialize all variables initializeValues(); // Start of UI design p1 = new JPanel(); p2 = new JPanel(); // t will view the score t = new JTextArea("Score ==>" + score); t.setEnabled(false); t.setBackground(Color.BLACK); // snake have to eat bonousfood to growup bonusfood =new JButton(); bonusfood.setEnabled(false); // will make first snake createFirstSnake(); p1.setLayout(null); p2.setLayout(new GridLayout(0, 1)); p1.setBounds(0, 0, x, y); p1.setBackground(Color.blue);
  • 15. p2.setBounds(0, y, x, 30); p2.setBackground(Color.RED); p2.add(t); // will contain score board // end of UI design getContentPane().setLayout(null); getContentPane().add(p1); getContentPane().add(p2); show(); setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener(this); // start thread myt = new Thread(this); myt.start(); // go to run() method } public void createFirstSnake() { // Initially the snake has small length 3 for (int i = 0; i < 3; i++) { lb[i] = new JButton("lb" + i); lb[i].setEnabled(false); p1.add(lb[i]); lb[i].setBounds(lbx[i], lby[i], 10, 10); lbx[i + 1] = lbx[i] - 10; lby[i + 1] = lby[i]; } } public void creatbar() { mymbar = new JMenuBar();
  • 20. game = new JMenu("Game"); JMenuItem newgame = new JMenuItem("New Game"); JMenuItem exit = new JMenuItem("Exit"); newgame.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { reset(); } }); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); game.add(newgame); game.addSeparator(); game.add(exit);
  • 21. mymbar.add(game); level = new JMenu("Level"); mymbar.add(level); help = new JMenu("Help");
  • 25. JMenuItem creator = new JMenuItem("Creator"); JMenuItem instruction = new JMenuItem("Instraction"); creator.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(p2, "Name :Abdullah al hasbnRollno :2006331093nSub :cseninstitute :sust"); } }); help.add(creator); help.add(instruction); mymbar.add(help); setJMenuBar(mymbar); } void reset() { initializeValues(); p1.removeAll(); myt.stop(); createFirstSnake(); t.setText("Score==>" + score); myt = new Thread(this); myt.start();
  • 30. lb[gu] = new JButton(); lb[gu].setEnabled(false); p1.add(lb[gu]); int a = 10 + (10 * r.nextInt(48)); int b = 10 + (10 * r.nextInt(23)); lbx[gu] = a; lby[gu] = b; lb[gu].setBounds(a,b, 10, 10); gu++; } // this method contains the logic to move the snake. player will define the derection // this method just forward the snake to the right derection which deriction is pressed // by the player. void moveForward() { for (int i = 0; i < gu; i++) { lbp[i] = lb[i].getLocation(); } lbx[0] += directionx; lby[0] += directiony; lb[0].setBounds(lbx[0], lby[0], 10, 10); for (int i = 1; i < gu; i++) { lb[i].setLocation(lbp[i - 1]); } if (lbx[0] == x) { lbx[0] = 10;
  • 31. } else if (lbx[0] == 0) { lbx[0] = x - 10;
  • 35. } else if (lby[0] == y) { lby[0] = 10; } else if (lby[0] == 0) { lby[0] = y - 10; } if (lbx[0] == lbx[gu - 1] && lby[0] == lby[gu - 1]) { food = false; score += 5; t.setText("Score==>" + score); if (score % 50 == 0 && bonusflag == true) { p1.add(bonusfood); bonusfood.setBounds((10* r.nextInt(50)), (10 * r.nextInt(25)), 15, 15); bfp = bonusfood.getLocation(); bonusflag = false; } } if (bonusflag == false) { if (bfp.x <= lbx[0] && bfp.y <= lby[0] && bfp.x + 10 >= lbx[0] && bfp.y + 10 >= lby[0]) { p1.remove(bonusfood); score += 100; t.setText("Score ==>" + score); bonusflag = true; } } if (food == false) { growup(); food = true; } else { lb[gu - 1].setBounds(lbx[gu - 1], lby[gu - 1], 10, 10); }
  • 39. for (int i = 1; i < gu; i++) { if (lbp[0] == lbp[i]) { t.setText("GAME OVER " + score); try { myt.join(); } catch (InterruptedException ie) { } break; } } p1.repaint(); show(); } public void keyPressed(KeyEvent e) { // snake move to left when player pressed left arrow if (runl == true && e.getKeyCode() == 37) { directionx = -10; // means snake move right to left by 10pixels directiony = 0; runr = false; // run right(runr) means snake cant move from left to right runu = true; // run up (runu) means snake can move from down to up rund = true; // run down (run down) means snake can move from up to down } // snake move to up when player pressed up arrow
  • 40. if (runu == true && e.getKeyCode() == 38) { directionx = 0; directiony = -10; // means snake move from down to up by 10 pixel rund = false; // run down (run down) means snake can move from up to down runr = true; // run right(runr) means snake can move from left to right runl = true; // run left (runl) means snake can move from right to left
  • 44. } // snake move to right when player pressed right arrow if (runr == true && e.getKeyCode() == 39) { directionx = +10; // means snake move from left to right by 10 pixel directiony = 0; runl = false; runu = true; rund = true; } // snake move to down when player pressed down arrow if (rund == true && e.getKeyCode() == 40) { directionx = 0; directiony = +10; // means snake move from left to right by 10 pixel runu = false; runr = true; runl = true; } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void run() { for (;;) { // Move the snake move forword. In the start of the game snake move left to right, // if player press up, down, right or left arrow snake change its direction according to // pressed arrow moveForward(); try { Thread.sleep(speed);
  • 47. } catch (InterruptedException ie) { } } } } OUTPUT:-
  • 49. Conclusion:- The snake game in java is implemented and executed Successfully. I hereby thanks to to tanuja Rajput mam for her supportand guidance which helped us to complete this project.