Compare commits
5 commits
e39d35d72f
...
01f51faf10
Author | SHA1 | Date | |
---|---|---|---|
Yash Karandikar | 01f51faf10 | ||
Yash Karandikar | a18db70127 | ||
Yash Karandikar | 47de327e05 | ||
Yash Karandikar | 487f2e40fe | ||
Yash Karandikar | 1495e9fe51 |
BIN
ibm_bios-2y.ttf
Normal file
BIN
ibm_bios-2y.ttf
Normal file
Binary file not shown.
92
src/main.rs
92
src/main.rs
|
@ -1,10 +1,10 @@
|
||||||
use cat_box::{get_keyboard_state, Game, Sprite, SpriteCollection};
|
use cat_box::{draw_text, get_keyboard_state, Game, Sprite, SpriteCollection};
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use sdl2::keyboard::Scancode;
|
use sdl2::keyboard::Scancode;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||||
enum Direction {
|
enum Direction {
|
||||||
Up,
|
Up,
|
||||||
Down,
|
Down,
|
||||||
|
@ -12,6 +12,14 @@ enum Direction {
|
||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! set_if_not_opp {
|
||||||
|
($i:ident, $e:expr, $opp:expr) => {
|
||||||
|
if $i != $opp {
|
||||||
|
$i = $e
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let game = Game::new("Snake", 1000, 1000);
|
let game = Game::new("Snake", 1000, 1000);
|
||||||
let snake_boxes: Vec<(i32, i32)> = vec![(13, 13), (14, 13)];
|
let snake_boxes: Vec<(i32, i32)> = vec![(13, 13), (14, 13)];
|
||||||
|
@ -31,16 +39,31 @@ fn main() {
|
||||||
|
|
||||||
let mut dir = Direction::Left;
|
let mut dir = Direction::Left;
|
||||||
|
|
||||||
|
let mut score = 0u64;
|
||||||
|
|
||||||
game.run(|ctx| {
|
game.run(|ctx| {
|
||||||
|
draw_text(
|
||||||
|
ctx,
|
||||||
|
format!("Score: {}", score),
|
||||||
|
"ibm_bios-2y.ttf",
|
||||||
|
36,
|
||||||
|
(100, 100),
|
||||||
|
cat_box::TextMode::Transparent {
|
||||||
|
colour: (255, 255, 255),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let keys = get_keyboard_state(ctx).keys;
|
let keys = get_keyboard_state(ctx).keys;
|
||||||
|
|
||||||
for key in keys {
|
for key in keys {
|
||||||
|
use Direction::*;
|
||||||
match key {
|
match key {
|
||||||
Scancode::Q => game.terminate(),
|
Scancode::Q => game.terminate(),
|
||||||
Scancode::W | Scancode::Up => dir = Direction::Up,
|
Scancode::W | Scancode::Up => set_if_not_opp!(dir, Up, Down),
|
||||||
Scancode::A | Scancode::Left => dir = Direction::Left,
|
Scancode::A | Scancode::Left => set_if_not_opp!(dir, Left, Right),
|
||||||
Scancode::S | Scancode::Down => dir = Direction::Down,
|
Scancode::S | Scancode::Down => set_if_not_opp!(dir, Down, Up),
|
||||||
Scancode::D | Scancode::Right => dir = Direction::Right,
|
Scancode::D | Scancode::Right => set_if_not_opp!(dir, Right, Left),
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -52,8 +75,8 @@ fn main() {
|
||||||
let (lastx, lasty) = last_part;
|
let (lastx, lasty) = last_part;
|
||||||
let (x, y) = s.position();
|
let (x, y) = s.position();
|
||||||
let (xdiff, ydiff) = (lastx - x, y - lasty);
|
let (xdiff, ydiff) = (lastx - x, y - lasty);
|
||||||
s.translate((xdiff, ydiff));
|
|
||||||
last_part = s.position();
|
last_part = s.position();
|
||||||
|
s.translate((xdiff, ydiff));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,13 +96,51 @@ fn main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if cat_box::physics::check_for_collision(&snake[0], &apple) {
|
if !cat_box::physics::check_for_collision_with_collection(&apple, &snake).is_empty() {
|
||||||
let x = thread_rng().gen_range(0..=27) * 37;
|
let x = thread_rng().gen_range(0..=27) * 37;
|
||||||
let y = thread_rng().gen_range(0..=27) * 37;
|
let y = thread_rng().gen_range(0..=27) * 37;
|
||||||
|
|
||||||
let (currx, curry) = apple.position();
|
let (currx, curry) = apple.position();
|
||||||
let (xdiff, ydiff) = (x - currx, curry - y);
|
let (xdiff, ydiff) = (x - currx, curry - y);
|
||||||
apple.translate((xdiff, ydiff));
|
apple.translate((xdiff, ydiff));
|
||||||
|
let second_to_last = snake[snake.len() - 2].position();
|
||||||
|
let last = snake[snake.len() - 1].position();
|
||||||
|
|
||||||
|
let direc = check_direction(last, second_to_last);
|
||||||
|
|
||||||
|
let (newx, newy) = match direc {
|
||||||
|
Direction::Left => (last.0 - 37, last.1),
|
||||||
|
Direction::Right => (last.0 + 37, last.1),
|
||||||
|
Direction::Up => (last.0, last.1 - 37),
|
||||||
|
Direction::Down => (last.0, last.1 + 37),
|
||||||
|
};
|
||||||
|
|
||||||
|
let s = Sprite::new("snakecell.png", newx, newy).unwrap();
|
||||||
|
snake.push(s);
|
||||||
|
|
||||||
|
score += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let (mut x, mut y) = snake[0].position();
|
||||||
|
x /= 37;
|
||||||
|
y /= 37;
|
||||||
|
|
||||||
|
if dir == Direction::Left || dir == Direction::Right {
|
||||||
|
if x == 0 {
|
||||||
|
snake[0].translate((27 * 37, 0));
|
||||||
|
} else if x == 27 {
|
||||||
|
snake[0].translate((-27 * 37, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if dir == Direction::Up || dir == Direction::Down {
|
||||||
|
if y == 0 {
|
||||||
|
snake[0].translate((0, -27 * 37));
|
||||||
|
} else if y == 27 {
|
||||||
|
snake[0].translate((0, 27 * 37));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// So that the snake doesn't move at super speed
|
// So that the snake doesn't move at super speed
|
||||||
|
@ -89,3 +150,18 @@ fn main() {
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_direction(square1: (i32, i32), square2: (i32, i32)) -> Direction {
|
||||||
|
if square1.0 < square2.0 {
|
||||||
|
return Direction::Left;
|
||||||
|
} else if square1.0 > square2.0 {
|
||||||
|
return Direction::Right;
|
||||||
|
};
|
||||||
|
if square1.1 > square2.1 {
|
||||||
|
return Direction::Up;
|
||||||
|
} else if square1.1 < square2.1 {
|
||||||
|
return Direction::Down;
|
||||||
|
};
|
||||||
|
|
||||||
|
Direction::Up
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue