在线客服
扫描二维码
下载博学谷APP扫描二维码
关注博学谷微信公众号
Java课程设计是必不可少的一个重要学习环节,Java程序设计的目的就是加深Java学习者对Java理论基础内容的理解和掌握。今天我们要讲的Java课程设计就是贪吃蛇的小程序设计,以下是具体讲解:
1、执行程序:程序启动的入口
package cn.hncu;
public?class?GreedySnake {
public static void main(String[] args) {
Model model=new Model(80,?50);
Control control=new Control(model);
View view=new View(model,control);
model.addObserver(view);
(new Thread(model)).start();
}
}
控制类:主要进行键盘的按键收集和传递
package cn.hncu;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Control implements KeyListener{
Model model;
public Control(Model?model){
this.model=model;
}
public void keyPressed(KeyEvent?e){
//此方法收集按键
int key=e.getKeyCode();
if (model.running) {
switch (key) {
case KeyEvent.VK_UP:
model.changeDirection(model.up);
break;
case KeyEvent.VK_DOWN:
model.changeDirection(model.down);
break;
case KeyEvent.VK_LEFT:
model.changeDirection(model.left);
break;
case KeyEvent.VK_RIGHT:
model.changeDirection(model.right);
break;
default:
break;
}
}
if (key==KeyEvent.VK_ENTER) {
model.reset();
}
}
public void keyReleased(KeyEvent?e) {
}
public void keyTyped(KeyEvent e) {
}
}
模型类:创建蛇身和蛇的运动方式的实现,用到了线程。
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Random;
import javax.swing.JOptionPane;
public class Model extends Observable implements Runnable{
public static final int left=1;
public static final int up=2;
public static final int right=3;
public?static?final?int?down=4;
public?boolean?coordinate[][];//用这个来当做界面的坐标
public?LinkedList?node=new?LinkedList();
public?int?direction=2;
boolean?running=false;
public?int?maxX,maxY;
Node?food;
public?int?sleeptime=200;
public?Model(int?maxX,int?maxY){
this.maxX=maxX;
this.maxY=maxY;
reset();
}
public?void?reset()?{
direction=this.up;
sleeptime=200;
coordinate=new?boolean[maxX][];
for?(int?i?=?0;?i?<?maxX;?i++)?{
coordinate[i]=new?boolean[maxY];
Arrays.fill(coordinate[i],?false);
}
//initialize?the?Snake'body
int?initlenght=10;
node.clear();
for?(int?j?=?0;?j?<?initlenght;?j++)?{
int?x=maxX/2+j;
int?y=maxY/2;
node.addLast(new?Node(x,y));
coordinate[x][y]=true;
}
food=createFood();
coordinate[food.x][food.y]=true;
}
public?boolean?move(){
Node?n=(Node)node.getFirst();
int?x=n.x;
int?y=n.y;
switch?(direction)?{
case?up:
y--;
break;
case?down:
y++;
break;
case?left:
x--;
break;
case?right:
x++;
break;
default:
break;
}
if?((x>=0&&x<maxX)&&(y>=0&&y<maxY))?{
if?(coordinate[x][y])?{
if?(x==food.x&&y==food.y)?{
node.addFirst(food);
if?(sleeptime>35)?{
sleeptime-=20;
}
food=createFood();
coordinate[food.x][food.y]=true;
return?true;
}else?{
return?false;
}
}else?{
node.addFirst(new?Node(x,y));
coordinate[x][y]=true;
n=(Node)node.getLast();
node.removeLast();
coordinate[n.x][n.y]=false;
return?true;
}
}
return?false;
}
public?void?changeDirection(int?newdir){
if?(direction!=newdir)?{
direction=newdir;
}
}
public?Node?createFood()?{
int?x=0,y=0;
do?{
Random?r?=?new?Random();
x?=?r.nextInt(maxX);
y?=?r.nextInt(maxY);
}?while?(coordinate[x][y]);
return?new?Node(x,?y);
}
public?void?run()?{
running=true;
while(running){
try?{
Thread.sleep(sleeptime);
}?catch?(Exception?e)?{
break;
}
if?(move())?{
setChanged();
notifyObservers();
}else?{
JOptionPane.showMessageDialog(null,?"Game?Over");
break;
}
}
}
}
class?Node{//创建蛇身
public?int?x,y;
public?Node(int?x,int?y){
this.x=x;
this.y=y;
}
}
界面层:展现给用户看的,用图形界面展现蛇的运动
[java]?view plain?copy
import?java.awt.BorderLayout;
import?java.awt.Canvas;
import?java.awt.Color;
import?java.awt.Container;
import?java.awt.Graphics;
import?java.util.Iterator;
import?java.util.LinkedList;
import?java.util.Observable;
import?java.util.Observer;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
public?class?View?extends?JFrame?implements?Observer{
Control?control;
Model?model;
Canvas?canvas;
public?static?final?int?canvasWidth=800,canvasHeight=500;
public?static?final?int?nodeWidth=10,nodeHeight=10;
public?View(Model?model,Control?control){
super("GreedySnake");
this.control=control;
this.model=model;
this.setLocation(400,?300);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
canvas=new?Canvas();
canvas.setSize(canvasWidth+1,?canvasHeight+1);
canvas.addKeyListener(control);
this.add(canvas,BorderLayout.NORTH);
JPanel?panel=new?JPanel();
this.add(panel,BorderLayout.SOUTH);
JLabel?label=new?JLabel("Enter?for?restart");
panel.add(label);
this.pack();
this.addKeyListener(control);??
this.setVisible(true);
}
public?void?repaint()?{
Graphics?g=canvas.getGraphics();
// draw?background
g.setColor(Color.white);
g.fillRect(0,?0,?canvasWidth,?canvasHeight);
// draw snake
g.setColor(Color.red);
LinkedList?node=model.node;
Iterator?it=node.iterator();
while(it.hasNext()){
Node?n=(Node)it.next();
drawNode(g,n);
}
// draw food
g.setColor(Color.black);
Node?n=model.food;
drawNode(g,n);
}
private?void?drawNode(Graphics?g,?Node?n)?{
g.fillOval(n.x*nodeWidth,?n.y*nodeHeight,?nodeWidth,?nodeHeight);
}
public?void?update(Observable?o,?Object?arg){
repaint();
}
}
以上就是Java课程设计贪吃蛇的全部详细讲解。大家应该都能看明白吧,还有什么不懂的,可以上博学谷在线学习。
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
Java基础 "=="和 equals 方法究竟有什么区别?
Java基础 "=="和 equals 方法究竟有什么区别?==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同,要比较两个基本类型的数据或两个引用变量是否相等,只能用==操作符。
8509
2019-05-23 11:31:26
学习MySql需要掌握哪些基础知识?
学习MySql基础知识,需要掌握需求分析、关系型数据库用来描述实体与实体之间的关系、MySql数据库服务器、MySql得安装和卸载、MySql的sql语句及操作相关的知识。
6033
2020-03-04 17:06:07
SpringMVC注解解析
今天我们要来梳理的是请求映射注解@RequestMapping。相信大家对SpringMVC注解并不陌生,下面就来让我们一起来解析SpringMVC注解的内容吧,包括SpringMVC注解的概念和属性、mvc命名空间引入和组件扫描。
4180
2020-04-30 10:03:02
在IDEA中如何使用Git?详细步骤讲解
在IDEA中如何使用Git?首先我们要安装好IntelliJ IDEA,然后在IDEA中配置Git,才能开始使用Git。下面我们来详细讲解一下在IDEA中使用Git,大概需要十个步骤,分别是在IDEA中创建工程并将工程添加至Git,将文件添加到暂存区,提交文件,将代码推送到远程仓库,从远程仓库克隆工程到本地,从远程拉取代码,版本对比,创建分支,切换分支和分支合并。
9135
2020-04-30 10:34:03
成为Java架构师需要学习哪些知识?
成为 Java 架构师需要学习哪些知识?相信 Java 架构师是很多程序员的终极目标,其实立志成为Java 架构师,最应该做好准备的是保持一颗持之以恒学习的心。学习专业技能知识固然重要,但是如果没有这个前提,许多人在通往梦想的路上就已经放弃了。因此,学习态度是一切的前提和决定成败的关键因素。
5875
2020-06-19 10:52:01