我用eclipse做了一个贪吃蛇的游戏,项目工程表如下:
主要的类GamePanel的代码如下:
package pack;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class GamePanel extends JPanel
implements ActionListener {
static final int SCREEN_WIDTH=600
,SCREEN_HEIGHT=600;
static final int UNIT_SIZE=25;
static final int GAME_UNIT = (SCREEN_WIDTH*SCREEN_HEIGHT) / UNIT_SIZE;
static final int DELAY = 75;
static final int x[] = new int[GAME_UNIT],
y[] = new int[GAME_UNIT];
int body_parts = 6;
int apple_eatten;
int appleX,appleY;
char direction = 'R';
int bodyParts = 6;
boolean running = false;
Timer timer;
Random rand;
/**
* 构造
*/
GamePanel() {
rand = new Random();
setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
setBackground(Color.black);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}
/**函数库*/
/**
* 开始游戏
*/
public void startGame() {
addApple();
running = true;
timer = new Timer(DELAY,this);
timer.start();
}
/**
* 绘制组件
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
/**
* 添加苹果
*/
public void addApple() {
appleX = rand.nextInt((int)SCREEN_WIDTH/UNIT_SIZE)*UNIT_SIZE;
appleY = rand.nextInt((int)SCREEN_HEIGHT/UNIT_SIZE)*UNIT_SIZE;
}
/**
* 绘制
*/
public void draw(Graphics g) {
if (running) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.setColor(Color.white);
for (int i = 0;i < SCREEN_HEIGHT/UNIT_SIZE;i++)
{
g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT);
}
for (int i = 0;i < SCREEN_WIDTH/UNIT_SIZE;i++)
{
g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE);
}
g.setColor(Color.red);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0;i < body_parts;i++) {
if (i==0) {
g.setColor(Color.green);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(new Color(45,180,0));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
}else {
g.setColor(Color.red);
g.setFont(new Font("Sans Serif",Font.BOLD,50));
g.drawString("Game Over", 150, 300);
}
}
/**
* 移动
*/
public void move() {
for (int i = bodyParts; i > 0 ; i--) {
x[i] = x[i-1];
y[i] = y[i-1];
}
switch(direction) {
case 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
/**
* 侦测碰到苹果
*/
public void sensingTouchingApple() {
if ((x[0]==appleX) && (y[0]==appleY)) {
body_parts++;
apple_eatten++;
addApple();
}
}
/**
* 侦测碰撞
*/
public void sensingCollisions() {
//侦测自己碰到自己
for (int i = body_parts;i > 0;i--) {
if ((x[0]==x[i])&&(y[0]==y[i])) {
running = false;
}
}
//侦测碰到边缘
if ((x[0]<0) || (x[0]>SCREEN_WIDTH) || (y[0]>SCREEN_HEIGHT) || (y[0]<0)) {
running =false;
}
//不运行了
if (!running) {
timer.stop();
}
}
/**
* 接口函数
*/
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
sensingTouchingApple();
sensingCollisions();
}
repaint();
}
/**
* 钥匙匹配器
*/
public class MyKeyAdapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction!='R')
{
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction!='L')
{
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction!='D')
{
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if(direction!='U')
{
direction = 'D';
}
break;
}
}
}
}
GameFrame代码如下:
package pack;
import javax.swing.*;
public class GameFrame extends JFrame{
/**
* 构造
*/
public GameFrame() {
//属性
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,600);
setLocationRelativeTo(null);
setResizable(false);
//添加游戏面板
GamePanel panel = new GamePanel();
getContentPane().add(panel);
}
}
start类就是入口了。
这个游戏有一些bug,请大家修复!