2016-03-25 8 views
-1

私は、プレイヤーが避けなければならないランダムな四角形を生成しようとしています。私の衝突方法は、ランダムに生成された1つの矩形で作業していました。私はこれらの10を描きたいと思うし、私はフィニッシュラインとタイマーを追加します。 今、問題を理解していますが、修正方法がわかりません。ボール/プレイヤーの移動は、x座標またはy座標を10ずつ変更して円を再描画することによって実行されます。私は現在、同じペイント方法で長方形を持っているので、プレーヤが移動するたびに長方形が再生成されます。最初のランダムな世代の後に同じ場所にいてほしいです。私は実際にこれを行う方法を実際にはわかりません... また、私は同じ場所に留まる長方形を得ることができる場合、私の衝突メソッドは、複数の長方形でも動作しますか?それとも私もそれを改訂する必要がありますか?Javaアプレットの複数のpaint()メソッド?

修正する必要がある部分がわからないため、プログラムのコード全体を投稿するだけです。

import java.awt.*; 
import java.awt.Rectangle; 
import java.awt.Shape; 

import javafx.scene.shape.*; 
import java.awt.event.*; 
import java.util.Random; 
import java.awt.geom.Area; 
import javax.swing.JOptionPane; 

import java.applet.Applet; 

public class Main extends Applet 
implements ActionListener{ 
boolean end = false; 
private Rectangle rectangle; 
//creates buttons to move player 
    private Button run = new Button("Run"); 
    private Button jump = new Button("Jump"); 
private Button fall = new Button("Fall"); 
//creates player and obstacles 
private Circle player = new Circle(110,110,20); 
private makeRect block = new makeRect(150, 120, 30, 10); 
//initiates the buttons with actionListener 
public void init(){ 
    add(run); 
    add(jump); 
    add(fall); 
    run.addActionListener(this); 
    jump.addActionListener(this); 
    fall.addActionListener(this); 
} 
//draws the player and blocks on the screen 

    public void paint(Graphics g){ 
    for(int numBlocks = 0; numBlocks<11; numBlocks++){ 
    block.draw(g);} 
    player.draw(g); 
} 
//if methods to be control movement 
public void actionPerformed(ActionEvent e){ 


     if(e.getSource() instanceof Button){ 
     if(e.getSource() == run) 
      player.horiz(10); 
     else if (e.getSource()== jump){ 
      player.vert(-10); 
      } 
     else if (e.getSource()== fall){ 
      player.down(10); 
      } 
     repaint(); 
     collision(); 
    } 

} 
public void collision(){ 
    if(player.getBounds().intersects(block.getBounds())){ 
     JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION); 
     System.exit(ABORT); 
     end = true; 
    } 

} 


class Circle{ 
private final Color theColor = Color.BLUE; 
private int radius; 
private int x,y; 
public Circle(){ 
    x = 110; y = 110; 
    radius = 20; 
} 

public Circle(int x0, int y0, int rad){ 
    x = x0; y = y0; radius = rad; 
} 
public void draw(Graphics g){ 
    g.fillOval(x - radius, y-radius, 2*radius, 2*radius); 
    g.setColor(theColor); 
} 
public void horiz(int val){ 

    for(int c = 0; c<val+1; c++){ 
     x++; 
     repaint();} 
} 
public void vert(int val){ 
    y += val; 
} 
public void down(int val){ 
    y += val; 
} 
public Rectangle getBounds(){ 
    return new Rectangle(x-radius, y-radius, 2*radius, 2*radius); 
} 


} 
class makeRect{ 
private int Xmax = 250; 
private int Xmin = 140; 
private int Wmax = 50; 
private int Hmax = 25; 
private int Wmin = 10; 
private int Hmin = 5; 
Random rand = new Random(); 
private int randx; 
private int randh; 

private int x, y, width, height; 
public makeRect(){ 
    x = 150; y = 120; 
    width = 30; height = 10; 
} 


public makeRect(int x0, int y0, int w0, int h0){ 
    x = x0; y = y0; width = w0; height = h0; 
} 
public void draw(Graphics g) { 

int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin; 
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin; 
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin; 
g.drawRect(randx, 110+randh, randh, randw); 

} 
public Rectangle getBounds(){ 
return new Rectangle(randx, 110+randh, 30, 10); 
} 
} 
} 

ありがとうございます!

答えて

0

そのためには、最初に初期化時に10個の矩形を作成する必要があります(配列の使用を検討してください)。つまり、描出時ではなく、矩形が構築されているときに、後置ランダム化が行われます。

あなたが持っているものは、paint()が呼び出されるたびに異なる場所で10回描画された同じ矩形です。

関連する問題