Advertisement
Cassimus

Pipe

Jun 21st, 2025
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1.  
  2. using System.Numerics;
  3. using Raylib_cs;
  4.  
  5. namespace FlappyBird;
  6.  
  7. public class Pipe
  8. {
  9.     private const int moveSpeed = 2;
  10.  
  11.     private Vector2 position;
  12.     private readonly int width;
  13.     private readonly int height;
  14.     private readonly int gap;
  15.     private readonly Raylib_cs.Color color;
  16.  
  17.     public Vector2 Position { get { return position; } }
  18.     public int Width => width;
  19.     public bool Passed { get; set; }
  20.  
  21.     public Pipe(Vector2 position, int width, int height, int gap, Color color)
  22.     {
  23.         this.position = position;
  24.         this.width = width;
  25.         this.height = height;
  26.         this.gap = gap;
  27.         this.color = color;
  28.         this.Passed = false;
  29.     }
  30.  
  31.     public void Update()
  32.     {
  33.         position.X -= moveSpeed;
  34.     }
  35.  
  36.     public void Draw()
  37.     {
  38.         Raylib.DrawRectangle((int)position.X, (int)position.Y, width, height, color);
  39.  
  40.         Raylib.DrawRectangle((int)position.X, (int)position.Y + height + gap,
  41.                 width, Game.screenHeight - height - gap, color);
  42.     }
  43.  
  44.     public bool IsOffScreen()
  45.     {
  46.         return position.X + width < 0;
  47.     }
  48.  
  49.     public bool CheckCollision(Vector2 birdPosition)
  50.     {
  51.         bool collisionTopPipe = false;
  52.         bool collisionBottomPipe = false;
  53.  
  54.         collisionTopPipe = birdPosition.X + Bird.size > position.X &&
  55.                             birdPosition.X - Bird.size < position.X + width &&
  56.                             birdPosition.Y - Bird.size < position.Y + height;
  57.  
  58.         collisionBottomPipe = birdPosition.X + Bird.size > position.X &&
  59.                             birdPosition.X - Bird.size < position.X + width &&
  60.                             birdPosition.Y + Bird.size > position.Y + height + gap;
  61.  
  62.         return collisionTopPipe || collisionBottomPipe;
  63.     }
  64.  
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement