Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created January 4, 2023 22:26
Show Gist options
  • Save tail-call/0a47b18163950a44c3263a3fe113dd7c to your computer and use it in GitHub Desktop.
Save tail-call/0a47b18163950a44c3263a3fe113dd7c to your computer and use it in GitHub Desktop.
#lang racket/gui
(require srfi/26)
(define-syntax-rule (when condition . body)
(if condition
(begin . body)
(void)))
(define auto-clicker-prices
'((10 . 1000)
(100 . 500)
(1000 . 250)
(10000 . 125)
(100000 . 70)
(1000000 . 35)))
(define (current-auto-clicker-price)
(if (empty? auto-clicker-prices)
#f
(car auto-clicker-prices)))
(define (increase-auto-clicker-price)
(if (empty? auto-clicker-prices)
#f
(set! auto-clicker-prices
(cdr auto-clicker-prices))))
(define-syntax-rule (define-button name label . callback-body)
(define name (make-button label (lambda (button event) . callback-body))))
; Make a frame by instantiating the frame% class
(define frame (new frame% [label "Clicker"]))
; Game state
(define counter 0)
(define (increase-counter)
(update-counter (lambda (c) (+ c 1))))
(define (update-counter updater)
(set! counter (updater counter))
(send msg set-label (format "Clicks $~a" counter)))
; Make a static text message in the frame
(define msg (new message% [parent frame]
[label "No clicks so far..."]))
(define (make-button name callback)
(new button% [parent frame]
[label name]
[callback callback]))
; Make a button in the frame
(define-button click-button "Click"
(increase-counter))
; Make timer for auto clicker
(define auto-clicker-timer
(new timer% [notify-callback increase-counter]
[interval 1000]
[just-once? #f]))
; Don't run immediately
(send auto-clicker-timer stop)
(define (buy-auto-clicker price)
(when (>= counter price)
(update-counter (cut - <> 10))
(send auto-clicker-timer start 1000)
(send upgrade-button show #f)))
; Make another button for upgrade
(define-button upgrade-button "Buy auto clicker ($10)"
(buy-auto-clicker 10))
; Show the frame by calling its show method
(send frame show #t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment