Note that there are some explanatory texts on larger screens.

plurals
  1. POExtending my llittle game with a TimeCouter and highscore
    primarykey
    data
    text
    <p>Hello Together recently i learnd about keyListeners so i tried to code a little game with it. Now it is kind of finished but i would like to implement some extra features. I thaught of a timer or so.So you are just able to play it 1min or so.. and after this it counts your points and adds it to the higescores but i have no idea how i could make this. would love it if someone could help me out. Thanks. </p> <p>For now all my code is in one Class. This is what ive got:</p> <pre><code>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 &amp;&amp; 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload