How to make a snake game in java source code
In this blog
I am going to show you how to make a snake game in java it’s very easy copy the
source code for each class and make some classes for them and paste it to your classes
There are five
classes in this whole snake game so what
you need here . you just have to make five
classes. 1st one by name Display
Create a first class and name it
1 mainGame
package snake;
public class mainGame {
public static void main(String[]args){
snakeCals
snake = new snakeCals("java snake game",500,580);
snake.start();
}
}
Create a 2nd
class and name it
2 Dispaly
package snake;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Display {
private String title;
private int width;
private int height;
private JFrame frame;
private Canvas canvas;
public Display(String title,int width,int height){
this.title = title;
this.width = width;
this.height = height;
createDisplay();
}
public void createDisplay(){
frame = new JFrame(title);
frame.setVisible(true);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width,height));
canvas.setFocusable(false);
frame.add(canvas);
}
public Canvas getCanvas(){
return canvas;
}
public JFrame getFrame(){
return frame;
}
}
Create a 3rd class and
name it
3 snakeCals
package snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import
java.util.LinkedList;
import java.util.Random;
public class snakeCals implements
Runnable,KeyListener{
private Thread thread;
private Display display;
private String title;
private int width,height;
private BufferStrategy buffer;
private Graphics g;
private
LinkedList<Point> snake;
private int direction = Direction.no_direction ;
//rectangle
set up
public static final int WIDTH = 15;
public static final int HEIGHT =15;
public static final int BOX_WIDTH =30;
public static final int BOX_HEIGHT =30;
//Fruit
private Point fruit;
//score
private int score;
private Level2 level2;
public snakeCals(String title,int width,int height){
this.title = title;
this.width = width;
this.height = height ;
}
public void init(){
display = new Display(title,width,height);
display.getFrame().addKeyListener(this);
snake = new
LinkedList<Point>();
level2 = new Level2();
reStart();
randFruit();
}
public void Draw(Graphics g){
DrawRect(g);
DrawSnake(g);
DrawFruit(g);
DrawScore(g);
level2.DrawWall(g);
}
public void DrawRect(Graphics g){
Color
color = new Color(179,250,130);
g.setColor(color);
g.fillRect(0, 0, WIDTH*BOX_WIDTH, HEIGHT*BOX_HEIGHT);
g.setColor(Color.red);
}
private void DrawSnake(Graphics g){ g.setColor(Color.blue);
for(Point p:snake){
g.fillRect(p.x*BOX_WIDTH, p.y*BOX_HEIGHT, BOX_WIDTH,BOX_HEIGHT );
}
g.setColor(Color.black);
}
public void reStart(){
direction = Direction.no_direction;
snake.clear();
level2.list.clear();
score = 0;
snake.add(new Point(9,7));
snake.add(new Point(8,7));
snake.add(new Point(7,7));
}
public void DrawFruit(Graphics g){
g.setColor(Color.red);
g.fillOval(fruit.x*BOX_WIDTH, fruit.y*BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.BLACK);
}
private void DrawScore(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("arial",Font.PLAIN,37));
g.drawString("Score =
"+score,22,HEIGHT*BOX_HEIGHT+40);
g.setColor(Color.black);
}
public void randFruit(){
Random
rand = new Random();
int randX = rand.nextInt(WIDTH);
int randY = rand.nextInt(HEIGHT);
fruit = new Point(randX,randY);
while(snake.contains(fruit) || level2.list.contains(fruit)){
fruit = new Point(randX,randY);
}
}
public void move(){
Point
head = snake.peekFirst();
Point
newPoint = head;
switch(direction){
case Direction.up:
newPoint = new Point(head.x,head.y -1);
break;
case Direction.down:
newPoint = new Point(head.x,head.y +1);
break;
case Direction.right:
newPoint = new Point(head.x +1,head.y);
break;
newPoint = new Point(head.x -1,head.y);
break;
}
if(newPoint.x >= WIDTH){
newPoint = new Point(newPoint.x = 0,newPoint.y);
}
else if (newPoint.x < 0){
newPoint = new Point(newPoint.x = WIDTH -1 , newPoint.y);
}
else if (newPoint.y < 0){
newPoint = new Point(newPoint.x, newPoint.y = HEIGHT -1);
} else if(newPoint.y >= HEIGHT){
newPoint = new Point(newPoint.x, newPoint.y = 0);
}
if(snake.contains(newPoint) || level2.list.contains(newPoint)){
reStart();
return;
}
snake.remove(snake.peekLast());
if(newPoint.equals(fruit) ){
Point
add = (Point) newPoint.clone();
score = score +5;
snake.push(add);
if(score >= 20){
level2.level2();
}
randFruit();
}
snake.push(newPoint);
}
No comments:
Post a Comment