) 请帮助解决问题。我正在做一个小游戏并画一个硬币)给玩家去收集它们。
以下是收集的过程(即代码):
if (x + w3 == 190) {
coins++;
mon = null;
} else {
g.drawImage(mon, 190 - x, 265, wm, hm, this);
}
其中 190 - 是硬币的位置
其中 x + w3 玩家的坐标
也就是当touch coin的坐标消失了,但是随着这枚硬币的消失,其他的,其实-mon=null;
我应该怎么办?真的每一枚硬币都能拍出你的照片吗?
提前致谢。
UPD
所有代码:
package GameTs;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Game extends JPanel {
private static final long serialVersionUID = 1L;
public static final String NAME = "DrakGo Game";
public static final int WIDTH = 500;
public static final int HEIGHT = 460;
private static final int MAP_SIZE_X = 100;
private Image img;
private Image img2;
private Image mon;
private Image mon2;
private Image mon3;
private Image pl;
private Image mag;
private Image drak;
private Input input = new Input(this);
static JFrame frame;
static int x = 0;
int y = 0;
int xp = 110;
int yp = 180;
int coins = 0;
URL url4;
URL url7;
public Game() {
URL url2 = getClass().getResource("images/Grass.png");
img = Toolkit.getDefaultToolkit().getImage(url2);
URL url = getClass().getResource("images/Cloud.png");
img2 = Toolkit.getDefaultToolkit().getImage(url);
URL url3 = getClass().getResource("images/Mon.png");//THIS
mon = Toolkit.getDefaultToolkit().getImage(url3);
mon2 = Toolkit.getDefaultToolkit().getImage(url3);//THIS
mon3 = Toolkit.getDefaultToolkit().getImage(url3);//THIS
url4 = getClass().getResource("images/Right_Pic1.png");
URL url5 = getClass().getResource("images/Mag.png");
URL url6 = getClass().getResource("images/Dr.png");
url7 = getClass().getResource("images/Right_Pic2.png");
pl = Toolkit.getDefaultToolkit().getImage(url4);
mag = Toolkit.getDefaultToolkit().getImage(url5);
drak = Toolkit.getDefaultToolkit().getImage(url6);
}
public void paint(Graphics g) {
render(g);
repaint();
}
private void move(Graphics g) {
/*
* if (input.left) { x--; if (x == -1) { x++; }
*
* }
*/
if (input.right) {
x++;
if (x == -1) {
x--;
}
if ((x % 2) == 0) {
pl = Toolkit.getDefaultToolkit().getImage(url7);
} else {
pl = Toolkit.getDefaultToolkit().getImage(url4);
}
}
}
public void render(Graphics g) {
super.paint(g);
move(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 500, 360);
int w = 180;
int h = 180;
for (int i = 0; i < MAP_SIZE_X * 2; i++) {
g.drawImage(img, i * w - x, 260, (int) w, (int) h, this);
}
int w2 = 90;
int h2 = 50;
for (int c = 0; c < MAP_SIZE_X * 100; c += 200) {
g.drawImage(img2, 100 + c - x, 70, w2, h2, this);
}
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
Font[] allFonts = ge.getAllFonts();
Font font = allFonts[4].deriveFont(30.0F);
g.setFont(font);
g.setColor(Color.DARK_GRAY);
g.drawString("DrakGo - Game", 10, 40);
g.drawString("X: " + x, 300, 40);
g.drawString("Coins: " + coins, 400, 40);
int w3 = 50;
int h3 = 50;
int wm = 30;
int hm = 30;
if (x + w3 == 190) {
coins++;
mon = null;
} else {
g.drawImage(mon, 190 - x, 265, wm, hm, this);
}
//THIS!!!!!!!
if (x + w3 == 350) {
coins++;
mon2 = null;
} else {
g.drawImage(mon2, 350 - x, 265, wm, hm, this);
}
if (x + w3 == 650) {
coins++;
mon3 = null;
} else {
g.drawImage(mon3, 650 - x, 265, wm, hm, this);
}
int w4 = 50;
int h4 = 50;
if (x + w3 == 3500) {
drak = null;
} else {
g.drawImage(drak, 3500 - x, 260, w4, h4, this);
}
g.drawImage(mag, 1000 - x, 260, w3, h3, this);
g.drawImage(pl, 0, 260, w3, h3, this);
}
public static void main(String[] args) {
Game game = new Game();
game.setSize(WIDTH, HEIGHT);
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(game);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}
}