Hallo, wir ich bin dabei ein kleines "Schatzsuche" spiel zu programmieren und will 40 Schätze auf ein Spielfeld von 15x15 verteilen!
Jedoch werden die Schätze zwar in zufälliger y Werte eingetragen , doch sind die X-Were immer gleich. So entstehen immer senkrechte Reihen mit Schätzen. Wo steckt der Fehler
Ich freue mich über jeden Hinweis
Danke im voraus
Jedoch werden die Schätze zwar in zufälliger y Werte eingetragen , doch sind die X-Were immer gleich. So entstehen immer senkrechte Reihen mit Schätzen. Wo steckt der Fehler
Code:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.math.*;
public class Schatzsuche_02 extends Applet
{
Button startBut;
final int sand = 0;
final int kein_schatz = 1;
final int schatz = 2;
final int felderPS = 15;
final int pixelPS = 450;
final int pixelPF = 30;
int xzuf = 0;
int yzuf = 0;
int y = 0;
int x = 0;
int xpos = 0;
int ypos = 0;
int x30 = 0;
int y30 = 0;
int xposfeld = 0;
int yposfeld = 0;
int yposm3 = 0;
int xposm3 = 0;
public int xposm = 0;
public int yposm = 0;
Graphics g0;
Random Zufallsgenerator = new Random();
int feld[][] = new int [felderPS][felderPS];
Image sand_img;
Image kein_sand_img;
Image schatz_img;
//Felder füllen mit Bildern
public void init () {
startBut = new Button(" Start ");
add(startBut);
g0 = getGraphics();
sand_img = getImage(getCodeBase(), "bilder/sand.gif");
kein_sand_img = getImage(getCodeBase(), "bilder/nicht_sand.gif");
schatz_img = getImage(getCodeBase(), "bilder/schatz.gif");
startBut.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e) {
for (x=0; x < 40; x++) {
int xzufall1 = (int) (14* Math.random());
int yzufall1 = (int) (14* Math.random());
int yzuffall = yzufall1;
int xzuffall = xzufall1;
if (feld[yzufall][xzufall] == 2) {
x--;
}
else
feld[yzufall][xzufall] = 2;
}
for (yzuf=0; yzuf < 14; yzuf++){
for (xzuf=0; xzuf < 14; xzuf++) {
if (feld[yzuf][xzuf] != 2) {
feld[yzuf][xzuf] = 1;
}
}
}
}
});
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
xposm = e.getX();
yposm = e.getY();
test();
}
});
}
public void paint(Graphics g) {
int xpos, ypos;
Dimension d = this.getSize();
for (x=0; x < felderPS; x++) {
for (y=0; y < felderPS; y++) {
xpos = x * pixelPF;
ypos = y * pixelPF;
g.drawImage(sand_img, xpos, ypos, this);
}
}
}
public void test()
{
xposfeld = xposm/pixelPF;
yposfeld = xposm/pixelPF;
if(feld[xposfeld][yposfeld] == 2)
{
xposm3 = xposm/pixelPF;
yposm3 = yposm/pixelPF;
xposm3 = xposm3*pixelPF;
yposm3 = yposm3*pixelPF;
g0.drawImage(schatz_img, xposm3, yposm3, this);
}
else
{
xposm3 = xposm/pixelPF;
yposm3 = yposm/pixelPF;
xposm3 = xposm3*pixelPF;
yposm3 = yposm3*pixelPF;
g0.drawImage(kein_sand_img, xposm3, yposm3, this);
}
}
}
Ich freue mich über jeden Hinweis
Danke im voraus