Skip to content

Commit 5ff5a51

Browse files
Refactor code
1 parent e59025e commit 5ff5a51

File tree

6 files changed

+278
-246
lines changed

6 files changed

+278
-246
lines changed

resources/background.png

-97.4 KB
Binary file not shown.
File renamed without changes.

src/dead.rs

Lines changed: 146 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,150 @@
1-
use ggez::{graphics::Color, Context, GameResult, graphics::{self, Scale, Text, TextFragment}};
1+
use ggez::{
2+
graphics::Color,
3+
graphics::{self, Scale, Text, TextFragment},
4+
Context, GameResult,
5+
};
26

3-
pub fn draw(this: &mut crate::MyGame, ctx: &mut Context) -> GameResult<()> {
4-
graphics::clear(ctx, graphics::BLACK);
7+
pub struct Death {
8+
consolas: graphics::Font,
9+
ferris_planet: graphics::Image
10+
}
511

6-
let dead = Text::new(TextFragment {
7-
// `TextFragment` stores a string, and optional parameters which will override those
8-
// of `Text` itself. This allows inlining differently formatted lines, words,
9-
// or even individual letters, into the same block of text.
10-
text: "YOU DEAD".to_string(),
11-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
12-
// `Font::default()` always exists and maps to DejaVuSerif.
13-
scale: Some(Scale::uniform(35.0)),
14-
font: Some(this.consolas),
15-
color: Some(Color::new(1.0, 80.0 / 255.0, 76.0 / 255.0, 1.0)),
16-
// This doesn't do anything at this point; can be used to omit fields in declarations.
17-
..Default::default()
18-
});
12+
impl Death {
13+
pub fn spawn(ctx: &mut Context) -> Self {
14+
Self {
15+
consolas: graphics::Font::new(ctx, "/Consolas.ttf").unwrap(),
16+
ferris_planet: graphics::Image::new(ctx, "/ferris_planet.png").unwrap()
17+
}
18+
}
1919

20-
let unsafe_dead = Text::new(TextFragment {
21-
// `TextFragment` stores a string, and optional parameters which will override those
22-
// of `Text` itself. This allows inlining differently formatted lines, words,
23-
// or even individual letters, into the same block of text.
24-
text: "unsafe".to_string(),
25-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
26-
// `Font::default()` always exists and maps to DejaVuSerif.
27-
scale: Some(Scale::uniform(30.0)),
28-
font: Some(this.consolas),
29-
color: Some(Color::new(74.0 / 255.0, 129.0 / 255.0, 191.0 / 255.0, 1.0)),
30-
// This doesn't do anything at this point; can be used to omit fields in declarations.
31-
..Default::default()
32-
});
33-
34-
let unsafe_dead_block_start = Text::new(TextFragment {
35-
// `TextFragment` stores a string, and optional parameters which will override those
36-
// of `Text` itself. This allows inlining differently formatted lines, words,
37-
// or even individual letters, into the same block of text.
38-
text: "{".to_string(),
39-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
40-
// `Font::default()` always exists and maps to DejaVuSerif.
41-
scale: Some(Scale::uniform(30.0)),
42-
font: Some(this.consolas),
43-
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
44-
// This doesn't do anything at this point; can be used to omit fields in declarations.
45-
..Default::default()
46-
});
47-
48-
let unsafe_dead_block_func = Text::new(TextFragment {
49-
// `TextFragment` stores a string, and optional parameters which will override those
50-
// of `Text` itself. This allows inlining differently formatted lines, words,
51-
// or even individual letters, into the same block of text.
52-
text: "dead()".to_string(),
53-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
54-
// `Font::default()` always exists and maps to DejaVuSerif.
55-
scale: Some(Scale::uniform(30.0)),
56-
font: Some(this.consolas),
57-
color: Some(Color::new(214.0 / 255.0, 208.0 / 255.0, 132.0 / 255.0, 1.0)),
58-
// This doesn't do anything at this point; can be used to omit fields in declarations.
59-
..Default::default()
60-
});
61-
62-
let unsafe_dead_block_end = Text::new(TextFragment {
63-
// `TextFragment` stores a string, and optional parameters which will override those
64-
// of `Text` itself. This allows inlining differently formatted lines, words,
65-
// or even individual letters, into the same block of text.
66-
text: "}".to_string(),
67-
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
68-
// `Font::default()` always exists and maps to DejaVuSerif.
69-
scale: Some(Scale::uniform(30.0)),
70-
font: Some(this.consolas),
71-
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
72-
// This doesn't do anything at this point; can be used to omit fields in declarations.
73-
..Default::default()
74-
});
75-
76-
graphics::draw(
77-
ctx,
78-
&dead,
79-
(ggez::mint::Point2 {
80-
x: (crate::WIDTH / 2.0) - 60.0,
81-
y: 40.0,
82-
},),
83-
)?;
84-
85-
graphics::draw(
86-
ctx,
87-
&unsafe_dead,
88-
(ggez::mint::Point2 {
89-
x: (crate::WIDTH / 2.0) - 200.0,
90-
y: 200.0,
91-
},),
92-
)?;
93-
94-
graphics::draw(
95-
ctx,
96-
&unsafe_dead_block_start,
97-
(ggez::mint::Point2 {
98-
x: (crate::WIDTH / 2.0) - 90.0,
99-
y: 200.0,
100-
},),
101-
)?;
102-
103-
graphics::draw(
104-
ctx,
105-
&unsafe_dead_block_func,
106-
(ggez::mint::Point2 {
107-
x: (crate::WIDTH / 2.0) - 125.0,
108-
y: 260.0,
109-
},),
110-
)?;
111-
112-
graphics::draw(
113-
ctx,
114-
&unsafe_dead_block_end,
115-
(ggez::mint::Point2 {
116-
x: (crate::WIDTH / 2.0) - 200.0,
117-
y: 300.0,
118-
},),
119-
)?;
120-
121-
graphics::draw(
122-
ctx,
123-
&this.ferris_planet,
124-
(ggez::nalgebra::Point2::new((crate::WIDTH / 2.0) - 10.0, 240.0),),
125-
).unwrap();
126-
127-
graphics::present(ctx)
20+
pub fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
21+
graphics::clear(ctx, graphics::BLACK);
22+
23+
let dead = Text::new(TextFragment {
24+
// `TextFragment` stores a string, and optional parameters which will override those
25+
// of `Text` itself. This allows inlining differently formatted lines, words,
26+
// or even individual letters, into the same block of text.
27+
text: "YOU DEAD".to_string(),
28+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
29+
// `Font::default()` always exists and maps to DejaVuSerif.
30+
scale: Some(Scale::uniform(35.0)),
31+
font: Some(self.consolas),
32+
color: Some(Color::new(1.0, 80.0 / 255.0, 76.0 / 255.0, 1.0)),
33+
// This doesn't do anything at this point; can be used to omit fields in declarations.
34+
..Default::default()
35+
});
36+
37+
let unsafe_dead = Text::new(TextFragment {
38+
// `TextFragment` stores a string, and optional parameters which will override those
39+
// of `Text` itself. This allows inlining differently formatted lines, words,
40+
// or even individual letters, into the same block of text.
41+
text: "unsafe".to_string(),
42+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
43+
// `Font::default()` always exists and maps to DejaVuSerif.
44+
scale: Some(Scale::uniform(30.0)),
45+
font: Some(self.consolas),
46+
color: Some(Color::new(74.0 / 255.0, 129.0 / 255.0, 191.0 / 255.0, 1.0)),
47+
// This doesn't do anything at this point; can be used to omit fields in declarations.
48+
..Default::default()
49+
});
50+
51+
let unsafe_dead_block_start = Text::new(TextFragment {
52+
// `TextFragment` stores a string, and optional parameters which will override those
53+
// of `Text` itself. This allows inlining differently formatted lines, words,
54+
// or even individual letters, into the same block of text.
55+
text: "{".to_string(),
56+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
57+
// `Font::default()` always exists and maps to DejaVuSerif.
58+
scale: Some(Scale::uniform(30.0)),
59+
font: Some(self.consolas),
60+
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
61+
// This doesn't do anything at this point; can be used to omit fields in declarations.
62+
..Default::default()
63+
});
64+
65+
let unsafe_dead_block_func = Text::new(TextFragment {
66+
// `TextFragment` stores a string, and optional parameters which will override those
67+
// of `Text` itself. This allows inlining differently formatted lines, words,
68+
// or even individual letters, into the same block of text.
69+
text: "dead()".to_string(),
70+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
71+
// `Font::default()` always exists and maps to DejaVuSerif.
72+
scale: Some(Scale::uniform(30.0)),
73+
font: Some(self.consolas),
74+
color: Some(Color::new(214.0 / 255.0, 208.0 / 255.0, 132.0 / 255.0, 1.0)),
75+
// This doesn't do anything at this point; can be used to omit fields in declarations.
76+
..Default::default()
77+
});
78+
79+
let unsafe_dead_block_end = Text::new(TextFragment {
80+
// `TextFragment` stores a string, and optional parameters which will override those
81+
// of `Text` itself. This allows inlining differently formatted lines, words,
82+
// or even individual letters, into the same block of text.
83+
text: "}".to_string(),
84+
// `Font` is a handle to a loaded TTF, stored inside the `Context`.
85+
// `Font::default()` always exists and maps to DejaVuSerif.
86+
scale: Some(Scale::uniform(30.0)),
87+
font: Some(self.consolas),
88+
color: Some(Color::new(1.0, 1.0, 1.0, 1.0)),
89+
// This doesn't do anything at this point; can be used to omit fields in declarations.
90+
..Default::default()
91+
});
92+
93+
graphics::draw(
94+
ctx,
95+
&dead,
96+
(ggez::mint::Point2 {
97+
x: (crate::WIDTH / 2.0) - 60.0,
98+
y: 40.0,
99+
},),
100+
)?;
101+
102+
graphics::draw(
103+
ctx,
104+
&unsafe_dead,
105+
(ggez::mint::Point2 {
106+
x: (crate::WIDTH / 2.0) - 200.0,
107+
y: 200.0,
108+
},),
109+
)?;
110+
111+
graphics::draw(
112+
ctx,
113+
&unsafe_dead_block_start,
114+
(ggez::mint::Point2 {
115+
x: (crate::WIDTH / 2.0) - 90.0,
116+
y: 200.0,
117+
},),
118+
)?;
119+
120+
graphics::draw(
121+
ctx,
122+
&unsafe_dead_block_func,
123+
(ggez::mint::Point2 {
124+
x: (crate::WIDTH / 2.0) - 125.0,
125+
y: 260.0,
126+
},),
127+
)?;
128+
129+
graphics::draw(
130+
ctx,
131+
&unsafe_dead_block_end,
132+
(ggez::mint::Point2 {
133+
x: (crate::WIDTH / 2.0) - 200.0,
134+
y: 300.0,
135+
},),
136+
)?;
137+
138+
graphics::draw(
139+
ctx,
140+
&self.ferris_planet,
141+
(ggez::nalgebra::Point2::new(
142+
(crate::WIDTH / 2.0) - 10.0,
143+
240.0,
144+
),),
145+
)
146+
.unwrap();
147+
148+
graphics::present(ctx)
149+
}
128150
}

src/game.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1-
use ggez::{Context, GameResult, graphics};
1+
use std::sync::Mutex;
22

3-
#[allow(dead_code)]
3+
use ggez::{Context, GameResult, audio::Source, event::KeyCode, audio::SoundSource, graphics};
44

5-
const WIDTH: f32 = crate::WIDTH;
6-
#[allow(dead_code)]
7-
8-
const HEIGHT: f32 = crate::HEIGHT;
5+
use crate::WIDTH;
6+
use crate::HEIGHT;
97

108
pub struct Game {
11-
pub ferris_borrow_fail: graphics::Image,
12-
pub pos_y: f32,
9+
pub ferris_ninja: graphics::Image,
10+
pub ferris_death_audio: Source,
11+
pub pos_y: f32,
1312
}
1413

15-
pub fn draw(game_state: &Game, ctx: &mut Context) -> GameResult<()> {
16-
graphics::clear(ctx, graphics::BLACK);
17-
18-
graphics::draw(
19-
ctx,
20-
&game_state.ferris_borrow_fail,
21-
(ggez::nalgebra::Point2::new((WIDTH / 2.0) - 80.0, game_state.pos_y),),
22-
).unwrap();
14+
impl Game {
15+
pub fn create(ctx: &mut Context) -> Mutex<Self> {
16+
Mutex::new(
17+
Self {
18+
ferris_ninja: graphics::Image::new(ctx, "/ferris_ninja.png").unwrap(),
19+
ferris_death_audio: Source::new(ctx, "/dead.mp3").unwrap(),
20+
pos_y: 10.0
21+
}
22+
)
23+
}
24+
25+
pub fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
26+
graphics::clear(ctx, graphics::BLACK);
27+
28+
graphics::draw(
29+
ctx,
30+
&self.ferris_ninja,
31+
(ggez::nalgebra::Point2::new(
32+
(WIDTH / 2.0) - 80.0,
33+
self.pos_y,
34+
),),
35+
)
36+
.unwrap();
37+
38+
graphics::present(ctx)
39+
}
40+
41+
pub fn update(&self, _ctx: &mut Context) -> GameResult<()> {
42+
Ok(())
43+
}
44+
45+
pub fn key_press(&mut self, _keycode: KeyCode) -> Option<crate::Screen> {
46+
self.ferris_death_audio.play().expect("Cannot play the sad violin.");
2347

24-
graphics::present(ctx)
48+
None
49+
}
2550
}

0 commit comments

Comments
 (0)