Java实现简单版贪吃蛇游戏

时间:2022-06-02 04:03:56

本文实例为大家分享了Java实现简单版贪吃蛇游戏的具体代码,供大家参考,具体内容如下

这是一个比较简洁的小游戏,主要有三个类,一个主类,一个食物类,一个贪吃蛇类。

1、首先定义主类,主类中主要用来创建窗口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Main {
 
 public static final int WIDTH=600;
 public static final int HEIGHT=600;
 public static void main(String[] args) {
  JFrame win =new JFrame();
  win.setVisible(true);
  win.setSize(WIDTH, HEIGHT);
  win.setDefaultCloseOperation(3);
  win.setLocationRelativeTo(null);
  
  SnakePanel panle =new SnakePanel();
  win.add(panle);
  SnakePanel.Key l = panle.new Key();
  win.addKeyListener(l);
  panle.addKeyListener(l);
  panle.run();
 }
 
}

2、其次是定义食物类,食物有长和宽,还有在窗口中的位置

?
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
import java.util.Random;
public class Cell {
 protected int x;
 protected int y;
 protected int width;
 protected int height;
 Random ran=new Random();
 public Cell(){
  Random ran=new Random();
  this.x=ran.nextInt(25)*15+60;
  this.y=ran.nextInt(25)*15+50;
  this.width=15;
  this.height=15;
 }
 public Cell(int x,int y){
  this();
  this.x=x;
  this.y=y;
 }
 public int getX() {
  return x;
 }
 public void setX(int x) {
  this.x = x;
 }
 public int getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }
 public int getWidth() {
  return width;
 }
 public void setWidth(int width) {
  this.width = width;
 }
 public int getHeight() {
  return height;
 }
 public void setHeight(int height) {
  this.height = height;
 }
}

3、最后是蛇类

?
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
107
108
109
110
111
112
113
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;
public class SnakePanel extends JPanel {
 final int RIGHT=1;
 final int LEFT=2;
 final int UP=3;
 final int DOWN=4;
 int moved=1;
 Cell food;
 Cell[] snake;
 public SnakePanel(){
  food=new Cell();
  snake=new Cell[5];
  for(int i=0;i<snake.length;i++){
   snake[i]=new Cell(210+i*15,50);
  }
 }
 class Key extends KeyAdapter{
  @Override
  public void keyPressed(KeyEvent e){
   int code=e.getKeyCode();
   //System.out.println(code);
   if(code==37&&moved!=RIGHT){
    moved=LEFT;
   }
   if(code==39&&moved!=LEFT){
    moved=RIGHT;
   }
   if(code==38&&moved!=DOWN){
    moved=UP;
   }
   if(code==40&&moved!=UP){
    moved=DOWN;
   }
  }
 }
 public void paint(Graphics g){
  super.paint(g);
  g.setFont(new Font(Font.SERIF,Font.BOLD,28));
  g.drawRect(60, 50, 450, 450);
  for(int i=15;i<=450;i+=15){
   g.drawLine(60+i, 50, 60+i, 500);
  }
  for(int i=15;i<=450;i+=15){
   g.drawLine(60, 50+i, 510, 50+i);
  }
  g.setColor(Color.BLUE);
  g.drawString("欢迎来到贪吃蛇大战", 140, 40);
  g.fillRect(food.x, food.y, food.width, food.height);
  for(int i=0;i<snake.length;i++){
   g.fillRect(snake[i].x, snake[i].y, snake[i].width, snake[i].height);
  }
 }
 int speed = 250;
 public void run(){
  Timer timer=new Timer();
  TimerTask task=new TimerTask(){
   @Override
   public void run() {
    step();
    repaint();
   }
  };
  if(snake.length>=10){
   speed=125;
  }else if(snake.length>=20){
   speed=60;
  }else if(snake.length>=30){
   speed=30;
  }else if(snake.length>=40){
   speed=15;                                            
  }
  timer.schedule(task, 1000, speed);
 }
 public void step(){
  for(int i=1;i<snake.length;i++){
   snake[i-1] = snake[i];
  }
  Cell c = new Cell(snake[snake.length-1].getX(),snake[snake.length-1].getY());
  if(moved == RIGHT){
   c.setX(c.getX()+15);
  }
  if(moved == UP){
   c.setY(c.getY()-15);
  }
  if(moved == DOWN){
   c.setY(c.getY()+15);
  }
  if(moved == LEFT){
   c.setX(c.getX()-15);
  }
  snake[snake.length-1] = c;
  //System.out.println(c.getX()+","+c.getY());
  if(snake[snake.length-1].getX()>510||
    snake[snake.length-1].getX()<60||
    snake[snake.length-1].getY()>500||
    snake[snake.length-1].getY()<50){
     System.exit(0);
    }
  if(snake[snake.length-1].x==food.x &&snake[snake.length-1].y==food.y){
   snake=Arrays.copyOf(snake,snake.length+1);
   snake[snake.length-1]=food;
   food=new Cell();
  }
 }
}

至此,一个简单的贪吃蛇小游戏就完成了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/YMH520ZL/article/details/120458888