栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java GUI编程之贪吃蛇游戏简单实现方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java GUI编程之贪吃蛇游戏简单实现方法

本文实例讲述了Java GUI编程之贪吃蛇游戏简单实现方法。分享给大家供大家参考,具体如下:

例子简单,界面简陋 请见谅

项目结构如下

Constant.jvava 代码如下:

package snake;

public class Constant {

public static final int LEFT = 0;

public static final int RIGHT = 1;

public static final int UP = 3;

public static final int DOWN = 4;

public static final int COLS = 30;

public static final int ROWS = 30;

public static final int BODER_SIZE = 15;
}

Node.java代码如下:

package snake;

public class Node {

private int row;

private int col;
public Node() {
};
public Node(int row, int col) {
this.row = row;
this.col = col;
};

public Node(int dir, Node node) {
if (dir == Constant.LEFT) {
this.col = node.getCol() - 1;
this.row = node.getRow();
} else if (dir == Constant.RIGHT) {
this.col = node.getCol() + 1;
this.row = node.getRow();
} else if (dir == Constant.UP) {
this.row = node.getRow() - 1;
this.col = node.getCol();
} else {
this.row = node.getRow() + 1;
this.col = node.getCol();
}
}

public boolean equals(Object obj) {
if (obj instanceof Node) {
Node node = (Node) obj;
if (this.col == node.col && this.row == node.row) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public String toString() {
return "col:" + this.col + " row:" + this.row;
}
}

Egg.java代码如下:

package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Egg extends Node {

Color color;

public static Random random = new Random();

public Egg(int row, int col) {
super(row, col);
this.color = Color.green;
}

public Egg() {
super();
int col = random.nextInt(Constant.COLS - 4) + 2;
int row = random.nextInt(Constant.ROWS - 4) + 2;
this.setCol(col);
this.setRow(row);
}

void draw(Graphics g) {
if (this.color == Color.green) {
this.color = Color.red;
} else {
this.color = Color.green;
}
g.setColor(this.color);
int boderSize = Constant.BODER_SIZE;
g.fillOval(this.getCol() * boderSize, this.getRow() * boderSize,
boderSize, boderSize);
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}

Snake.java代码如下:

package snake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class Snake {

int dir;

List nodeList = new ArrayList();

boolean isOverstep = false;

public Snake() {
this.dir = Constant.LEFT;
for (int i = 0; i < 3; i++) {
Node node = new Node(20, 15 + i);
this.nodeList.add(node);
}
}

void forward() {
addNode();
nodeList.remove(nodeList.size() - 1);
}

private void addNode() {
Node node = nodeList.get(0);
node = new Node(dir, node);
nodeList.add(0, node);
}

boolean eatEgg(Egg egg) {
if (nodeList.contains(egg)) {
addNode();
return true;
} else {
return false;
}
}

void draw(Graphics g) {
g.setColor(Color.black);
for (int i = 0; i < this.nodeList.size(); i++) {
Node node = this.nodeList.get(i);
if (node.getCol() > (Constant.COLS - 2) || node.getCol() < 2
|| node.getRow() > (Constant.ROWS - 2) || node.getRow() < 2) {
this.isOverstep = true;
}
g.fillRect(node.getCol() * Constant.BODER_SIZE, node.getRow()
* Constant.BODER_SIZE, Constant.BODER_SIZE,
Constant.BODER_SIZE);
}
forward();
}

void keyPress(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
if (this.dir != Constant.LEFT)
this.dir = Constant.LEFT;
break;
case KeyEvent.VK_RIGHT:
if (this.dir != Constant.RIGHT)
this.dir = Constant.RIGHT;
break;
case KeyEvent.VK_UP:
if (this.dir != Constant.UP)
this.dir = Constant.UP;
break;
case KeyEvent.VK_DOWN:
if (this.dir != Constant.DOWN)
this.dir = Constant.DOWN;
break;
default:
break;
}
}
public int getDir() {
return dir;
}
public void setDir(int dir) {
this.dir = dir;
}
public List getNodeList() {
return nodeList;
}
public void setNodeList(List nodeList) {
this.nodeList = nodeList;
}
public boolean isOverstep() {
return isOverstep;
}
public void setOverstep(boolean isOverstep) {
this.isOverstep = isOverstep;
}
}

主界面Mainframe.java代码如下:

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;

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);
}

int getScore() {
return snake.getNodeList().size();
}

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;
}

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);
}
}
}

@SuppressWarnings("deprecation")
public static void main(String[] args) {
Mainframe mainframe = new Mainframe();
mainframe.show();
}
}

运行效果:

附:完整实例代码点击此处本站下载

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144220.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号