-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent-handler.rkt
More file actions
80 lines (66 loc) · 2.79 KB
/
Copy pathevent-handler.rkt
File metadata and controls
80 lines (66 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#lang racket
(provide (all-defined-out))
(require "world-state.rkt" "gui.rkt" "variable.rkt" "database.rkt")
(require 2htdp/image 2htdp/universe)
;; Key event
; handle-key-event take state and key
(define (key-event state key)
; each state has its own key handler
(cond
[(game-start? state) (key/game-start state key)]
[(playing? state) (key/playing state key)]
[(game-over? state) (key/game-over state key)]
[(highscore? state) (key/highscore state key)]
(else state)))
(define (key/game-start state key)
; when the game in game-start mode
; the game will start with any key
init-playing)
(define (key/playing state key)
(cond
((key=? key "left")
(update-playing-position character-pos-left state))
((key=? key "right")
(update-playing-position character-pos-right state))
;;((key=? key "up") (update-playing-tree (playing-tree state) state))
(else (make-playing (playing-time state) (playing-score state) (playing-position state) (playing-tree state)))))
(define (key/game-over state key)
(cond ([or (key=? key " ") (key=? key "r")] init-playing)
([key=? key "h"] (highscore (game-over-score state)))
(else
(game-over (game-over-score state) (game-over-position state) (game-over-tree state) 0))))
(define (key/highscore state key)
(cond ([key=? key " "] init-playing)
(else
(highscore (highscore-current-score state)))))
; mouse event
(define (mouse-event state x y mouse)
(cond ([game-over? state] (mouse-event/game-over state x y mouse))
;;([highscore? state] (mouse-event/highscore state x y mouse))
(else
state)))
(define (mouse-event/game-over state x y mouse)
(cond
((and (mouse=? mouse "button-down")
;; play again
(< (* (- (* 1/4 WIDTH) play-again-width) SCALE) x) (< (* (- (* 3/4 HEIGHT) play-again-height) SCALE) y)
(> (* (+ (* 1/4 WIDTH) play-again-width) SCALE) x) (> (* (+ (* 3/4 HEIGHT) play-again-height) SCALE) y))
init-playing)
((and (mouse=? mouse "button-down")
(< (* (- (* 3/4 WIDTH) highscore-width) SCALE) x) (< (* (- (* 3/4 HEIGHT) highscore-height) SCALE) y)
(> (* (+ (* 3/4 WIDTH) highscore-width) SCALE) x) (> (* (+ (* 3/4 HEIGHT) highscore-height) SCALE) y))
(highscore (game-over-score state)))
(else (game-over (game-over-score state) (game-over-position state) (game-over-tree state) 0))))
;;(else (init-playing))))
;; Tick events
; tick-event
(define (tick-event state)
(cond
((playing? state) (tick-event/playing state))
((chopping? state) (tick-event/chopping state))
(else state)))
(define (tick-event/playing state)
;;(update-playing-time (playing-time state) state))
(update-game state))
(define (tick-event/chopping state)
(back-to-playing state))