package Snakes;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX=new int[28];
int[] snakeY=new int[28];
String fx;
int foodX;
int foodY;
int socer;
Random random=new Random();
boolean game=false;
boolean ing=false;
Timer time=new Timer(100,this);
public boolean getGame(){
return game;
}
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
time.start();
}
public void init(){
length=3;
socer=0;
snakeX[0]=100;snakeY[0]=100;
snakeX[1]=75;snakeY[1]=100;
snakeX[2]=50;snakeY[2]=100;
fx="R";
foodX=50+25*random.nextInt(27);
foodY=75+25*random.nextInt(25);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.gg.paintIcon(this,g,0,0);
g.fillRect(25,50,700,675);
Data.dd.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,foodX,foodY);
if(fx.equals("R")){
Data.dd.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if(fx.equals("L")){
Data.aa.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if(fx.equals("W")){
Data.ww.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if(fx.equals("S")){
Data.ss.paintIcon(this,g,snakeX[0],snakeY[0]);
}
if(game==false){
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按空格开始或暂停",250,300);
}
if(ing){
g.setColor(Color.red);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("失败,空格重新开始",250,300);
}
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,20));
g.drawString("分数:"+socer,600,35);
g.drawString("长度:"+length,400,35);
}
@Override
public void keyPressed(KeyEvent e) {
int a = e.getKeyCode();
if (a == KeyEvent.VK_SPACE) {
if(ing){
ing=false;
init();
}else {
game=!game;
}
repaint();
}
if(a == KeyEvent.VK_RIGHT){
fx="R";
}else if(a == KeyEvent.VK_LEFT){
fx="L";
}if(a == KeyEvent.VK_UP){
fx="W";
}if(a == KeyEvent.VK_DOWN){
fx="S";
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(foodX==snakeX[0]&&foodY==snakeY[0]){
length++;
socer=socer+5;
foodX=50+25*random.nextInt(27);
foodY=75+25*random.nextInt(25);
}
if(game&&ing==false){
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]==725){snakeX[0]=25;}
}else if(fx.equals("L")){
snakeX[0]=snakeX[0]-25;
if(snakeX[0]==25){snakeX[0]=725;}
}else if(fx.equals("W")){
snakeY[0]=snakeY[0]-25;
if(snakeY[0]==50){snakeY[0]=750;}
}else if(fx.equals("S")){
snakeY[0]=snakeY[0]+25;
if(snakeY[0]==725){snakeY[0]=50;}
}
for (int i = 1; i <length ; i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]) {
ing=true;
}
}
repaint();
}
time.start();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
package Snakes;
import javax.swing.*;
public class Start {
public static void main(String[] args) {
JFrame frame = new JFrame("贪吃蛇");
frame.add(new GamePanel());
frame.setVisible(true);
frame.setResizable(false);
frame.setBounds(600,200,750,750);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
package Snakes;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL g=Data.class.getResource("statics/1.png");
public static URL f=Data.class.getResource("statics/food.png");
public static URL b=Data.class.getResource("statics/shenti.png");
public static URL w=Data.class.getResource("statics/tou.png");
public static URL s=Data.class.getResource("statics/s.png");
public static URL d=Data.class.getResource("statics/d.png");
public static URL a=Data.class.getResource("statics/a.png");
public static ImageIcon gg=new ImageIcon(g);
public static ImageIcon food=new ImageIcon(f);
public static ImageIcon body=new ImageIcon(b);
public static ImageIcon ww=new ImageIcon(w);
public static ImageIcon dd=new ImageIcon(d);
public static ImageIcon ss=new ImageIcon(s);
public static ImageIcon aa=new ImageIcon(a);
}