PNG高塔任务游戏 java编写哺育达东说念主入门者


发布日期:2023-12-19 11:15    点击次数:90


   java小门径代码PNG高塔任务游戏

  用具/原料eclipse情势/法子1import java.awt.image.BufferedImage;

  public abstract class Fish {public BufferedImage image;//每条鱼的沿路是由多张图片构成public BufferedImage [] images;public int width;public int height;public int x;public int y;//每条鱼齐有我方专有的走路神志public abstract void step();}

  2import java.awt.image.BufferedImage;

  import javax.imageio.ImageIO;import javax.swing.JPanel;

  public class FishGame extends JPanel{public static final int WIDTH=700;public static final int HEIGHT=400;public static BufferedImage buyu;public static BufferedImage fy1;public static BufferedImage fy2;public static BufferedImage fy3;public static BufferedImage fy4;public static BufferedImage fy5;public static BufferedImage fy6;public static BufferedImage fy7;public static BufferedImage fy8;public static BufferedImage fy9;public static BufferedImage fy10;public static BufferedImage fy11;public static BufferedImage fy12;public static BufferedImage fy13;public static BufferedImage fy14;public static BufferedImage fy15;public static BufferedImage fy16;public static BufferedImage fy17;

  3public static BufferedImage zy1;public static BufferedImage zy2;public static BufferedImage zy3;public static BufferedImage zy4;public static BufferedImage zy5;public static BufferedImage zy6;public static BufferedImage zy7;public static BufferedImage zy8;public static BufferedImage zy9;public static BufferedImage zy10;public static BufferedImage zy11;public static BufferedImage zy12;public static BufferedImage zy13;public static BufferedImage zy14;public static BufferedImage zy15;public static BufferedImage zy16;

  4//在静态代码块中对图片进行开动化操作,静态代码块会跟着类的加载就延迟//而况只延迟一次,是以静态代码块绝顶符合作念资源加载static{try{buyu=ImageIO.read(FishGame.class.getResource("backgroundimage/buyu.jpg"));}catch( Exception e ){System.out.println("图片未找到");}}

  5public void paint(Graphics g) {g.drawImage(buyu, 0, 0, null);for (int i = 0; i < Fy.length; i++) {Fy fy = Fy[i];g.drawImage(fy.image, fy.x, fy.y, null);}}

  // 加多鱼// int index = 0;

  public void addOneFy() {if (true) {Fy = Arrays.copyOf(Fy, Fy.length + 1);Fy[Fy.length - 1] = nextOneFy();}}

  // 整个的鱼齐移动一步public void stepAction() {for (int i = 0; i < Fy.length; i++) {Fy fy = Fy[i];fy.step();}}

  Timer timer = new Timer();

  public void action() {timer.schedule(new TimerTask() {

  @Overridepublic void run() {addOneFy();stepAction();repaint();}

  }, 0, 1000);}

  // 产生一个Fy类的鱼public Fy nextOneFy() {return new Fy();}

  6public static void main(String[] args) {JFrame frame = new JFrame();FishGame game = new FishGame();frame.add(game);frame.setTitle("哺育达东说念主");frame.setSize(buyu.getWidth(), buyu.getHeight());frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setAlwaysOnTop(true);frame.setLocationRelativeTo(null);frame.setVisible(true);

  game.action();}

  ENDPNG高塔任务游戏