【学习心得】
第三步 绘制动作方法和绘图
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.LayoutManager;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.image.ImageObserver;
importjava.util.Arrays;
importjava.util.Random;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
public class wormextends JPanel {
public static final int ROWS = 40;
public static final int COLS = 40;
private Cell[][] wall = newCell[ROWS][COLS];
/** 每一格的大小规格 */
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 = newJFrame("贪吃蛇");
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 voidkeyPressed(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(KeyEvente) {
int key =e.getKeyCode();
}// 按键松开
public voidkeyTyped(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 = newCell(bodyCells[bodyCells.length - 1].row,
bodyCells[bodyCells.length -1].col,
bodyCells[bodyCells.length -1].color);
}
/** 增加1格 tempBody:吃到食物前一格时的最后一格 */
public Cell[] grow(Cell[] bodyCells, Cellfood, Cell tempBody) {
if (eatFood(bodyCells[0], food)) {
bodyCells =Arrays.copyOf(bodyCells, bodyCells.length + 1);
bodyCells[bodyCells.length - 1] =tempBody;
score = (bodyCells.length-5)*10;
newFood();
return bodyCells;
}
return bodyCells;
}
第三步 绘制动作方法和绘图
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.LayoutManager;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.image.ImageObserver;
importjava.util.Arrays;
importjava.util.Random;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
public class wormextends JPanel {
public static final int ROWS = 40;
public static final int COLS = 40;
private Cell[][] wall = newCell[ROWS][COLS];
/** 每一格的大小规格 */
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 = newJFrame("贪吃蛇");
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 voidkeyPressed(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(KeyEvente) {
int key =e.getKeyCode();
}// 按键松开
public voidkeyTyped(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 = newCell(bodyCells[bodyCells.length - 1].row,
bodyCells[bodyCells.length -1].col,
bodyCells[bodyCells.length -1].color);
}
/** 增加1格 tempBody:吃到食物前一格时的最后一格 */
public Cell[] grow(Cell[] bodyCells, Cellfood, Cell tempBody) {
if (eatFood(bodyCells[0], food)) {
bodyCells =Arrays.copyOf(bodyCells, bodyCells.length + 1);
bodyCells[bodyCells.length - 1] =tempBody;
score = (bodyCells.length-5)*10;
newFood();
return bodyCells;
}
return bodyCells;
}