Closed ChMeghana closed 5 years ago
There will be number of apples, which are going to hit the blocks moving to and fro in the base level. As the apples are operated to hit the blocks, when it happened score will be increased. You should hit the blocks with given number of apples or the game will be ended.
extern crate piston_window; extern crate find_folder; extern crate rand;
use piston_window::*; use rand::Rng;
struct Folk { pub x: f64, pub y: f64, pub w: f64, pub h: f64, pub active: bool, ltr: bool, speed: f64, blue: [f32; 4], color: [f32; 4] // red }
impl Folk { pub fn new(param_ltr:bool, param_speed:f64) -> Folk { Folk { x: Folk::decide_x(param_ltr), y: 630.0, w: 40.0, h: 40.0, active: true, ltr: param_ltr, speed: param_speed, blue: [0.0, 0.0, 1.0, 1.0], color: [1.0, 0.0, 0.0, 1.0] } } fn decide_x(ltr:bool) -> f64 { if ltr { 0.0 } else { 500.0 } } pub fn update(&mut self) { if self.active { if self.ltr { self.x += 1.0 self.speed; if self.x > 1000.0 { self.active = false; } } else { self.x -= 1.0 self.speed; if self.x < 0.0 - self.w { self.active = false; } } } } pub fn deactivate(&mut self) { self.active = false; self.color = self.blue; } }
struct Apple { pub active: bool, pub x: f64, pub y: f64, pub w: f64, pub h: f64 }
impl Apple { pub fn new(param_x: f64, param_y: f64) -> Apple { Apple { active: true, x: param_x, y: param_y, w: 40.0, h: 40.0 } } pub fn update(&mut self) { if self.active { self.y += 1.0;
}
struct Shots { pub total: i32, pub gone: i32, pub left: i32 }
impl Shots { pub fn new() -> Shots { Shots { total: 10, gone: 0, left: 10 } } pub fn fire(&mut self) { if self.left > 0 { self.gone += 1; self.left -= 1; } } }
struct Player { pub rows: i32, pub columns: i32, pub x: i32, pub y: i32, factor: f64, pub shots: Shots, pub apples: Vec
}
impl Player { pub fn new() -> Player { Player { rows: 4, columns: 3, x: 0, y: 0, factor: 110.0, shots: Shots::new(), apples: vec![] } } fn calc_coord(&mut self, pos: f64) -> f64 { self.factor + (pos * self.factor) } pub fn throw(&mut self) { let x = self.x as f64; let y = self.y as f64; let x = self.calc_coord(x); let y = self.calc_coord(y); println!("{}", self.shots.left); if self.shots.left > 0 { self.shots.fire(); self.apples.push(Apple::new(x, y)); } } pub fn update(&mut self) { for a in self.apples.iter_mut() { a.update(); } } pub fn moving(&mut self, x: i32, y: i32) { self.x += x; if self.x > self.columns -1 { self.x = 0; } else if self.x < 0 { self.x = self.columns -1; } self.y += y; if self.y > self.rows -1 { self.y = 0; } else if self.y < 0 { self.y = self.rows -1; } } }
struct Game { pub scene: usize, pub folks: Vec,
pub player: Player,
last_folk: f64,
folk_interval: f64,
global_time: f64,
pub score: i32
}
impl Game { pub fn new(param_scene: usize) -> Game { Game { scene: param_scene, folks: vec![], player: Player::new(), last_folk: 1.0, folk_interval: 2.0, global_time: 0.0, score: 0 } } pub fn set_scene(&mut self, scene: usize) { self.scene = scene; }
}
fn main() { let opengl = OpenGL::V3_2;
}