【问题标题】:Acceleration/Deceleration of Velocity速度的加速/减速
【发布时间】:2014-02-26 23:10:39
【问题描述】:
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Zhang extends JFrame implements Runnable, KeyListener
{

    Container con = getContentPane();
    Thread t = new Thread(this);
    int Hogwartsx = 10, Hogwartsy= 10, Snitchx = 200, Snitchy = 200, Loopx = 50, Loopy =       300, Loop2x = 120, Loop2y = 10,
    Loopx2 = 190, Loopy2 = 300, Loop2x2 = 260, Loop2y2 = 10,Loopx3 = 320, 
    Loopy3 = 300, Loop2x3 = 380, Loop2y3 = 10,
    SnitchxVel = 10, SnitchyVel = 10;
    Image Loop;
    Image Loop2;
    Image Snitch;
    Image Hogwarts;
    public Zhang()
    {
        addKeyListener(this);
        Hogwarts =     Toolkit.getDefaultToolkit().getImage(getClass().getResource("Hogwarts.gif"));
        Hogwarts = Hogwarts.getScaledInstance(500, 500, 1);
        Loop2 =   Toolkit.getDefaultToolkit().getImage(getClass().getResource("Loop2.gif"));
        Loop2 = Loop2.getScaledInstance(200, 200, 1);
        Loop = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Loop.gif"));
        Loop = Loop.getScaledInstance(200, 200, 1);
        Snitch = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Snitch.gif"));
        Snitch = Snitch.getScaledInstance(150, 150, 1);
        con.setLayout(new FlowLayout());
        t.start();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void run()
    {
        try{
            while(true)
            {
                t.sleep(100);
                repaint();
                Snitchy += SnitchyVel;
                if (Snitchy > 500)
                {
                    System.exit(0);
                }
            }
        }
        catch(Exception e){}
    }

    public void update(Graphics g)
    {
        paint(g);
    } 

    public void paint(Graphics gr)
    {
        Image i=createImage(getSize().width, getSize().height);
        Graphics2D g2 = (Graphics2D)i.getGraphics();

        g2.drawImage(Hogwarts, Hogwartsx, Hogwartsy, this); 
        g2.drawImage(Loop2, Loop2x, Loop2y, this); 
        g2.drawImage(Loop,Loopx, Loopy, this);
        g2.drawImage(Loop2, Loop2x2, Loop2y2, this); 
        g2.drawImage(Loop,Loopx2, Loopy2, this);
        g2.drawImage(Loop2, Loop2x3, Loop2y3, this); 
        g2.drawImage(Loop,Loopx3, Loopy3, this);
        g2.drawImage(Snitch,Snitchx,Snitchy, this);       
        g2.dispose();
        gr.drawImage(i, 0, 0, this);
    }

    public static void main(String[] args)
    {
        Zhang frame = new Zhang();
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void keyReleased (KeyEvent k) 
    {

    }

    public void keyPressed (KeyEvent k) 
    {
        if( k.getKeyCode() == 38)
    {
        for (SnitchyVel = 10; SnitchyVel>= 10; SnitchyVel++)
        {
            Snitchy-=SnitchyVel;
            for (SnitchyVel = 0; SnitchyVel<=10; SnitchyVel--)
            {
                Snitchy+=SnitchyVel;
            }
        }
    }
    }

    public void keyTyped (KeyEvent k) 
    {}

}

所以在我的编程课上,我们正在尝试编写 Flappy Bird 或它的一个版本。就我而言,我正在使用哈利波特主题。当我按下键盘上的向上箭头直到速度为 0 时,我的飞贼应该会向上减速,这将导致它停止移动。一旦速度达到 0,则假定飞贼在下降时会加速,直到达到预先声明的速度 10。有人可以向我解释如何加速和减速吗?

【问题讨论】:

  • 他们在编程课上教 Flappy Bird 克隆?!
  • 好吧,我只是在高中编程课上,我们班想编写一个游戏,所以 Flappy Bird 就是这样。
  • 整洁。有趣的是,你正在学习一些你可以与之相关的东西。
  • 哈哈是的!不必向每个人解释游戏,这让老师的生活变得更加轻松。那么,嗯,你知道我的问题的答案吗?
  • 你没有指示是否正在加速。您应该将按键逻辑移动到运行方法中。使用向上或向下按键将标志从真或假切换。如果真加速如果假减速。然后在你的 run 方法中检查是否需要添加或删除速度停止,如果在 0 或 10 取决于标志。

标签: java velocity flappy-bird-clone


【解决方案1】:

我不知道这个游戏,只听说过。

您的代码存在几个问题。经典:

  • 不要扩展 JFrame
  • 暗示:永远不要覆盖 JFrame 的 updatepaint 方法
  • 在事件调度线程上创建 GUI
  • 不要让您的顶级类实现这些接口
  • 使用适当的变量名
  • 避免使用魔法常数(Loop2x3 = 380、Loop2y3 = 10 等...)

具体的:

  • 动画的速度应该取决于时间,而不是帧速率
  • 您应该考虑如何对动画进行建模。如果您想执行位置、速度和加速度的计算,那么您应该有这些的表示。特别是,您应该尝试将这些存储在 int 值中。对于简单的 2D 游戏,Point2D 可能是一个不错的选择。

一个非常非常简单的方法:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class CrappyBird
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.getContentPane().setLayout(new BorderLayout());

        CrappyBirdGame crappyBirdGame = new CrappyBirdGame();
        CrappyBirdPanel crappyBirdPanel = new CrappyBirdPanel(crappyBirdGame);
        crappyBirdGame.setComponent(crappyBirdPanel);
        f.getContentPane().add(crappyBirdPanel, BorderLayout.CENTER);
        crappyBirdGame.start();

        f.setSize(800, 800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

class CrappyBirdGame
{
    private Point2D position = new Point2D.Double(0, 500);
    private Point2D velocity = new Point2D.Double(20, 0);
    private Point2D acceleration = new Point2D.Double(0, -9.81);

    private JComponent component;

    void setComponent(JComponent component)
    {
        this.component = component;
    }

    void start()
    {
        Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run() 
            {
                long oldNS = System.nanoTime();
                while (true)
                {
                    long newNS = System.nanoTime();
                    performAnimation(newNS - oldNS);
                    oldNS = newNS;
                    component.repaint();
                    try
                    {
                        Thread.sleep(20);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }

                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }


    private void performAnimation(long ns)
    {
        double delta = (ns / 1e9) * 15;

        double newVx = velocity.getX() + delta * acceleration.getX();
        double newVy = velocity.getY() + delta * acceleration.getY();
        velocity.setLocation(newVx, newVy);

        double newPx = position.getX() + delta * velocity.getX();
        double newPy = position.getY() + delta * velocity.getY();
        if (newPy < 0)
        {
            newPy = 0;
            velocity.setLocation(velocity.getX(), 0);
        }
        position.setLocation(newPx, newPy);


    }

    public Point2D getPosition()
    {
        return new Point2D.Double(position.getX(),  position.getY());
    }

    public void flap()
    {
        double newVx = velocity.getX();
        double newVy = velocity.getY() + 50;
        newVy = Math.min(30, newVy);
        velocity.setLocation(newVx, newVy);
    }

}


class CrappyBirdPanel extends JPanel
{
    private final CrappyBirdGame crappyBirdGame;

    CrappyBirdPanel(final CrappyBirdGame crappyBirdGame)
    {
        this.crappyBirdGame = crappyBirdGame;
        setFocusable(true);
        requestFocusInWindow();
        KeyListener keyListener = new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if( e.getKeyCode() == KeyEvent.VK_UP)
                {
                    crappyBirdGame.flap();
                }
            }
        };
        addKeyListener(keyListener);
    }

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

        Point2D p = crappyBirdGame.getPosition();
        int x = (int)p.getX() % getWidth();
        int y = getHeight() - (int)p.getY();

        g.setColor(Color.BLUE);
        g.fillOval(x-15, y-15, 30, 30);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多