Home > Back-end >  For bosses to help fix the bug
For bosses to help fix the bug

Time:09-21

I was a little white, wrote a snake game, found that there is a bug, the code will be posted below, the main code is online looking for only a part of the wormhole is written by myself

CodePudding user response:

Package the snake;

The import javax.mail. Swing. JFrame;

Public class Snake extends JFrame {
//create a game interface framework
JFrame JFrame=new JFrame (snake "premium");
Static int I;
Public Snake () {
//set the game interface
,10,1080,600 jframe. SetBounds (10);
Jframe. SetTitle (" snake ");
Jframe. SetResizable (false);//set the interface size can't change the
Jframe. SetDefaultCloseOperation (jframe. EXIT_ON_CLOSE);//set the close button to close the interface
SnakePanel panel=new SnakePanel ();
Jframe. Add (panel);//add the canvas
Jframe. SetVisible (true);
}
Public static void main (String [] args) {
The new Snake ();
}


}

CodePudding user response:

Package the snake;

Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. The awt. Event. KeyEvent;
Import the Java. The awt. Event. KeyListener;
Import the Java. The awt. Graphics;
Import the Java. The awt. The Font;
Import the Java. The awt. Color;
Import the Java. Util. Random;

The import javax.mail. Swing. ImageIcon;
The import javax.mail. Swing. JPanel;
The import javax.mail. Swing. The Timer;

Public class SnakePanel extends JPanel implements ActionListener, KeyListener {
//seven pictures variables, represents the seven picture
ImageIcon up=new ImageIcon (" D: \ \ \ \ PIC up. PNG ");//up the snake
ImageIcon down=new ImageIcon (" D: \ \ \ \ PIC to the PNG ");//down the snake
ImageIcon left=new ImageIcon (" D: \ \ PIC \ \ left PNG ");//to the left of the head of the snake
ImageIcon right=new ImageIcon (" D: \ \ \ \ PIC right. PNG ");//to the right of the snake
ImageIcon food=new ImageIcon (" D: \ \ PIC \ \ food PNG ");//food
ImageIcon body=new ImageIcon (" D: \ \ PIC \ \ body PNG ");//the snake's body
ImageIcon hole=new ImageIcon (" D: \ \ \ \ PIC wormhole. PNG ");//the wormhole

//snake every part of the
Int [] snakex=new int [750].
Int [] snakey=new int [750].

//random food
The Random rand=new Random ();
Int foodx=rand. NextInt (40) * 25 + 25;//this value according to the size of the design of the game interface to determine
Int foody=rand. NextInt (16) * 25 + 75;

//set the default properties of the game
Int len=3;
Int score=0;
String direction="R";//U D L left R right under the

Boolean isStarted=false;//whether the game start
Boolean isFailed=false;//whether the game end

Int [] x={150200400550830880};//worm hole in the x coordinate
250150100450,50,300 int [] y={};//worm hole in the x coordinate

//set the timer
Int t=1;
The Timer Timer=new Timer (180/t, this);//once every 180/I milliseconds call ActionPerform


Public SnakePanel () {//build the constructor of the canvas
Enclosing setFocusable (true);//get focus
Enclosing addKeyListener (this);//listen to keyboard events
The setup ();
The timer. The start ();
}

Public void paint (Graphics g) {//Graphics brush

Enclosing setBackground (Color BLACK);//set the background color of the canvas

G.f illRect (25, 25103, 500);//use brush set game box

//to draw the snake (note that determine the direction of the head of the snake)
If (direction) equals (" R "))
Right. PaintIcon (this, g, snakex [0], snakey [0]).
Else if (direction) equals (" L "))
Left. PaintIcon (this, g, snakex [0], snakey [0]).
Else if (direction) equals (" U "))
Up. PaintIcon (this, g, snakex [0], snakey [0]).
Else if (direction) equals (" D "))
The paintIcon (this, g, snakex [0], snakey [0]).

//draw the snake's body
for(int i=1; I & lt; len; I + +)
Body. The paintIcon (this, g, snakex [I], snakey [I]);

//show the wormhole
for(int i=0; i<6; I++)
Hole. PaintIcon (this, g, x [I], [I] y);

//judge if the game didn't begin to show,,,
if (! IsStarted) {
G.s etColor (Color. WHITE);
G.s etFont (new Font (" arial ", the Font, BOLD, 30));
G.d rawString (" Press Space to start/pause ", 200, 300);
}

//display if the game is over,,,
If (isFailed) {
G.s etColor (Color. WHITE);
G.s etFont (new Font (" arial ", the Font, BOLD, 30));
G.d rawString (" Game Over! Press space to restart ", 200, 300);
}

//show the food
Food. PaintIcon (this, g, foodx foody);



//set points and the length of the snake
G.s etColor (Color. WHITE);
G.s etFont (new Font (" arial ", the Font. PLAIN, 15));
G.d rawString (Score: "" + Score, 650, 37).
G.d rawString (" Len: "+ Len, 650, 57).
}
Public void setup () {//game initialization
IsStarted=false;
IsFailed=false;
Len=3;
score=0;
Snakex [0]=100; Snakex [1]=75; Snakex [2]=50;
Snakey [0]=100; Snakey [1]=100; Snakey [2]=100;
}

@ Override
Public void keyPressed KeyEvent (e) {

//keyboard response
Int KeyCode=um participant etKeyCode ();
If (KeyCode==KeyEvent. VK_SPACE) {//percussion space reality/eliminate tooltip
If (isFailed) {
IsStarted=false;//this two lines in the setup can be
IsFailed=false;
The setup ();
} the else
IsStarted=! IsStarted;
} else if (KeyCode==KeyEvent. VK_UP & amp; & Direction!="D")
Direction="U";
Else if (KeyCode==KeyEvent. VK_DOWN & amp; & Direction!="U")
Direction="D";
Else if (KeyCode==KeyEvent. VK_RIGHT & amp; & Direction!="L")
Direction="R";
Else if (KeyCode==KeyEvent. VK_LEFT & amp; & Direction!="R")
Direction="L";
}

@ Override
Public void actionPerformed (an ActionEvent e) {
//1. To define an alarm clock
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related