The document provides code for a Snake game written in Java. It includes requirements for hardware and software, source code for the Snake.java file, and a brief conclusion acknowledging guidance from a professor. The code implements a Snake game with menus for new game, exit, help, and creator details. It uses threads to continuously move the snake forward according to arrow key input and checks for collisions with walls or its body segments.
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
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;
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
41.
42.
43.
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);
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.