clicket/clicket.rkt

76 lines
3 KiB
Racket

#lang racket/gui
; setup main frame layout
(define frame (new (class frame% (super-new)
(define/augment (on-close)
(send timer stop)))
[label "Clicket"]
[border 5]))
(define panel (new horizontal-panel%
[parent frame]
[spacing 15]))
(define left-side (new vertical-pane% [parent panel]))
(define right-side (new vertical-pane% [parent panel]))
; setup click var and associated display text
(define clicks 0)
(define (click-str) (~a "Clicks: " clicks))
(define click-msg (new message%
[parent left-side]
[label (click-str)]
[auto-resize #t]))
(define cps 0)
(define (cps-str) (~a "Auto-clicks per second: " cps))
(define cps-msg (new message%
[parent left-side]
[label (cps-str)]
[auto-resize #t]))
; helper procedure
(define-syntax-rule (new-inc-button container name counter)
(new button% [parent container]
[label name]
[callback (λ (b e) (set! counter (add1 counter)))]))
; assume we're placing all buy buttons in the right-side container
(define-syntax-rule (new-buy-button cl)
(let* ([button-pane (new horizontal-pane% [parent right-side])]
[counter-msg (new message%
[parent button-pane]
[label "Owned: 0"]
[auto-resize #t])])
(new button% [parent button-pane]
[label (~a "Buy " (clicker-name cl) " (" (clicker-price cl) "¢)")]
[callback (λ (b e)
(when (>= clicks (clicker-price cl))
(set-clicker-count! cl (add1 (clicker-count cl)))
(set! clicks (- clicks (clicker-price cl)))
(send counter-msg set-label (~a "Owned: " (clicker-count cl)))))])))
; button to manually increase click count
(new-inc-button left-side "Clicket?" clicks)
; clicks on a timer
(struct clicker (name price multiplier (count #:mutable)))
(define all-clickers (list
(clicker "mouse" 10 1 0)
(clicker "coder" 150 5 0)
(clicker "paren farm" 1000 10 0)))
(for ([cl all-clickers]) (new-buy-button cl))
; setup timer
(define timer-ctr 0)
(define timer (new timer% [interval 100]
[notify-callback
(λ ()
(set! cps 0)
(for ([cl all-clickers])
(when (= 0 (modulo timer-ctr 10))
(set! clicks (+ clicks (* (clicker-multiplier cl) (clicker-count cl)))))
(set! cps (+ cps (* (clicker-multiplier cl) (clicker-count cl)))))
(send click-msg set-label (click-str))
(send cps-msg set-label (cps-str))
(set! timer-ctr (add1 timer-ctr)))]))
; show frame
(send frame show #t)