⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 block.java

📁 希望大家多多交流
💻 JAVA
字号:
package snake;

import java.awt.*;
import javax.swing.*;

import system.*;

//蛇身的一块(包含蛇头)
public class Block{
  private Image image;//蛇身块的图片
  //蛇头的图片(上,下,左,右)
  private Image head_up;
  private Image head_down;
  private Image head_left;
  private Image head_right;

  private int x = -20;//块的x坐标
  private int y = -20;//块的y坐标

  public static final int WIDTH = 20;//块的宽度
  public static final int HEIGHT = 20;//块的长度

  public Block() {
    //设置蛇身的图片
    this.image = this.loadImage(Config.URL_SNAKE+"block.png");

    //取得蛇头的图片
    this.head_up = this.loadImage(Config.URL_SNAKE+"head_up.png");
    this.head_down = this.loadImage(Config.URL_SNAKE+"head_down.png");
    this.head_left = this.loadImage(Config.URL_SNAKE+"head_left.png");
    this.head_right = this.loadImage(Config.URL_SNAKE+"head_right.png");
  }

  //根据移动方向更改图片,用于蛇头
  public void changeImage(int direction){
    if(direction == Direction.UP){
      this.image = this.head_up;
    }
    else if(direction == Direction.DOWN){
      this.image = this.head_down;
    }
    else if(direction == Direction.LEFT){
      this.image = this.head_left;
    }
    else if(direction == Direction.RIGHT){
      this.image = this.head_right;
    }
  }

  public int getX(){
    return this.x;
  }

  public int getY(){
    return this.y;
  }

  //加载图片
  private Image loadImage(String fileName){
    return new ImageIcon(fileName).getImage();
  }

  //设置块的显示位置
  public void setLocation(int x, int y){
    this.x = x;
    this.y = y;
  }

  //返回块封装的图象
  public Image getImage(){
    return this.image;
  }

  //判断两个块是否在相同位置
  public boolean isAtSamePlace(Block b){
    if(this.x == b.x && this.y == b.y){
      return true;
    }
    return false;
  }
}//:~zj

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -