- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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 方法,您可以省去很多麻烦。
注意我所做的关键更改:
我现在正在将您的游戏组件生成为 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.
}
关于java - 如何让随机生成的对象出现在我的屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54263302/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!