1
1
use std:: sync:: Mutex ;
2
2
3
- use ggez:: { Context , graphics:: DrawParam , GameResult , audio:: SoundSource , audio:: Source , event:: KeyCode , graphics, timer} ;
3
+ use ggez:: {
4
+ audio:: SoundSource , audio:: Source , event:: KeyCode , graphics, graphics:: DrawParam , timer,
5
+ Context , GameResult ,
6
+ } ;
4
7
use rand:: Rng ;
5
8
6
9
use crate :: HEIGHT ;
@@ -17,7 +20,7 @@ pub enum Direction {
17
20
#[ derive( PartialEq ) ]
18
21
pub enum AppleType {
19
22
Referenced ,
20
- Dereferenced
23
+ Dereferenced ,
21
24
}
22
25
23
26
pub struct Game {
@@ -72,7 +75,6 @@ impl Game {
72
75
pos_x as f32 ,
73
76
pos_y as f32 ,
74
77
) ) ;
75
-
76
78
} else {
77
79
food_vector. push ( (
78
80
AppleType :: Dereferenced ,
@@ -95,7 +97,7 @@ impl Game {
95
97
ferris_direction : Direction :: Right ,
96
98
97
99
points : 0 ,
98
- hp : 50 ,
100
+ hp : 40 ,
99
101
100
102
food : food_vector,
101
103
} )
@@ -105,15 +107,7 @@ impl Game {
105
107
graphics:: clear ( ctx, graphics:: BLACK ) ;
106
108
107
109
for food in self . food . iter ( ) {
108
- graphics:: draw (
109
- ctx,
110
- & food. 1 ,
111
- ( ggez:: nalgebra:: Point2 :: new (
112
- food. 2 ,
113
- food. 3 ,
114
- ) , ) ,
115
- )
116
- . unwrap ( ) ;
110
+ graphics:: draw ( ctx, & food. 1 , ( ggez:: nalgebra:: Point2 :: new ( food. 2 , food. 3 ) , ) ) . unwrap ( ) ;
117
111
}
118
112
119
113
let hp_pos_y;
@@ -124,62 +118,43 @@ impl Game {
124
118
& self . ferris_pacman ,
125
119
DrawParam :: default ( )
126
120
. dest ( ggez:: nalgebra:: Point2 :: new (
127
- self . ferris_pos . 0 ,
128
- self . ferris_pos . 1 ,
129
- )
130
- )
131
- . rotation (
132
- if self . ferris_direction == Direction :: Up {
133
- hp_pos_y = self . ferris_pos . 1 + 10.0 ;
134
- hp_pos_x = self . ferris_pos . 0 - 20.0 ;
135
-
136
- 80.0
137
- }
138
-
139
- else if self . ferris_direction == Direction :: Down {
140
- hp_pos_y = self . ferris_pos . 1 - 50.0 ;
141
- hp_pos_x = self . ferris_pos . 0 - 80.0 ;
142
-
143
- -80.0
144
- }
145
-
146
- else if self . ferris_direction == Direction :: Left {
147
- hp_pos_y = self . ferris_pos . 1 - 100.0 ;
148
- hp_pos_x = self . ferris_pos . 0 - 80.0 ;
149
-
150
- 160.0
151
- }
152
-
153
- else {
154
- hp_pos_y = self . ferris_pos . 1 - 50.0 ;
155
- hp_pos_x = self . ferris_pos . 0 - 20.0 ;
156
-
157
- 0.0
158
- }
159
- )
121
+ self . ferris_pos . 0 ,
122
+ self . ferris_pos . 1 ,
123
+ ) )
124
+ . rotation ( if self . ferris_direction == Direction :: Up {
125
+ hp_pos_y = self . ferris_pos . 1 + 10.0 ;
126
+ hp_pos_x = self . ferris_pos . 0 - 20.0 ;
127
+
128
+ 80.0
129
+ } else if self . ferris_direction == Direction :: Down {
130
+ hp_pos_y = self . ferris_pos . 1 - 50.0 ;
131
+ hp_pos_x = self . ferris_pos . 0 - 80.0 ;
132
+
133
+ -80.0
134
+ } else if self . ferris_direction == Direction :: Left {
135
+ hp_pos_y = self . ferris_pos . 1 - 100.0 ;
136
+ hp_pos_x = self . ferris_pos . 0 - 80.0 ;
137
+
138
+ 160.0
139
+ } else {
140
+ hp_pos_y = self . ferris_pos . 1 - 50.0 ;
141
+ hp_pos_x = self . ferris_pos . 0 - 20.0 ;
142
+
143
+ 0.0
144
+ } ) ,
160
145
) ?;
161
146
162
147
let hp_rect_full = graphics:: Mesh :: new_rectangle (
163
148
ctx,
164
149
graphics:: DrawMode :: fill ( ) ,
165
- graphics:: Rect :: new (
166
- hp_pos_x,
167
- hp_pos_y,
168
- 100.0 ,
169
- 20.0 ,
170
- ) ,
150
+ graphics:: Rect :: new ( hp_pos_x, hp_pos_y, 100.0 , 20.0 ) ,
171
151
[ 1.0 , 0.0 , 0.0 , 1.0 ] . into ( ) ,
172
152
) ?;
173
153
174
154
let hp_rect_cur = graphics:: Mesh :: new_rectangle (
175
155
ctx,
176
156
graphics:: DrawMode :: fill ( ) ,
177
- graphics:: Rect :: new (
178
- hp_pos_x,
179
- hp_pos_y,
180
- self . hp as f32 ,
181
- 20.0 ,
182
- ) ,
157
+ graphics:: Rect :: new ( hp_pos_x, hp_pos_y, self . hp as f32 , 20.0 ) ,
183
158
[ 0.0 , 1.0 , 0.0 , 1.0 ] . into ( ) ,
184
159
) ?;
185
160
@@ -189,7 +164,7 @@ impl Game {
189
164
graphics:: present ( ctx)
190
165
}
191
166
192
- pub fn update ( & mut self , ctx : & mut Context ) -> GameResult < ( ) > {
167
+ pub fn update ( & mut self , ctx : & mut Context ) -> GameResult < Option < crate :: Screen > > {
193
168
if timer:: ticks ( ctx) % 6 == 0 {
194
169
if self . ferris_pacman == self . ferris_pacman_collection [ 0 ] {
195
170
self . ferris_pacman = self . ferris_pacman_collection [ 1 ] . to_owned ( ) ;
@@ -203,21 +178,26 @@ impl Game {
203
178
if apple. 2 == self . ferris_pos . 0 && apple. 3 == self . ferris_pos . 1 {
204
179
if apple. 0 == AppleType :: Referenced {
205
180
self . points += 1 ;
206
-
181
+
207
182
if self . hp != 100 {
208
183
self . hp += 20 ;
209
184
}
210
185
211
186
self . food . remove ( i) ;
212
- }
213
-
214
- else {
187
+ } else {
215
188
self . points -= 1 ;
216
189
217
190
if self . hp - 20 != 0 {
218
191
self . hp -= 20 ;
219
192
}
220
193
194
+ else {
195
+ self . hp = 0 ;
196
+ self . ferris_death_audio . play ( ) ?;
197
+
198
+ return Ok ( Some ( crate :: Screen :: Dead ) ) ;
199
+ }
200
+
221
201
self . food . remove ( i) ;
222
202
}
223
203
}
@@ -244,7 +224,13 @@ impl Game {
244
224
self . ferris_pos . 1 = HEIGHT ;
245
225
}
246
226
247
- Ok ( ( ) )
227
+ if self . hp == 0 {
228
+ self . ferris_death_audio . play ( ) ?;
229
+
230
+ return Ok ( Some ( crate :: Screen :: Dead ) ) ;
231
+ }
232
+
233
+ Ok ( None )
248
234
}
249
235
250
236
pub fn key_press ( & mut self , keycode : KeyCode ) -> Option < crate :: Screen > {
0 commit comments