ScakeGame类
package snake;
import javax.swing.*;
public class ScakeGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,750);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
Data类
package snake;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL headerURL = Data.class.getResource("statics/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static URL bodyURL = Data.class.getResource("statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Data.class.getResource("statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
public static URL upURL = Data.class.getResource("statics/up.png");
public static ImageIcon up = new ImageIcon(upURL);
public static URL downURL = Data.class.getResource("statics/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL leftURL = Data.class.getResource("statics/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL rightURL = Data.class.getResource("statics/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
}
GamePanel类
package snake;
import com.sun.corba.se.spi.orbutil.fsm.Action;
import javafx.scene.input.KeyCode;
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.sql.Time;
import java.util.Random;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int [] snakeX = new int [600];
int [] snakeY = new int [600];
String fx;
boolean isStart = false ;
int footx;
int footy;
Random random = new Random();
boolean isFail = false;
int score;
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
Timer timer = new Timer(100,this);
public void init(){
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
fx="R";
score=0;
isFail=false;
isStart=false;
footx = 25+25*random.nextInt(34);
footy = 25+25*random.nextInt(24);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
g.setColor(Color.white);
g.drawString("长度"+length,750,30);
g.drawString("分数"+score,750,50);
if(fx.equals("R")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if (fx.equals("L")){
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if (fx.equals("U")){
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if (fx.equals("D")){
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
}
for(int i = 1;i< length;i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
Data.food.paintIcon(this,g,footx,footy);
if(isStart==false){
g.setColor(Color.white);
g.drawString("开始游戏",300,300);
}
if(isFail){
g.setColor(Color.red);
g.drawString("失败",300,300);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
if(KeyCode == KeyEvent.VK_SPACE){
if(isFail){
init();
} else{
isStart=!isStart;
}
repaint();
}
if(KeyCode==KeyEvent.VK_UP){
fx="U";
}
else if(KeyCode==KeyEvent.VK_DOWN){
fx="D";
}
else if(KeyCode==KeyEvent.VK_LEFT){
fx="L";
}
else if(KeyCode==KeyEvent.VK_RIGHT){
fx="R";
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart&&isFail==false){
if(snakeX[0]==footx&&snakeY[0]==footy){
length++;
score=score+10;
footx = 25+25*random.nextInt(32);
footy = 25+25*random.nextInt(18);
}
for(int i= length-1;i>0;i--){
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
if(fx.equals("R")){
snakeX[0]=snakeX[0]+25;
if(snakeX[0]>850) snakeX[0]=25;
}else if(fx.equals("L")){
snakeX[0]=snakeX[0]-25;
if(snakeX[0]<25) snakeX[0]=850;
}else if(fx.equals("U")){
snakeY[0]=snakeY[0]-25;
if(snakeY[0]<75) snakeY[0]=650;
}else if(fx.equals("D")){
snakeY[0]=snakeY[0]+25;
if(snakeY[0]>650) snakeY[0]=75;
}
for (int i=1;i<length;i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
isFail=true;break;
}
}
repaint();
}
timer.start();
}
}