Guest User

Untitled

a guest
Apr 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. extern crate ggez;
  2. extern crate ncollide;
  3. extern crate nalgebra;
  4. extern crate rand;
  5. extern crate rodio;
  6. use std::fs::File;
  7. use ggez::{graphics, Context, GameResult};
  8. use ggez::graphics::Font;
  9. use nalgebra::Vector2;
  10. use rand::{Rng,thread_rng};
  11.  
  12. #[derive(Debug)]
  13. pub enum Direction {
  14. Idle,
  15. Left,
  16. Right,
  17. }
  18.  
  19. pub struct Player {
  20. pub pos: Vector2<f32>,
  21. pub speed: f32,
  22. pub direction: Direction,
  23. pub size: Vector2<f32>,
  24.  
  25. pub vel: Vector2<f32>,
  26. }
  27.  
  28. impl Player {
  29. pub fn new(_ctx: &mut Context) -> GameResult<Player> {
  30. Ok(Player {
  31. pos:Vector2::new(200.0,700.0),
  32. speed: 200.0,
  33. // faster speed: 350.0,
  34. // slower speed: 50.0,
  35. direction: Direction::Idle,
  36. size: Vector2::new(50.0,35.0),
  37.  
  38. vel: Vector2::new(0.0,0.0),
  39. })
  40. }
  41. pub fn update(&mut self) {
  42. self.pos.x = self.vel.x;
  43.  
  44. }
  45. pub fn player_handle_input(&mut self, _ctx: &mut Context, dt: f32, player_width: u32) {
  46. match self.direction {
  47. Direction::Left => {
  48. if self.pos.x > 0.0 {
  49. self.vel.x = self.pos.x - (self.speed * dt)
  50. }
  51. }
  52. Direction::Right => {
  53. let (screen_width, _) = graphics::get_size(_ctx);
  54. let bound = (screen_width - player_width) as f32;
  55.  
  56. if self.pos.x < bound {
  57. self.vel.x = self.pos.x + (self.speed * dt);
  58. }
  59. }
  60.  
  61. _ => (),
  62. }
  63. }
  64.  
  65.  
  66. }
  67.  
  68. pub struct Bullet {
  69. pub pos: Vector2<f32>,
  70. pub size: Vector2<f32>,
  71. }
  72.  
  73. impl Bullet {
  74. pub fn new(player_x: f32, player_y: f32, player_width: u32) -> GameResult<Bullet> {
  75. Ok(Bullet {
  76. pos: Vector2::new(player_x + (player_width as f32 / 2.0), player_y),
  77. size: Vector2::new(30.0, 20.0),
  78.  
  79. })
  80. }
  81. #[allow(non_snake_case)]
  82. pub fn moveBy(&mut self, _ctx: &mut Context, dt: f32) {
  83. self.pos.y += -500.0 * dt;
  84. }
  85.  
  86. }
  87.  
  88.  
  89. pub struct Enemy {
  90. pub pos: Vector2<f32>,
  91. pub size: Vector2<f32>,
  92. }
  93.  
  94. impl Enemy {
  95. pub fn new() -> GameResult<Enemy> {
  96. let mut rng = thread_rng();
  97. Ok(
  98. Enemy {
  99. pos: Vector2::new(rng.gen_range(20.0,1100.0), rng.gen_range(1.0, 100.0)),
  100. size: Vector2::new(100.0, 50.0),
  101.  
  102. }
  103. )
  104. }
  105. #[allow(non_snake_case)]
  106. pub fn moveBy(&mut self, _ctx: &mut Context, dt: f32) {
  107. self.pos.y += 700.0 * dt - 10.0;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. }
  115. use ggez::audio::Source;
  116. pub struct Assets {
  117. pub player: graphics::Image,
  118. pub bullet: graphics::Image,
  119. pub enemy: graphics::Image,
  120. pub font: Font,
  121. pub lose_a: Source,
  122. pub shot: Source,
  123. }
  124.  
  125. impl Assets {
  126. pub fn new(_ctx: &mut Context) -> GameResult<Assets> {
  127. Ok(Assets {
  128. player: graphics::Image::new(_ctx, "/ship.png").unwrap(),
  129. bullet: graphics::Image::new(_ctx, "/laser.png").unwrap(),
  130. enemy: graphics::Image::new(_ctx, "/enemyBlack1.png").unwrap(),
  131. font: graphics::Font::new(_ctx, "/font.ttf",23).unwrap(),
  132. lose_a: Source::new(_ctx,"/lose.wav").unwrap(),
  133. shot: Source::new(_ctx, "/shot.wav").unwrap(),
  134. })
  135. }
  136. }
Add Comment
Please, Sign In to add comment