Skip to content

Commit 46913b5

Browse files
Add the menu screen and quit buttons and also you win text
1 parent 6dfa115 commit 46913b5

File tree

2 files changed

+98
-15
lines changed

2 files changed

+98
-15
lines changed

src/game.rs

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{collections::HashMap, io::Read, sync::Mutex};
1+
use std::{collections::HashMap, io::Read, process::exit, sync::Mutex};
22

3-
use ggez::{Context, GameResult, audio::{SoundSource, Source}, event::KeyCode, graphics::{self, Color, DrawParam, Shader, Text}, mint, nalgebra::Point2, timer};
3+
use ggez::{Context, GameResult, audio::{SoundSource, Source}, event::KeyCode, graphics::{self, Color, DrawParam, Drawable, Shader, Text}, mint, nalgebra::Point2, timer};
44
use ggez_goodies::{
55
camera::{Camera, CameraDraw},
66
nalgebra_glm::Vec2,
@@ -10,19 +10,14 @@ use graphics::{Font, GlBackendSpec, Image, Scale, ShaderGeneric, TextFragment};
1010
use mint::Vector2;
1111
use rand::Rng;
1212

13-
use crate::{
14-
components::{
13+
use crate::{HEIGHT, Screen, WIDTH, components::{
1514
barrel::Barrel,
1615
bullet::Turbofish,
1716
cloud::Cloud,
1817
enemy::Enemy,
1918
player::{Direction, Player},
2019
tile::Tile,
21-
},
22-
utils::{lerp, remap},
23-
map::Map,
24-
Screen, HEIGHT, WIDTH,
25-
};
20+
}, map::Map, utils::{lerp, remap}};
2621

2722
use gfx::*;
2823

@@ -64,7 +59,8 @@ pub struct Game {
6459

6560
end: Option<String>,
6661

67-
draw_end_text: (bool, Option<usize>, bool, bool) // Thread Sleeped?, Current Iters, Done?, Win?
62+
draw_end_text: (bool, Option<usize>, bool, bool), // Thread Sleeped?, Current Iters, Done?, Win?
63+
can_die: bool
6864
}
6965

7066
impl Game {
@@ -175,10 +171,11 @@ impl Game {
175171
draw_end_text: (false, None, false, false),
176172

177173
end: map_1.end,
174+
can_die: true
178175
})
179176
}
180177

181-
pub fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
178+
pub fn draw(&mut self, ctx: &mut Context) -> GameResult<Option<Screen>> {
182179
if let Some(_t) = self.tics {
183180
{
184181
let _lock = graphics::use_shader(ctx, &self.dim_shader);
@@ -215,12 +212,82 @@ impl Game {
215212

216213
draw_pos += 20.0;
217214
}
215+
216+
// Press & to go to menu screen
217+
let menu_rect = graphics::Mesh::new_rectangle(
218+
ctx,
219+
graphics::DrawMode::fill(),
220+
graphics::Rect::new(
221+
(WIDTH / 2.) + 20.,
222+
(HEIGHT / 2.) + (draw_pos * 2.),
223+
220.0,
224+
40.0,
225+
),
226+
[36.0 / 255.0, 36.0 / 255.0, 36.0 / 255.0, 0.9].into(),
227+
)?;
228+
229+
let menu_rect_dim = menu_rect.dimensions(ctx).unwrap();
230+
231+
let menu_frag_to = &Text::new(TextFragment::new("Press & go to the")
232+
.font(self.consolas)
233+
);
234+
235+
let menu_screen = &Text::new(TextFragment::new("MENU SCREEN")
236+
.font(self.consolas)
237+
.scale(Scale::uniform(20.0))
238+
);
239+
240+
graphics::draw(ctx, &menu_rect, DrawParam::default())?;
241+
graphics::draw(
242+
ctx, menu_frag_to, DrawParam::default()
243+
.dest(Point2::new((WIDTH / 2.) + 20., ((HEIGHT / 2.) + (draw_pos * 2.)) - 20.0))
244+
)?;
245+
246+
graphics::draw(
247+
ctx, menu_screen, DrawParam::default()
248+
.dest(Point2::new((WIDTH / 2.) + 70., ((HEIGHT / 2.) + (draw_pos * 2.)) + 12.0))
249+
)?;
250+
251+
// Press * to quit
252+
let quit_rect = graphics::Mesh::new_rectangle(
253+
ctx,
254+
graphics::DrawMode::fill(),
255+
graphics::Rect::new(
256+
((WIDTH / 2.) - menu_rect_dim.w) - 20.0,
257+
(HEIGHT / 2.) + (draw_pos * 2.),
258+
220.0,
259+
40.0,
260+
),
261+
[36.0 / 255.0, 36.0 / 255.0, 36.0 / 255.0, 0.9].into(),
262+
)?;
263+
264+
let quit_frag_to = &Text::new(TextFragment::new("Press * to")
265+
.font(self.consolas)
266+
);
267+
268+
let press_quit = &Text::new(TextFragment::new("QUIT")
269+
.font(self.consolas)
270+
.scale(Scale::uniform(20.))
271+
);
272+
273+
graphics::draw(ctx, &quit_rect, DrawParam::default())?;
274+
graphics::draw(
275+
ctx, quit_frag_to, DrawParam::default()
276+
.dest(Point2::new(((WIDTH / 2.) - menu_rect_dim.w) - 20., ((HEIGHT / 2.) + (draw_pos * 2.)) - 20.))
277+
)?;
278+
279+
graphics::draw(
280+
ctx, press_quit, DrawParam::default()
281+
.dest(Point2::new((((WIDTH / 2.) - menu_rect_dim.w) - 20.) + 90., (((HEIGHT / 2.) + (draw_pos * 2.)) - 20.) + 30.))
282+
)?;
218283
}
219284
} else {
220285
self.inner_draw(ctx)?;
221286
}
222287

223-
graphics::present(ctx)
288+
graphics::present(ctx)?;
289+
290+
Ok(None)
224291
}
225292

226293
fn inner_draw(&mut self, ctx: &mut Context) -> GameResult<()> {
@@ -368,6 +435,7 @@ impl Game {
368435
pub fn inner_update(&mut self, ctx: &mut Context) -> GameResult<Option<crate::Screen>> {
369436
if self.enemies.len() == 0 {
370437
self.draw_end_text.3 = true;
438+
self.can_die = false;
371439

372440
if self.draw_end_text.1.is_none() {
373441
self.draw_end_text.1 = Some(timer::ticks(ctx));
@@ -415,7 +483,9 @@ impl Game {
415483
.move_to(Vec2::new(self.player.pos_x, self.player.pos_y));
416484

417485
if self.player.pos_y < -800. {
418-
return Ok(Some(Screen::Dead));
486+
if self.can_die {
487+
return Ok(Some(Screen::Dead));
488+
}
419489
}
420490

421491
for i in 0..self.enemies.len() {
@@ -619,8 +689,13 @@ impl Game {
619689
}
620690
}
621691
KeyCode::Up => {
622-
// TODO: Add chromatic aberration on slow motion.
623692
self.tics = Some(6);
693+
},
694+
KeyCode::Key7 => {
695+
return Some(Screen::Menu);
696+
}
697+
KeyCode::Key8 => {
698+
exit(0);
624699
}
625700
_ => (),
626701
}

src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ impl EventHandler for MyGame {
9797
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
9898
match self.screen {
9999
Screen::Menu => self.menu_screen.draw(ctx),
100-
Screen::Play => self.game_screen.lock().unwrap().draw(ctx),
100+
Screen::Play => {
101+
let change = self.game_screen.lock().unwrap().draw(ctx)?;
102+
103+
if let Some(s) = change {
104+
self.screen = s;
105+
}
106+
107+
Ok(())
108+
},
101109
Screen::Dead => self.death_screen.draw(ctx),
102110
}
103111
}

0 commit comments

Comments
 (0)