- File Changes
- .gitignore
- Cargo.toml
- assets/bardo.png
- assets/darkdimension.png
- assets/reaper.png
- src/main.rs
- assets
-
src
- main.rs
- .gitignore
- Cargo.toml
+37
-0
src/main.rs
| 1 | 1 |
|
| 2 | 2 |
|
| 3 | 3 |
|
| 4 | 4 |
|
| 5 | 5 |
|
| 6 | 6 |
|
| 7 | 7 |
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 8 | 12 |
|
| 9 | 13 |
|
| 10 | 14 |
|
| 11 | 15 |
|
| 12 | 16 |
|
| 13 | 17 |
|
| 14 | 18 |
|
| 15 | 19 |
|
| 16 | 20 |
|
| 17 | 21 |
|
| 18 | 22 |
|
| 19 | 23 |
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 20 | 57 |
|
| 21 | 58 |
|
| 22 | 59 |
|
| 23 | 60 |
|
| 24 | 61 |
|
| 25 | 62 |
|
| 26 | 63 |
|
| 27 | 64 |
|
| 28 | 65 |
|
| 29 | 66 |
|
| 30 | 67 |
|
| 31 | 68 |
|
| 32 | 69 |
|
| 33 | 70 |
|
| 34 | 71 |
|
| 35 | 72 |
|
| 36 | 73 |
|
| 37 | 74 |
|
| 38 | 75 |
|
| 39 | 76 |
|
| 40 | 77 |
|
| 41 | 78 |
|
| 42 | 79 |
|
| 43 | 80 |
|
| 44 | 81 |
|
| 45 | 82 |
|
| 46 | 83 |
|
| 47 | 84 |
|
| 48 | 85 |
|
| 49 | 86 |
|
| 50 | 87 |
|
| 51 | 88 |
|
| 52 | 89 |
|
| 53 | 90 |
|
| 54 | 91 |
|
| 55 | 92 |
|
| 56 | 93 |
|
| 57 | 94 |
|
| 58 | 95 |
|
| 59 | 96 |
|
| 60 | 97 |
|
| 61 | 98 |
|
| 62 | 99 |
|
| 63 | 100 |
|
| 64 | 101 |
|
| 65 | 102 |
|
| 66 | 103 |
|
| 67 | 104 |
|
| 68 | 105 |
|
| 69 | 106 |
|
| 70 | 107 |
|
| 71 | 108 |
|
| 72 | 109 |
|
| 73 | 110 |
|
| 74 | 111 |
|
| 75 | 112 |
|
| 76 | 113 |
|
| 77 | 114 |
|
| 78 | 115 |
|
| 79 | 116 |
|
| 80 | 117 |
|
| 81 | 118 |
|
| 82 | 119 |
|
| 83 | 120 |
|
| 84 | 121 |
|
| 85 | 122 |
|
| 86 | 123 |
|
| 87 | 124 |
|
| 88 | 125 |
|
| 89 | 126 |
|
| 90 | 127 |
|
| 91 | 128 |
|
| 92 | 129 |
|
| 93 | 130 |
|
| 94 | 131 |
|
| 95 | 132 |
|
| 96 | 133 |
|
| 97 | 134 |
|
| 98 | 135 |
|
| 99 | 136 |
|
| 100 | 137 |
|
| 101 | 138 |
|
| 102 | 139 |
|
| 103 | 140 |
|
| 104 | 141 |
|
| 105 | 142 |
|
| 106 | 143 |
|
| 107 | 144 |
|
| 108 | 145 |
|
| 109 | 146 |
|
| 110 | 147 |
|
| 111 | 148 |
|
| 112 | 149 |
|
| 113 | 150 |
|
| 114 | 151 |
|
| 115 | 152 |
|
| 116 | 153 |
|
| 117 | 154 |
|
| 118 | 155 |
|
| 119 | 156 |
|
| 120 | 157 |
|
| 121 | 158 |
|
| 122 | 159 |
|
| 123 | 160 |
|
| 124 | 161 |
|
| 125 | 162 |
|
| 126 | 163 |
|
| 127 | 164 |
|
| 128 | 165 |
|
| 129 | 166 |
|
| 130 | 167 |
|
| 131 | 168 |
|
| 132 | 169 |
|
| 133 | 170 |
|
| 134 | 171 |
|
| 135 | 172 |
|
| 136 | 173 |
|
| 137 | 174 |
|
| 138 | 175 |
|
| 139 | 176 |
|
| 140 | 177 |
|
| 141 | 178 |
|
| 142 | 179 |
|
| 143 | 180 |
|
| 144 | 181 |
|
| 145 | 182 |
|
| 146 | 183 |
|
| 147 | 184 |
|
| 148 | 185 |
|
| 149 | 186 |
|
| 150 | 187 |
|
| 151 | 188 |
|
| 152 | 189 |
|
| 153 | 190 |
|
| 154 | 191 |
|
| 155 | 192 |
|
| 156 | 193 |
|
| 157 | 194 |
|
| 158 | 195 |
|
| 159 | 196 |
|
| 160 | 197 |
|
| 161 | 198 |
|
| 162 | 199 |
|
| 163 | 200 |
|
| 164 | 201 |
|
| 165 | 202 |
|
| 166 | 203 |
|
| 167 | 204 |
|
| 168 | 205 |
|
| 169 | 206 |
|
| 170 | 207 |
|
| 171 | 208 |
|
+37
-0
src/main.rs
| 1 |
|
1 |
|
| 2 |
|
2 |
|
| 3 |
|
3 |
|
| 4 |
|
4 |
|
| 5 |
|
5 |
|
| 6 |
|
6 |
|
| 7 |
|
7 |
|
| 8 |
|
||
| 9 |
|
||
| 10 |
|
||
| 11 |
|
||
| 8 |
|
12 |
|
| 9 |
|
13 |
|
| 10 |
|
14 |
|
| 11 |
|
15 |
|
| 12 |
|
16 |
|
| 13 |
|
17 |
|
| 14 |
|
18 |
|
| 15 |
|
19 |
|
| 16 |
|
20 |
|
| 17 |
|
21 |
|
| 18 |
|
22 |
|
| 19 |
|
23 |
|
| 24 |
|
||
| 25 |
|
||
| 26 |
|
||
| 27 |
|
||
| 28 |
|
||
| 29 |
|
||
| 30 |
|
||
| 31 |
|
||
| 32 |
|
||
| 33 |
|
||
| 34 |
|
||
| 35 |
|
||
| 36 |
|
||
| 37 |
|
||
| 38 |
|
||
| 39 |
|
||
| 40 |
|
||
| 41 |
|
||
| 42 |
|
||
| 43 |
|
||
| 44 |
|
||
| 45 |
|
||
| 46 |
|
||
| 47 |
|
||
| 48 |
|
||
| 49 |
|
||
| 50 |
|
||
| 51 |
|
||
| 52 |
|
||
| 53 |
|
||
| 54 |
|
||
| 55 |
|
||
| 56 |
|
||
| 20 |
|
57 |
|
| 21 |
|
58 |
|
| 22 |
|
59 |
|
| 23 |
|
60 |
|
| 24 |
|
61 |
|
| 25 |
|
62 |
|
| 26 |
|
63 |
|
| 27 |
|
64 |
|
| 28 |
|
65 |
|
| 29 |
|
66 |
|
| 30 |
|
67 |
|
| 31 |
|
68 |
|
| 32 |
|
69 |
|
| 33 |
|
70 |
|
| 34 |
|
71 |
|
| 35 |
|
72 |
|
| 36 |
|
73 |
|
| 37 |
|
74 |
|
| 38 |
|
75 |
|
| 39 |
|
76 |
|
| 40 |
|
77 |
|
| 41 |
|
78 |
|
| 42 |
|
79 |
|
| 43 |
|
80 |
|
| 44 |
|
81 |
|
| 45 |
|
82 |
|
| 46 |
|
83 |
|
| 47 |
|
84 |
|
| 48 |
|
85 |
|
| 49 |
|
86 |
|
| 50 |
|
87 |
|
| 51 |
|
88 |
|
| 52 |
|
89 |
|
| 53 |
|
90 |
|
| 54 |
|
91 |
|
| 55 |
|
92 |
|
| 56 |
|
93 |
|
| 57 |
|
94 |
|
| 58 |
|
95 |
|
| 59 |
|
96 |
|
| 60 |
|
97 |
|
| 61 |
|
98 |
|
| 62 |
|
99 |
|
| 63 |
|
100 |
|
| 64 |
|
101 |
|
| 65 |
|
102 |
|
| 66 |
|
103 |
|
| 67 |
|
104 |
|
| 68 |
|
105 |
|
| 69 |
|
106 |
|
| 70 |
|
107 |
|
| 71 |
|
108 |
|
| 72 |
|
109 |
|
| 73 |
|
110 |
|
| 74 |
|
111 |
|
| 75 |
|
112 |
|
| 76 |
|
113 |
|
| 77 |
|
114 |
|
| 78 |
|
115 |
|
| 79 |
|
116 |
|
| 80 |
|
117 |
|
| 81 |
|
118 |
|
| 82 |
|
119 |
|
| 83 |
|
120 |
|
| 84 |
|
121 |
|
| 85 |
|
122 |
|
| 86 |
|
123 |
|
| 87 |
|
124 |
|
| 88 |
|
125 |
|
| 89 |
|
126 |
|
| 90 |
|
127 |
|
| 91 |
|
128 |
|
| 92 |
|
129 |
|
| 93 |
|
130 |
|
| 94 |
|
131 |
|
| 95 |
|
132 |
|
| 96 |
|
133 |
|
| 97 |
|
134 |
|
| 98 |
|
135 |
|
| 99 |
|
136 |
|
| 100 |
|
137 |
|
| 101 |
|
138 |
|
| 102 |
|
139 |
|
| 103 |
|
140 |
|
| 104 |
|
141 |
|
| 105 |
|
142 |
|
| 106 |
|
143 |
|
| 107 |
|
144 |
|
| 108 |
|
145 |
|
| 109 |
|
146 |
|
| 110 |
|
147 |
|
| 111 |
|
148 |
|
| 112 |
|
149 |
|
| 113 |
|
150 |
|
| 114 |
|
151 |
|
| 115 |
|
152 |
|
| 116 |
|
153 |
|
| 117 |
|
154 |
|
| 118 |
|
155 |
|
| 119 |
|
156 |
|
| 120 |
|
157 |
|
| 121 |
|
158 |
|
| 122 |
|
159 |
|
| 123 |
|
160 |
|
| 124 |
|
161 |
|
| 125 |
|
162 |
|
| 126 |
|
163 |
|
| 127 |
|
164 |
|
| 128 |
|
165 |
|
| 129 |
|
166 |
|
| 130 |
|
167 |
|
| 131 |
|
168 |
|
| 132 |
|
169 |
|
| 133 |
|
170 |
|
| 134 |
|
171 |
|
| 135 |
|
172 |
|
| 136 |
|
173 |
|
| 137 |
|
174 |
|
| 138 |
|
175 |
|
| 139 |
|
176 |
|
| 140 |
|
177 |
|
| 141 |
|
178 |
|
| 142 |
|
179 |
|
| 143 |
|
180 |
|
| 144 |
|
181 |
|
| 145 |
|
182 |
|
| 146 |
|
183 |
|
| 147 |
|
184 |
|
| 148 |
|
185 |
|
| 149 |
|
186 |
|
| 150 |
|
187 |
|
| 151 |
|
188 |
|
| 152 |
|
189 |
|
| 153 |
|
190 |
|
| 154 |
|
191 |
|
| 155 |
|
192 |
|
| 156 |
|
193 |
|
| 157 |
|
194 |
|
| 158 |
|
195 |
|
| 159 |
|
196 |
|
| 160 |
|
197 |
|
| 161 |
|
198 |
|
| 162 |
|
199 |
|
| 163 |
|
200 |
|
| 164 |
|
201 |
|
| 165 |
|
202 |
|
| 166 |
|
203 |
|
| 167 |
|
204 |
|
| 168 |
|
205 |
|
| 169 |
|
206 |
|
| 170 |
|
207 |
|
| 171 |
|
208 |
|
/target**/*.rs.bk
[package]name = "game-tutorial"version = "0.1.0"authors = ["Sunjay Varma <[email protected]>"]edition = "2018"[dependencies]specs = "0.14"specs-derive = "0.4"[dependencies.sdl2]version = "0.32.1"default-features = falsefeatures = ["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 listeduse sdl2::image::{self, LoadTexture, InitFlag};use specs::prelude::*;use specs_derive::Component;use std::time::Duration;const PLAYER_MOVEMENT_SPEED: i32 = 20;#[derive(Debug, Clone, Copy, PartialEq, Eq)]enum Direction {Up,Down,Left,Right,}/// The current position of a given entity#[derive(Component, Debug)]#[storage(VecStorage)]struct Position(Point);/// The current speed and direction of a given entity#[derive(Component, Debug)]#[storage(VecStorage)]struct Velocity {speed: i32,direction: Direction,}#[derive(Component, Debug)]#[storage(VecStorage)]struct Sprite {/// The specific spritesheet to render fromspritesheet: usize,/// The current region of the spritesheet to be renderedregion: Rect,}#[derive(Component, Debug)]#[storage(VecStorage)]struct MovementAnimation {// The current frame in the animation of the direction this entity is moving incurrent_frame: usize,up_frames: Vec<Sprite>,down_frames: Vec<Sprite>,left_frames: Vec<Sprite>,right_frames: Vec<Sprite>,}#[derive(Debug)]struct Player {position: Point,sprite: Rect,speed: i32,direction: Direction,current_frame: i32,}/// Returns the row of the spritesheet corresponding to the given directionfn direction_spritesheet_row(direction: Direction) -> i32 {use self::Direction::*;match direction {Up => 3,Down => 0,Left => 1,Right => 2,}}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()?;let (frame_width, frame_height) = player.sprite.size();let current_frame = Rect::new(player.sprite.x() + frame_width as i32 * player.current_frame,player.sprite.y() + frame_height as i32 * direction_spritesheet_row(player.direction),frame_width,frame_height,);// Treat the center of the screen as the (0, 0) coordinatelet screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);let screen_rect = Rect::from_center(screen_position, frame_width, frame_height);canvas.copy(texture, current_frame, 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);},}// Only continue to animate if the player is movingif player.speed != 0 {// Cheat: using the fact that all animations are 3 frames (NOT extensible)player.current_frame = (player.current_frame + 1) % 3;}}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,current_frame: 0,};let mut event_pump = sdl_context.event_pump()?;let mut i = 0;'running: loop {// Handle eventsfor 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;},_ => {}}}// Updatei = (i + 1) % 255;update_player(&mut player);// Renderrender(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;// Time management!::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 20));}Ok(())}