java gui 贪吃蛇_java GUI编程-贪吃蛇游戏简单实现

package snake;

import java.awt.Color;

import java.awt.Font;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

/**

* 贪吃蛇展示页面

*

* @author hjn

*

*/

public class MainFrame extends Frame {

/**

* 版本

*/

private static final long serialVersionUID = -5227266702753583633L;

/**

* 背景颜色

*/

Color color = Color.gray;

/**

* 蛋

*/

static Egg egg = new Egg();

/**

* 蛇

*/

Snake snake = new Snake();

/**

* 游戏是否失败

*/

boolean gameOver = false;

/**

* 给画笔起一个线程

*/

PaintThread paintThread = new PaintThread();

/**

* 构造方法

*/

public MainFrame() {

init();

}

/**

* 界面初始化

*/

void init() {

this.setBounds(200, 200, Constant.COLS * Constant.BODER_SIZE,

Constant.ROWS * Constant.BODER_SIZE);

this.setResizable(true);

this.repaint();

/**

* 窗口关闭监听事件

*/

this.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

/**

* 添加键盘监听事件

*/

this.addKeyListener(new KeyMomiter());

/**

* 画笔线程启动

*/

new Thread(paintThread).start();

}

/**

* 画笔画界面

*/

public void paint(Graphics g) {

Color c = g.getColor();

g.setColor(Color.GRAY);

g.fillRect(0, 0, Constant.COLS * Constant.BODER_SIZE, Constant.ROWS

* Constant.BODER_SIZE);

g.setColor(Color.DARK_GRAY);

for (int i = 0; i < Constant.ROWS; i++) {

g.drawLine(0, i * Constant.BODER_SIZE, Constant.COLS

* Constant.BODER_SIZE, i * Constant.BODER_SIZE);

}

for (int i = 0; i < Constant.COLS; i++) {

g.drawLine(i * Constant.BODER_SIZE, 0, i * Constant.BODER_SIZE,

Constant.ROWS * Constant.BODER_SIZE);

}

g.setColor(Color.yellow);

g.setFont(new Font("宋体", Font.BOLD, 20));

g.drawString("score:" + getScore(), 10, 60);

if (gameOver) {

g.setColor(Color.red);

g.drawString("GAME OVER", 100, 60);

this.paintThread.pause = true;

}

g.setColor(c);

if (snake.eatEgg(egg)) {

egg = new Egg();

}

snake.draw(g);

egg.draw(g);

}

/**

* 获取分数

*

* @return int 分数

*/

int getScore() {

return snake.getNodeList().size();

}

/**

* 画笔的线程

*

* @author hjn

*/

class PaintThread implements Runnable {

private boolean isRun = true;

private boolean pause = false;

@Override

public void run() {

while (isRun) {

if (pause) {

continue;

} else {

if (snake.isOverstep == true) {

gameOver = true;

}

repaint();

}

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

/**

* 暂停

*/

public void pause() {

this.pause = true;

}

/**

* 重新开始

*/

public void restart() {

this.pause = true;

snake = new Snake();

}

/**

* 游戏结束

*/

public void gameOver() {

isRun = false;

}

}

/**

* 停止

*/

void stop() {

gameOver = true;

}

/**

* 键盘监听器

*

* @author hjn

*

*/

class KeyMomiter extends KeyAdapter {

@Override

public void keyPressed(KeyEvent e) {

super.keyPressed(e);

int key = e.getKeyCode();

if (key == KeyEvent.VK_F2) {

paintThread.restart();

} else {

snake.keyPress(e);

}

}

}

/**

* 启动程序入口

*

* @param args

*/

@SuppressWarnings("deprecation")

public static void main(String[] args) {

MainFrame mainFrame = new MainFrame();

mainFrame.show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值