使用TimeCutter和高分扩展我的小游戏

时间:2013-02-20 10:30:12

标签: graphics timer jframe keylistener

Hello Together最近我了解了keyListeners,所以我试着用它编写一个小游戏。 现在它已经完成但我想实现一些额外的功能。我只需要一个计时器。所以你只能玩它1分钟左右..然后它计算你的积分并将其添加到higescores但我不知道我怎么能做到这一点。如果有人可以帮助我,我会喜欢它。谢谢。

现在我的所有代码都在一个类中。这就是我所拥有的:

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

public class Main extends JPanel implements KeyListener {
    private static final long serialVersionUID = 1L;

    private static int x = 350;
    private static int y = 350;
    private static int punkte = 0;
    private static boolean isThere = true;
    private static int apx = 250;
    private static int apy = 350;

    public Main() {
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent evt) {
        int keyCode = evt.getKeyCode();
        int d = 0;

        if (evt.isShiftDown())
            d = 5;
        else
            d = 1;

        if (keyCode == KeyEvent.VK_LEFT) {
            x -= d;
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            x += d;
        } else if (keyCode == KeyEvent.VK_UP) {

            y -= d;
        } else if (keyCode == KeyEvent.VK_DOWN) {
            y += d;
        }
    }

    public void keyReleased(KeyEvent evt) {
    }

    public void keyTyped(KeyEvent evt) {
    }

    public boolean isFocusTraversable() {
        return true;
    }

    public void paint(Graphics g) {

        int c1 = (int) (Math.floor(Math.random() * 254) + 1);
        int c2 = (int) (Math.floor(Math.random() * 254) + 1);
        int c3 = (int) (Math.floor(Math.random() * 254) + 1);

        int cx1 = (int) (Math.floor(Math.random() * 254) + 1);
        int cx2 = (int) (Math.floor(Math.random() * 254) + 1);
        int cx3 = (int) (Math.floor(Math.random() * 254) + 1);

        int x1 = (int) (Math.floor(Math.random() * 680) + 1);
        int y1 = (int) (Math.floor(Math.random() * 600) + 1);

        String punkteStr = String.valueOf(punkte);

        g.setColor(new Color(c1, c2, c3));

        g.fillRect(x, y, 50, 50);
        g.setFont(new Font("Arial", Font.BOLD, 50));
        g.drawString(punkteStr, 10, 40);
        g.setColor(new Color(cx1, cx2, cx3));
        if (isThere) {

            g.fillOval(apx, apy, 50, 50);
        }
        if (x == apx && y == apy) {
            punkte++;
            apx = (int) (Math.floor(Math.random() * 680) + 1);
            apy = (int) (Math.floor(Math.random() * 600) + 1);
        }
        repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Lightning Catcher v1 by Jonas");
        frame.setSize(700, 650);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);

        Container contentPane = frame.getContentPane();
        contentPane.add(new Main());
        frame.setVisible(true);
    }
}

0 个答案:

没有答案