GUI编程
04 贪吃蛇小游戏
4.3 第三步:让小蛇动起来(键盘控制)
首先,在构造器中要获取焦点事件、键盘监听事件并加入定时器(定时器定义需要实现ActionListener接口并重写actionPerformed方法):
//构造器public GamePanel() {init();this.setFocusable(true); //获取焦点事件this.addKeyListener(this); //获取键盘监听事件timer.start(); //游戏一开始,定时器就启动}
然后,在键盘监听事件中,通过定时器来实现监听(即通过固定的时间进行刷新),并加入对上下左右按键的判断,以实现改变小蛇身体的对应走向:
//键盘监听事件@Overridepublic void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode(); //获取键盘按键if (keyCode == KeyEvent.VK_SPACE) {isStart = !isStart;repaint();}//键盘监听到按键后画上对应方向的蛇头if (keyCode == KeyEvent.VK_UP) {direction = "U";} else if (keyCode == KeyEvent.VK_DOWN) {direction = "D";} else if (keyCode == KeyEvent.VK_LEFT) {direction = "L";} else if (keyCode == KeyEvent.VK_RIGHT) {direction = "R";}}
下面对实现ActionListener接口的actionPerformed方法进行重写:
//事件监听需要定时器来实现,即通过固定的时间进行刷新(例如1s=10次)@Overridepublic void actionPerformed(ActionEvent e) {if (isStart) {//小蛇身体右移for (int i = length - 1; i > 0; i--) { //后一节身体移动至前一节 snakeX[1] = snakeX[0]snakeX[i] = snakeX[i - 1];snakeY[i] = snakeY[i - 1];}//判断蛇头走向switch (direction) {case "R":snakeX[0] += 25;//边界判断if (snakeX[0] > 850) {snakeX[0] = 25;}break;case "L":snakeX[0] -= 25;//边界判断if (snakeX[0] < 25) {snakeX[0] = 850;}break;case "U":snakeY[0] -= 25;//边界判断if (snakeY[0] < 75) {snakeY[0] = 650;}break;case "D":snakeY[0] += 25;//边界判断if (snakeY[0] > 650) {snakeY[0] = 75;}break;}repaint();}timer.start();}
综上,GamePanel类中代码如下:
package com.duo.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.util.Objects;//游戏的面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {//定义蛇的数据结构int length; //小蛇总长int[] snakeX = new int[600]; //蛇的X坐标 25*25int[] snakeY = new int[500]; //蛇的Y坐标 25*25String direction; //蛇头方向boolean isStart = false; //默认未开始游戏//定时器Timer timer = new Timer(250, this); //100ms执行一次//构造器public GamePanel() {init();this.setFocusable(true); //获取焦点事件this.addKeyListener(this); //获取键盘监听事件timer.start(); //游戏一开始,定时器就启动}//初始化方法public void init() {length = 3;snakeX[0] = 100; snakeY[0] = 100; //脑袋的坐标snakeX[1] = 75; snakeY[1] = 100; //第一节身体的坐标snakeX[2] = 50; snakeY[2] = 100; //第二节身体的坐标direction = "R"; //蛇头初始向右}//绘制静态面板@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g); //起到清屏的作用Data.header.paintIcon(this, g, 25, 11); //将广告栏画入面板g.fillRect(25, 75, 850, 600); //默认的游戏界面(黑色区域)this.setBackground(Color.white);//将小蛇画入面板中的游戏区域,且蛇头部分需要判断方向switch (direction) {case "R":Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);break;case "L":Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);break;case "U":Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);break;case "D":Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);break;}//画小蛇的身体部分for (int i = 1; i < length; i++) {Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);}//绘制当前游戏状态if (!isStart) {g.setColor(Color.white);g.setFont(new Font("宋体", Font.BOLD, 40));g.drawString("按下空格开始游戏", 300, 300);}}//键盘监听事件@Overridepublic void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode(); //获取键盘按键if (keyCode == KeyEvent.VK_SPACE) {isStart = !isStart;repaint();}//键盘监听到按键后画上对应方向的蛇头if (keyCode == KeyEvent.VK_UP) {direction = "U";} else if (keyCode == KeyEvent.VK_DOWN) {direction = "D";} else if (keyCode == KeyEvent.VK_LEFT) {direction = "L";} else if (keyCode == KeyEvent.VK_RIGHT) {direction = "R";}}//事件监听需要定时器来实现,即通过固定的时间进行刷新(例如1s=10次)@Overridepublic void actionPerformed(ActionEvent e) {if (isStart) {//小蛇身体右移for (int i = length - 1; i > 0; i--) { //后一节身体移动至前一节 snakeX[1] = snakeX[0]snakeX[i] = snakeX[i - 1];snakeY[i] = snakeY[i - 1];}//判断蛇头走向switch (direction) {case "R":snakeX[0] += 25;//边界判断if (snakeX[0] > 850) {snakeX[0] = 25;}break;case "L":snakeX[0] -= 25;//边界判断if (snakeX[0] < 25) {snakeX[0] = 850;}break;case "U":snakeY[0] -= 25;//边界判断if (snakeY[0] < 75) {snakeY[0] = 650;}break;case "D":snakeY[0] += 25;//边界判断if (snakeY[0] > 650) {snakeY[0] = 75;}break;}repaint();}timer.start();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void keyTyped(KeyEvent e) {}
}
运行结果如下:
游戏未开始时:
未按方向键,小蛇默认一直向右移动直到边界,满足边界判断条件并重新回到界面左端:
按下下键,小蛇向下移动:
至此,小蛇可根据键盘按键进行对应方向的移动。此时还未加入食物,且当蛇头触碰蛇身时并不会触发游戏失败,这将在下一篇内容中给出。