Skip to content

Commit 79766df

Browse files
Add dead logic
1 parent 5a56f52 commit 79766df

File tree

2 files changed

+59
-65
lines changed

2 files changed

+59
-65
lines changed

src/game.rs

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::sync::Mutex;
22

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+
};
47
use rand::Rng;
58

69
use crate::HEIGHT;
@@ -17,7 +20,7 @@ pub enum Direction {
1720
#[derive(PartialEq)]
1821
pub enum AppleType {
1922
Referenced,
20-
Dereferenced
23+
Dereferenced,
2124
}
2225

2326
pub struct Game {
@@ -72,7 +75,6 @@ impl Game {
7275
pos_x as f32,
7376
pos_y as f32,
7477
));
75-
7678
} else {
7779
food_vector.push((
7880
AppleType::Dereferenced,
@@ -95,7 +97,7 @@ impl Game {
9597
ferris_direction: Direction::Right,
9698

9799
points: 0,
98-
hp: 50,
100+
hp: 40,
99101

100102
food: food_vector,
101103
})
@@ -105,15 +107,7 @@ impl Game {
105107
graphics::clear(ctx, graphics::BLACK);
106108

107109
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();
117111
}
118112

119113
let hp_pos_y;
@@ -124,62 +118,43 @@ impl Game {
124118
&self.ferris_pacman,
125119
DrawParam::default()
126120
.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+
}),
160145
)?;
161146

162147
let hp_rect_full = graphics::Mesh::new_rectangle(
163148
ctx,
164149
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),
171151
[1.0, 0.0, 0.0, 1.0].into(),
172152
)?;
173153

174154
let hp_rect_cur = graphics::Mesh::new_rectangle(
175155
ctx,
176156
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),
183158
[0.0, 1.0, 0.0, 1.0].into(),
184159
)?;
185160

@@ -189,7 +164,7 @@ impl Game {
189164
graphics::present(ctx)
190165
}
191166

192-
pub fn update(&mut self, ctx: &mut Context) -> GameResult<()> {
167+
pub fn update(&mut self, ctx: &mut Context) -> GameResult<Option<crate::Screen>> {
193168
if timer::ticks(ctx) % 6 == 0 {
194169
if self.ferris_pacman == self.ferris_pacman_collection[0] {
195170
self.ferris_pacman = self.ferris_pacman_collection[1].to_owned();
@@ -203,21 +178,26 @@ impl Game {
203178
if apple.2 == self.ferris_pos.0 && apple.3 == self.ferris_pos.1 {
204179
if apple.0 == AppleType::Referenced {
205180
self.points += 1;
206-
181+
207182
if self.hp != 100 {
208183
self.hp += 20;
209184
}
210185

211186
self.food.remove(i);
212-
}
213-
214-
else {
187+
} else {
215188
self.points -= 1;
216189

217190
if self.hp - 20 != 0 {
218191
self.hp -= 20;
219192
}
220193

194+
else {
195+
self.hp = 0;
196+
self.ferris_death_audio.play()?;
197+
198+
return Ok(Some(crate::Screen::Dead));
199+
}
200+
221201
self.food.remove(i);
222202
}
223203
}
@@ -244,7 +224,13 @@ impl Game {
244224
self.ferris_pos.1 = HEIGHT;
245225
}
246226

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)
248234
}
249235

250236
pub fn key_press(&mut self, keycode: KeyCode) -> Option<crate::Screen> {

src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ impl EventHandler for MyGame {
6767
fn update(&mut self, ctx: &mut Context) -> GameResult {
6868
match self.screen {
6969
Screen::Menu => self.menu_screen.update(ctx),
70-
Screen::Play => self.game_screen.lock().unwrap().update(ctx),
70+
Screen::Play => {
71+
let change = self.game_screen.lock().unwrap().update(ctx)?;
72+
73+
if let Some(s) = change {
74+
self.screen = s;
75+
}
76+
77+
Ok(())
78+
}
7179
Screen::Dead => self.death_screen.update(ctx),
7280
}
7381
}

0 commit comments

Comments
 (0)