贪吃蛇(JAVA_GUI)
JAVA-GUI编程入门,一小时开发贪吃蛇游戏。
开发环境:JDK1.8.0_131、IEDA
用到的知识点:JFrame、JPanel、KeyListener、ActionListener、Graphics、ImageIcon、Timer、数组
基本功能:按下空格键开始游戏,小蛇吃一个食物加一分,碰到边界或者自己身体游戏结束,再按下空格键继续游戏。
素材来自:https://www.iconfont.cn
游戏概况
1.新建JAVA项目
2.src目录下新建statics文件夹,存放素材文件
3.话不多说,直接上代码
3.1新建启动类StartGame.java
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("贪吃蛇");
frame.setBounds(100, 100, 500, 432);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
因为我的图片素材为20×20,所以fram的大小我定义为500×432。
3.2新建素材类Data.java
包含小蛇的头、蛇体、食物。
public class Data {
// public static URL headerURL = Data.class.getResource(“statics/header.png”);
// public static ImageIcon header = new ImageIcon(headerURL);
public static URL upURL = Data.class.getResource("statics/up.png");
public static URL downURL = Data.class.getResource("statics/down.png");
public static URL leftURL = Data.class.getResource("statics/left.png");
public static URL rightURL = Data.class.getResource("statics/right.png");
public static ImageIcon up = new ImageIcon(upURL);
public static ImageIcon down = new ImageIcon(downURL);
public static ImageIcon left = new ImageIcon(leftURL);
public static ImageIcon right = new ImageIcon(rightURL);
public static URL bodyURL = Data.class.getResource("statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Data.class.getResource("statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
}
3.3新建小蛇控制类GamePanel.java
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX = new int[200];
int[] snakeY = new int[200];
String fx;
boolean isStart = false;
boolean isFail = false;
int foodX,foodY;
Random random = new Random();
int score;
Timer timer = new Timer(100, this);
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
this.timer.start();
}
public void init() {
fx = "R";
score = 0;
length = 3;
snakeX[0] = 40;snakeY[0] = 50;
snakeX[1] = 20;snakeY[1] = 50;
snakeX[2] = 0;snakeY[2] = 50;
foodX = 0 + 20*random.nextInt(10);
foodY = 50 + 20*random.nextInt(10);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
g.setColor(Color.decode("#DCDCDC"));
g.fillRect(0,50, 500, 360);
g.setColor(Color.white);
for (int i = 1; i < 18; i++) {
g.drawLine(0,50+i*20,525,50+i*20);
}
for (int i = 1; i < 25; i++) {
g.drawLine(i*20,50,i*20,410);
}
Data.food.paintIcon(this,g,foodX,foodY);
if (fx.equals("R")) {
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (fx.equals("L")) {
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (fx.equals("U")) {
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (fx.equals("D")) {
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (!isStart) {
g.setColor(Color.black);
g.setFont(new Font("微软雅黑", Font.BOLD, 20));
g.drawString("按下空格开始游戏", 170, 35);
}
if (isFail) {
g.setColor(Color.red);
g.setFont(new Font("微软雅黑", Font.BOLD, 20));
g.drawString("分数:"+score+"\n按下空格重新开始", 140, 35);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
if (isFail) {
isFail = false;
init();
} else {
isStart = !isStart;
}
repaint();
}
if (fx.equals("R")) {
if (keyCode==KeyEvent.VK_UP) {
fx = "U";
} else if (keyCode==KeyEvent.VK_DOWN) {
fx = "D";
} else if (keyCode==KeyEvent.VK_RIGHT) {
fx = "R";
}
}
if (fx.equals("L")) {
if (keyCode==KeyEvent.VK_UP) {
fx = "U";
} else if (keyCode==KeyEvent.VK_DOWN) {
fx = "D";
} else if (keyCode==KeyEvent.VK_LEFT) {
fx = "L";
}
}
if (fx.equals("U")) {
if (keyCode==KeyEvent.VK_UP) {
fx = "U";
} else if (keyCode==KeyEvent.VK_LEFT) {
fx = "L";
} else if (keyCode==KeyEvent.VK_RIGHT) {
fx = "R";
}
}
if (fx.equals("D")) {
if (keyCode==KeyEvent.VK_DOWN) {
fx = "D";
} else if (keyCode==KeyEvent.VK_LEFT) {
fx = "L";
} else if (keyCode==KeyEvent.VK_RIGHT) {
fx = "R";
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isStart && !isFail) {
//吃食物
if (snakeX[0] == foodX && snakeY[0] == foodY) {
length++;
score++;
foodX = 0 + 20*random.nextInt(10);
foodY = 50 + 20*random.nextInt(10);
}
//身体+1
for (int i = length-1; i > 0; i--) {
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
if (fx.equals("R")) {
snakeX[0] = snakeX[0]+20;
if (snakeX[0]>490) {isFail = true;}
} else if (fx.equals("L")) {
snakeX[0] = snakeX[0]-20;
if (snakeX[0]<0) {isFail = true;}
} else if (fx.equals("U")) {
snakeY[0] = snakeY[0]-20;
if (snakeY[0]<50) {isFail = true;}
} else if (fx.equals("D")) {
snakeY[0] = snakeY[0]+20;
if (snakeY[0]>400) {isFail = true;}
}
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isFail = true;
}
}
repaint();
}
timer.start();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}