<> one . Engineering documents

<> two .Main.java
package com.company; import javax.swing.*; public class Main { public static
void main(String[] args) { // Create a form object JFrame frame = new JFrame(); // Create form parameters ()
frame.setBounds(10,10,900,720); // Setting does not allow size changes //frame.setResizable(false);
// Set closing mode frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add topic logical framework frame.
add(new Mpanel()); // Set form visible frame.setVisible(true); } }
<> three .Mpanel.java
package com.company; import javax.imageio.ImageIO; import javax.sound.sampled.*
; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import
java.awt.event.KeyListener; import java.io.BufferedInputStream; import java.io.
IOException; import java.io.InputStream; import java.util.Random;
// Basic Routine , inherit JPanel, realization KeyListener, ActionListener Interface public class Mpanel extends
JPanel implements KeyListener, ActionListener { // Define the required picture elements // title ImageIcon
title; // Body parts of snakes ImageIcon body; // Snake's head up ImageIcon up; // Snake's head down ImageIcon down
; // Snake's head to the left ImageIcon left; // Snake's head to the right ImageIcon right; // Food elements ImageIcon food;
// Define initial length int len = 3; // Define initial score int score = 0; // An array for storing the position coordinates of snakes int[] snakex = new
int[750]; int[] snakey = new int[750]; // Define direction String fx = "R"; // Define whether the game starts or not
Boolean isStart= false; // Define whether the game fails or not Boolean isFailed = false;
// Defining the game clock , Here you can set the speed of the snake , default 100ms Timer timer = new Timer(100,this); // Define the coordinates of the food int
foodx; int foody; // Introducing random numbers , Random positions used to generate food Random random = new Random(); // Defining background music
Clip bgm; // Construction method public Mpanel(){ // Loading picture elements loadImages(); // Initialization data initSnake();
// Set focus this.setFocusable(true); // Set keyboard monitoring this.addKeyListener(this); // Time starts timer
.start(); // Loading background music loadBGM(); } // Set drawing elements public void paintComponent(Graphics g){
// Call parent class method super.paintComponent(g); // Set the background color to white this.setBackground(Color.white);
// Set title element location title.paintIcon(this,g,25,11); // Set white background fill position g.fillRect(25,75,850,600)
; // Set the brush color to white g.setColor(Color.white); // Set length display g.drawString("Len: " + len,750,
35); // Set score display g.drawString("Score: " + score,750,50); // Call the corresponding snakehead element according to the direction if(fx ==
"R"){ right.paintIcon(this,g,snakex[0],snakey[0]); } else if(fx == "L"){ left.
paintIcon(this,g,snakex[0],snakey[0]); } else if(fx == "U"){ up.paintIcon(this,g
,snakex[0],snakey[0]); } else if(fx == "D"){ down.paintIcon(this,g,snakex[0],
snakey[0]); } for(int i=1;i<len;i++){ body.paintIcon(this,g,snakex[i],snakey[i])
; } // Call food element food.paintIcon(this,g,foodx,foody); // How to deal with the beginning of the game if(isStart == false){
// Set brush color g.setColor(Color.white); // Set font g.setFont(new Font("arial",Font.BOLD,40
)); // Set specific text g.drawString("Press Space To Start",250,300); } // How to deal with the game failure if(
isFailed){ // Set brush color g.setColor(Color.red); // Set font g.setFont(new Font("arial",
Font.BOLD,40)); // Set specific text g.drawString("Failed: Press Space To Start",250,300); }
} // Initialization data public void initSnake(){ len = 3; snakex[0] = 100; snakey[0] = 100;
snakex[1] = 75; snakey[1] = 100; snakex[2] = 50; snakey[2] = 100;
// Set the initial randomly generated position of the food foodx = 25 + 25 * random.nextInt(34); foody = 75 + 25 * random.
nextInt(24); // Set initial value fx = "R"; score = 0; } // Implementation of interface requirements @Override public void
keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { // Get keyboard monitor
int keyCode = e.getKeyCode(); // Space if(keyCode == KeyEvent.VK_SPACE){ // If it fails, reset the information
if(isFailed){ isFailed = !isFailed; // restart initSnake(); } // Set start else { isStart
= !isStart; } // After judging the start, loop back the background music if(isStart){ bgm.loop(Clip.LOOP_CONTINUOUSLY); }
// No background music will be played before start and pause else { bgm.stop(); } // Redraw repaint(); } // Right click processing and set not to go back else if(
keyCode== KeyEvent.VK_LEFT && fx != "R"){ fx = "L"; } // Left key else if(keyCode ==
KeyEvent.VK_RIGHT && fx != "L"){ fx = "R"; } // Up key else if(keyCode == KeyEvent.
VK_UP&& fx != "D"){ fx = "U"; } // Next key else if(keyCode == KeyEvent.VK_DOWN && fx
!= "U"){ fx = "D"; } } // Implementation of interface requirements @Override public void keyReleased(KeyEvent e)
{ } // event listeners @Override public void actionPerformed(ActionEvent e) { // Start without failure
if(isStart && !isFailed){ // Move the snake in a circle for(int i=len-1;i>0;i--){ snakex[i]=snakex[i-
1]; snakey[i]=snakey[i-1]; } // Move to the right and judge if it hit the wall , Hitting the wall is a failure if(fx == "R"){ snakex[0]=snakex
[0]+25; if(snakex[0]>825)isFailed=true; } // Move to the left and judge if it hit the wall , Hitting the wall is a failure else if(fx == "L")
{ snakex[0]=snakex[0]-25; if(snakex[0]<50)isFailed=true; } // Move up and judge if it hit the wall , Hitting the wall is a failure
else if(fx == "U"){ snakey[0]=snakey[0]-25; if(snakey[0]<100)isFailed=true; }
// Move down and judge if it hit the wall , Hitting the wall is a failure else if(fx == "D"){ snakey[0]=snakey[0]+25; if(snakey[0]>625
)isFailed=true; } // The judgment of eating food if(snakex[0]==foodx&&snakey[0]==foody){
// Body lengthening , More points , And randomly generate new foods len++; score += 10; foodx = 25 + 25 * random.nextInt(34);
foody= 75 + 25 * random.nextInt(24); } // How to deal with the collision for(int i=1;i<len;i++){ if(
snakex[i]==snakex[0] && snakey[i]==snakey[0]){ isFailed = true; } } // Redraw
repaint(); } // Event refresh timer.start(); } // Loading background music public void loadBGM(){ try {
// Import from the file where the class is located bgm = AudioSystem.getClip(); InputStream is = this.getClass().
getClassLoader().getResourceAsStream("com/company/sound/bgm.wav");
AudioInputStream ais= AudioSystem.getAudioInputStream(new BufferedInputStream(is
)); bgm.open(ais); //bgm.loop(Clip.LOOP_CONTINUOUSLY); } // Corresponding exception handling catch (
LineUnavailableException e) { e.printStackTrace(); } catch (
UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e)
{ e.printStackTrace(); } } // Load picture public void loadImages(){ InputStream is; try
{ // Add response elements in turn is = getClass().getClassLoader().getResourceAsStream(
"com/company/img/title.jpg"); title = new ImageIcon(ImageIO.read(is)); is =
getClass().getClassLoader().getResourceAsStream("com/company/img/body.png");
body= new ImageIcon(ImageIO.read(is)); is = getClass().getClassLoader().
getResourceAsStream("com/company/img/up.png"); up = new ImageIcon(ImageIO.read(
is)); is = getClass().getClassLoader().getResourceAsStream(
"com/company/img/down.png"); down = new ImageIcon(ImageIO.read(is)); is =
getClass().getClassLoader().getResourceAsStream("com/company/img/left.png");
left= new ImageIcon(ImageIO.read(is)); is = getClass().getClassLoader().
getResourceAsStream("com/company/img/right.png"); right = new ImageIcon(ImageIO.
read(is)); is = getClass().getClassLoader().getResourceAsStream(
"com/company/img/food.png"); food = new ImageIcon(ImageIO.read(is)); } catch (
IOException e) { e.printStackTrace(); } } }
<> four . effect

Technology