【发布时间】:2019-01-19 01:15:56
【问题描述】:
This is my background Image that i have used我是一名新程序员,我还在学习编程。我正在尝试创建一个飞扬的小鸟游戏,但我的代码似乎不起作用。我要做的是创建代码,在我的程序中生成随机管道,并试图让它们与红球发生碰撞。
我试图四处寻找可以向我解释它是如何工作的 Flappy Bird 游戏,但它在我的代码中不起作用,或者它非常复杂,我无法理解。
您好,感谢您的帮助,但我曾尝试实现您的代码,但出现此错误。我正在尝试解决这个问题。至于您的其他 cmets,我正在尝试制作一个名为 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();
}
}
我到目前为止所拥有的一切。希望你能帮助我
我打算在程序中生成不同高度的管道。
【问题讨论】:
-
究竟是什么问题:您发布的代码是否运行?您收到错误消息了吗?
-
在您的水平上,您可能应该遵循一些有据可查的游戏教程,而不是开发自己的游戏。