Guest User

Untitled

a guest
May 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. //extern crate termion;
  2.  
  3. //use termion::event::Key;
  4. //use termion::input::TermRead;
  5. //use termion::raw::IntoRawMode;
  6. use std::collections::LinkedList;
  7. use std::io::{stdin, stdout, Write};
  8.  
  9. #[derive(Debug, Copy, Clone)]
  10. pub enum Direction {
  11. Up,
  12. Down,
  13. Left,
  14. Right,
  15. }
  16.  
  17. #[derive(Clone, Debug)]
  18. pub struct Location {
  19. x: i16,
  20. y: i16,
  21. }
  22.  
  23. #[derive(Clone, Debug)]
  24. pub struct Block {
  25. loc: Location,
  26. color: String,
  27. }
  28.  
  29. #[derive(Debug)]
  30. pub struct Snake {
  31. dir: Direction,
  32. head: Block,
  33. body: LinkedList<Block>,
  34. last: Option<Block>,
  35. alive: bool,
  36. moving: bool,
  37. }
  38.  
  39. impl Location {
  40. pub fn new(x: i16, y: i16) -> Location {
  41. Location { x: x, y: y }
  42. }
  43. }
  44.  
  45. impl Direction {
  46. pub fn opposite(dir: Direction) -> Direction {
  47. match dir {
  48. Direction::Up => Direction::Down,
  49. Direction::Down => Direction::Up,
  50. Direction::Left => Direction::Right,
  51. Direction::Right => Direction::Left,
  52. }
  53. }
  54. pub fn delta(dir: Direction) -> (i16, i16) {
  55. match dir {
  56. Direction::Up => (0, 1),
  57. Direction::Down => (0, -1),
  58. Direction::Left => (-1, 0),
  59. Direction::Right => (1, 0),
  60. }
  61. }
  62. }
  63.  
  64. impl Block {
  65. pub fn new(x: i16, y: i16, color: String) -> Block {
  66. Block {
  67. loc: Location { x, y },
  68. color: color,
  69. }
  70. }
  71. }
  72.  
  73. impl Snake {
  74. pub fn new(x: i16, y: i16, len: i16, dir: Direction) -> Snake {
  75. let mut body: LinkedList<Block> = LinkedList::new();
  76. let delta = Direction::delta(dir);
  77. for s in 1..len + 1 {
  78. let bx: i16 = x + (delta.0 * s);
  79. let by: i16 = y + (delta.1 * s);
  80. body.push_back(Block::new(bx, by, "0".to_string()));
  81. }
  82. Snake {
  83. dir: dir,
  84. head: Block::new(x, y, "0".to_string()),
  85. body: body,
  86. last: None,
  87. alive: true,
  88. moving: false,
  89. }
  90. }
  91.  
  92. pub fn update(&mut self, dir: Direction) {
  93. self.dir = dir;
  94. let mut b: Block = self.head.clone();
  95. let delta = Direction::delta(dir);
  96. self.head.loc.x += delta.0;
  97. self.head.loc.y += delta.1;
  98. }
  99.  
  100. fn moveblock(target_block: &mut Block, tool_block: Block) {
  101. target_block.loc = tool_block.loc;
  102. }
  103. }
  104.  
  105. fn main() {
  106. let stdin = stdin();
  107. //let mut stdout = stdout().into_raw_mode().unwrap();
  108.  
  109. let _width = 30;
  110. let _height = 30;
  111.  
  112. // Clear Terminal, Hide cursor
  113. /*
  114. write!(
  115. stdout,
  116. "{}{}{}",
  117. termion::clear::All,
  118. termion::cursor::Goto(1, 1),
  119. termion::cursor::Hide
  120. ).unwrap();
  121. stdout.flush().unwrap();
  122. */
  123.  
  124. let mut _s = Snake::new(5, 5, 3, Direction::Up);
  125.  
  126. for b in _s.body {
  127. //write!(
  128. //stdout,
  129. //"{}#",
  130. //termion::cursor::Goto(b.loc.x as u16, b.loc.y as u16)
  131. //);
  132. }
  133.  
  134. // Handle Keys
  135. //for c in stdin.keys() {
  136. //write!(
  137. //stdout,
  138. //"{}{}",
  139. //termion::cursor::Goto(1, 1),
  140. //termion::clear::CurrentLine
  141. //).unwrap();
  142. //match c.unwrap() {
  143. //Key::Char('q') => break,
  144. //Key::Left => println!("<"),
  145. //Key::Right => println!(">"),
  146. //Key::Up => println!("^"),
  147. //Key::Down => println!("v"),
  148. //_ => {}
  149. //}
  150. //stdout.flush().unwrap();
  151. //}
  152. //write!(stdout, "{}", termion::cursor::Show).unwrap();
  153. }
Add Comment
Please, Sign In to add comment