blob: b17106915d3e5279e2691088bf6a3701f01a05c5 (
plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
;;; Copyright (C) 2025 Gene Pasquet
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(require hyrule [case])
(import pygame.sprite [Sprite]
pygame [Surface]
pygame
utils [neg merge-moves Direction]
enum [Enum])
(defclass PlayerKilled [Exception])
(defclass Entity [Sprite]
;;; Game entity
(setv _fixed False)
(setv _type "none")
(defn __init__ [self id tile tile-size x y]
(.__init__ (super))
(setv self.id id)
(setv self.tile-size tile-size)
(setv self._surf (Surface #(tile-size tile-size)))
(.blit self._surf tile #(0 0))
(setv self._rect (.get_rect self._surf
:left (* x tile-size)
:top (* y tile-size))))
(defn [property] fixed [self]
self._fixed)
(defn [property] rect [self]
self._rect)
(defn [property] surf [self]
self._surf)
(defn [property] type [self]
self._type)
(defn [property] pos [self]
#((/ self.rect.x self.tile-size)
(/ self.rect.y self.tile-size))))
(defclass LevelTile [Entity]
(setv _fixed True)
(setv _type "level")
(defn __init__ [self id tile tile-size x y scaling]
(let [tile-width (* (.get_width tile) scaling)
tile-height (* (.get_height tile) scaling)
tile_ (if (!= scaling 1)
(pygame.transform.scale tile #(tile-width tile-height))
tile)]
(.__init__ (super) id tile_ tile-size x y))))
(defclass Goal [Entity]
(setv _type "goal")
(setv _fixed True))
(defclass Player [Entity]
(setv _type "player")
(setv SPEED 3)
(setv JUMP_IMPULSE 10)
(setv MAX_JUMPING 100)
(defn __init__ [self id tiles tile-size x y]
(.__init__ (super) id (get tiles 0) tile-size x y)
(setv self.tiles tiles)
(setv self.jumping False)
(setv self.jump-move 0)
(setv self.moves [])
(setv self._disp_surf (.copy self._surf))
(setv self.facing Direction.RIGHT)
(setv self.attacking False)
(setv self.animate-end 0))
(defn move [self move]
(.append self.moves move)
(.move_ip self._rect (get move 0) (get move 1))
(when (!= (get move 0) 0)
(setv self.facing
(.x-from-move Direction move))))
(defn [property] total-move [self]
(merge-moves self.moves))
(defn attack [self]
(setv self.attacking True))
(defn jump [self]
(setv self.jumping True)
(when (< self.jump-move self.MAX_JUMPING)
(setv self.jump-move (+ self.jump-move self.JUMP_IMPULSE))
(.move self #(0 (neg self.JUMP_IMPULSE)))))
(defn ground [self]
(setv self.jump-move 0))
(defn animate [self ticks]
;; Attack animation
(when (and self.attacking (= self.animate-end 0))
(setv self.animate-end (+ ticks 200))
(.blit self._surf (get self.tiles 1) #(0 0)))
(when (and self.attacking (> ticks self.animate-end))
(setv self.animate-end 0)
(setv self.attacking False)
(.blit self._surf (get self.tiles 0) #(0 0)))
;; Facing direction
(setv self._disp_surf
(case self.facing
Direction.LEFT (pygame.transform.flip self._surf True False)
Direction.RIGHT (.copy self._surf)))
(.flush self))
(defn flush [self]
(setv self.moves []))
(defn [property] surf [self]
self._disp_surf))
(defclass Enemy [Entity]
(setv _type "enemy")
(defn __init__ [self id tiles tile-size x y]
(.__init__ (super) id (get tiles 0) tile-size x y)
(setv self.tiles tiles)
(setv self.facing Direction.RIGHT)
(setv self.attacking False)
(setv self.animate-end 0)
(.flush self))
(defn move [self move]
(.append self.moves move)
(.move_ip self._rect (get move 0) (get move 1))
(when (!= (get move 0) 0)
(setv self.facing
(.x-from-move Direction move))))
(defn attack [self]
(setv self.attacking True))
(defn flush [self]
(setv self.moves []))
(defn ground [self])
(defn animate [self ticks]
;; Attack animation
(when (and self.attacking (= self.animate-end 0))
(setv self.animate-end (+ ticks 200))
(.blit self._surf (get self.tiles 1) #(0 0)))
(when (and self.attacking (> ticks self.animate-end))
(setv self.animate-end 0)
(setv self.attacking False)
(raise PlayerKilled)
(.blit self._surf (get self.tiles 0) #(0 0)))
(setv self._disp_surf
(case self.facing
Direction.LEFT (pygame.transform.flip self._surf True False)
Direction.RIGHT (.copy self._surf)))
(.flush self))
(defn [property] total-move [self]
(merge-moves self.moves))
(defn [property] surf [self]
self._disp_surf))
|