达内学习心得和TTS 有所不同的贪吃蛇 能穿墙而不是撞墙.doc_第1页
达内学习心得和TTS 有所不同的贪吃蛇 能穿墙而不是撞墙.doc_第2页
达内学习心得和TTS 有所不同的贪吃蛇 能穿墙而不是撞墙.doc_第3页
达内学习心得和TTS 有所不同的贪吃蛇 能穿墙而不是撞墙.doc_第4页
达内学习心得和TTS 有所不同的贪吃蛇 能穿墙而不是撞墙.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

达内学习心得:和TTS 有所不同的贪吃蛇 能穿墙而不是撞墙参赛人:陈彬豪获得奖项:三等奖内容: 第一步,就是先把单个格子的属性创建出来public class Cell int row; int col; int color; static int indexRow = 10000;/这个参数是上下穿墙用的,为了保证不用完,要尽可能的大 static int indexCol = 10000;/这个参数是左右穿墙用的,为了保证不用完,要尽可能的大 public Cell()/对于父类,无参数构造器不一定用到,应该习惯的写上 public Cell(int x, int y, int color) super(); this.row = x; this.col = y; this.color = color; /各属性的get set方法 public int getColor() return color; public void setColor(int color) this.color = color; public int getRow() return row; public void setRow(int x) this.row = x; public int getCol() return col; public void setCol(int y) this.col = y; /*向X轴负正向走*/ public void left() row = (indexRow+(-row)%40;/*这就是穿墙的关键,40是贪吃蛇墙的长,40就能使蛇走不出墙,注意!(-row)不能写成(row-) */ /*向X轴负方向走*/ public void right() row= (indexRow+(+row)%40; /*向Y轴负方向走*/ public void up() col = (indexCol+(-col)%40; /*向Y轴正方向走*/ public void down() col = (indexCol+(+col)%40; public String toString() return row+,+col; 第二步,对蛇身的构造,以及蛇运动的方法import java.util.Arrays;public class Body extends Cell /*蛇身的颜色*/ public static final int BADY_COLOR = 0x66ccff; /* 身体 */ public Cell bodys; /*头*/ protected Cell head; public Body() bodys = new Cell5; head = new Cell(4,10,BADY_COLOR); bodys0 = head; bodys1 = new Cell(3,10,BADY_COLOR); bodys2 = new Cell(2,10,BADY_COLOR); bodys3 = new Cell(1,10,BADY_COLOR); bodys4 = new Cell(0,10,BADY_COLOR); /*绝对左移*/ public Cell moveLeft() int i; worm Worm = new worm(); /当前蛇身的长度 int length = Worm.getBodyCellsLength(); /对蛇身数组的扩容 bodys = Arrays.copyOf(bodys, length); for(i = length-1;i0;i-) if(bodysi=null)/如果吃了食物就会扩容,扩容的新元素为null bodysi=new Cell(); bodysi.col=bodysi-1.col; bodysi.row=bodysi-1.row; head.left(); return bodys; /*绝对左移*/ public Cell moveRight() int i; worm Worm = new worm(); int length = Worm.getBodyCellsLength(); bodys = Arrays.copyOf(bodys, length); for(i = length-1;i0;i-) if(bodysi=null) bodysi=new Cell(); bodysi.setCol(bodysi-1.col); bodysi.setRow(bodysi-1.row); head.right(); return bodys; /*绝对上移*/ public Cell goUp() int i; worm Worm = new worm(); int length = Worm.getBodyCellsLength(); bodys = Arrays.copyOf(bodys, length); for(i = length-1;i0;i-) if(bodysi=null) bodysi=new Cell(); bodysi.col=bodysi-1.col; bodysi.row=bodysi-1.row; head.up(); return bodys; /*绝对下移*/ public Cell goDown() int i; worm Worm = new worm(); int length = Worm.getBodyCellsLength(); bodys = Arrays.copyOf(bodys, length); for(i = length-1;i0;i-) if(bodysi=null) bodysi=new Cell(); bodysi.col=bodysi-1.col; bodysi.row=bodysi-1.row; head.down(); return bodys; public Cell getBody() return bodys; public String toString() return Arrays.toString(bodys); 第三步 绘制动作方法和绘图import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.LayoutManager;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.image.ImageObserver;import java.util.Arrays;import java.util.Random;import java.util.Timer;import java.util.TimerTask;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class worm extends JPanel public static final int ROWS = 40; public static final int COLS = 40; private Cell wall = new CellROWSCOLS; /* 每一格的大小规格 */ public static final int CELL_SIZE = 10; /* 与边框的距离 */ public static final int ADD = 20; public static final int BG_COLOR = 0xc3d5ea; /* 背景色 */ public static final int FONT_COLOR = 0xffffff; /* 边框线条颜色 */ public static final int BORDER_COLOR = 0x667799; /* 食物的颜色 */ public static final int FOOD_COLOR = 0xff0000; /* 蛇身的颜色 */ public static final int BADY_COLOR = 0x66ccff; /* 是否吃到的判定 */ public static boolean eat = false; /* 正在向绝对上走 */ public static boolean goingUp = false; /* 正在向绝对下走 */ public static boolean goingDwon = false; /* 正在向绝对右走 */ public static boolean moveingRight = false; /* 正在向绝对左走 */ public static boolean moveingLeft = false; /*游戏结束*/ public static boolean gameOver = false; /*暂停判定*/ public static boolean pause = false; /*速度*/ public static int speed; /*分数*/ public static int score; /* 食物 */ public static Cell food; /* 吃到食物前一格时的最后一格 */ public Cell tempBody; public Timer timer; public static Body body; /* 蛇身 */ public static Cell bodyCells; public static JFrame frame = new JFrame(贪吃蛇); public static worm panel = new worm(null); / -开始- public worm(LayoutManager layout) super(layout); public worm() public static void main(String args) Random ra = new Random(); /* 食物 */ food = new Cell(ra.nextInt(ROWS), ra.nextInt(COLS), FOOD_COLOR); frame.setSize(COLS +7) * CELL_SIZE-20, (ROWS+8) * CELL_SIZE + 90); frame.setResizable(false);/禁止窗口缩放 frame.setLocationRelativeTo(null);/ 窗口居中/null是窗口的相对位置,没有就是居中 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/ 设置关闭窗口就关闭软件 frame.add(panel);/ 窗口中添加面板 frame.setVisible(true);/ 显示窗口 panel.action(); /* 动作流程 */ private void action() startAction(); this.requestFocus();/ 为当前面板请求获得输入焦点,this对象就获得了输入焦点,以后任何的键盘输入目标就是这个面板 this.addKeyListener(new KeyListener() / 添加键盘监听, public void keyPressed(KeyEvent e) / 按键按下去 System.out.println(type: + e.getKeyCode(); int key = e.getKeyCode(); if(gameOver!= true) switch (key) case A: if(goingUp|goingDwon) moveLeftAction(); pause=false; break; case D: if(goingUp | goingDwon) moveRightAction(); pause=false; break; case W: if(moveingRight | moveingLeft) goUpAction(); pause=false; break; case S: if(moveingLeft | moveingRight) goDwonAction(); pause=false; break; case KeyEvent.VK_SPACE: if(pause=false) pauseAction(); pause=true; repaint(); if(key=KeyEvent.VK_Q) gameOver=true; pause=false; timer.cancel(); repaint(); if(gameOver) if(key=KeyEvent.VK_ENTER) startAction(); /*ESC 退出系统*/ if(key=KeyEvent.VK_ESCAPE) System.exit(0); public void keyReleased(KeyEvent e) int key = e.getKeyCode(); / 按键松开 public void keyTyped(KeyEvent e) int key = e.getKeyCode(); if(key=T) System.exit(0); / 按键按一下 ); /*开始流程*/ public void startAction() gameOver = false; pause = false; score = 0; level(); body = new Body(); bodyCells = body.bodys; newFood(); moveingRight = true; timer = new Timer(); timer.schedule(new TimerTask() public void run() TempBody(bodyCells); bodyCells = body.moveRight(); level(); gameOverAction(); bodyCells = grow(bodyCells, food, tempBody); repaint(); , 500, speed); repaint(); /*结束流程*/ public void gameOverAction() if (gameOver() System.out.println(gameOver); timer.cancel(); private void pauseAction() timer.cancel(); pause = true; /* 向上走流程 */ private void goUpAction() timer.cancel(); goingUp = true; goingDwon = false; moveingRight = false; moveingLeft = false; level(); timer = new Timer(); timer.schedule(new TimerTask() public void run() TempBody(bodyCells); bodyCells = body.goUp(); gameOverAction(); bodyCells = grow(bodyCells, food, tempBody); repaint(); , 0, speed); /* 向下走流程 */ private void goDwonAction() timer.cancel(); goingUp = false; goingDwon = true; moveingRight = false; moveingLeft = false; level(); timer = new Timer(); timer.schedule(new TimerTask() public void run() TempBody(bodyCells); bodyCells = body.goDown(); gameOverAction(); bodyCells = grow(bodyCells, food, tempBody); repaint(); , 0, speed); /* 向左走流程 */ private void moveLeftAction() timer.cancel(); goingUp = false; goingDwon = false; moveingRight = false; moveingLeft = true; level(); timer = new Timer(); timer.schedule(new TimerTask() public void run() TempBody(bodyCells); bodyCells = body.moveLeft(); gameOverAction(); bodyCells = grow(bodyCells, food, tempBody); repaint(); , 0, speed); /* 想右走流程 */ private void moveRightAction() timer.cancel(); goingUp = false; goingDwon = false; moveingRight = true; moveingLeft = false; level(); timer = new Timer(); timer.schedule(new TimerTask() public void run() TempBody(bodyCells); bodyCells = body.moveRight(); gameOverAction(); bodyCells = grow(bodyCells, food, tempBody); repaint(); , 0, speed); /* 吃到食物前一格时的最后一格 */ public void TempBody(Cell bodyCells) tempBody = new Cell(bodyCellsbodyCells.length - 1.row, bodyCellsbodyCells.length - 1.col, bodyCellsbodyCells.length - 1.color); /* 增加1格 tempBody:吃到食物前一格时的最后一格 */ public Cell grow(Cell bodyCells, Cell food, Cell tempBody) if (eatFood(bodyCells0, food) bodyCells = Arrays.copyOf(bodyCells, bodyCells.length + 1); bodyCellsbodyCells.length - 1 = tempBody; score = (bodyCells.length-5)*10; newFood(); return bodyCells; return bodyCells; /* 判断是否吃到食物 */ public static boolean eatFood(Cell head, Cell food) eat = food.row = head.row & food.col = head.col; return eat; /* 刷新食物 */ public void newFood() if (eat) Random ra = new Random(); /* 食物 */ food = new Cell(ra.nextInt(20), ra.nextInt(20), FOOD_COLOR); for(Cell cell: bodyCells) if(food.row=cell.row & food.col= cell.col) newFood(); eat = false; private void level() int index = 100; int scoreIndex = score; speed=200; if(scoreIndex=index) speed-=50; scoreIndex-=index; if(scoreIndex=index) speed-=50; scoreIndex-=index; index=50; if(scoreIndex=index) speed-=50; scoreIndex-=index; index=100; if(scoreIndex=index) speed-=10; scoreIndex-=index; /* 游戏结束判定 */ private boolean gameOver() for (int i = 1; i bodyCells.length; i+) gameOver = bodyCells0.row = bodyCellsi.row & bodyCells0.col = bodyCellsi.col; if (gameOver = true) return gameOver; return gameOver; public int getBodyCellsLength() return bodyCells.length; public worm getworm() worm we = new worm(); return we; / - /* JPanel 类利用paint(图画)方法绘制界面,子类重写paint方法可以修改绘图逻辑 */ public void paint(Graphics g) paintBackBorder(g); /paintWall(g);/绘制墙的格子,方便蛇的运动,但玩的时候最好注释掉,这要更美观 paintBody(g); paintFood(g); paintScore(g); paintTetrisBorder(g); /* 填充背景 */ private void paintBackBorder(Graphics g) g.setColor(new Color(0x000000); g.fillRect(ADD, ADD+CELL_SIZE * 10, CELL_SIZE * ROWS, CELL_SIZE * COLS); g.setColor(new Color(0xc3d5ea); g.fillRect(ADD, ADD, CELL_SIZE *ROWS, CELL_SIZE * 10); /* 绘制墙 */ private void paintWall(Graphics g) for (int row = 0; row ROWS; row+) for (int col = 0; col COLS; col+) Cell cell = wallrowcol; / 墙的格子/ g.setColor(new Color(BORDER_COLOR);/ g.drawRect(ADD +col * CELL_SIZE, ADD+10*CELL_SIZE+row * CELL_SIZE,/ CELL_SIZE, CELL_SIZE); /* 绘制食物 */ private void paintFood(Graphics g) int row = food.getRow(); int col = food.getCol(); g.setColor(new Color(food.getColor(); g.fill3DRect(ADD + row * CELL_SIZE, ADD + col * CELL_SIZE+CELL_SIZE * 10, CELL_SIZE, CELL_SIZE, true); g.setColor(new Color(0xffffff); g.draw3DRect(ADD + row * CELL_SIZE, ADD + col * CELL_SIZE+CELL_SIZE * 10, CELL_SIZE, CELL_SIZE, true); /* 绘制蛇身 */ private void paintBody(Graphics g) for (Cell cell : bodyCells) int row = cell.getRow(); int col = cell.getCol(); g.setColor(new Color(0x66ccff); g.fill3DRect(ADD + row * CELL_SIZE, ADD + col * CELL_SIZE+CELL_SIZE * 10, CELL_SIZE, CELL_SIZE, true); g.setColor(new Color(0xffffff); g.draw3DRect(ADD + row * CELL_SIZE, ADD + col * CELL_SIZE+CELL_SIZE * 10, CELL_SIZE, CELL_SIZE, true); /* 绘制文字 */ private void paintScore(Graphics g) Font font = new Font(getFont().getName(), Font.BOLD, 35); String str = SCORE : + score; g.setColor(new Color(FONT_COLOR); g.setFont(font); g.drawString(str, 13 * CELL_SIZE, 7 * CELL_SIZE); str = SPACEPAUSE; font = new Font(getFont().getName(), Font.BOLD, 20); g.setFont(font); g.drawString(str, 4 *

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论