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 Display
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;
case Direction.left:
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>=HEIGHT){
newPoint = new Point(newPoint.x, newPoint.y=0);
}else if (newPoint.y<0){
newPoint = new Point(newPoint.x,newPoint.y=HEIGHT-1);
}
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);
}
public void render(){
buffer = display.getCanvas().getBufferStrategy();
if(buffer == null ){
display.getCanvas().createBufferStrategy(3);
return;
}
g = buffer.getDrawGraphics();
g.clearRect(0, 0, width, height);
//draw here
Draw(g);
//draw end
buffer.show();
g.dispose();
}
public synchronized void start(){
if(thread ==null){
thread = new Thread(this);
thread.start();
}
}
public synchronized void stop(){
try {
thread.join();
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
init();
while(true){
render();
move();
Thread.currentThread();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
if(direction != Direction.down)
direction = Direction.up;
break;
case KeyEvent.VK_DOWN:
if(direction != Direction.up)
direction =Direction.down;
break;
case KeyEvent.VK_LEFT:
if(direction != Direction.right)
direction =Direction.left;
break;
case KeyEvent.VK_RIGHT:
if(direction != Direction.left)
direction = Direction.right;
break;
}
}
public void keyReleased(KeyEvent
e) {
}
public void keyTyped(KeyEvent e) {
}
}
Create a 4th class and name it
4 Direction
package snake;
public class Direction {
public static final int no_direction = 0;
public static final int up = 1;
public static final int down =2 ;
public static final int right = 3;
public static final int left =4;
}
Create
a 5th class and name it
5 Level2
package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import
java.util.LinkedList;
public class Level2 {
public static
LinkedList<Point> list;
public Level2(){
init();
}
public void init(){
list = new
LinkedList<Point>();
}
public void DrawWall(Graphics g){
g.setColor(Color.black);
for(Point w: list)
g.fillRect(w.x*snakeCals.BOX_WIDTH, w.y*snakeCals.BOX_HEIGHT, snakeCals.BOX_WIDTH, snakeCals.BOX_HEIGHT);
}
public void level2(){
//wall 1
list.add(new Point(3,4));
list.add(new Point(4,4));
list.add(new Point(5,4));
list.add(new Point(6,4));
list.add(new Point(7,4));
list.add(new Point(8,4));
list.add(new Point(9,4));
list.add(new Point(10,4));
list.add(new Point(11,4));
//wall 2
list.add(new Point(3,12));
list.add(new Point(4,12));
list.add(new Point(5,12));
list.add(new Point(6,12));
list.add(new Point(7,12));
list.add(new Point(8,12));
list.add(new Point(9,12));
list.add(new Point(10,12));
list.add(new Point(11,12));
}
}
No comments:
Post a Comment