package game;import javaxsoundsampled;import javaxswing;import javaawt;import javaawteventKeyEvent;import javaawteventKeyListener;import javaioFile;import javaioIOException;import javautilArrayList;im
package game;
import javax.sound.sampled.; 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.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Random;
public class SnakeGame extends JFrame implements KeyListener, ActionListener {
// 游戏界面大小
private final int WIDTH = 800;
private final int HEIGHT = 840;
// 网格大小
private static final int GRID_SIZE = 40;
// 贪吃蛇移动速度
private static final int SNAKE_SPEED = GRID_SIZE;
// 贪吃蛇的区块
private final ArrayList
//把图片jlabel一下
JLabel j=new JLabel(icon);
JLabel jdead=new JLabel(deadicon);
JLabel jeat=new JLabel(eaticon);
//开始游戏按钮
JButton startButton = new JButton("开始游戏");
//排行榜按钮
JButton rankButton = new JButton("排行榜");
public SnakeGame() {
// 设置窗口大小和标题
setSize(WIDTH, HEIGHT);
setTitle("贪吃蛇小游戏");
// 设置窗口居中
setLocationRelativeTo(null);
// 添加键盘监听器
addKeyListener(this);
// 添加开始游戏按钮
startButton.setBounds(350, 400, 100, 50);
startButton.addActionListener(this);
add(startButton);
// 添加排行榜按钮
rankButton.setBounds(350, 460, 100, 50);
rankButton.addActionListener(this);
add(rankButton);
// 初始化贪吃蛇和食物
// 播放背景音乐
//playMusic("C:\\Users\\彭 杨\\IdeaProjects\\untitled\\src\\G0tch_ - 托马斯臭火车_1_1.WAV");
//playMusic("C:\\Users\\彭 杨\\IdeaProjects\\untitled\\src\\snake\\【蔡徐坤】蜜雪鸡城_1.WAV");
//playMusic("C:\\Users\\彭 杨\\IdeaProjects\\untitled\\src\\snake\\【蔡徐坤】
原文地址: http://www.cveoy.top/t/topic/g95q 著作权归作者所有。请勿转载和采集!