java编写贪吃蛇小游戏

时间:2022-02-02 15:26:04

废话不多说,直接奉上代码:

Frame.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package snake;
 
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
import javax.swing.JFrame;
 
public class Frame extends JFrame implements KeyListener {
 
  /**
   *
   */
   
  Boolean isAlive;
  Boolean isPause;
  Panel panel;
  Character direction;
  private static final long serialVersionUID = 1L;
 
  public Frame(){
    // TODO Auto-generated constructor stub
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,300);
    addKeyListener(this);
    panel = new Panel();
    add(panel);
    setVisible(true);
    isAlive = true;
    isPause = false;
    direction = new Character('d');
    MenuBar menuBar = new MenuBar();
    Menu menu = new Menu("menu");
    MenuItem reset = new MenuItem("newgame");
    MenuItem pause= new MenuItem("pause");
     
    pause.addActionListener(new ActionListener(){
 
      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(!isPause) isPause= true;
        else     isPause= false;
      }
       
    });
     
    reset.addActionListener(new ActionListener(){
 
      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        reset();
      }
       
    });
     
    menu.add(reset);
    menu.add(pause);
    menuBar.add(menu);
    setMenuBar(menuBar);
     
     
  }
   
  public void reset(){
    panel.reset();
    isAlive = true;
  }
 
  @Override
  public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
     
  }
 
  @Override
  public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode()==KeyEvent.VK_UP)  direction = 'w';
    if(e.getKeyCode()==KeyEvent.VK_DOWN) direction = 's';
    if(e.getKeyCode()==KeyEvent.VK_LEFT) direction = 'a';
    if(e.getKeyCode()==KeyEvent.VK_RIGHT) direction = 'd';
  }
 
  @Override
  public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
     
  }
   
  public void paint(Graphics g){
    panel.repaint();
  }
 
}

Launch.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package snake;
 
import java.util.Timer;
import java.util.TimerTask;
 
public class Launch extends TimerTask {
 
  Frame frame = new Frame();
  public Launch() {
    // TODO Auto-generated constructor stub
     
  }
   
  boolean crashWall(){
    SnakeBody sb = frame.panel.snake.getFirst();
    if((sb.x<0)||(sb.y<0)||(sb.x>=Panel.LINE)||(sb.y>=Panel.LINE))
      return true;
    else
      return false;
  }
  void initial(){
    frame.panel.snake.add(newBody());
    frame.panel.food = newBody();
  }
  @Override
  public void run() {
    // TODO Auto-generated method stub
    if(frame.panel.snake.isEmpty())
      initial();
    if(frame.isAlive)
      if(!frame.isPause){
        if(goStraight())
           frame.isAlive = false;
        frame.repaint();
      }
    if(crashWall()) frame.isAlive = false;
  }
   
  SnakeBody newBody(){
    SnakeBody sb = new SnakeBody();
 
    boolean overlap = true;
    while(overlap){
      overlap =false;
      sb.x = (int) (Math.random()*(Panel.LINE-2)+1);
      sb.y = (int) (Math.random()*(Panel.LINE-2)+1);
      if(!frame.panel.snake.isEmpty())
      for(SnakeBody s : frame.panel.snake)
        if(sb.equals(s))
          overlap =true;
    }
     
    return sb;
  }
 
   
  void eat(SnakeBody sb){
    frame.panel.snake.addFirst(sb);
  }
  boolean goStraight(){
     
    boolean result = false;
     
    SnakeBody sb =new SnakeBody(frame.panel.snake.getFirst());
    frame.panel.snake.removeLast();
    if(frame.direction=='w')
      sb.turnUp();
    if(frame.direction=='s')
      sb.turnDown();
    if(frame.direction=='a')
      sb.turnLeft();
    if(frame.direction=='d')
      sb.turnRight();
     
    for(SnakeBody s : frame.panel.snake){
      if(sb.equals(s)) result = true;
    }
     
    frame.panel.snake.addFirst(sb);
    if(sb.equals(frame.panel.food)){
      if(frame.direction=='w')
        frame.panel.food.turnUp();
      if(frame.direction=='s')
        frame.panel.food.turnDown();
      if(frame.direction=='a')
        frame.panel.food.turnLeft();
      if(frame.direction=='d')
        frame.panel.food.turnRight();
      eat(frame.panel.food);
      frame.panel.food = newBody();
    }
     
    return result;
  }
 
   
   
  public static void main(String[] args){
    // TODO Auto-generated method stub
     Launch timertask = new Launch();
     Timer timer = new Timer();
     
     timer.schedule(timertask,0,500);
  }
 
}

Panel.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package snake;
 
import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
 
import javax.swing.JPanel;
 
 public class Panel extends JPanel {
 
    /**
   *
   */
  private static final long serialVersionUID = 1L;
  public LinkedList<SnakeBody> snake = new LinkedList<SnakeBody>();
  static final int LINE = 10;
  SnakeBody food = new SnakeBody(-99,-99);
    
  public Panel() {
    // TODO Auto-generated constructor stub
  }
   
  public void reset(){
    snake.clear(); 
  }
   
  public void paint(Graphics g){
    g.setColor(Color.white);
    g.fillRect(0, 0, getWidth(), getHeight());
 
  
    for(SnakeBody sb : snake){
      g.setColor(Color.black);
      g.drawRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
      g.setColor(Color.orange);
      g.fillRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
    }
    g.setColor(Color.red);
    g.fillRect(food.x*getWidth()/LINE,food.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
     
  }
}

SnakeBody.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package snake;
 
 class SnakeBody {
   
  int x;
  int y;
   
  public SnakeBody() {
    // TODO Auto-generated constructor stub
    x = 0;
    y = 0;
  }
   
  public SnakeBody(int a,int b){
    x = a;
    y = b;
  }
  public SnakeBody(SnakeBody sb){
    this(sb.x,sb.y);
  }
   
  public void turnUp(){
    y--;
  }
  public void turnDown(){
    y++;
  }
  public void turnLeft(){
    x--;
  }
  public void turnRight(){
    x++;
  }
   
  boolean equals(SnakeBody s){
    if((x==s.x)&&(y==s.y)) return true;
    else          return false;
  }
 
}

以上所述就是本文给大家分享的贪吃蛇的全部代码了,希望能够对大家熟练掌握java有所帮助。