Skip to content

Instantly share code, notes, and snippets.

@welel
Last active April 4, 2021 13:02
Show Gist options
  • Save welel/625450a5bc2d07cba3d3c5776d65c58a to your computer and use it in GitHub Desktop.
Save welel/625450a5bc2d07cba3d3c5776d65c58a to your computer and use it in GitHub Desktop.
Simple GUI game on Java. The main goal is to hit a target on the screen as faster as you can.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Clicker extends JFrame
implements Runnable, ActionListener {
private Thread th = null;
// Window size
private int width = 1100;
private int height = 1000;
// Target coordinates
private int x = 0;
private int y = 0;
// Counter of clicks on target
private int count = 0;
// Argument for sleep() [Defines moves speed of target]
private int speed = 1500;
// Component for drawing
private Canvas canvas;
// Start/pause button
private Button button;
// Flag -- is button pressed
private boolean button_flag = true;
// Random for generating random coordinates
private Random rand = new Random(1000);
// Color of information messages
private Color color = Color.RED;
public Clicker() {
setTitle("Game: Clicker");
setSize(width,height);
canvas = new Canvas() {
public void paint(Graphics g) {
// Drawing of target
g.setColor(Color.BLACK);
g.fillRect(x,y,50,50);
// Drawing of counter (hits)
g.setFont(new Font ("TimesRoman", Font.BOLD, 18));
g.setColor(Color.YELLOW);
g.drawString(""+count, x+20, y+29);
// Info message about game speed
g.setColor(color);
g.drawString("Sleep: "+speed, 850,900);
// Info about goal score
g.setColor(color);
g.drawString("Need: 100", 20, 20 );
// Winning message
g.setFont(new Font ("TimesRoman", Font.BOLD, 200));
g.setColor(Color.PINK);
if(count > 100) g.drawString("GOOD JOB", 0, 500);
}
};
canvas.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
// Is click on target
if (e.getX() > x && e.getX() < (x+50) && e.getY()> y && e.getY() < y+50)
{
count++;
speed +=5;
canvas.repaint();
}
}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
button = new Button("Start/Pause");
button.addActionListener(this);
add(button, BorderLayout.NORTH);
add(canvas, BorderLayout.CENTER);
setVisible(true);
th = new Thread(this);
th.start();
}
public void actionPerformed(ActionEvent ae) {
if ( button == ae.getSource()) {
if ( button_flag == true ) {
button_flag = false;
}
else {
button_flag = true;
}
}
}
public void run() {
while (true) {
width = getWidth();
height = getHeight();
// Generate random coordinates
try {
x = rand.nextInt(width-200);
y = rand.nextInt(height-100);
} catch ( IllegalArgumentException ie ) {
x = 0;
y = 0;
}
while (!button_flag) {
try {
th.sleep(200);
} catch(InterruptedException e) {}
}
try {
th.sleep(this.speed);
} catch(InterruptedException e) {}
// Limit of game speed
if (speed > 500)
speed-=10;
// Random color for goal score info
color = new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
this.canvas.repaint();
}
}
public static void main(String[] args) {
Clicker game = new Clicker();
}
}
# Depenpency: jdk-7
# 1. Download
# 2. Compile
javac Clicker.java
# 3. Run
java Clicker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment