一.工程文件
二.Main.java
package com.company;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Mpanel());
frame.setVisible(true);
}
}
三.Mpanel.java
package com.company;
import javax.imageio.ImageIO;
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.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
public class Mpanel extends JPanel implements KeyListener, ActionListener {
ImageIcon title;
ImageIcon body;
ImageIcon up;
ImageIcon down;
ImageIcon left;
ImageIcon right;
ImageIcon food;
int len = 3;
int score = 0;
int[] snakex = new int[750];
int[] snakey = new int[750];
String fx = "R";
Boolean isStart = false;
Boolean isFailed = false;
Timer timer = new Timer(100,this);
int foodx;
int foody;
Random random = new Random();
Clip bgm;
public Mpanel(){
loadImages();
initSnake();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
loadBGM();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.white);
title.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
g.setColor(Color.white);
g.drawString("Len: " + len,750,35);
g.drawString("Score: " + score,750,50);
if(fx == "R"){
right.paintIcon(this,g,snakex[0],snakey[0]);
}
else if(fx == "L"){
left.paintIcon(this,g,snakex[0],snakey[0]);
}
else if(fx == "U"){
up.paintIcon(this,g,snakex[0],snakey[0]);
}
else if(fx == "D"){
down.paintIcon(this,g,snakex[0],snakey[0]);
}
for(int i=1;i<len;i++){
body.paintIcon(this,g,snakex[i],snakey[i]);
}
food.paintIcon(this,g,foodx,foody);
if(isStart == false){
g.setColor(Color.white);
g.setFont(new Font("arial",Font.BOLD,40));
g.drawString("Press Space To Start",250,300);
}
if(isFailed){
g.setColor(Color.red);
g.setFont(new Font("arial",Font.BOLD,40));
g.drawString("Failed: Press Space To Start",250,300);
}
}
public void initSnake(){
len = 3;
snakex[0] = 100;
snakey[0] = 100;
snakex[1] = 75;
snakey[1] = 100;
snakex[2] = 50;
snakey[2] = 100;
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
fx = "R";
score = 0;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_SPACE){
if(isFailed){
isFailed = !isFailed;
initSnake();
}
else {
isStart = !isStart;
}
if(isStart){
bgm.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
bgm.stop();
}
repaint();
}
else if(keyCode == KeyEvent.VK_LEFT && fx != "R"){
fx = "L";
}
else if(keyCode == KeyEvent.VK_RIGHT && fx != "L"){
fx = "R";
}
else if(keyCode == KeyEvent.VK_UP && fx != "D"){
fx = "U";
}
else if(keyCode == KeyEvent.VK_DOWN && fx != "U"){
fx = "D";
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart && !isFailed){
for(int i=len-1;i>0;i--){
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
if(fx == "R"){
snakex[0]=snakex[0]+25;
if(snakex[0]>825)isFailed=true;
}
else if(fx == "L"){
snakex[0]=snakex[0]-25;
if(snakex[0]<50)isFailed=true;
}
else if(fx == "U"){
snakey[0]=snakey[0]-25;
if(snakey[0]<100)isFailed=true;
}
else if(fx == "D"){
snakey[0]=snakey[0]+25;
if(snakey[0]>625)isFailed=true;
}
if(snakex[0]==foodx&&snakey[0]==foody){
len++;
score += 10;
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}
for(int i=1;i<len;i++){
if(snakex[i]==snakex[0] && snakey[i]==snakey[0]){
isFailed = true;
}
}
repaint();
}
timer.start();
}
public void loadBGM(){
try {
bgm = AudioSystem.getClip();
InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/company/sound/bgm.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
bgm.open(ais);
}
catch (LineUnavailableException e) {
e.printStackTrace();
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void loadImages(){
InputStream is;
try {
is = getClass().getClassLoader().getResourceAsStream("com/company/img/title.jpg");
title = new ImageIcon(ImageIO.read(is));
is = getClass().getClassLoader().getResourceAsStream("com/company/img/body.png");
body = new ImageIcon(ImageIO.read(is));
is = getClass().getClassLoader().getResourceAsStream("com/company/img/up.png");
up = new ImageIcon(ImageIO.read(is));
is = getClass().getClassLoader().getResourceAsStream("com/company/img/down.png");
down = new ImageIcon(ImageIO.read(is));
is = getClass().getClassLoader().getResourceAsStream("com/company/img/left.png");
left = new ImageIcon(ImageIO.read(is));
is = getClass().getClassLoader().getResourceAsStream("com/company/img/right.png");
right = new ImageIcon(ImageIO.read(is));
is = getClass().getClassLoader().getResourceAsStream("com/company/img/food.png");
food = new ImageIcon(ImageIO.read(is));
} catch (IOException e) {
e.printStackTrace();
}
}
}
四.效果