Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ed91c40c8f | |||
| c8e9be72a8 | |||
| 62b8e94969 | |||
| 94ec661a22 | |||
| 3ddcb84265 | |||
| 2a4c4889af |
Generated
+91
-29
@@ -1,5 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.6"
|
||||
@@ -12,12 +14,6 @@ version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.0"
|
||||
@@ -35,6 +31,12 @@ dependencies = [
|
||||
"constant_time_eq",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "case"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e88b166b48e29667f5443df64df3c61dc07dc2b1a0b0d231800e07f09a33ecc1"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.66"
|
||||
@@ -55,13 +57,22 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.1"
|
||||
version = "0.8.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d"
|
||||
checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive-error"
|
||||
version = "0.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb6238eafbc141db51c6d0d599dde74224b28437c7cf7c3a1755e3e3e5e00806"
|
||||
dependencies = [
|
||||
"case",
|
||||
"quote 0.3.15",
|
||||
"syn 0.11.11",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -84,6 +95,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dtoa"
|
||||
version = "0.4.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88d7ed2934d741c6b37e33e3832298e8850b53fd2d2bea03873375596c7cea4e"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.16"
|
||||
@@ -106,18 +123,18 @@ dependencies = [
|
||||
"wasi 0.10.1+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929"
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "ncurses"
|
||||
version = "5.100.0"
|
||||
@@ -147,9 +164,15 @@ version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
"unicode-xid 0.2.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "0.3.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.8"
|
||||
@@ -183,9 +206,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.1"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom 0.2.2",
|
||||
]
|
||||
@@ -244,19 +267,43 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3fcab8778dc651bc65cfab2e4eb64996f3c912b74002fb379c94517e1f27c46"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"quote 1.0.8",
|
||||
"syn 1.0.60",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.8.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23"
|
||||
dependencies = [
|
||||
"dtoa",
|
||||
"linked-hash-map",
|
||||
"serde",
|
||||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "snake"
|
||||
version = "0.2.1"
|
||||
version = "0.2.3"
|
||||
dependencies = [
|
||||
"derive-error",
|
||||
"dirs",
|
||||
"ncurses",
|
||||
"rand",
|
||||
"serde",
|
||||
"toml",
|
||||
"serde_yaml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "0.11.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
|
||||
dependencies = [
|
||||
"quote 0.3.15",
|
||||
"synom",
|
||||
"unicode-xid 0.0.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -266,19 +313,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
"quote 1.0.8",
|
||||
"unicode-xid 0.2.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.8"
|
||||
name = "synom"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
|
||||
checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"unicode-xid 0.0.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
@@ -318,3 +371,12 @@ name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "yaml-rust"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
|
||||
dependencies = [
|
||||
"linked-hash-map",
|
||||
]
|
||||
|
||||
+3
-4
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "snake"
|
||||
version = "0.2.1"
|
||||
version = "0.2.3"
|
||||
authors = ["Uttarayan Mondal <uttarayan21@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
@@ -9,8 +9,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
ncurses = "5.100.0"
|
||||
serde = { version = "1.0.121", features = ["derive"] }
|
||||
# serde = "1.0.121"
|
||||
# serde_derive = "1.0.121"
|
||||
toml = "0.5.8"
|
||||
serde_yaml = "0.8.17"
|
||||
rand = "0.8.0"
|
||||
dirs = "3.0.1"
|
||||
derive-error = "0.0.5"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
Copyright 2021 Uttarayan Mondal
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
+9
-9
@@ -8,10 +8,9 @@ use std::ops::Sub;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Copy)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
|
||||
pub enum Difficulty {
|
||||
Linear(f32),
|
||||
Linear(u8),
|
||||
Flat,
|
||||
}
|
||||
|
||||
@@ -162,9 +161,10 @@ impl Board {
|
||||
}
|
||||
pub fn check_collision(&mut self, snake: &Snake) -> bool {
|
||||
let (snake_line, snake_col): (u32, u32) = snake.posyx();
|
||||
if (snake_line >= self.maxlines - 1) || (snake_col >= self.maxcols - 1)
|
||||
// || (snake_line <= 0)
|
||||
// || (snake_col <= 0)
|
||||
if (snake_line >= self.maxlines - 1)
|
||||
|| (snake_col >= self.maxcols - 1)
|
||||
|| (snake_line == 0)
|
||||
|| (snake_col == 0)
|
||||
{
|
||||
self.gamestate = GameState::Failed(FailState::Wall);
|
||||
return true;
|
||||
@@ -222,7 +222,7 @@ pub struct Snake {
|
||||
direction: Direction,
|
||||
difficulty: Difficulty,
|
||||
grow: bool,
|
||||
speed: u32,
|
||||
speed: f32,
|
||||
last_tail: Option<Cell>,
|
||||
}
|
||||
impl Snake {
|
||||
@@ -275,13 +275,13 @@ impl Snake {
|
||||
// let time: std::time::Duration =
|
||||
// std::time::Duration::from_millis((1000 / self.speed) as u64);
|
||||
// sleep(time);
|
||||
sleep(Duration::from_millis((1000 / self.speed) as u64));
|
||||
sleep(Duration::from_millis(1000 / self.speed as u64));
|
||||
self.smove(self.direction);
|
||||
}
|
||||
pub fn scale_difficulty(&mut self) {
|
||||
match self.difficulty {
|
||||
Difficulty::Flat => (),
|
||||
Difficulty::Linear(scale) => self.speed = (self.speed as f32 * scale) as u32,
|
||||
Difficulty::Linear(scale) => self.speed *= scale as f32 / 256_f32 + 1_f32,
|
||||
}
|
||||
}
|
||||
pub fn grow(&mut self) {
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ pub fn start(config: &Config) {
|
||||
loop {
|
||||
frontend::draw_snake(&snake, game_win); // always draw snake before board because the snake will clear the game win
|
||||
frontend::draw_board(&board, game_win);
|
||||
frontend::_log(&snake, &board);
|
||||
// frontend::_log(&snake, &board);
|
||||
if board.check_collision(&snake) {
|
||||
// Add stuff here to show the score and
|
||||
// how You lose screen
|
||||
|
||||
+2
-2
@@ -1,10 +1,11 @@
|
||||
extern crate ncurses;
|
||||
#[macro_use]
|
||||
extern crate derive_error;
|
||||
mod game;
|
||||
mod highscore;
|
||||
mod menu;
|
||||
mod settings;
|
||||
// use game::{Cell, Snake};
|
||||
// use ncurses::*;
|
||||
use ncurses::{
|
||||
curs_set, endwin, getmaxyx, initscr, keypad, noecho, raw, refresh, setlocale, stdscr,
|
||||
LcCategory, CURSOR_VISIBILITY,
|
||||
@@ -36,5 +37,4 @@ fn main() {
|
||||
}
|
||||
refresh();
|
||||
endwin();
|
||||
config.write();
|
||||
}
|
||||
|
||||
+16
-13
@@ -1,7 +1,5 @@
|
||||
extern crate dirs;
|
||||
extern crate serde;
|
||||
// extern crate serde_derive;
|
||||
extern crate toml;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use serde_derive::{Deserialize, Serialize};
|
||||
use crate::game::Difficulty;
|
||||
@@ -10,21 +8,26 @@ use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::thread::sleep;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub difficulty: Difficulty,
|
||||
pub speed: u32,
|
||||
pub speed: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
FileSystemError(std::io::Error),
|
||||
ParsingError(serde_yaml::Error),
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Result<Self, std::io::Error> {
|
||||
pub fn new() -> Result<Self, ConfigError> {
|
||||
let config_path = Config::config_path().unwrap();
|
||||
if config_path.exists() {
|
||||
let mut config_file = File::open(config_path).unwrap();
|
||||
let mut config_file_str: String = String::new();
|
||||
config_file.read_to_string(&mut config_file_str).unwrap();
|
||||
// let config: Config = toml::from_str(&config_file_str).expect("Error in config file");
|
||||
let config: Config = match toml::from_str(&config_file_str) {
|
||||
let config: Config = match serde_yaml::from_str(&config_file_str) {
|
||||
Ok(config) => config,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
@@ -33,7 +36,7 @@ impl Config {
|
||||
e,
|
||||
);
|
||||
eprintln!("Falling back to using the default values");
|
||||
sleep(std::time::Duration::from_millis(3000));
|
||||
sleep(std::time::Duration::from_millis(1000));
|
||||
Config::default()
|
||||
}
|
||||
};
|
||||
@@ -46,11 +49,11 @@ impl Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
difficulty: Difficulty::Flat, //Default set to ten blocks per second
|
||||
speed: 10,
|
||||
speed: 10_f32,
|
||||
}
|
||||
}
|
||||
pub fn write(&self) {
|
||||
let config_file_str: String = toml::to_string(self).unwrap();
|
||||
pub fn _write(&self) {
|
||||
let config_file_str: String = serde_yaml::to_string(self).unwrap();
|
||||
let config_path = Config::config_path().unwrap();
|
||||
let mut config_file: File =
|
||||
File::create(config_path).expect("Couldn't open config file to write");
|
||||
@@ -62,13 +65,13 @@ impl Config {
|
||||
let config_path: PathBuf = match dirs::config_dir() {
|
||||
Some(mut path) => {
|
||||
path.push("snake");
|
||||
path.push("snake.toml");
|
||||
path.push("snake.yaml");
|
||||
path
|
||||
}
|
||||
None => {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"snake.toml not found",
|
||||
"snake.yaml not found",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user