Skip to content

Commit d6468aa

Browse files
Better menu code
1 parent f0f9a05 commit d6468aa

File tree

2 files changed

+164
-147
lines changed

2 files changed

+164
-147
lines changed

src/main.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{sync::Mutex, process::exit};
1+
use std::sync::Mutex;
22

33
use ggez::{conf::WindowSetup, audio::{SoundSource, Source}, event::{self, EventHandler}};
44
use ggez::graphics;
@@ -34,7 +34,7 @@ fn main() {
3434
}
3535
}
3636

37-
enum Screen {
37+
pub enum Screen {
3838
Menu,
3939
Play,
4040
Dead
@@ -49,30 +49,38 @@ pub struct MyGame {
4949
velocity: f64,
5050
consolas: graphics::Font,
5151
#[allow(dead_code)]
52-
ferris_dead: Source
52+
ferris_dead: Source,
53+
54+
menu_screen: menu::Menu
5355
}
5456

5557
impl MyGame {
5658
pub fn new(ctx: &mut Context) -> Self {
5759
let ferris_borrow_angry = graphics::Image::new(ctx, "/ferris_borrow_angry.png").unwrap();
5860
let ferris_planet = graphics::Image::new(ctx, "/ferris_planet.png").unwrap();
61+
let consolas = graphics::Font::new(ctx, "/Consolas.ttf").unwrap();
5962

6063
Self {
61-
ferris_borrow_fail: ferris_borrow_angry,
64+
ferris_borrow_fail: graphics::Image::new(ctx, "/ferris_borrow_angry.png").unwrap(),
6265
screen: Screen::Menu,
6366
game: None,
6467
velocity: 0.0,
6568
ferris_planet,
66-
consolas: graphics::Font::new(ctx, "/Consolas.ttf").unwrap(),
67-
ferris_dead: Source::new(ctx, "/dead.mp3").unwrap()
69+
consolas,
70+
ferris_dead: Source::new(ctx, "/dead.mp3").unwrap(),
71+
72+
menu_screen: menu::Menu {
73+
consolas,
74+
ferris_borrow_fail: ferris_borrow_angry
75+
}
6876
}
6977
}
7078
}
7179

7280
impl EventHandler for MyGame {
7381
fn update(&mut self, ctx: &mut Context) -> GameResult {
7482
match self.screen {
75-
Screen::Menu => menu::draw(self, ctx),
83+
Screen::Menu => self.menu_screen.draw(ctx),
7684
Screen::Play => {
7785
self.velocity += 0.7;
7886
self.game.as_mut().unwrap().lock().unwrap().pos_y += self.velocity as f32;
@@ -94,7 +102,7 @@ impl EventHandler for MyGame {
94102

95103
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
96104
match self.screen {
97-
Screen::Menu => menu::draw(self, ctx),
105+
Screen::Menu => self.menu_screen.draw(ctx),
98106
Screen::Play => {
99107
let game_state = self.game.as_ref().unwrap();
100108

@@ -115,17 +123,16 @@ impl EventHandler for MyGame {
115123
) {
116124
match self.screen {
117125
Screen::Menu => {
118-
if keycode == KeyCode::Key8 {
119-
exit(0);
120-
} else if keycode == KeyCode::Key7 {
121-
self.screen = Screen::Play;
122-
self.game = Some(Mutex::new(
123-
game::Game {
124-
ferris_borrow_fail: self.ferris_borrow_fail.to_owned(),
125-
pos_y: HEIGHT / 2.0
126-
}
127-
));
126+
let change = self.menu_screen.key_press(keycode);
127+
128+
if let Some(s) = change {
129+
self.screen = s;
130+
self.game = Some(Mutex::new(game::Game {
131+
ferris_borrow_fail: self.ferris_borrow_fail.to_owned(),
132+
pos_y: HEIGHT / 2.0
133+
}))
128134
}
135+
129136
}
130137
Screen::Play => {
131138
if keycode == KeyCode::Space {

src/menu.rs

Lines changed: 139 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,141 @@
1-
use ggez::{
2-
graphics::{self, Color, Scale, Text, TextFragment},
3-
Context, GameResult,
4-
};
1+
use ggez::{Context, GameResult, event::KeyCode, graphics::{self, Color, Scale, Text, TextFragment}};
2+
use std::process::exit;
53

6-
#[allow(dead_code)]
7-
8-
const WIDTH: f32 = crate::WIDTH;
9-
#[allow(dead_code)]
10-
11-
const HEIGHT: f32 = crate::HEIGHT;
12-
13-
pub fn draw(this: &mut crate::MyGame, ctx: &mut Context) -> GameResult<()> {
14-
graphics::clear(ctx, graphics::BLACK);
15-
16-
let call_of_ferris_text = Text::new(TextFragment {
17-
// `TextFragment` stores a string, and optional parameters which will override those
18-
// of `Text` itself. This allows inlining differently formatted lines, words,
19-
// or even individual letters, into the same block of text.
20-
text: "Call of Ferris".to_string(),
21-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
22-
// `Font::default()` always exists and maps to DejaVuSerif.
23-
font: Some(this.consolas),
24-
scale: Some(Scale::uniform(33.0)),
25-
// This doesn't do anything at this point; can be used to omit fields in declarations.
26-
..Default::default()
27-
});
28-
29-
let ownership_war = Text::new(TextFragment {
30-
// `TextFragment` stores a string, and optional parameters which will override those
31-
// of `Text` itself. This allows inlining differently formatted lines, words,
32-
// or even individual letters, into the same block of text.
33-
text: "Ownership War".to_string(),
34-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
35-
// `Font::default()` always exists and maps to DejaVuSerif.
36-
font: Some(this.consolas),
37-
scale: Some(Scale::uniform(14.0)),
38-
39-
color: Some(Color::new(1.0, 80.0 / 255.0, 76.0 / 255.0, 1.0)),
40-
..Default::default()
41-
});
42-
43-
graphics::draw(
44-
ctx,
45-
&this.ferris_borrow_fail,
46-
(ggez::nalgebra::Point2::new((WIDTH / 2.0) - 85.0, 100.0),),
47-
)
48-
.unwrap();
49-
graphics::draw(
50-
ctx,
51-
&call_of_ferris_text,
52-
(ggez::nalgebra::Point2::new((WIDTH / 2.0) - 130.0, 250.0),),
53-
)
54-
.unwrap();
55-
graphics::draw(
56-
ctx,
57-
&ownership_war,
58-
(ggez::nalgebra::Point2::new((WIDTH / 2.0) - 50.0, 300.0),),
59-
)
60-
.unwrap();
61-
62-
let play_rect = graphics::Mesh::new_rectangle(
63-
ctx,
64-
graphics::DrawMode::fill(),
65-
graphics::Rect::new((WIDTH / 2.0) - 320.0, 410.0, 160.0, 60.0),
66-
[1.0, 0.5, 0.0, 1.0].into(),
67-
)?;
68-
69-
let dirty_pointer = graphics::Mesh::new_rectangle(
70-
ctx,
71-
graphics::DrawMode::fill(),
72-
graphics::Rect::new((WIDTH / 2.0) + 180.0, 410.0, 160.0, 60.0),
73-
[4.0 / 255.0, 129.0 / 255.0, 191.0 / 255.0, 1.0].into(),
74-
)?;
75-
76-
let play_borrow = Text::new(TextFragment {
77-
// `TextFragment` stores a string, and optional parameters which will override those
78-
// of `Text` itself. This allows inlining differently formatted lines, words,
79-
// or even individual letters, into the same block of text.
80-
text: "& to play".to_string(),
81-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
82-
// `Font::default()` always exists and maps to DejaVuSerif.
83-
font: Some(this.consolas),
84-
scale: Some(Scale::uniform(25.0)),
85-
86-
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
87-
// This doesn't do anything at this point; can be used to omit fields in declarations.
88-
..Default::default()
89-
});
90-
91-
let dirty_pointer_quit = Text::new(TextFragment {
92-
// `TextFragment` stores a string, and optional parameters which will override those
93-
// of `Text` itself. This allows inlining differently formatted lines, words,
94-
// or even individual letters, into the same block of text.
95-
text: "* to quit".to_string(),
96-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
97-
// `Font::default()` always exists and maps to DejaVuSerif.
98-
font: Some(this.consolas),
99-
scale: Some(Scale::uniform(25.0)),
100-
101-
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
102-
// This doesn't do anything at this point; can be used to omit fields in declarations.
103-
..Default::default()
104-
});
105-
106-
graphics::draw(ctx, &play_rect, (ggez::mint::Point2 { x: 0.0, y: 0.0 },))?;
107-
graphics::draw(
108-
ctx,
109-
&dirty_pointer,
110-
(ggez::mint::Point2 { x: 0.0, y: 0.0 },),
111-
)?;
112-
113-
graphics::draw(
114-
ctx,
115-
&play_borrow,
116-
(ggez::mint::Point2 {
117-
x: (WIDTH / 2.0) - 300.0,
118-
y: 430.0,
119-
},),
120-
)?;
121-
graphics::draw(
122-
ctx,
123-
&dirty_pointer_quit,
124-
(ggez::mint::Point2 {
125-
x: (WIDTH / 2.0) + 200.0,
126-
y: 430.0,
127-
},),
128-
)?;
129-
130-
graphics::present(ctx)
4+
pub struct Menu {
5+
pub consolas: graphics::Font,
6+
pub ferris_borrow_fail: graphics::Image
1317
}
8+
9+
impl Menu {
10+
pub fn draw(&self, ctx: &mut Context) -> GameResult<()> {
11+
graphics::clear(ctx, graphics::BLACK);
12+
13+
let call_of_ferris_text = Text::new(TextFragment {
14+
// `TextFragment` stores a string, and optional parameters which will override those
15+
// of `Text` itself. This allows inlining differently formatted lines, words,
16+
// or even individual letters, into the same block of text.
17+
text: "Call of Ferris".to_string(),
18+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
19+
// `Font::default()` always exists and maps to DejaVuSerif.
20+
font: Some(self.consolas),
21+
scale: Some(Scale::uniform(33.0)),
22+
// This doesn't do anything at this point; can be used to omit fields in declarations.
23+
..Default::default()
24+
});
25+
26+
let ownership_war = Text::new(TextFragment {
27+
// `TextFragment` stores a string, and optional parameters which will override those
28+
// of `Text` itself. This allows inlining differently formatted lines, words,
29+
// or even individual letters, into the same block of text.
30+
text: "Ownership War".to_string(),
31+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
32+
// `Font::default()` always exists and maps to DejaVuSerif.
33+
font: Some(self.consolas),
34+
scale: Some(Scale::uniform(14.0)),
35+
36+
color: Some(Color::new(1.0, 80.0 / 255.0, 76.0 / 255.0, 1.0)),
37+
..Default::default()
38+
});
39+
40+
graphics::draw(
41+
ctx,
42+
&self.ferris_borrow_fail,
43+
(ggez::nalgebra::Point2::new((crate::WIDTH / 2.0) - 85.0, 100.0),),
44+
)
45+
.unwrap();
46+
graphics::draw(
47+
ctx,
48+
&call_of_ferris_text,
49+
(ggez::nalgebra::Point2::new((crate::WIDTH / 2.0) - 130.0, 250.0),),
50+
)
51+
.unwrap();
52+
graphics::draw(
53+
ctx,
54+
&ownership_war,
55+
(ggez::nalgebra::Point2::new((crate::WIDTH / 2.0) - 50.0, 300.0),),
56+
)
57+
.unwrap();
58+
59+
let play_rect = graphics::Mesh::new_rectangle(
60+
ctx,
61+
graphics::DrawMode::fill(),
62+
graphics::Rect::new((crate::WIDTH / 2.0) - 320.0, 410.0, 160.0, 60.0),
63+
[1.0, 0.5, 0.0, 1.0].into(),
64+
)?;
65+
66+
let dirty_pointer = graphics::Mesh::new_rectangle(
67+
ctx,
68+
graphics::DrawMode::fill(),
69+
graphics::Rect::new((crate::WIDTH / 2.0) + 180.0, 410.0, 160.0, 60.0),
70+
[4.0 / 255.0, 129.0 / 255.0, 191.0 / 255.0, 1.0].into(),
71+
)?;
72+
73+
let play_borrow = Text::new(TextFragment {
74+
// `TextFragment` stores a string, and optional parameters which will override those
75+
// of `Text` itself. This allows inlining differently formatted lines, words,
76+
// or even individual letters, into the same block of text.
77+
text: "& to play".to_string(),
78+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
79+
// `Font::default()` always exists and maps to DejaVuSerif.
80+
font: Some(self.consolas),
81+
scale: Some(Scale::uniform(25.0)),
82+
83+
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
84+
// This doesn't do anything at this point; can be used to omit fields in declarations.
85+
..Default::default()
86+
});
87+
88+
let dirty_pointer_quit = Text::new(TextFragment {
89+
// `TextFragment` stores a string, and optional parameters which will override those
90+
// of `Text` itself. This allows inlining differently formatted lines, words,
91+
// or even individual letters, into the same block of text.
92+
text: "* to quit".to_string(),
93+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
94+
// `Font::default()` always exists and maps to DejaVuSerif.
95+
font: Some(self.consolas),
96+
scale: Some(Scale::uniform(25.0)),
97+
98+
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
99+
// This doesn't do anything at this point; can be used to omit fields in declarations.
100+
..Default::default()
101+
});
102+
103+
graphics::draw(ctx, &play_rect, (ggez::mint::Point2 { x: 0.0, y: 0.0 },))?;
104+
graphics::draw(
105+
ctx,
106+
&dirty_pointer,
107+
(ggez::mint::Point2 { x: 0.0, y: 0.0 },),
108+
)?;
109+
110+
graphics::draw(
111+
ctx,
112+
&play_borrow,
113+
(ggez::mint::Point2 {
114+
x: (crate::WIDTH / 2.0) - 300.0,
115+
y: 430.0,
116+
},),
117+
)?;
118+
graphics::draw(
119+
ctx,
120+
&dirty_pointer_quit,
121+
(ggez::mint::Point2 {
122+
x: (crate::WIDTH / 2.0) + 200.0,
123+
y: 430.0,
124+
},),
125+
)?;
126+
127+
graphics::present(ctx)
128+
}
129+
130+
pub fn key_press(&self, keycode: KeyCode) -> Option<crate::Screen> {
131+
if keycode == KeyCode::Key7 {
132+
return Some(crate::Screen::Play);
133+
}
134+
135+
else if keycode == KeyCode::Key8 {
136+
exit(0);
137+
}
138+
139+
return None;
140+
}
141+
}

0 commit comments

Comments
 (0)