📄 snakeunit.java
字号:
package com.cxlife.snake;
/**
* 功能描述 :所有的移动单元都是从这个类派生。
* 该类的职责:绘制移动单元
*/
import java.awt.Graphics;
import java.awt.Component;
//这是个抽象类,用来绘制蛇身
public abstract class SnakeUnit
implements java.io.Serializable,
java.lang.Cloneable,
com.cxlife.snake.Direction {
protected int curr_direction = Direction.EAST;//下一步的移动方向,所有移动单元的下一步的移动方向
//实际上都是由,"蛇头"来控制
protected int next_direction = Direction.EAST;//移动单元当前移动方向,由它决定后一个移动单元的下
//一步骤的移动方向.
//在派生类中要用到这个变量,所以设为保护类型
//protected Graphics2D canvas = null;//画布,所以的绘制动作都是在它身上发生.
protected int width=10; //蛇身的宽度
protected int height=10;//蛇身的高度
//--------------------------------------------------
//这两个值决定蛇身在画布中的位置.
protected int left=10; //蛇身在画布中的左上角的X坐标
protected int top=10; //蛇身在画布中的左上角的Y坐标
protected java.awt.Color snake_color = java.awt.Color.blue;//蛇身的颜色.
//--------------------------------------------------
//--------------------------------------------------
//在指定的构件上.绘制蛇身
//返回值是画完后的画布状态,以便其它模块接着画.
protected abstract void doDraw(final Graphics g);//绘制蛇身
public void draw(final Component pValue){
if (pValue == null)
return ;
Graphics g = pValue.getGraphics();
doDraw(g);
g.dispose();
}
//移动蛇身
//蛇如何移动由下一步移动方位确定.
public void move(final ISnake pSnake,final Component pComponent){
if (pSnake == null || pComponent == null)
return;
pSnake.clearUnit(left,top,width,height);//想清除蛇身
int HStep = pSnake.getHStep();//获取水平方向移动步长
int VStep = pSnake.getVStep();//获取垂直方向移动步长.
//根据移动方向来设置蛇身的位置(left,top)
if (next_direction == Direction.NORTH){
top -= VStep;
} else if (next_direction == Direction.SOUTH){
top += VStep;
} else if (next_direction == Direction.WEAST){
left -= HStep;
} else {
left += HStep;
}
draw(pComponent);//绘制移动单元.
//这个时候的当前运动方向移动发生了变化,和next_direction是一样的.
this.curr_direction = this.next_direction;
}
//这个方法,只负责移动,它不做清除工作。(在画蛇体的时候要用到这个东东)
public void go(final ISnake pSnake,final Component pComponent){
if (pSnake == null || pComponent == null)
return;
int HStep = pSnake.getHStep();//获取水平方向移动步长
int VStep = pSnake.getVStep();//获取垂直方向移动步长.
//根据移动方向来设置蛇身的位置(left,top)
if (next_direction == Direction.NORTH){
top -= VStep;
} else if (next_direction == Direction.SOUTH){
top += VStep;
} else if (next_direction == Direction.WEAST){
left -= HStep;
} else {
left += HStep;
}
draw(pComponent);//绘制移动单元.
//这个时候的当前运动方向移动发生了变化,和next_direction是一样的.
this.curr_direction = this.next_direction;
}
//获取当前移动单元的移动方向.
public int getCurr_direction() {
return curr_direction;
}
//设置当前移动单元的移动方向
public void setCurr_direction(final int curr_direction) {
this.curr_direction = curr_direction;
}
//设置移动单元的下一步的移动方向
public void setNext_direction(final int next_direction) {
this.next_direction = next_direction;
}
//获取移动单元的下一步的移动方向
public int getNext_direction() {
return next_direction;
}
//设置移动单元的颜色
public void setSnake_color(java.awt.Color snake_color) {
this.snake_color = snake_color;
}
//获取移动单元格的颜色
public java.awt.Color getSnake_color() {
return snake_color;
}
//获取移动单元的高度
public int getHeight() {
return height;
}
//获取移动单元左上角横坐标
public int getLeft() {
return left;
}
//设置移动单元的高度
protected void setHeight(final int height) {
this.height = height;
}
//获取移动单元的右上角横坐标
protected void setLeft(final int left) {
this.left = left;
}
//获取移动单元的右上角丛坐标
public int getTop() {
return top;
}
//获取移动单元的宽度
public int getWidth() {
return width;
}
//设置移动单元的位置
protected void setTop(final int top) {
this.top = top;
}
//指定移动单元的宽度
protected void setWidth(final int width) {
this.width = width;
}
public abstract Object clone();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -