gpt4 book ai didi

java - Buggy Pipe 游戏输出到 JFrame

转载 作者:行者123 更新时间:2023-11-30 04:02:41 29 4
gpt4 key购买 nike

我曾经将我的程序作为小程序运行,当时它工作得很好,但我决定尝试让它在 JFrame 中运行。我最近遇到了覆盖问题,但我想我终于解决了这个问题。这是我最近一直在制作的一款游戏,它基本上是《Flappy Bird》的重现。我不打算出售我的成品,因为我相信功劳不属于我。现在,我希望制作一根管道穿过屏幕,并计划在任何给定时间在屏幕上最多制作 3 组管道。

已修复框架未打开的错误新错误:不需要的输出

这是我的主类(游戏)

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Game {

Pipes panel = new Pipes();

public Game() {
JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.setTitle("Pipe Game");
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setResizable(false);

Timer timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.move();
panel.repaint();
}
});
timer.start();
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new Game();
}
});
}
}

这是我绘制(管道)的类

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Pipes extends JPanel {
//Declare and initialiaze variables
int height = setHeight();
int x1 = 754; //xVal start
int x2 = 75; //pipe width
int y1 = -1; //yVal start
int y2; //pipe height
int vel; //pipe velocity
int gap = 130; //gap height

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

y2 = height;
g.clearRect(0,0,750,500); //Clear screen
g.drawRect(x1,y1,x2,y2); //Draw part 1
g.drawRect(x1-3,y2-1,x2+6,25); //Draw part 2
g.drawRect(x1-3,y2+25+gap,x2+6,25); //Draw part 3
g.drawRect(x1,y2+25+gap+25,x2,500-y2-49-gap); //Draw part 4
}

public void move() {
x1--;
}

public int getY() {
return y2+25;
}

public int getX() {
return x1-3;
}

public int setHeight() {
int num = (int)(9*Math.random() + 1);
int val = 0;
if (num == 9)
{
val = 295;
}
else if (num == 8)
{
val = 246;
}
else if (num == 7)
{
val = 216;
}
else if (num == 6)
{
val = 185;
}
else if (num == 5)
{
val = 156;
}
else if (num == 4)
{
val = 125;
}
else if (num == 3)
{
val = 96;
}
else if (num == 2)
{
val = 66;
}
else
{
val = 25;
}
return val;
}

@Override
public Dimension getPreferredSize() {
return new Dimension(751, 501);
}
}

最佳答案

我想你想要

public void run() {
new Game();
}

而不是

public void run() {
new Pipes();
}

关于java - Buggy Pipe 游戏输出到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21565810/

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