java黄金矿工源代码
时间: 2024-01-30 08:12:10 浏览: 92
以下是Java黄金矿工游戏的源代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class GoldMinerGame extends JFrame {
private ArrayList<Gold> objectList;
public GoldMinerGame() {
objectList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
double random = Math.random();
if (random < 0.3) {
objectList.add(new Gold.GoldMini());
} else if (random < 0.7) {
objectList.add(new Gold());
} else {
objectList.add(new Gold.GoldPlus());
}
}
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
for (Gold gold : objectList) {
gold.draw(g);
}
}
public static void main(String[] args) {
new GoldMinerGame();
}
static class Gold {
protected int x;
protected int y;
protected int width;
protected int height;
protected int m;
protected Image img;
public void draw(Graphics g) {
g.drawImage(img, x, y, width, height, null);
}
static class GoldMini extends Gold {
GoldMini() {
this.width = 32;
this.height = 38;
this.m = 15;
this.img = Toolkit.getDefaultToolkit().getImage("imgs/gold3.png");
}
}
static class GoldPlus extends Gold {
GoldPlus() {
this.x = (int) (Math.random() * 650);
this.width = 120;
this.height = 132;
this.m = 60;
this.img = Toolkit.getDefaultToolkit().getImage("imgs/gold1.png");
}
}
}
}
```
阅读全文