- File Changes
- .gitignore
- Cargo.toml
- assets/bardo.png
- assets/darkdimension.png
- assets/reaper.png
- src/main.rs
- assets
-
src
- main.rs
- .gitignore
- Cargo.toml
+54
-10
src/main.rs
1 | 1 |
|
2 | 2 |
|
3 | 3 |
|
4 | 4 |
|
5 | 5 |
|
6 | 6 |
|
7 | 7 |
|
8 | 8 |
|
9 | 9 |
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
10 | 20 |
|
11 | 21 |
|
12 | 22 |
|
13 | 23 |
|
14 | 24 |
|
25 |
|
|
15 | 26 |
|
16 | 27 |
|
17 | 28 |
|
18 | 29 |
|
19 | 30 |
|
20 | 31 |
|
21 | 32 |
|
22 | 33 |
|
23 | 34 |
|
24 | 35 |
|
25 | 36 |
|
26 | 37 |
|
27 | 38 |
|
28 | 39 |
|
29 | 40 |
|
30 | 41 |
|
31 | 42 |
|
32 | 43 |
|
33 | 44 |
|
34 | 45 |
|
35 | 46 |
|
36 | 47 |
|
37 | 48 |
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
38 | 70 |
|
39 | 71 |
|
40 | 72 |
|
41 | 73 |
|
42 | 74 |
|
43 | 75 |
|
44 | 76 |
|
45 | 77 |
|
46 | 78 |
|
47 | 79 |
|
48 | 80 |
|
49 | 81 |
|
50 | 82 |
|
51 | 83 |
|
52 | 84 |
|
53 | 85 |
|
54 | 86 |
|
55 | 87 |
|
56 | 88 |
|
57 | 89 |
|
58 | 90 |
|
59 | 91 |
|
60 |
|
|
92 |
|
|
93 |
|
|
61 | 94 |
|
62 | 95 |
|
63 | 96 |
|
64 | 97 |
|
65 | 98 |
|
66 | 99 |
|
67 | 100 |
|
68 | 101 |
|
69 | 102 |
|
70 | 103 |
|
71 | 104 |
|
72 | 105 |
|
73 |
|
|
74 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
75 | 109 |
|
76 |
|
|
77 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
78 | 113 |
|
79 |
|
|
80 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
81 | 117 |
|
82 |
|
|
83 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
84 | 127 |
|
85 | 128 |
|
86 | 129 |
|
87 | 130 |
|
88 | 131 |
|
89 | 132 |
|
90 | 133 |
|
134 |
|
|
91 | 135 |
|
92 | 136 |
|
93 | 137 |
|
94 | 138 |
|
95 | 139 |
|
96 |
|
|
140 |
|
|
97 | 141 |
|
98 | 142 |
|
99 | 143 |
|
100 | 144 |
|
+54
-10
src/main.rs
1 |
|
1 |
|
2 |
|
2 |
|
3 |
|
3 |
|
4 |
|
4 |
|
5 |
|
5 |
|
6 |
|
6 |
|
7 |
|
7 |
|
8 |
|
8 |
|
9 |
|
9 |
|
10 |
|
||
11 |
|
||
12 |
|
||
13 |
|
||
14 |
|
||
15 |
|
||
16 |
|
||
17 |
|
||
18 |
|
||
19 |
|
||
10 |
|
20 |
|
11 |
|
21 |
|
12 |
|
22 |
|
13 |
|
23 |
|
14 |
|
24 |
|
25 |
|
||
15 |
|
26 |
|
16 |
|
27 |
|
17 |
|
28 |
|
18 |
|
29 |
|
19 |
|
30 |
|
20 |
|
31 |
|
21 |
|
32 |
|
22 |
|
33 |
|
23 |
|
34 |
|
24 |
|
35 |
|
25 |
|
36 |
|
26 |
|
37 |
|
27 |
|
38 |
|
28 |
|
39 |
|
29 |
|
40 |
|
30 |
|
41 |
|
31 |
|
42 |
|
32 |
|
43 |
|
33 |
|
44 |
|
34 |
|
45 |
|
35 |
|
46 |
|
36 |
|
47 |
|
37 |
|
48 |
|
49 |
|
||
50 |
|
||
51 |
|
||
52 |
|
||
53 |
|
||
54 |
|
||
55 |
|
||
56 |
|
||
57 |
|
||
58 |
|
||
59 |
|
||
60 |
|
||
61 |
|
||
62 |
|
||
63 |
|
||
64 |
|
||
65 |
|
||
66 |
|
||
67 |
|
||
68 |
|
||
69 |
|
||
38 |
|
70 |
|
39 |
|
71 |
|
40 |
|
72 |
|
41 |
|
73 |
|
42 |
|
74 |
|
43 |
|
75 |
|
44 |
|
76 |
|
45 |
|
77 |
|
46 |
|
78 |
|
47 |
|
79 |
|
48 |
|
80 |
|
49 |
|
81 |
|
50 |
|
82 |
|
51 |
|
83 |
|
52 |
|
84 |
|
53 |
|
85 |
|
54 |
|
86 |
|
55 |
|
87 |
|
56 |
|
88 |
|
57 |
|
89 |
|
58 |
|
90 |
|
59 |
|
91 |
|
60 |
|
92 |
|
93 |
|
||
61 |
|
94 |
|
62 |
|
95 |
|
63 |
|
96 |
|
64 |
|
97 |
|
65 |
|
98 |
|
66 |
|
99 |
|
67 |
|
100 |
|
68 |
|
101 |
|
69 |
|
102 |
|
70 |
|
103 |
|
71 |
|
104 |
|
72 |
|
105 |
|
73 |
|
106 |
|
74 |
|
107 |
|
108 |
|
||
75 |
|
109 |
|
76 |
|
110 |
|
77 |
|
111 |
|
112 |
|
||
78 |
|
113 |
|
79 |
|
114 |
|
80 |
|
115 |
|
116 |
|
||
81 |
|
117 |
|
82 |
|
118 |
|
83 |
|
119 |
|
120 |
|
||
121 |
|
||
122 |
|
||
123 |
|
||
124 |
|
||
125 |
|
||
126 |
|
||
84 |
|
127 |
|
85 |
|
128 |
|
86 |
|
129 |
|
87 |
|
130 |
|
88 |
|
131 |
|
89 |
|
132 |
|
90 |
|
133 |
|
134 |
|
||
91 |
|
135 |
|
92 |
|
136 |
|
93 |
|
137 |
|
94 |
|
138 |
|
95 |
|
139 |
|
96 |
|
140 |
|
97 |
|
141 |
|
98 |
|
142 |
|
99 |
|
143 |
|
100 |
|
144 |
|
/target
**/*.rs.bk
[package]
name = "game-tutorial"
version = "0.1.0"
authors = ["Sunjay Varma <[email protected]>"]
edition = "2018"
[dependencies]
[dependencies.sdl2]
version = "0.32.1"
default-features = false
features = ["image"]
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::rect::{Point, Rect};
// "self" imports the "image" module itself as well as everything else we listed
use sdl2::image::{self, LoadTexture, InitFlag};
use std::time::Duration;
const PLAYER_MOVEMENT_SPEED: i32 = 20;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
Up,
Down,
Left,
Right,
}
#[derive(Debug)]
struct Player {
position: Point,
sprite: Rect,
speed: i32,
direction: Direction,
}
fn render(
canvas: &mut WindowCanvas,
color: Color,
texture: &Texture,
player: &Player,
) -> Result<(), String> {
canvas.set_draw_color(color);
canvas.clear();
let (width, height) = canvas.output_size()?;
// Treat the center of the screen as the (0, 0) coordinate
let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);
let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height());
canvas.copy(texture, player.sprite, screen_rect)?;
canvas.present();
Ok(())
}
// Update player a fixed amount based on their speed.
// WARNING: Calling this function too often or at a variable speed will cause the player's speed
// to be unpredictable!
fn update_player(player: &mut Player) {
use self::Direction::*;
match player.direction {
Left => {
player.position = player.position.offset(-player.speed, 0);
},
Right => {
player.position = player.position.offset(player.speed, 0);
},
Up => {
player.position = player.position.offset(0, -player.speed);
},
Down => {
player.position = player.position.offset(0, player.speed);
},
}
}
fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
// Leading "_" tells Rust that this is an unused variable that we don't care about. It has to
// stay unused because if we don't have any variable at all then Rust will treat it as a
// temporary value and drop it right away!
let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
let window = video_subsystem.window("game tutorial", 800, 600)
.position_centered()
.build()
.expect("could not initialize video subsystem");
let mut canvas = window.into_canvas().build()
.expect("could not make a canvas");
let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?;
let mut player = Player {
position: Point::new(0, 0),
sprite: Rect::new(0, 0, 26, 36),
speed: 0,
direction: Direction::Right,
};
let mut event_pump = sdl_context.event_pump()?;
let mut i = 0;
'running: loop {
// Handle events
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running;
},
Event::KeyDown { keycode: Some(Keycode::Left), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Left;
},
Event::KeyDown { keycode: Some(Keycode::Right), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Right;
},
Event::KeyDown { keycode: Some(Keycode::Up), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Up;
},
Event::KeyDown { keycode: Some(Keycode::Down), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Down;
},
Event::KeyUp { keycode: Some(Keycode::Left), repeat: false, .. } |
Event::KeyUp { keycode: Some(Keycode::Right), repeat: false, .. } |
Event::KeyUp { keycode: Some(Keycode::Up), repeat: false, .. } |
Event::KeyUp { keycode: Some(Keycode::Down), repeat: false, .. } => {
player.speed = 0;
},
_ => {}
}
}
// Update
i = (i + 1) % 255;
update_player(&mut player);
// Render
render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
// Time management!
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 20));
}
Ok(())
}