-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygame.comp
More file actions
77 lines (61 loc) · 2.08 KB
/
pygame.comp
File metadata and controls
77 lines (61 loc) · 2.08 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
/**
Pygame chimp tutorial example
Punch a monkey to win prizes.
Demonstrates event dispatch, animation, and game loop
using shape-based function overloading.
- Game state is an immutable shape, each frame produces a new state rather than
mutating in place
- Event handlers are overloaded on `~pg.event` shape with different type
constraints, dispatch replaces the typical event type switch statement
- `@update` on handlers means each one only returns the fields that changed, the
engine merges them onto the existing state
- `pick` maps a boolean to one of two values, replaces ternary operators
*/
!import pg comp "pygame-comp-spectacular@1.0.4"
!shape state ~{
bgd~pg.card
fist~pg.card
chimp~pg.card
}
!startup game (
!my window [
pg.display
| window {1280 480} scaled
| caption "Monkey Fever"
| mouse-visible false
]
!my state {
bgd = [{window.size fill={0.7 0.9 0.7}} | card]
fist = [{res.fist pivot={235 80}} | card]
chimp = [{res.chimp pivot=center} | card]
}
[pg.loop window quit-on={event.quit key.escape} :(
!param state ~state // loop sets this up as context?
[$.events | each handle]
[$.draw
| clear {0.7 0.9 0.7}
| draw-cards {state.chimp state.fist}]
)]
)
/// Handle mouse down: punch and check for hit
!func down.handle ~pg.event<type=mouse.up> @update (
!param state ~state
[if [state.fist state.chimp | intersect overlap=5] :{
chimp.rotate = [state.chimp.rotate | spin 360 0.5 ease-out]
chimp.translate.x = [state.chimp.translate.x | update after=chimp.rotate]
commands = [audio.play res.punch]
}
| else :{
commands = [audio.play res.whiff]
}]
)
/// Handle mouse up: retract fist
!func up.handle ~pg.event<type=mouse.up> @update ( //x this isn't a legal way to do equality of fields
!param state ~state
[state.fist | move {-10 -90}]
)
/// Handle mouse move: track fist position
!func move.handle ~pg.event<type=mouse.move> @update ( //x this isn't a legal way to do equality of fields
!param state ~state
[state.fist | position $.position]
)