作者:raz4150266 | 来源:互联网 | 2024-11-08 17:59
本文介绍了一款使用Java语言开发的经典贪吃蛇游戏的实现。游戏主要由两个核心类组成:`GameFrame`和`GamePanel`。`GameFrame`类负责设置游戏窗口的标题、关闭按钮以及是否允许调整窗口大小,并初始化数据模型以支持绘制操作。`GamePanel`类则负责管理游戏中的蛇和苹果的逻辑与渲染,确保游戏的流畅运行和良好的用户体验。
9 创建了GameFrame和GamePanel两个类
Frame设定了title、关闭按钮、 窗体是否可以自由改变大小、构造数据模型允许paint。
Panel 设定了蛇和苹果,设定了延迟175,蛇和苹果位置用数组实现
package dd;import javax.swing.*;/*** @author 郑道炫* @version 1.0*/
public class GameFrame extends JFrame {GameFrame(){GamePanel gamePanel = new GamePanel();this.add(gamePanel);this.setTitle("snake");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.pack();this.setVisible(true);this.setLocationRelativeTo(null);}
}
package dd;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
/*** @author 郑道炫* @version 1.0*/public class GamePanel extends JPanel implements ActionListener {static final int SCREEN_WIDTH = 800;static final int SCREEN_HEIGHT = 700;static final int UNIT_SIZE = 35;static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / (UNIT_SIZE*UNIT_SIZE);static final int DELAY = 175;final int x[] = new int[GAME_UNITS];final int y[] = new int[GAME_UNITS];int bodyParts = 3;int applesEaten;int appleX;int appleY;char direction = 'R';boolean running = false;Timer timer;Random randoms;GamePanel() {randoms = new Random();this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));this.setBackground(Color.black);this.setFocusable(true);this.addKeyListener(new MyKeyAdapter());startGame();}public void startGame() {newApple();running = true;timer = new Timer(DELAY, this);timer.start();}public void paintComponent(Graphics g) {super.paintComponent(g);draw(g);}public void draw(Graphics g) {if (running) {
// for (int i = 0; i // g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_HEIGHT);
// g.drawLine(0, i * UNIT_SIZE, SCREEN_WIDTH, i * UNIT_SIZE);
// }//用完表格取消g.setColor(Color.red);g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);for (int i &#61; 0; i 0&&appleX/UNIT_SIZE 0&&appleY/UNIT_SIZE 0; i--) {x[i] &#61; x[i - 1];y[i] &#61; y[i - 1];}switch (direction) {case &#39;U&#39;:y[0] &#61; y[0] - UNIT_SIZE;break;case &#39;D&#39;:y[0] &#61; y[0] &#43; UNIT_SIZE;break;case &#39;L&#39;:x[0] &#61; x[0] - UNIT_SIZE;break;case &#39;R&#39;:x[0] &#61; x[0] &#43; UNIT_SIZE;break;}}public void checkApple() {if ((x[0] &#61;&#61; appleX) && (y[0] &#61;&#61; appleY)) {bodyParts&#43;&#43;;applesEaten&#43;&#43;;newApple();}}public void checkCollisions() {//头撞到身体for (int i &#61; bodyParts; i > 0; i--) {if ((x[0] &#61;&#61; x[i]) && (y[0] &#61;&#61; y[i])) {running &#61; false;}}//头碰到左边的框框if (x[0] <0) {running &#61; false;}//头撞到右边if (x[0] > SCREEN_WIDTH) {running &#61; false;}//头碰到上边的框框if (y[0]<0){running &#61;false;}//头撞到下面的框框if (y[0] > SCREEN_HEIGHT) {running &#61; false;}if (!running) {timer.stop();}}public void gameOver(Graphics g) {//Game Over textg.setColor(Color.RED);g.setFont(new Font("Ink Free", Font.BOLD, 45));FontMetrics metrics1 &#61; getFontMetrics(g.getFont());g.drawString("Score: " &#43; applesEaten,(SCREEN_WIDTH - metrics1.stringWidth("Score:" &#43; applesEaten)) / 2,g.getFont().getSize());g.setFont(new Font("Ink Free", Font.BOLD, 75));FontMetrics metrics2 &#61; getFontMetrics(g.getFont());g.drawString("gameOver ",(SCREEN_WIDTH - metrics2.stringWidth("GameOver"))/2,SCREEN_HEIGHT/2);}&#64;Overridepublic void actionPerformed(ActionEvent e) {//TODO Auto-generated method stubif (running) {move();checkApple();checkCollisions();}repaint();}public class MyKeyAdapter extends KeyAdapter {&#64;Overridepublic void keyPressed(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_A:if (direction !&#61; &#39;R&#39;) {direction &#61; &#39;L&#39;;}break;case KeyEvent.VK_D:if (direction !&#61; &#39;L&#39;) {direction &#61; &#39;R&#39;;}break;case KeyEvent.VK_W:if (direction !&#61; &#39;D&#39;) {direction &#61; &#39;U&#39;;}break;case KeyEvent.VK_S:if (direction !&#61; &#39;U&#39;) {direction &#61; &#39;D&#39;;}break;}}}
}
package dd;/*** &#64;author 郑道炫* &#64;version 1.0*/
public class snakebord {public static void main(String[] args) {//TODO Auto-generated method subGameFrame frame &#61; new GameFrame();}
}
效果
但是有一个技术问题吃到4个或者5个苹果后&#xff0c;苹果消失&#xff0c;在屏幕找不到&#xff0c;考虑是因为苹果座位表超出了整体frame。