gpt4 book ai didi

java - 如何让随机生成的对象出现在我的屏幕上?

转载 作者:行者123 更新时间:2023-11-30 12:08:38 25 4
gpt4 key购买 nike

This is my background Image that i have used我是一名新程序员,我仍在学习编码。我正在尝试创建一个 flappy bird 游戏,但我的代码似乎不起作用。我要做的是创建在我的程序中生成随机管道的代码,并尝试使它们与红球发生碰撞。

我试图四处寻找可以向我解释它是如何工作的 flappy bird 游戏,但它在我的代码中不起作用,或者它非常非常复杂,我无法理解。

您好,感谢您的帮助,但我曾尝试实现您的代码,但遇到了这个错误。我正在尝试解决这个问题。至于你的其他评论,我正在尝试制作一个名为 flappy bird 的游戏,在该游戏中,我必须生成随机管道,管道高度不同,管道之间的空间量相同。这是正在发生的事情:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Rectangle;

/**
* Auto Generated Java Class.
*/
public class Game extends JFrame implements ActionListener{
static final int NUMBER_OF_OBSTACLES = 30; // pre-assign the number of obstacles to be generated

// setup our objects for our game
JLabel[] columns = new JLabel [NUMBER_OF_OBSTACLES];
public Rectangle bird;
public final int WIDTH = 100, HEIGHT = 100;
public Random rand;

int number;
ImageIcon lblBackground = new ImageIcon("Test1.jpg");
ImageIcon lblBackground2 = new ImageIcon("Background2.jpg");
static int randomNumber;
int xLocation = 0;
int yLocation = 0;
int wLocation = 450;
int xForRect = 300;
int yForBall = 300;
int xForRectTop = 300;
static int randY;
int xSpeed = 1;
int ySpeed = 1;
int delay = 5;
Rectangle rect;

public Game() {
Rectangle column [];
column = new Rectangle [150];

setLayout(null);
setSize (404, 600);
setVisible(true); //sets everything to visable

}
private void generateRectangles() {
for (int i = 0; i < columns.length; i ++ ) {
columns[i] = new JLabel("" + i); // the name of the object will be shown as the number it is
columns[i].setBackground(Color.green);
columns[i].setOpaque(true);
columns[i].setBounds(randomRect());
}
}

private Rectangle randomRect() {

// create a rectangle to store the bounds of our new object
Rectangle rect = new Rectangle();

rect.height = randomizer(5, 100); // random height 1 to 100
rect.width = randomizer(5, 100); // random width 1 to 100

return rect;
}

private static int randomizer(int min, int max) {
Random random = new Random(); // create new randomizer

int number = random.nextInt(max - min); //randomizes a number from 0 to dist between them (for example if we are generating from 50 to 100 it will run from 0 to 50)
number = number + min; // then add 50 to match the bottom range

return number; // return this random number to the method that called for it
}



public void makeColumn(Graphics g, Rectangle column) {
g.setColor(Color.green);
g.fillRect(column.x, column.y, column.width, column.height);
}


public static int addRandomColumn() {
randomNumber = (int)(Math.random() * 300 + 250);
randomNumber = randY;
return randY;
}


public void paint(Graphics g){
super.paint(g);
for (int p = 0; p < 9999999; p = p+1) {
for (int i = 0; i <=630; i= i + 1){

lblBackground.paintIcon(this,g, xLocation, yLocation);
System.out.println(xLocation);
xLocation = xLocation - xSpeed;



g.setColor(Color.red);
g.fillRect(150, yForBall, 20, 20);

yForBall = yForBall + 1;

rect = new Rectangle(xForRect,450,50,number);

makeColumn(g,rect);
rect = new Rectangle(xForRectTop,0,50,number);
makeColumn(g,rect);

xForRect = xForRect -1;
xForRectTop = xForRectTop -1;

try {
Thread.sleep(delay);
}
catch (Exception error) {
}
}
}

}

public void actionPerformed(ActionEvent evt){

}
public static void main(String[] args) {
new Game();
}

}

那是我目前所拥有的。希望你能帮助我

我打算做的是在程序中生成不同高度的管道。

最佳答案

我将从两个方面回答您的问题。

<强>1。一、项目设计

最好在提出问题或开始编写程序之前了解您想要什么。从它的声音来看,我假设你正在尝试制作一个游戏,在这个游戏中你控制一只必须避开障碍物的鸟。

目前您正在通过将组件绘制到框架上来生成您的游戏。这是一个不错的选择,但我更喜欢将组件生成为对象(通常是 JLabel,这样可以很容易地为它们提供图形)。通过创建对象,如果您想在游戏中移动对象,它允许您仅修改屏幕上的对象,而不是重新绘制所有组件。

Java 是一种面向对象的编程语言,因此最好使用模块化结构设计程序。

以下是必须创建的程序的主要功能:

  • 背景 JFrame
  • 障碍制造者
  • 输入监听器
  • 碰撞检查器

我只会解决障碍的制造者,因为这就是您在问题中所问的内容。

在编写障碍创建器时,您希望以一种健壮且易于多次使用的方式编写它。理想情况下,这将以类或函数的形式出现,可以在屏幕上调用并返回和对象。这将提供最大的灵 active 。

另外值得注意的是,除非您计划在其他类中使用您的过程,否则修饰符 public 是不必要的,使用 private void 是更好的做法。

<强>2。编写代码

在这里,我以更简单的方式重写了您的程序。通过不覆盖 paint 方法,您可以省去很多麻烦。

注意我所做的关键更改:

  1. 我现在正在将您的游戏组件生成为 JLabel。它们由 Game() 方法中的过程创建。

  2. 不再绘制矩形,这将在您编写该部分程序时更容易进行碰撞检测

  3. 请注意我是如何创建一个随机生成数字的函数的。我现在可以稍后从程序的其他地方轻松调用此函数。

  4. 最后,我删除了您的延迟 Thread.sleep(delay); 代码。最好使用 timer thread处理您的事件而不是延迟。我建议您创建一个与您的程序并行运行的计时器线程。线程运行时每隔半秒,您可以将小鸟朝输入的方向移动,然后检查它是否与 columns 数组中的对象发生碰撞。

    import javax.swing.*;       
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import java.awt.Rectangle;


    public class Game extends JFrame implements ActionListener{

    static final int NUMBER_OF_OBSTACLES = 30; // pre-assign the number of obstacles to be generated

    // setup our objects for our game
    JLabel[] columns = new JLabel [NUMBER_OF_OBSTACLES];
    JLabel bird;

    public Game() {

    // setup the frame
    setLayout(null);
    setSize (404, 600);
    setVisible(true); //sets everything to visable
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    generateRectangles();

    placeBird();

    }

    private void placeBird() {
    // create the new bird
    bird = new JLabel("B");
    bird.setBackground(Color.red); // set background
    bird.setOpaque(true); // make the label's background color not invisible but opaque
    bird.setBounds(this.getWidth()/2, 0 , 30, 30); // place bird at top midpoint of screen, its size is a 30,30 square
    this.add(bird); // add the bird to our frame
    bird.setVisible(true); // show the bird on our frame
    }

    private void generateRectangles() {
    for (int i = 0; i < columns.length; i ++ ) {
    columns[i] = new JLabel("" + i); // the name of the object will be shown as the number it is
    columns[i].setBackground(Color.green);
    columns[i].setOpaque(true);
    columns[i].setBounds(randomRect());
    this.add(columns[i]);
    columns[i].setVisible(true);
    }
    }

    private Rectangle randomRect() {

    // create a rectangle to store the bounds of our new object
    Rectangle rect = new Rectangle();

    rect.height = randomizer(5, 100); // random height 1 to 100
    rect.width = randomizer(5, 100); // random width 1 to 100
    rect.x = randomizer(1, this.getWidth()); // random x position up to the width of the frame
    rect.y = randomizer(30, this.getHeight()); // random y position from 30 up to the height of the frame (NOTE: the 30 gap is so no obstacles start on top of the bird)

    return rect;
    }

    private static int randomizer(int min, int max) {
    Random random = new Random(); // create new randomizer

    int number = random.nextInt(max - min); //randomizes a number from 0 to dist between them (for example if we are generating from 50 to 100 it will run from 0 to 50)
    number = number + min; // then add 50 to match the bottom range

    return number; // return this random number to the method that called for it
    }


    // main runs on startup creating our frame
    public static void main(String[] args) {
    Game game = new Game(); // start a new game
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

关于java - 如何让随机生成的对象出现在我的屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54263302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com