This is my background Image that i have used我是一名新程序员,我仍在学习编码。我正试图创建一个飞扬的鸟游戏,但是我的代码似乎不起作用。我正在尝试做的是创建代码,该代码在程序中生成随机管道,并尝试使其与红色Ball发生碰撞。
我试图四处寻找能解释其工作原理的飞鸟游戏,但是它在我的代码中不起作用,或者它非常复杂,我无法理解。
嘿,谢谢您的帮助,但我尝试实现您的代码,但出现此错误。我正在尝试解决此问题。至于您的其他评论,我正在尝试制作一个名为“飞扬的小鸟”的游戏,在该游戏中,我必须生成随机的管道,管道的高度各不相同,并且两者之间的空间相同。这是正在发生的事情:
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();
}
}
我到目前为止所拥有的。希望你能帮助我
我打算做的是在程序中生成高度不同的管道。
答案 0 :(得分:0)
我将在两个方面解决您的问题。
1。首先是项目设计
在提出问题或开始编写程序之前先了解您想要的东西是一件好事。从声音上看,我认为您正在尝试制作一款可以控制障碍物的鸟儿游戏。
当前,您正在通过将组件绘制到框架上来生成游戏。这是一个不错的选择,但是我更喜欢将组件生成为对象(通常是JLabel
,这样很容易为它们提供图形)。通过创建对象,如果您要在游戏中移动它,则只允许修改屏幕上的对象,而不用重新绘制所有组件。
Java是一种面向对象的编程语言,因此最好使用模块化结构设计程序。
以下是必须创建的程序的关键功能:
JFrame
我将只谈谈障碍创造者,因为这就是您在问题中提出的问题。
编写障碍物创建者时,您想要以一种健壮且易于使用的方式编写它。理想情况下,它将以类或函数的形式出现,可以在屏幕上调用并返回对象。这将提供最大的灵活性。
另外值得注意的是,除非计划在其他类中使用过程,否则不必使用修饰符public
,而使用private void
是更好的做法。
2。编写代码
在这里,我以一种更简单的方式重新编写了您的程序。通过不覆盖绘画方法,您可以省去很多麻烦。
请注意我所做的主要更改:
我现在正在将您的游戏组件生成为JLabel。它们是通过Game()方法中的过程创建的。
不再绘制矩形,这将使您在编写程序的那部分时更容易检测冲突
注意我如何创建一个随机生成数字的函数。我现在可以稍后在程序中的其他位置轻松调用此函数。
最近,我删除了您的延迟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.
}