gogoWebsite

Java implementation of the Snake Battle mini-game (full version)

Updated to 6 months ago

Hello everyone, today try to useswing technologyWrite a Snake Battle mini-game for your reference.

Follow the public number "stale orange and green (idiom); green with age", reply to "Snake" to get the full source code!


 Effective demonstration


catalogs

Effective demonstration

I. Game Interface

II. Scoring

Projects

Project Background

total requirement

implementation process

Code Showcase

Main class : Demo class

 MyPanel class

 ①Construction method

 ②Initialization method

 ③Drawing method

 ④Timer execution method

Framework Ui Classes

Project structure

summarize



Effective demonstration

I. Game Interface

II. Scoring


Projects

Project Background

The game "Gluttony Battle" is a classic game, it is easy to operate, entertaining, since the computer to realize, by the majority of computer players love, this project is based on Java technology, developed a Simple operation, beautiful interface, more complete functionsThe "Snake" game. Through the development of this game, theLearn Java Technologycap (a poem)Familiar with software development processThe purpose of the

total requirement

 This system is mainly to complete the basic operation of the game of Snake, so that users can practice and entertain themselves. The following requirements need to be met.

(1) Use the arrow keys to change the running direction of the snake.
(2) The spacebar pauses or resumes the game and spawns food in random places.
(3) When you eat food, you turn into a new snake body (the length of the snake becomes longer and the game continues).

implementation process

(1) Draw the form object.
(2) Static UI design (including snakes, food, game area and title area).
(3) Use keyboard listen event and timer to realize the movement of the snake.
(4) Realization of a small snake colliding with food.
(5) Define the variable to store the length of the snake, and traverse the array to realize the function of increasing the body of the snake.
(6) Exit condition: When the game points reach the specified score, the game is exited.


Code Showcase

Main class : Demo class

package Demo;
/**
 * 1. Draw the form object
 *
 *
 * @author Qing00-Java
 /* @author Qing00-Java
 */
public class Demo0 {

}

MyPanel class

constructor method

package Demo;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
//canvas class
public class MyPanel extends JPanel implements KeyListener,ActionListener{//predecessor
	private static final Component This = null;
//Keyboard Listening Interactive Interface
int length;
int score;
int[] snakeX=new int[500];//coordinate (geometry)
int[] snakeY=new int[500];
String fx;
boolean ifStart=false;
//timers
Timer timer =new Timer(100,this);
int foodx;
int foody;
Random random =new Random();

public MyPanel(){
	init();
	//Adding a Listener Event
	(true);
	(this);
	score=0;
}

initialization method

public void init(){
	length=3;
	fx="r";
	//Initialize the position of the snake
	snakeX[0]=100;
	snakeY[0]=150;
	snakeX[1]=75;
	snakeY[1]=150;
	snakeX[2]=50;
	snakeY[2]=150;
	foodx=25+25*(57);
	foody=125+25*(27);
	(foodx);
	(foody);
	();
}

Drawing method

	@Override
	 protected void paintComponent(Graphics g){//Brush Objects
		 (g);
		 //Drawing the title at the top
		 new ImageIcon("C:/Users/Desktop/img/").paintIcon(this,g,25,11);//create an object
		 //Drawing the play area
		 (25, 125, 1450, 700);
		 //Drawing a Snake
		 //head of a snake
		 new ImageIcon("C:/Users/Desktop/img/").paintIcon(this, g, snakeX[0], snakeY[0]);
			 
		 //snake body
		 for(int i=1;i<length;i++){
			 new ImageIcon("C:/Users/Desktop/img/").paintIcon(This, g, snakeX[i], snakeY[i]);
		 }
		 //Tips for the game
		 if(ifStart==false){
			 ();
			 (new Font("Microsoft Black and White (computer science)",,40));
			 ("Press the space bar to continue the game", 550,500);
			 ("Endless Mode", 650,400);
		 }
		 //Draw the location of the food
		 new ImageIcon("C:/Users/Desktop/img/").paintIcon(this, g, foodx, foody);
	}
	
	@Override//push (a button)
	public void keyPressed(KeyEvent e) {
		int keyCode=();
		if(keyCode==KeyEvent.VK_SPACE){
			ifStart=!ifStart;
		}
		if(ifStart==true){
			if(keyCode==KeyEvent.VK_LEFT&&fx!="r"){
				fx="l";
			}else if(keyCode==KeyEvent.VK_RIGHT&&fx!="l"){
				fx="r";
			}else if(keyCode==KeyEvent.VK_UP&&fx!="d"){
				fx="u";
			}else if(keyCode==KeyEvent.VK_DOWN&&fx!="u"){
				fx="d";
			}
	}
		repaint();
}

Timer execution method

@Override
	public void actionPerformed(ActionEvent e) {
		//Changing the position of the snake
		if(ifStart==true){
			for(int i=length-1;i>0;i--){
				snakeX[i]=snakeX[i-1];
				snakeY[i]=snakeY[i-1];
			}
			if(("l")){
				snakeX[0]=snakeX[0]-25;
			    if(snakeX[0]<25){
					snakeX[0]=1450;
				}
			}else if(("r")){
				snakeX[0]=snakeX[0]+25;
				if(snakeX[0]>1450){
					snakeX[0]=25;
				}
			}else if(("u")){
				snakeY[0]=snakeY[0]-25;
				if(snakeY[0]<120){
					snakeY[0]=800;
				}
			}else if(("d")){
				snakeY[0]=snakeY[0]+25;
				if(snakeY[0]>800){
					snakeY[0]=120;
				}
			}
			//Determining if you've eaten food
			if(snakeX[0]==foodx&&snakeY[0]==foody){
				length++;
				score++;
				if(score==100){
					ifStart=!ifStart;
				}
				foodx=25+25*(57);
				foody=125+25*(27);
				("Current food coordinates("+foodx+",");
				(foody+")");
				("|||current score:"+score+" ");
			}
			repaint();
		}
		();
	}
	
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

Framework Ui Classes

package Demo;

import ;

public class Ui {

	public static void main(String[] args){
		JFrame frame=new JFrame("Endless version of Snake");
		(1500, 900);
		(null);//center
		(JFrame.EXIT_ON_CLOSE);
		(true);
		(new MyPanel());
		(true);
	}

}

Project structure

This program is used for beginners to learn and encapsulates three classes, namelyDemo class, MyPanel class, Framework Ui classThe structure is simple and all methods are inMyPanelclass is implemented.


summarize

The design of this game is similar to the game "Snake Battle", the program in some of the functions of the implementation of many deficiencies, but the realization of the project on the Java novice has a great ability to enhance the actual combat.

Many problems were encountered while writing this project in Java, and in solving the problems, a deeper understanding and knowledge of object-oriented programming in java can be gained.


Pictures used: