游戏界面:
图片素材:
背景图片 蛇身 食物 蛇头 标题
源代码:
运行界面
package com.snake.game;public class snakeApp {public static void main(String[] args) {//添加界面new snakeJFrame();}
}
游戏界面类JFrame
package com.snake.game;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.util.Random;public class snakeJFrame extends JPanel implements KeyListener, ActionListener {//不太懂private static final Component This=null;//添加分数int score=0;//创建小蛇的长度int length=0;//设置蛇头方向String fx;//创建食物的坐标int foodX=0;int foodY=0;//公开创建JFrame,Random的类JFrame jFrame=new JFrame();Random r=new Random();//创建小蛇的坐标int[] dateX=new int[22];int[] dateY=new int[24];//检测游戏是否开始boolean isStart=false;Timer timer=new Timer(1000,this);//创建空参构造public snakeJFrame(){//初始化界面initJFrame();//添加鼠标监听this.addKeyListener(this);//添加鼠标点击事件this.setFocusable(true);}//初始化蛇和食物的位置private void initAddress(){length=3;fx="R";//初始化小蛇的位置dateX[0]=56;dateY[0]=55;dateX[1]=31;dateY[1]=55;dateX[2]=6;dateY[2]=55;foodX=6+25*r.nextInt(23);foodY=55+25*r.nextInt(21);System.out.println(foodX);System.out.println(foodY);//创建定时器,实现小蛇的不断移动timer.start();}//界面设置private void initJFrame(){//设置页面大小jFrame.setSize(625,630);//设置游戏标题jFrame.setTitle("贪吃蛇游戏 -- v1.0");//设置游戏关闭模式jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//将页面居中jFrame.setLocationRelativeTo(null);//改变界面颜色jFrame.getContentPane().setBackground(Color.white);//取消图片的默认居中,图片才能按照xy排列jFrame.setLayout(null);//初始化坐标initAddress();//添加图片initImage();//将页面显示出来jFrame.setVisible(true);}//放置图片protected void initImage(){//添加标题ImageIcon title=new ImageIcon("D:\\idea.java\\Snake\\title.png");JLabel titlejLabel=new JLabel(title);titlejLabel.setBounds(6,0,600,50);jFrame.getContentPane().add(titlejLabel);JLabel fontScore=new JLabel("score:"+score);fontScore.setBounds(0,0,50,20);jFrame.getContentPane().add(fontScore);//添加蛇头图片ImageIcon head=null;if(fx=="R"){head=new ImageIcon("D:\\idea.java\\Snake\\headright.png");}else if(fx=="L"){head=new ImageIcon("D:\\idea.java\\Snake\\headleft.png");}else if(fx=="U"){head=new ImageIcon("D:\\idea.java\\Snake\\headup.png");}else if(fx=="D"){head=new ImageIcon("D:\\idea.java\\Snake\\headdown.png");}JLabel headjLabel=new JLabel(head);headjLabel.setBounds(dateX[0],dateY[0],25,25);jFrame.getContentPane().add(headjLabel);//添加身体for(int i=1;i<length;i++){ImageIcon body=new ImageIcon("D:\\idea.java\\Snake\\body.png");JLabel bodyjLabel=new JLabel(body);bodyjLabel.setBounds(dateX[i],dateY[i],25,25);jFrame.getContentPane().add(bodyjLabel);}if(isStart){JLabel font=new JLabel("点击空格,重新开始游戏");font.setForeground(Color.yellow);jFrame.getContentPane().add(font);}//添加食物图片ImageIcon food=new ImageIcon("D:\\idea.java\\Snake\\food.png");JLabel foodjLabel=new JLabel(food);foodjLabel.setBounds(foodX,foodY,25,25);jFrame.getContentPane().add(foodjLabel);//添加背景图片,只有背景图片放在最后,才能将其图片显示出来,因为背景图片太大了ImageIcon backGround=new ImageIcon("D:\\idea.java\\Snake\\background.png");JLabel bgjLabel=new JLabel(backGround);bgjLabel.setBounds(6,55,600,600);jFrame.getContentPane().add(bgjLabel);}@Overridepublic void keyTyped(KeyEvent e) {}//鼠标按下@Overridepublic void keyPressed(KeyEvent e) {int keyCode=e.getKeyCode();//按住空格小蛇停止移动if(keyCode==KeyEvent.VK_SPACE){isStart=!isStart;}//改变蛇头位置if(isStart){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";}repaint();}}//鼠标松开public void keyReleased(KeyEvent e) {}@Overridepublic void actionPerformed(ActionEvent e) {if (isStart) {for (int i = 1; i < length; i++) {dateX[i] = dateX[i - 1];dateY[i] = dateY[i - 1];}if (fx.equals("R")) {dateX[0] = dateX[0] + 25;if (dateX[0] > 588) {dateX[0] = 6;}}if (fx.equals("L")) {dateX[0] = dateX[0] - 25;if (dateX[0] < 6) {dateX[0] = 588;}}if (fx.equals("U")) {dateY[0] = dateY[0] - 25;if (dateY[0] < 55) {dateY[0] = 555;}}if (fx.equals("D")) {dateY[0] = dateY[0] - 25;if (dateY[0] >= 580) {dateY[0] = 55;}}}if (dateX[0] == foodX && dateY[0] == foodY) {length++;score += 10;if (score == 100) {isStart = !isStart;}foodX = 6 + 25 * r.nextInt(23);foodY = 55 + 25 * r.nextInt(21);}initImage();repaint();timer.start();}}
感谢大家的观看,相互支持!