package com.tarena.test;
import java.awt.Color;
import javax.swing.JFrame;//Frame框架,相框
import javax.swing.JPanel;//panel面板
import javax.swing.border.LineBorder;
import java.awt.Graphics;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
/*
* 显示窗口与绘图
*/
public class JFrameDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("窗口");
Stage pane = new Stage();
frame.setLayout(null);//取消窗口的默认布局,取消自动充满
frame.add(pane);//窗口添加面板
pane.setSize(10*35,10*35);//面板大小
pane.setLocation(50, 50);//面板位置
frame.setSize(450, 480);
pane.setBorder(new LineBorder(Color.black));//添加边框
frame.setLocationRelativeTo(null);//frame居中
frame.setVisible(true);
//在Swing中如下代码可以实现对键盘事件的监听,
//也就是获得到底哪个键盘按键被按下了
pane.requestFocus();//获取输入“焦点”
//也就是使pane变成键盘输入的目标
//继承KeyAdapter比实现KeyListener更加简洁
//在pane上添加键盘事件的监听,获得到底哪个按键被按下
pane.addKeyListener(new KeyListener(){
//在按键按下的时候执行的方法
public void keyPressed(KeyEvent e){
System.out.println("HI"+e.getKeyCode());
}
//在按键释放的时候执行的方法
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
});
}
}
class Stage extends JPanel{
//重写了默认的绘图方法
public void paint(Graphics g){//paint
g.setColor(Color.darkGray);
g.fillRect(0, 0, this.getWidth(), getHeight());
g.setColor(Color.RED);
g.fill3DRect(50, 50, 30, 20, true);
}
}
package com.tarena.test;
import org.junit.Test;
import com.tarena.worm.Worm;
import com.tarena.worm.Cell;
import com.tarena.worm.WormStage;
/*
* test 测试
* case 案例
*/
public class TestCase {
// @Test//来自JUnit的注释标记,用于执行测试方法
public void testWormInit() {
System.out.println("测试Worm构造器");
Worm worm = new Worm();
}
// @Test
public void testWormContains() {
System.out.println("测试Worm包含算法");
Worm worm = new Worm();
System.out.println(worm);
System.out.println(worm.contains(2, 0));// true
System.out.println(worm.contains(5, 6));// false
}
// @Test
public void testWormStage() {
System.out.println("创建舞台实例");
WormStage stage = new WormStage();
System.out.println(stage);
}
@Test
public void testCreep() {
System.out.println("爬行测试");
Worm worm = new Worm();
System.out.println(worm);
worm.creep();
System.out.println(worm);
}
@Test
public void testCreepForFood() {
System.out.println("检查食物的爬行");
Worm worm = new Worm();
Cell food = new Cell(1, 2);
System.out.println(worm);
System.out.println(worm.creep(Worm.DOWN, food));
System.out.println(worm);
System.out.println(worm.creep(Worm.DOWN, food));
System.out.println(worm);
System.out.println(worm.creep(Worm.RIGHT, food));
System.out.println(worm);
}
@Test
public void testHit(){
System.out.println("碰撞测试");
Worm worm = new Worm();
Cell food = new Cell(10,10);
System.out.println(worm);
System.out.println(worm.creep(Worm.DOWN,food));
System.out.println(worm);
System.out.println(worm.creep(Worm.DOWN,food));
System.out.println(worm);
System.out.println(worm.hit(Worm.LEFT));
System.out.println(worm.hit(Worm.RIGHT));
System.out.println(worm.creep(Worm.RIGHT,food));
System.out.println(worm);
System.out.println(worm.creep(Worm.RIGHT,food));
System.out.println(worm);
System.out.println(worm.creep(Worm.UP,food));
System.out.println(worm);
System.out.println(worm.creep(Worm.UP,food));
System.out.println(worm);
}
}
package com.tarena.worm;
/*
* 一个单元格子
*/
public class Cell {
private int x;
private int y;
public Cell(){
}
public Cell(int x,int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public void setX(int x){
this.x = x;
}
public int getY(){
return y;
}
public void setY(int y){
this.y = y;
}
}
package com.tarena.worm;
import com.tarena.worm.Worm;
public class Test {
public static void main(String[] args) {
Worm worm = new Worm();
System.out.println(worm);
}
}
package com.tarena.worm;
import java.util.Arrays;
/*
* 贪吃蛇
*/
public class Worm {
public static final int DEFAULT_LENGTH = 12;
public static final int UP = 1;
public static final int DOWN = -1;
public static final int LEFT = 2;
public static final int RIGHT = -2;
private Cell[] cells;
/*蛇当前的运行方向*/
private int currentDirection;
public Worm() {
cells = new Cell[DEFAULT_LENGTH];
for (int i = 0; i < cells.length; i++) {
cells[i] = new Cell(i, 0);
}
currentDirection = DOWN;
// cells = new Cell[]{new Cell(0,0),new Cell(1,0),new Cell(2,0),new
// Cell(3,0)};
}
public boolean contains(int x, int y) {
for (int i = 0; i < cells.length; i++) {
Cell node = cells[i];
if (node.getX() == x && node.getY() == y) {
return true;
}
}
return false;
}
/*
* 1) 计算currentDirection与direction的和,
* 如果是0表示反向了,就结束方法返回,不进行任何动作
* 2) currentDirecton = direction改变当前的方向,作为下次运行的方向
* 3) 判断当前头节点的坐标与食物对象的坐标是否一致,如果一致说明是吃到食物了
* 4) 如果吃到食物,就将cells数组进行扩容,将cells数组内容的每个元素向后移动。
* 5)将新头节点插入到头的位置cells[0]=newHead
* 6) 返回是否吃到食物
*/
public boolean creep(int direction,Cell food){
if(currentDirection + direction ==0){
return false;//反向了,不进行任何动作
}
currentDirection = direction;
Cell head = createHead(direction);
boolean eat = head.getX() == food.getX() &&
head.getY() ==food.getY();
/*
boolean eat = false;
if(head.getX()==food.getX() && head.getY()==food.getY()){
eat = true;
}
*/
if(eat){
cells = Arrays.copyOf(cells,cells.length+1);//扩容
}
for(int i=cells.length-1;i>=1;i--){
cells[i] =cells[i-1];
}
cells[0] = head;
return eat;
}
public boolean hit(){
return hit(currentDirection);
}
public boolean hit(int direction){
Cell head = createHead(direction);
if(head.getX()<0 || head.getX()>=WormStage.COLS ||
head.getY()<0 || head.getY()>=WormStage.ROWS){
return true;
}
for(int i=0;i=1;i--){
cells[i] = cells[i-1];
}
cells[0] = createHead(currentDirection);
}
private Cell createHead(int direction){
int x = cells[0].getX();
int y = cells[0].getY();
switch(direction){
case DOWN:y++;break;
case UP:y--;break;
case LEFT: x--;break;
case RIGHT: x++;break;
}
return new Cell(x,y);
}
public Cell[] getCells(){
return Arrays.copyOf(cells, cells.length);
}
public String toString() {
return Arrays.toString(cells);
}
}
package com.tarena.worm;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
//import com.tarena.test.Stage;
public class WormStage extends JPanel {
//行数 常量
public static final int ROWS = 35;
//列数
public static final int COLS = 35;
//格子大小10个像素
public static final int CELL_SIZE = 35;
private Worm worm;
private Cell food;
public WormStage(){
worm = new Worm();
food = createFood();
}
/*
* 生成一个食物
* 1 生成随机数x,y
* 2 检查蛇是否包含x,y
* 2.1如果包含返回1
* 3创建食物节点
*/
private Cell createFood(){
int x;
int y;
Random r = new Random();
do{
x = r.nextInt(COLS);
y = r.nextInt(ROWS);
}while(worm.contains(x, y));
return new Cell(x,y);
}
public String toString(){
return "worm:"+worm+"\nfood:"+food;
}
/*
* 重写绘图方法
*/
public void paint(Graphics g){
//填充背景色
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,getWidth(),getHeight());
//绘制食物
g.setColor(Color.RED);
g.fill3DRect(CELL_SIZE*food.getX(), CELL_SIZE*food.getY(), CELL_SIZE, CELL_SIZE,true);
//绘制蛇
g.setColor(Color.GREEN);
Cell[] cells = worm.getCells();
for(int i=0;i
运行WormStage.java文件