Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate ggez;
- extern crate ncollide;
- extern crate nalgebra;
- extern crate rand;
- extern crate rodio;
- use std::fs::File;
- use ggez::{graphics, Context, GameResult};
- use ggez::graphics::Font;
- use nalgebra::Vector2;
- use rand::{Rng,thread_rng};
- #[derive(Debug)]
- pub enum Direction {
- Idle,
- Left,
- Right,
- }
- pub struct Player {
- pub pos: Vector2<f32>,
- pub speed: f32,
- pub direction: Direction,
- pub size: Vector2<f32>,
- pub vel: Vector2<f32>,
- }
- impl Player {
- pub fn new(_ctx: &mut Context) -> GameResult<Player> {
- Ok(Player {
- pos:Vector2::new(200.0,700.0),
- speed: 200.0,
- // faster speed: 350.0,
- // slower speed: 50.0,
- direction: Direction::Idle,
- size: Vector2::new(50.0,35.0),
- vel: Vector2::new(0.0,0.0),
- })
- }
- pub fn update(&mut self) {
- self.pos.x = self.vel.x;
- }
- pub fn player_handle_input(&mut self, _ctx: &mut Context, dt: f32, player_width: u32) {
- match self.direction {
- Direction::Left => {
- if self.pos.x > 0.0 {
- self.vel.x = self.pos.x - (self.speed * dt)
- }
- }
- Direction::Right => {
- let (screen_width, _) = graphics::get_size(_ctx);
- let bound = (screen_width - player_width) as f32;
- if self.pos.x < bound {
- self.vel.x = self.pos.x + (self.speed * dt);
- }
- }
- _ => (),
- }
- }
- }
- pub struct Bullet {
- pub pos: Vector2<f32>,
- pub size: Vector2<f32>,
- }
- impl Bullet {
- pub fn new(player_x: f32, player_y: f32, player_width: u32) -> GameResult<Bullet> {
- Ok(Bullet {
- pos: Vector2::new(player_x + (player_width as f32 / 2.0), player_y),
- size: Vector2::new(30.0, 20.0),
- })
- }
- #[allow(non_snake_case)]
- pub fn moveBy(&mut self, _ctx: &mut Context, dt: f32) {
- self.pos.y += -500.0 * dt;
- }
- }
- pub struct Enemy {
- pub pos: Vector2<f32>,
- pub size: Vector2<f32>,
- }
- impl Enemy {
- pub fn new() -> GameResult<Enemy> {
- let mut rng = thread_rng();
- Ok(
- Enemy {
- pos: Vector2::new(rng.gen_range(20.0,1100.0), rng.gen_range(1.0, 100.0)),
- size: Vector2::new(100.0, 50.0),
- }
- )
- }
- #[allow(non_snake_case)]
- pub fn moveBy(&mut self, _ctx: &mut Context, dt: f32) {
- self.pos.y += 700.0 * dt - 10.0;
- }
- }
- use ggez::audio::Source;
- pub struct Assets {
- pub player: graphics::Image,
- pub bullet: graphics::Image,
- pub enemy: graphics::Image,
- pub font: Font,
- pub lose_a: Source,
- pub shot: Source,
- }
- impl Assets {
- pub fn new(_ctx: &mut Context) -> GameResult<Assets> {
- Ok(Assets {
- player: graphics::Image::new(_ctx, "/ship.png").unwrap(),
- bullet: graphics::Image::new(_ctx, "/laser.png").unwrap(),
- enemy: graphics::Image::new(_ctx, "/enemyBlack1.png").unwrap(),
- font: graphics::Font::new(_ctx, "/font.ttf",23).unwrap(),
- lose_a: Source::new(_ctx,"/lose.wav").unwrap(),
- shot: Source::new(_ctx, "/shot.wav").unwrap(),
- })
- }
- }
Add Comment
Please, Sign In to add comment