Java实现贪吃蛇代码
代码参考:B站Java项目 我修改了部分内容和添加了音乐。
Snake.java 作为项目的启动类
package mysnake;
import javax.swing.JFrame;
public class Snake {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("贪吃蛇小游戏");
frame.setBounds(400, 200, 900, 720);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SnakePanel panel = new SnakePanel();
frame.add(panel);
frame.setVisible(true);
}
}
snakeJpanel.java作为项目的实现类
package mysnake;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakePanel extends JPanel implements KeyListener,ActionListener {
ImageIcon up = new ImageIcon("up.png");
ImageIcon down = new ImageIcon("down.png");
ImageIcon title = new ImageIcon("title.png");
ImageIcon right = new ImageIcon("right.png");
ImageIcon left = new ImageIcon("left.png");
ImageIcon body = new ImageIcon("body.png");
ImageIcon food = new ImageIcon("food.png");
//定义蛇的数据结构
int[] snakex = new int[750];
int[] snakey = new int[750];
int len = 3;
String direction = "R";
Random r = new Random();
int foodx = r.nextInt(34)*25+25;
int foody = r.nextInt(24)*25+75;
boolean isstarted = false;
boolean isfailed = false;
int score=0;
Timer timer = new Timer(200, this);
File f1 = new File("music.wav");
public SnakePanel() {
this.setFocusable(true);
musicplayer();
initsnake();
this.addKeyListener(this);
timer.start();
}
public void musicplayer() {
try {
URL url = f1.toURL();
AudioClip ac = Applet.newAudioClip(url);
ac.loop();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void initsnake() {
len = 3;
direction = "R";
isstarted = false;
isfailed = false;
snakex[0] = 100;
snakey[0] = 100;
snakex[1] = 75;
snakey[1] = 100;
snakex[2] = 50;
snakey[2] = 100;
score = 0;
}
public void paint(Graphics g) {
this.setBackground(Color.WHITE);
g.fillRect(25, 75, 850, 600);
title.paintIcon(this, g, 25, 11);
if(direction.equals("R")) {
right.paintIcon(this, g, snakex[0], snakey[0]);
}
else if (direction.equals("L")) {
left.paintIcon(this, g, snakex[0], snakey[0]);
}else if (direction.equals("U")) {
up.paintIcon(this, g, snakex[0], snakey[0]);
}else if (direction.equals("D")) {
down.paintIcon(this, g, snakex[0], snakey[0]);
}
for(int i=1;i<len;i++) {
body.paintIcon(this, g, snakex[i], snakey[i]);
}
if(!isstarted) {
g.setColor(Color.WHITE);
g.setFont(new Font("arial", Font.BOLD, 30));
g.drawString("Please press space to start", 300, 300);
}
food.paintIcon(this, g, foodx, foody);
if(isfailed) {
g.setColor(Color.WHITE);
g.setFont(new Font("arial", Font.BOLD, 30));
g.drawString("Game over", 300, 300);
}
g.setColor(Color.black);
g.setFont(new Font("arial", Font.PLAIN, 15));
g.drawString("score "+score,750,20);
g.drawString("len "+len, 750, 40);
}
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_SPACE) {
if(isfailed) {
initsnake();
}else
{
isstarted = !isstarted;
}
}
if(keycode == KeyEvent.VK_UP && !direction.equals("D")) {
direction = "U";
}
if(keycode == KeyEvent.VK_DOWN && !direction.equals("U")) {
direction = "D";
}
if(keycode == KeyEvent.VK_LEFT && !direction.equals("R")) {
direction = "L";
}
if(keycode == KeyEvent.VK_RIGHT && !direction.equals("L")) {
direction = "R";
}
}
public void actionPerformed(ActionEvent e) {
timer.start();
if(isstarted && !isfailed) {
for(int i=len;i>0;i--) {
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
if(direction.equals("R")) {
snakex[0] = snakex[0]+25;
if(snakex[0]>850) {
JOptionPane.showMessageDialog(this, "撞墙啦");
initsnake();
}
}
else if (direction.equals("L")) {
snakex[0] = snakex[0]-25;
if(snakex[0]<25) {
JOptionPane.showMessageDialog(this, "撞墙啦");
initsnake();
}
}else if (direction.equals("U")) {
snakey[0] = snakey[0]-25;
if(snakey[0]<75) {
JOptionPane.showMessageDialog(this, "撞墙啦");
initsnake();
}
}else if (direction.equals("D")) {
snakey[0] = snakey[0]+25;
if(snakey[0]>650) {
JOptionPane.showMessageDialog(this, "撞墙啦");
initsnake();
}
}
if(snakex[0] == foodx && snakey[0] == foody) {
len++;
score++;
foodx = r.nextInt(34)*25+25;
foody = r.nextInt(24)*25+75;
}
for(int i=1;i<len;i++) {
if(snakex[i] == snakex[0]&& snakey[i] == snakey[0]) {
isfailed = true;
}
}
}
repaint();
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
源码链接:https://pan.baidu.com/s/1GR4ta81a1_J7dlep0mU42A
提取码:ovuv
运行截图