Advertisement
SteelGolem

polymorph damn you

Jul 5th, 2025
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1.  
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using System;
  6.  
  7. class GAME : Game
  8. {
  9.     class SPRITE
  10.     {
  11.         public Vector2 pos;
  12.         public Texture2D tex;
  13.         public bool colliding(SPRITE other)
  14.         {
  15.             Rectangle this_rect = new Rectangle(
  16.                 (int)this.pos.X, (int)this.pos.Y, this.tex.Width, this.tex.Height);
  17.             Rectangle other_rect = new Rectangle(
  18.                 (int)other.pos.X, (int)other.pos.Y, other.tex.Width, other.tex.Height);
  19.             return this_rect.Intersects(other_rect);
  20.         }
  21.         public virtual void update() { }
  22.         public virtual void collide(SPRITE other) { }
  23.         public void draw(SpriteBatch sprite_batch) { sprite_batch.Draw(tex, pos, Color.White); }
  24.     }
  25.     class PLAYER : SPRITE
  26.     {
  27.         public PLAYER() { tex = player_tex; }
  28.         public override void update()
  29.         {
  30.             float speed = 10;
  31.             if (keyboard.IsKeyDown(Keys.Up)) this.pos.Y -= speed;
  32.             if (keyboard.IsKeyDown(Keys.Down)) this.pos.Y += speed;
  33.             if (keyboard.IsKeyDown(Keys.Left)) this.pos.X -= speed;
  34.             if (keyboard.IsKeyDown(Keys.Right)) this.pos.X += speed;
  35.             if (keyboard.IsKeyDown(Keys.Space) &&
  36.                 !last_keyboard.IsKeyDown(Keys.Space))
  37.             {
  38.                 BULLET bullet = new BULLET();
  39.                 bullet.pos = this.pos + new Vector2(50, 0);
  40.                 sprites.Add(bullet);
  41.             }
  42.         }
  43.     }
  44.     class BULLET : SPRITE
  45.     {
  46.         public BULLET() { tex = bullet_tex; }
  47.         public override void update() { this.pos.X += 20; }
  48.         public override void collide(SPRITE enemy) // SPRITE waaa
  49.         {
  50.             if (enemy.GetType() != typeof(ENEMY)) return; // waaa
  51.             enemy.tex = explosion_tex;
  52.         }
  53.     }
  54.     class ENEMY : SPRITE
  55.     {
  56.         public ENEMY() { tex = enemy_tex; }
  57.         public override void collide(SPRITE player) // SPRITE waaa
  58.         {
  59.             if (player.GetType() != typeof(PLAYER)) return; // waaa
  60.             player.tex = explosion_tex;
  61.         }
  62.     }
  63.     static Texture2D player_tex, bullet_tex, enemy_tex, explosion_tex;
  64.     static List<SPRITE> sprites = new List<SPRITE>();
  65.     static Random random = new Random();
  66.     static KeyboardState keyboard, last_keyboard;
  67.     static void Main() { new GAME().Run(); }
  68.     GAME()
  69.     {
  70.         GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);
  71.         graphics.PreferredBackBufferWidth = 1280;
  72.         graphics.PreferredBackBufferHeight = 720;
  73.         graphics.ApplyChanges();
  74.         IsMouseVisible = true;
  75.     }
  76.     protected override void LoadContent()
  77.     {
  78.         player_tex = Texture2D.FromFile(GraphicsDevice, "player.png");
  79.         bullet_tex = Texture2D.FromFile(GraphicsDevice, "bullet.png");
  80.         enemy_tex = Texture2D.FromFile(GraphicsDevice, "enemy.png");
  81.         explosion_tex = Texture2D.FromFile(GraphicsDevice, "explosion.png");
  82.         for (int i = 0; i < 10; i++)
  83.         {
  84.             ENEMY enemy = new ENEMY();
  85.             enemy.pos = new Vector2(random.Next(600, 1200), random.Next(720));
  86.             sprites.Add(enemy);
  87.         }
  88.         PLAYER player = new PLAYER();
  89.         player.pos = new Vector2(100, 360);
  90.         sprites.Add(player);
  91.         base.LoadContent();
  92.     }
  93.     protected override void Update(GameTime gameTime)
  94.     {
  95.         last_keyboard = keyboard; keyboard = Keyboard.GetState();
  96.         for (int i = 0; i < sprites.Count; i++)
  97.             sprites[i].update();
  98.         for (int i = 0; i < sprites.Count; i++)
  99.             for (int j = i + 1; j < sprites.Count; j++)
  100.                 if (sprites[i].colliding(sprites[j]))
  101.                 {
  102.                     sprites[i].collide(sprites[j]); // how do i make these two lines trigger
  103.                     sprites[j].collide(sprites[i]); // the derived classes' ver instead of SPRITE?
  104.                 }
  105.         base.Update(gameTime);
  106.     }
  107.     protected override void Draw(GameTime gameTime)
  108.     {
  109.         GraphicsDevice.Clear(Color.CornflowerBlue);
  110.         SpriteBatch sprite_batch = new SpriteBatch(GraphicsDevice);
  111.         sprite_batch.Begin();
  112.         for (int i = 0; i < sprites.Count; i++)
  113.             sprites[i].draw(sprite_batch);
  114.         sprite_batch.End();
  115.         base.Draw(gameTime);
  116.     }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement