Snake game

How to make the greedy snake eat a food or get a certain integral faster?
Boss, solve

Welcome to the Markdown editor

Hello! This is the welcome page displayed by the Markdown editor for the first time. If you want to learn how to use the Markdown editor, you can read this article carefully to understand the basic grammar of Markdown.

New changes

We have expanded some functions and syntax support for the Markdown editor. In addition to the standard Markdown editor functions, we have added the following new functions to help you blog with it:

  1. The new interface design will bring a new writing experience;
  2. Set your favorite code highlight style in the creation center, and Markdown will display the selected highlight style of code slice display;
  3. The picture drag function is added. You can drag local pictures directly to the editing area for direct display;
  4. New * * KaTeXpackage snake;

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.IOException;
import java.io.InputStream;
import java.util.Random;
import javax.sound.sampled.*;

public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length,H;
int[] snakeX=new int[600];
int[] snakeY=new int [500];
String F="R";
boolean isStart=false;// Default game not started
boolean isFail=false;// The default game does not fail
int score;
Timer timer=new Timer(100,this);// Timer, refresh

//food
int food1X;
int food1Y;
Random random=new Random();//Random generation
//bomb
int dangerX;
int dangerY;
//Fruit that can reduce body length and score
int buffX;
int buffY;

Clip bgm;

//constructor 
public GamePanel()
{   init();
    this.setFocusable(true);
    this.addKeyListener(this);
    timer.start();
    loadBGM();
}
//initialization
public void init()
{
    length=3;
    snakeX[0]=100;snakeY[0]=100;//Initialize head coordinates
    snakeX[1]=75;snakeY[1]=100;
    snakeX[2]=50;snakeY[2]=100;
    food1X=25+25*random.nextInt(34);//Initialize food coordinates
    food1Y=75+25*random.nextInt(24);
    dangerX=25+25*random.nextInt(33);
    dangerY=75+25*random.nextInt(23);//Initialize bomb coordinates
    buffX=25+25*random.nextInt(32);
    buffY=75+25*random.nextInt(22);



}

//Drawing board
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    this.setBackground(Color.BLUE);


    Data.header.paintIcon(this,g,25,11);
    g.fillRect(25,75,850,600);
    Data.background.paintIcon(this,g,25,75);//Set background
    if(F.equals("R")) { Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);}
    else if(F.equals("L")){ Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);}
    else if(F.equals("U")){ Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);}
    else if(F.equals("D")){ Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);}//Draw head images in all directions
    for(int i=1;i<length;i++)
    {
        Data.body .paintIcon(this,g,snakeX[i],snakeY[i] );//Painting body
    }
    Data.danger.paintIcon(this,g,dangerX,dangerY);//Painting bomb
    Data.food.paintIcon(this,g,food1X,food1Y);//Draw food
    if(length>=10){
        Data.buff.paintIcon(this,g,buffX,buffY);}//Draw fruit that reduces body length

    g.setColor(Color.BLUE);//stroke color 
    g.setFont(new Font("Microsoft YaHei ",Font.BOLD,15));
    g.drawString("Highest record:"+H,770,25);
    g.drawString("length:"+length,770,40);
    g.drawString("integral:"+score,770,55);//scoreboard

    if(!isStart) {
        g.setColor(Color.WHITE);//stroke color 
        g.setFont(new Font("Microsoft YaHei ",Font.BOLD,40));
        g.drawString("Press the space to start the game",300,300);
        g.setFont(new Font("Microsoft YaHei ",Font.BOLD,30));
        g.drawString("Tips:Red, eat extra points, the body becomes longer, black, eat until the end of the game",100,400);
        g.drawString("When the body length is greater than 15, blue appears, plus points and minus length.",100,450);
        g.drawString("Have a nice game!",300,500);//Prompt information


    }
    if(isFail)
    {
        g.setColor(Color.RED);
        g.setFont(new Font("Microsoft YaHei ",Font.BOLD,40));
        g.drawString("The game failed. Press the space to start again",225,300);
    }

}




@Override
public void keyPressed(KeyEvent e) {
    int keyCode=e.getKeyCode();//Get the pressed keyboard information
    if(keyCode==KeyEvent.VK_SPACE){
        if(isFail){isFail=false;
            init();}//Restart;

        else{
            isStart=!isStart;}
        if(isStart) {
            playBGM();
        } else {
            stopBGM();
        }//Play and pause of background music
        repaint();//Refresh interface
    }
    if(keyCode==KeyEvent.VK_LEFT)
    {F=F.equals("R")?"R":"L";}
    else if(keyCode==KeyEvent.VK_RIGHT)
    {F=F.equals("L")?"L":"R";}
    else if(keyCode==KeyEvent.VK_UP)
    {F=F.equals("D")?"D":"U";}
    else if(keyCode==KeyEvent.VK_DOWN)
    {F=F.equals("U")?"U":"D";}//Prevent snakes from turning 180 degrees

}



@Override
public void actionPerformed(ActionEvent e) {
    if(isStart&&!isFail){

        for(int i=length-1;i>0;i--){
            snakeX[i]=snakeX[i-1];
            snakeY[i]=snakeY[i-1];
        }
        //Control head movement
        if(F.equals("R")){ snakeX[0]=snakeX[0]+25;if(snakeX[0]>850) {snakeX[0]=25;}
        }else if(F.equals("L")){snakeX[0]=snakeX[0]-25;if(snakeX[0]<25) {snakeX[0]=850;}
        } else if (F.equals("U")) {snakeY[0]=snakeY[0]-25;if(snakeY[0]<75) {snakeY[0]=650;}
        }else if(F.equals("D")){snakeY[0]=snakeY[0]+25;if(snakeY[0]>650){snakeY[0]=75;}}//Make the snake appear from another direction after running out of the interface
        if(snakeX[0]==food1X&&snakeY[0]==food1Y)
        {
            length++;
            score=score+5;
            food1X=25+25*random.nextInt(34);
            food1Y=75+25*random.nextInt(24);
            dangerX=25+25*random.nextInt(33);
            dangerY=75+25*random.nextInt(23);
            buffX=25+25*random.nextInt(32);
            buffY=75+25*random.nextInt(22);
        }//Reset coordinates after eating

        if(length>=10){

            if(snakeX[0]==buffX&&snakeY[0]==buffY)
            { length--;
                score=score+5;
                buffX=25+25*random.nextInt(32);
                buffY=75+25*random.nextInt(22);
                food1X=25+25*random.nextInt(34);
                food1Y=75+25*random.nextInt(24);
                dangerX=25+25*random.nextInt(33);
                dangerY=75+25*random.nextInt(23);
            }
        }


        if (snakeX[0] == dangerX && snakeY[0]==dangerY)
        {
            isFail=true;H=score;score=0;
        }//Eat until the bomb is over

        for(int i=1;i<length;i++){
            if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]) {
                isFail=true;H=score;score=0;break;
            }
        }//Touch the body, the game is over

        timer.start();

        repaint();//Refresh interface
    }

}
private void loadBGM() {
    try {
        bgm = AudioSystem.getClip();
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("statics/jay.wav");//Get the resource and convert it into byte stream
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);//Convert to audio
        bgm.open(ais);
    } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
        e.printStackTrace();
    }//Catch exception

}

private void playBGM() {
    bgm.loop(Clip.LOOP_CONTINUOUSLY);
}//Start and cycle

private void stopBGM() { bgm.stop(); }//Pause playback



@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }

}

Mathematical formula * * syntax;
5. Added mermaid syntax supporting Gantt chart 1 Function;
6. The function of multi screen editing Markdown article is added;
7. Focus writing mode, preview mode, concise writing mode, left and right area synchronization wheel setting and other functions are added. The function button is located in the middle of the editing area and preview area;
8. Check list function is added.

Function shortcut

Undo: Ctrl/Command + Z
Redo: Ctrl/Command + Y
Bold: Ctrl/Command + B
Italic: Ctrl/Command + I
Title: Ctrl/Command + Shift + H
Unordered list: Ctrl/Command + Shift + U
Ordered list: Ctrl/Command + Shift + O
Checklist: Ctrl/Command + Shift + C
Insert code: Ctrl/Command + Shift + K
Insert link: Ctrl/Command + Shift + L
Insert picture: Ctrl/Command + Shift + G
Find: Ctrl/Command + F
Replace: Ctrl/Command + G

Creating a reasonable title is helpful to the generation of the directory

Directly input once #, and press space to generate level 1 title.
After entering twice #, and pressing space, a level 2 title will be generated.
By analogy, we support level 6 titles. It helps to generate a perfect directory after using TOC syntax.

How to change the style of text

Emphasize text emphasize text

Bold text bold text

Tag text

Delete text

Reference text

H2O is a liquid.

210 the result is 1024

Insert links and pictures

Link: link.

Picture:

Pictures with dimensions:

Centered picture:

Centered and sized picture:

Of course, in order to make users more convenient, we have added the image drag function.

How to insert a beautiful piece of code

go Blog settings Page, select a code slice highlighting style you like, and the same highlighted code slice is shown below

// An highlighted block
var foo = 'bar';

Generate a list that suits you

  • project
    • project
      • project
  1. Item 1
  2. Item 2
  3. Item 3
  • Planning tasks
  • Complete the task

Create a table

A simple table is created as follows:

projectValue
computer$1600
mobile phone$12
catheter$1

The setting content is centered, left and right

Use: ---------: Center
Use: --------- left
Usage -----------: right

First columnSecond columnThird column
First column text centeredThe text in the second column is on the rightThe text in the third column is left

SmartyPants

SmartyPants converts ASCII punctuation characters to "smart" printed punctuation HTML entities. For example:

TYPEASCIIHTML
Single backticks'Isn't this fun?''Isn't this fun?'
Quotes"Isn't this fun?""Isn't this fun?"
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash

Create a custom list

Markdown Text-to- HTML conversion tool Authors John Luke

How to create a footnote

A text with footnotes. 2

Annotations are also essential

Markdown converts text to HTML.

KaTeX mathematical formula

You can render LaTeX mathematical expressions using KaTeX:

Gamma formula display Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ (n)=(n−1)! ∀ N ∈ N is through Euler integral

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞​tz−1e−tdt.

You can find more information about LaTeX mathematical expressions here.

New Gantt chart features to enrich your articles

  • For Gantt chart syntax, refer to here,

UML diagram

UML diagrams can be used for rendering. Mermaid For example, a sequence diagram is generated as follows:

This will produce a flowchart.:

  • For Mermaid syntax, see here,

FLowchart

We will still support the flowchart of flowchart:

  • For Flowchart syntax, refer to here.

Export and import

export

If you want to try this editor, you can edit it at will in this article. When you have finished writing an article, find the article export in the upper toolbar to generate an article md file or html file for local saving.

Import

If you want to load an article you wrote md file. In the upper toolbar, you can select the import function to import the file with the corresponding extension,
Continue your creation.

  1. mermaid syntax description ↩︎

  2. Explanation of footnotes ↩︎

Keywords: Java intellij-idea

Added by waterox on Fri, 21 Jan 2022 10:04:30 +0200