那些年,我还在学习java
那些年接下来的课程就是java、jsp了,java语言还是很好的,对于我个人而言,只是对它的开发工具不是很满意,就是智能感应还没有自已敲来得快,当然这也是小事;在此已经了解了很多语言了,从C/C++到C#,现在开始学习另一门面向对象的语言,那就是java了,还是从基础开始,数据类型、读取文件、线程、网络编程、图形开发,applet等,还有java有很多包,比如说读取文件的三方包,等到了jsp时,还有javabean、servlet,后面还有SSH,这些内容也不是很多,当然今天只是java,将以一个游戏来完结它,下面来看一看吧。
这是一个很老的游戏—贪吃蛇,简单说一下这个游戏是怎么设计的:由一个主类作为游戏运行的入口,将贪吃蛇分为头、身二个类,还有一个食物类,主类继承自Frame类,通过键盘监听来完成蛇的移动,同时身体也跟着移动,当蛇头撞到食时,身体加增长,食物消,随机生成下一个食物,游戏继续,当蛇头撞到蛇身时,游戏结束,下面看一下代码吧!
1、蛇头类SnakeHead
public class SnakeHead { /************ 属性区 **************/ /** * 蛇头的每一节的坐标 */ public int snakeHeadX; public int snakeHeadY; /** * 蛇头的宽高,由于是正方形,所以宽高一样 */ public int snakeBodyWidthHeight = 20; /** * 蛇头的颜色 */ public Color color = Color.red; /** * 蛇移动的方向,让蛇头自动移动,上、下、左、右 */ public boolean directionUp, directionDown, directionLeft, directionRight; /** * 得到控制台的引用 */ public SnakeStart snakeStart = null; /** * 游戏是否结束 */ public boolean snakeDead = false; /** * 玩家的得分数 */ public int score = 0; /************** 方法区 ******************/ /** * constructor * * @param snakeBodyX * @param snakeHeadY * @param color */ public SnakeHead(int snakeHeadX, int snakeHeadY, SnakeStart snakeStart) { this.snakeHeadX = snakeHeadX; this.snakeHeadY = snakeHeadY; this.snakeStart = snakeStart; directionRight = true; } /** * 画出蛇头 * * @param g 画笔,向控制台画出蛇身 */ public void drawSnakeHead(Graphics g) { g.setColor(Color.BLACK); g.drawString("你的得分score:"+score, 10, 50); g.setColor(Color.darkGray); g.drawRect(snakeHeadX, snakeHeadY, snakeBodyWidthHeight, snakeBodyWidthHeight); g.setColor(color); g.fillRect(snakeHeadX, snakeHeadY, snakeBodyWidthHeight, snakeBodyWidthHeight); if (!snakeDead) autoMoveSnakeHead(); } /** * 蛇头的移动,并且吃食物 */ public void moveSnakeHead(int snakeBodyX, int snakeBodyY) { // 移动蛇头 this.snakeHeadX += snakeBodyX; this.snakeHeadY += snakeBodyY; if (this.snakeHeadX < 0) { this.snakeHeadX = snakeStart.frameWidth - 20; } if (this.snakeHeadX > snakeStart.frameWidth - 20) { this.snakeHeadX = 0; } if (this.snakeHeadY < 15) { this.snakeHeadY = snakeStart.frmaeHeight - 20; } if (this.snakeHeadY + 20 > snakeStart.frmaeHeight) { this.snakeHeadY = 25; } snakeDead(); eatFood(); } /** * 吃食物,通过二种检测的方式,对食物进行检测,完善BUG */ public void eatFood() { if ((this.snakeHeadX == snakeStart.snakeFood.snakeFoodX && this.snakeHeadY == snakeStart.snakeFood.snakeFoodY) || this.getRectangle().intersects( snakeStart.snakeFood.getRectangle())) { snakeStart.snakeFood.isSnakeBody = true; score+=10; snakeStart.snakeBody.snakeBody.add(snakeStart.snakeFood); snakeStart.hasFood = false; } } /** * 判断蛇是否死了,结束游戏 */ public void snakeDead() { for (int i = 0; i < snakeStart.snakeBody.snakeBody.size(); i++) { if (this.snakeHeadX == snakeStart.snakeBody.snakeBody.get(i).snakeFoodX && this.snakeHeadY == snakeStart.snakeBody.snakeBody.get(i).snakeFoodY && snakeStart.snakeBody.snakeBody.get(i).isSnakeBody) { snakeDead = true; } } } /** * 自动移动蛇头,按方向 */ public void autoMoveSnakeHead() { if (directionUp && !directionDown && !directionLeft && !directionRight) { moveSnakeHead(0, -20); } if (!directionUp && directionDown && !directionLeft && !directionRight) { moveSnakeHead(0, 20); } if (!directionUp && !directionDown && directionLeft && !directionRight) { moveSnakeHead(-20, 0); } if (!directionUp && !directionDown && !directionLeft && directionRight) { moveSnakeHead(20, 0); } snakeStart.snakeBody.moveSnakeBody(this); } /** * 按下键盘的监听函数,移动蛇头 */ public void keyPressEvent(KeyEvent event) { int code = event.getKeyCode(); directionUp = directionDown = directionLeft = directionRight = false; switch (code) { case KeyEvent.VK_LEFT: moveSnakeHead(-20, 0); directionLeft = true; break; case KeyEvent.VK_RIGHT: moveSnakeHead(20, 0); directionRight = true; break; case KeyEvent.VK_UP: moveSnakeHead(0, -20); directionUp = true; break; case KeyEvent.VK_DOWN: moveSnakeHead(0, 20); directionDown = true; break; case KeyEvent.VK_SPACE: break; default: moveSnakeHead(0, 0); break; } } /** * 得到食物的矩形 */ public Rectangle getRectangle() { return new Rectangle(snakeHeadX, snakeHeadY, snakeBodyWidthHeight, snakeBodyWidthHeight); } }
2、蛇身类SnakeBody
public class SnakeBody { /************ 属性区 **************/ /** * 蛇身的集合对象 */ public List<SnakeFood> snakeBody = new ArrayList<SnakeFood>(); /************** 方法区 ******************/ /** * constructor */ public SnakeBody(SnakeHead snakeHead) { SnakeFood foodOne = new SnakeFood(snakeHead.snakeHeadX, snakeHead.snakeHeadY, Color.red); foodOne.isSnakeBody=true; snakeBody.add(foodOne); SnakeFood foodTwo = new SnakeFood(snakeHead.snakeHeadX, snakeHead.snakeHeadY - 20, Color.green); foodTwo.isSnakeBody=true; snakeBody.add(foodTwo); } /** * 画出蛇身 * * @param g * 画笔,向控制台画出蛇身 */ public void drawSnakeBody(Graphics g) { for (int i = 0; i < snakeBody.size(); i++) { snakeBody.get(i).drawSnakeBody(g); } } /** * 蛇身的移动 */ public void moveSnakeBody(SnakeHead snakeHead) { for (int i = snakeBody.size() - 1; i > 0; i--) { snakeBody.get(i).snakeFoodX = snakeBody.get(i - 1).snakeFoodX; snakeBody.get(i).snakeFoodY = snakeBody.get(i - 1).snakeFoodY; } snakeBody.get(0).snakeFoodX = snakeHead.snakeHeadX; snakeBody.get(0).snakeFoodY = snakeHead.snakeHeadY; } }
3、食物类
View Code
public class SnakeFood {
/************ 属性区 **************/
/**
* 食物的的坐标
*/
public int snakeFoodX;
public int snakeFoodY;
/**
* 食物的宽高,由于是正方形,所以宽高一样
*/
public int snakeBodyWidthHeight = 20;
/**
* 食物的颜色
*/
public Color color = Color.black;
/**
* 是否是变为了蛇的身体
*/
public boolean isSnakeBody;
/************** 方法区 ******************/
/**
* constructor
*
* @param snakeBodyX
* @param snakeBodyY
* @param color
*/
public SnakeFood(int snakeFoodX, int snakeFoodY, Color color) {
if (snakeFoodY < 30) {
snakeFoodY += 40;
}
this.snakeFoodX = snakeFoodX;
this.snakeFoodY = snakeFoodY;
this.color = color;
}
/**
* 画出食物
*
* @param g
* 画笔,向控制台画出蛇身
*/
public void drawSnakeBody(Graphics g) {
g.setColor(Color.DARK_GRAY);
g.drawRect(snakeFoodX, snakeFoodY, snakeBodyWidthHeight,
snakeBodyWidthHeight);
g.setColor(color);
g.fillRect(snakeFoodX, snakeFoodY, snakeBodyWidthHeight,
snakeBodyWidthHeight);
}
/**
* 得到食物的矩形
*/
public Rectangle getRectangle() {
return new Rectangle(snakeFoodX, snakeFoodY, snakeBodyWidthHeight,
snakeBodyWidthHeight);
}
}
4、控制类
SnackStart
5、运行测式
总结
那些年开始学习java,一时还有点不适应,后来觉得也没有什么,现在android开发也使用java,所以学习java是很明显的,正所谓“技多不压身嘛”。此文以回忆那些年学习java的日子。