管道游戏 - 为什么游戏对象不移动? [英] PipeGame -Why are the game objects not moving?

查看:162
本文介绍了管道游戏 - 为什么游戏对象不移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我的游戏对象没有显示。如果我在for-each循环之外使用 g.drawRect ,但是调用 PipeObject ,则可以绘制到JPanel >循环内部似乎并没有为我工作。我在这里做错了什么?感谢您的任何帮助或解决方案。

这是我的旧问题的更新版本,可以找到> 。 strong> UPDATE - 通过调用 pipe.move(),管道绘制正确,但不移动到左边。



游戏

  import java.awt。*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing。*;
import javax.swing.JFrame;
import javax.swing.JPanel;
// import javax.swing.border.EmptyBorder;
导入javax.swing.SwingUtilities;
$ b $ public class Game {

public static void main(String [] args){
Runnable r = new Runnable(){
@Override
public void run(){
//用户看到的GUI(无框架)
final CardLayout cl = new CardLayout();
final JPanel gui = new JPanel(cl);
//如果不需要边框,则删除
//gui.setBorder(new EmptyBorder(10,10,10,10));

JPanel menu = new JPanel(new GridBagLayout());
JButton playGame = new JButton(Play!);

ActionListener playGameListener = new ActionListener(){
$ b @Override
public void actionPerformed(ActionEvent e){
cl.show(gui,game );
}
};
playGame.addActionListener(playGameListener);
Insets margin = new Insets(20,50,20,50);
playGame.setMargin(margin);
menu.add(playGame);
gui.add(menu);
cl.addLayoutComponent(menu,menu);

final JPanel pipes = new Pipes();
gui.add(pipes);
cl.addLayoutComponent(pipes,game);

JFrame f = new JFrame(PipeGame);
f.add(gui);
//确保框架关闭后JVM关闭,
//所有非守护线程完成
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//请参阅https://stackoverflow.com/a/7143398/418556进行演示。
f.setLocationByPlatform(true);

//确保框架的最小尺寸为
//以显示其中的组件
f.pack();
//应该最后完成,以避免闪烁,移动,
//调整工件的大小。
f.setVisible(true);
}
};
// Swing GUIs应该在EDT
上创建和更新// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);












$ b

  import java.util。*; 
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Pipes extends JPanel {
boolean gameNotOver = true;
int x1 = 754;
int y1 = setHeightVal();
int y2 = setHeightVal();
int y3 = setHeightVal();

List< PipeObject> pipes = new ArrayList< PipeObject>();

public Pipes(){
pipes.add(new PipeObject(x1,y1));
pipes.add(新的PipeObject(x1 + 300,y2));
pipes.add(新的PipeObject(x1 + 600,y3));

$ b public void drawEndlessPipes(){
if(gameNotOver){
定时器pipeSpeed = new Timer(10,new ActionListener(){
@ (PipeObject pipe:pipes){
pipe.move();
}
}
} public void actionPerformed(ActionEvent e){
} );
pipeSpeed.start();



保护void paintComponent(Graphics g){
super.paintComponent(g); (PipeObject pipe:pipes){
pipe.drawPipe(g);




public int setHeightVal(){//获取一个随机数并选择一个预设高度
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;
}

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




$ PipeObject

  import java.awt.Graphics; 

public class PipeObject {
//声明并初始化变量
int x1;
int x2 = 75; //管道宽度,总数是83
int y1 = -1; // Y应该是-1
int y2;
int gap = 130; //空隙高度

公共PipeObject(int x,int y){
this.x1 = x;
this.y2 = y;

$ b $ public void drawPipe(Graphics g / *,int x1,int y2 * /){
g.drawRect(x1,y1,x2,y2); //绘制第1部分
g.drawRect(x1-3,y2-1,x2 + 6,25); //绘制第二部分
g.drawRect(x1-3,y2 + 25 + gap,x2 + 6,25); //绘制第3部分
g.drawRect(x1,y2 + 25 + gap + 25,x2,500-y2-49-gap); //绘制第4部分
}

public void move(){
x1--;
}

public int getMyX(){//确定管道的水平位置
return x1-3;


public int getMyY(){//确定管道的垂直位置
return y2 + 25;



$ div $解析方案

正在绘制超出面板的视觉范围的管道。 Pipes 面板的维度是 751x501 ,但管道从 x1 = 754 。尝试在 Pipes 中更改 x1 1 ,您应该看到三个管道。或者,您可以使框架最大化,并且您应该在右侧看到缺少的管道。

I'm wondering why my game objects are not showing. I am able to draw to the JPanel if I use g.drawRect outside of the for-each loop, but calling PipeObject inside of the loop doesn't seem to work for me. Am I doing something wrong here? Thanks for any help or solutions.

This is an updated version of my old question, which can be found here.

UPDATE - The pipes are drawing correctly, but not moving to the left by calling pipe.move().

Game

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
//import javax.swing.border.EmptyBorder;
import javax.swing.SwingUtilities;

public class Game {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {             
                // the GUI as seen by the user (without frame)
                final CardLayout cl = new CardLayout();
                final JPanel gui = new JPanel(cl);
                // remove if no border is needed
                //gui.setBorder(new EmptyBorder(10,10,10,10));

                JPanel menu = new JPanel(new GridBagLayout());
                JButton playGame = new JButton("Play!");

                ActionListener playGameListener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cl.show(gui, "game");
                    }
                };
                playGame.addActionListener(playGameListener);
                Insets margin = new Insets(20, 50, 20, 50);
                playGame.setMargin(margin);
                menu.add(playGame);
                gui.add(menu);
                cl.addLayoutComponent(menu, "menu");

                final JPanel pipes = new Pipes();
                gui.add(pipes);
                cl.addLayoutComponent(pipes, "game");

                JFrame f = new JFrame("PipeGame");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

Pipes

import java.util.*;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Pipes extends JPanel {
    boolean gameNotOver = true;
    int x1 = 754;
    int y1 = setHeightVal();
    int y2 = setHeightVal();
    int y3 = setHeightVal();

    List<PipeObject> pipes = new ArrayList<PipeObject>();

    public Pipes() {
        pipes.add(new PipeObject(x1, y1));
        pipes.add(new PipeObject(x1 + 300, y2));
        pipes.add(new PipeObject(x1 + 600, y3));
    }

    public void drawEndlessPipes() {
        if (gameNotOver) {
            Timer pipeSpeed = new Timer(10, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (PipeObject pipe : pipes) {
                        pipe.move();
                    }
                }
            });
            pipeSpeed.start();
        }
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (PipeObject pipe : pipes) {
            pipe.drawPipe(g);
        }
    }

    public int setHeightVal() {     //Get a random number and select a preset height
        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;
    }

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

PipeObject

import java.awt.Graphics;

public class PipeObject {
    //Declare and initialiaze variables
    int x1;
    int x2 = 75;                //pipe width, total is 83
    int y1 = -1;                // Y should be -1
    int y2;
    int gap = 130;              //gap height

    public PipeObject(int x, int y) {
        this.x1 = x;
        this.y2 = y;
    }

    public void drawPipe(Graphics g/*, int x1, int y2*/) {
        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 getMyX() {   //To determine where the pipe is horizontally
        return x1-3;
    }

    public int getMyY() {   //To determine where the pipe is vertically
        return y2+25;
    }
}

解决方案

You are painting the pipes beyond the visual bounds of the panel. The dimension of Pipes panel is 751x501, but the pipes begin at x1 = 754. Try changing x1 to 1 in Pipes and you should see the three pipes. Or, you can maximize the frame, and you should see the missing pipes far on the right side.

这篇关于管道游戏 - 为什么游戏对象不移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆