贪吃蛇3Word下载.docx
- 文档编号:15338388
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:9
- 大小:18.35KB
贪吃蛇3Word下载.docx
《贪吃蛇3Word下载.docx》由会员分享,可在线阅读,更多相关《贪吃蛇3Word下载.docx(9页珍藏版)》请在冰豆网上搜索。
但是明显编写程序的素养不错,程序结构写得很清晰,有些细微得地方也写得很简洁,一时兴起之
下,认真解读了这个程序,发现数据和表现分开得很好,而近日正在学习mvc设计模式,
因此尝试把程序得结构改了一下,用mvc模式来实现,对源程序得改动不多。
同时也为程序增加了一些自己理解得注释,希望对大家阅读有帮助。
*/
packagemvctest;
/**
*@authorwangyu
*@version1.0
*description:
*<
/pre>
*createon:
date:
2005-6-13
time:
15:
57:
16
*lastmodified:
*history:
publicclassgreedsnake{
publicstaticvoidmain(string[]args){
snakemodelmodel=newsnakemodel(20,30);
snakecontrolcontrol=newsnakecontrol(model);
snakeviewview=newsnakeview(model,control);
//添加一个观察者,让view成为model的观察者
model.addobserver(view);
(newthread(model)).start();
}
}
-------------------------------------------------------------
2、
//snakecontrol.java
importjava.awt.event.keyevent;
importjava.awt.event.keylistener;
*mvc中的controler,负责接收用户的操作,并把用户操作通知model
publicclasssnakecontrol
implementskeylistener{
snakemodelmodel;
publicsnakecontrol(snakemodelmodel){
this.model=model;
publicvoidkeypressed(keyevente){
intkeycode=e.getkeycode();
if(model.running){
//运行状态下,处理的按键
switch(keycode){
casekeyevent.vk_up:
model.changedirection(snakemodel.up);
break;
casekeyevent.vk_down:
model.changedirection(snakemodel.down);
casekeyevent.vk_left:
model.changedirection(snakemodel.left);
casekeyevent.vk_right:
model.changedirection(snakemodel.right);
casekeyevent.vk_add:
casekeyevent.vk_page_up:
model.speedup();
casekeyevent.vk_subtract:
casekeyevent.vk_page_down:
model.speeddown();
casekeyevent.vk_space:
casekeyevent.vk_p:
model.changepausestate();
default:
//任何情况下处理的按键,按键导致重新启动游戏
if(keycode==keyevent.vk_r||
keycode==keyevent.vk_s||
keycode==keyevent.vk_enter){
model.reset();
publicvoidkeyreleased(keyevente){
publicvoidkeytyped(keyevente){
3、
*
*游戏的model类,负责所有游戏相关数据及运行
58:
33
//snakemodel.java
importjavax.swing.*;
importjava.util.arrays;
importjava.util.linkedlist;
importjava.util.observable;
importjava.util.random;
classsnakemodelextendsobservableimplementsrunnable{
boolean[][]matrix;
//指示位置上有没蛇体或食物
linkedlistnodearray=newlinkedlist();
//蛇体
nodefood;
intmaxx;
intmaxy;
intdirection=2;
//蛇运行的方向
booleanrunning=false;
//运行状态
inttimeinterval=200;
//时间间隔,毫秒
doublespeedchangerate=0.75;
//每次得速度变化率
booleanpaused=false;
//暂停标志
intscore=0;
//得分
intcountmove=0;
//吃到食物前移动的次数
//upanddownshouldbeeven
//rightandleftshouldbeodd
publicstaticfinalintup=2;
publicstaticfinalintdown=4;
publicstaticfinalintleft=1;
publicstaticfinalintright=3;
publicsnakemodel(intmaxx,intmaxy){
this.maxx=maxx;
this.maxy=maxy;
reset();
publicvoidreset(){
direction=snakemodel.up;
timeinterval=200;
paused=false;
score=0;
countmove=0;
//initialmatirx,全部清0
matrix=newboolean[maxx][];
for(inti=0;
i<
maxx;
++i){
matrix[i]=newboolean[maxy];
arrays.fill(matrix[i],false);
//initialthesnake
//初始化蛇体,如果横向位置超过20个,长度为10,否则为横向位置的一半
intinitarraylength=maxx>
20?
10:
maxx/2;
nodearray.clear();
initarraylength;
intx=maxx/2+i;
//maxx被初始化为20
inty=maxy/2;
//max
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 贪吃
![提示](https://static.bdocx.com/images/bang_tan.gif)