三个class:Data(图片数据)/GamePanel(游戏面板)/StartGame(开始)
1.Data
package com.zpf.greedysnakedemo;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL pigheadurl=Data.class.getResource("statics/pighead.jpg");
public static ImageIcon pighead=new ImageIcon(pigheadurl);
public static URL headerurl=Data.class.getResource("statics/header.jpg");
public static ImageIcon header=new ImageIcon(headerurl);
public static URL foodurl=Data.class.getResource("statics/food.jpg");
public static ImageIcon food=new ImageIcon(foodurl);
}
2.GamePanel
package com.zpf.greedysnakedemo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GamePanel extends JPanel {
int length;
int[] snakeX=new int[800];
int[] snakeY=new int[800];
boolean isStart;
boolean gameOver;
int[] foodXY=new int[2];
int direct;//0 右边 1 左边 2 上边 3 下边
Timer timer=new Timer(100,new MyActionListener());
public GamePanel(){
init();
}
public void init(){
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
direct=0;
isStart=false;
gameOver=false;
this.setFocusable(true);
this.addKeyListener(new MyKeyListener());
setFoodXY();
timer.start();
}
public void reset(){
length=3;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
direct=0;
isStart=false;
gameOver=false;
// this.setFocusable(true);
// this.addKeyListener(new MyKeyListener());
setFoodXY();
//timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);
g.setFont(new Font("微软雅黑",Font.BOLD,20));
g.drawString("分数:"+(length*5-15),700,20);
g.drawString("长度:"+length,700,50);
g.setColor(Color.WHITE);
g.fillRect(25,75,850,600);
Data.food.paintIcon(this,g,foodXY[0],foodXY[1]);
g.setColor(Color.BLACK);
for(int i=0;i<length;i++) {
Data.pighead.paintIcon(this, g, snakeX[i], snakeY[i]);
}
if(isStart==false&&gameOver==false){
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",200,200);
}
if(gameOver==true){
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("游戏结束了,按空格键重新开始",200,200);
}
}
protected void setFoodXY(){
foodXY[0]=(int)(Math.random()*825+25);
foodXY[1]=(int)(Math.random()*500+100);
foodXY[0]=foodXY[0]-foodXY[0]%25;
foodXY[1]=foodXY[1]-foodXY[1]%25;
}
protected void gameOver(){
gameOver=true;
isStart=false;
}
private class MyKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_SPACE){
isStart=!isStart;
repaint();
}
if(keyCode==KeyEvent.VK_SPACE&&gameOver==true){
reset();
gameOver=false;
isStart=!isStart;
repaint();
}
else {
switch (keyCode) {
case KeyEvent.VK_RIGHT:
direct=0;
break;
case KeyEvent.VK_LEFT:
direct=1;
break;
case KeyEvent.VK_UP:
direct=2;
break;
case KeyEvent.VK_DOWN:
direct=3;
break;
}
}
}
}
private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(isStart==true){
for (int i = length - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
//右移
if(direct==0) {
snakeX[0] = snakeX[0] + 25;
}
//左移
else if(direct==1){
snakeX[0] = snakeX[0] -25;
}
//上移
else if(direct==2){
snakeY[0] = snakeY[0] -25;
}
//下移
else if(direct==3){
snakeY[0] = snakeY[0] +25;
}
if(snakeX[0]>=850)
snakeX[0]=25;
if(snakeX[0]<25)
snakeX[0]=850;
if(snakeY[0]>=600)
snakeY[0]=75;
if(snakeY[0]<75)
snakeY[0]=600;
for(int i=0;i<length;i++){
if (snakeX[i]==foodXY[0]&&snakeY[i]==foodXY[1]) {
length++;
setFoodXY();
snakeX[length-1]=snakeX[length-2];
snakeY[length-1]=snakeY[length-2];
}
}
for(int i=1;i<length;i++){
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
gameOver();
break;
}
}
repaint();
}
}
}
}
3.StartGame
package com.zpf.greedysnakedemo;
import javax.swing.*;
public class StartGame {
public static void main(String[] args) {
JFrame jFrame=new JFrame("贪吃猪");
jFrame.add(new GamePanel());
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setBounds(10,10,900,720);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}