本帖最后由 yk812 于 2012-01-14 21:27:53 编辑

解决方案 »

  1.   

    import java.awt.Color;
    import java.awt.Graphics;import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.util.LinkedList;public class Snake {
    public static final int SPEED = 30;

    int x;
    int y;
    Yard yard;
    enum Direction {L, U, R, D}
    Direction dir = Direction.L;



    public Snake(int x, int y, Direction dir,Yard yard){
    this.x = x;
    this.y = y;
    this.dir = dir;
    this.yard = yard;
    }

    public void draw(Graphics g){
    Color c = g.getColor();
    g.setColor(Color.RED);
    g.fill3DRect(x, y, 30, 30, true);
    g.setColor(c);
    move();
    }

    private void move(){
    switch(dir){
    case L:
    x = x-SPEED;
    break;
    case U:
    y = y - SPEED;
    break;
    case R:
    x = x+SPEED;
    break;
    case D:
    y = y+SPEED;
    break;
    }

    yard.snakeBody.addFirst(new Point(x,y));
    yard.snakeBody.removeLast();

    if(y<0){
    y = Yard.GAME_HEIGHT ;
    }
    if(y>Yard.GAME_HEIGHT){
    y = 0;
    }
    if(x<0){
    x = Yard.GAME_WIDTH;
    }
    if(x>Yard.GAME_WIDTH){
    x = 0;
    }
    } public void keyReleased(KeyEvent e){
    int keyCode = e.getKeyCode();

    switch (keyCode){
    case KeyEvent.VK_LEFT:
    if(dir!= Direction.R) {dir = Direction.L;}
    break;
    case KeyEvent.VK_UP:
    if(dir!= Direction.D) {dir = Direction.U;}
    break;
    case KeyEvent.VK_RIGHT:
    if(dir!= Direction.L) {dir = Direction.R;}
    break;
    case KeyEvent.VK_DOWN:
    if(dir!= Direction.U) {dir = Direction.D;}
    break;
    }
    } public Rectangle getRect(){
    return new Rectangle(this.x, this.y, Yard.BLOCK_SIZE, Yard.BLOCK_SIZE);
    }

    public boolean knockWall(Wall wall){
    if(this.getRect().intersects(wall.getRect())){
    System.out.println("knock wall" );
    return true;
    }
    return false;
    }

    public boolean knockWallList(java.util.List<Wall> wallList){
    for(int i=0; i<wallList.size(); i++){
    Wall w = wallList.get(i);
    //knockWall(w);
    if(knockWall(w)){
    return true;
    }
    }
    return false;
    }

    public boolean eatEgg(Egg egg){
    if(this.getRect().intersects(egg.getRect())){
    egg.setLive(false);
    yard.snakeBody.add(1,new Point(x,y));
    return true;
    }
    return false;
    } public boolean knockMySelf(LinkedList<Point> snakeBody){

    for(int i=2; i<snakeBody.size(); i++){
    if(new Point(x,y).equals(snakeBody.get(i))){

    System.out.println("knock myself");
    return true;
    }
    }
    return false;
    }
    }
      

  2.   

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;public class Wall {
    public static final int LENGTH = 30;

    private int x;
    private int y;

    public Wall(int x, int y){
    this.x = x;
    this.y = y;
    }

    public void draw(Graphics g){
    Color c = g.getColor();
    g.setColor(Color.DARK_GRAY);
    g.fill3DRect(x, y, LENGTH, LENGTH, true);
    g.setColor(c);
    }

    public Rectangle getRect(){
    return new Rectangle(x, y, LENGTH, LENGTH);
    }
    }
      

  3.   

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;public class Egg {
    public static final int LENGTH = 30;

    private int x;
    private int y;
    private boolean live = true;
    public Egg(int x, int y){
    this.x = x;
    this.y = y;
    }

    public void draw(Graphics g){
    if(live){
    Color c = g.getColor();
    g.setColor(Color.WHITE);
    g.fill3DRect(x, y, LENGTH, LENGTH, true);
    g.setColor(c);
    }
    }

    public Rectangle getRect(){
    return new Rectangle(x, y, LENGTH, LENGTH);
    } public boolean isLive() {
    return live;
    } public void setLive(boolean live) {
    this.live = live;
    }


    }
      

  4.   


    真完全自己编写的,有BUG和不完善的地方。有些逻辑感觉越写越乱。
    我打算再学几个月,回头吧代码重构一下。另外问下,我的代码发出来格式怎么这么乱啊
      

  5.   

    给你代码前后加上
    [code =Java][/code]
      

  6.   

    对于类的静态变量访问,不需要使用this关键字