本篇实现的是贪吃蛇小游戏,使用的是Java语言,GUI编程。
//启动类
import snake.GamePanel;
import javax.swing.*;
import java.awt.*;
public class Start extends JFrame{
public Start() {
this.setBounds(10, 10, 900, 720);
setResizable(false);//窗口大小不可变
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
//正常游戏界面都应该在面上!
Game panel = new Game();
Container container = this.getContentPane();
container.add(panel);
}
//主类
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;
//绘制游戏界面
//初始化、画笔JPanel、监听:键盘;事件监听(吃食物)
public class Game extends JPanel implements KeyListener, ActionListener {
//属性
String dir;//蛇头方向
int[] snake_x =new int[600];//蛇位置
int[] snake_y =new int[600];
int food_x;//食物位置
int food_y;
Random random = new Random();
int length ;
int score;
//游戏状态
Boolean isstart =false;
Boolean isfail =false;
//定时器
Timer timer = new Timer(100, this);//100ms执行一次定时器
//无参构造
public Game() {
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
//游戏开始和重新开始需要初始化,重新开始小蛇位置和分数等,还原到原始状态
public void init() {
//蛇头初始位置
snake_x[0] = 100;snake_y[0] = 100;
snake_x[1] = 75;snake_y[1] = 100;
snake_x[2] = 50;snake_y[2] = 100;
//初始方向
dir = "R";
//随机初始食物位置
food_x= 25+25*random.nextInt(34);
food_y= 75+25*random.nextInt(24);
length=3;
//绘制计分器
score=0;
// timer.start();
}
//绘制面板 重写JComponet方法
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
//画蛇头
switch (dir) {
case"R":
Data.right.paintIcon(this,g,snake_x[0],snake_y[0]);
break;
case"L":
Data.left.paintIcon(this,g,snake_x[0],snake_y[0]);
break;
case"U":
Data.up.paintIcon(this,g,snake_x[0],snake_y[0]);
break;
case"D":
Data.down.paintIcon(this,g,snake_x[0],snake_y[0]);
break;
}
//画食物
Data.food.paintIcon(this,g,food_x,food_y);
//画蛇身
for (int i = 1; i 0 ; i--) {
snake_x[i]=snake_x[i-1];
snake_y[i]=snake_y[i-1];
}
switch (dir) {
case "U":
snake_y[0]-=25;
if(snake_y[0]<75){
snake_y[0]=650;
}
break;
case "D":
snake_y[0]+=25;
if(snake_y[0]>650){
snake_y[0]=75;
}
break;
case "L":
snake_x[0]-=25;
if(snake_x[0]<25){
snake_x[0]=850;
}
break;
case "R":
snake_x[0]+=25;
if(snake_x[0]>850){
snake_x[0]=25;
}
break;
}
repaint();
}
//小蛇移动
if(isstart)
//判断失败
for (int i = 1; i
//数据类
import javax.swing.*;
import java.net.URL;
/**
* 功能:加载图片资源,在Game类中加载到画板上
* */
public class Data {
//头部图片
public static URL headerURL = Data.class.getResource("statics/header.png");
public static ImageIcon header= new ImageIcon(headerURL);
//蛇头上下左右
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);
//蛇身
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);
}
运行结果:
注:程序中所有用到图片素材在主页上传的资源中,粉丝即可下载,无需积分!