Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- class GAME : Game
- {
- class SPRITE
- {
- public Vector2 pos;
- public Texture2D tex;
- public bool colliding(SPRITE other)
- {
- Rectangle this_rect = new Rectangle(
- (int)this.pos.X, (int)this.pos.Y, this.tex.Width, this.tex.Height);
- Rectangle other_rect = new Rectangle(
- (int)other.pos.X, (int)other.pos.Y, other.tex.Width, other.tex.Height);
- return this_rect.Intersects(other_rect);
- }
- public virtual void update() { }
- public virtual void collide(SPRITE other) { }
- public void draw(SpriteBatch sprite_batch) { sprite_batch.Draw(tex, pos, Color.White); }
- }
- class PLAYER : SPRITE
- {
- public PLAYER() { tex = player_tex; }
- public override void update()
- {
- float speed = 10;
- if (keyboard.IsKeyDown(Keys.Up)) this.pos.Y -= speed;
- if (keyboard.IsKeyDown(Keys.Down)) this.pos.Y += speed;
- if (keyboard.IsKeyDown(Keys.Left)) this.pos.X -= speed;
- if (keyboard.IsKeyDown(Keys.Right)) this.pos.X += speed;
- if (keyboard.IsKeyDown(Keys.Space) &&
- !last_keyboard.IsKeyDown(Keys.Space))
- {
- BULLET bullet = new BULLET();
- bullet.pos = this.pos + new Vector2(50, 0);
- sprites.Add(bullet);
- }
- }
- }
- class BULLET : SPRITE
- {
- public BULLET() { tex = bullet_tex; }
- public override void update() { this.pos.X += 20; }
- public override void collide(SPRITE enemy) // SPRITE waaa
- {
- if (enemy.GetType() != typeof(ENEMY)) return; // waaa
- enemy.tex = explosion_tex;
- }
- }
- class ENEMY : SPRITE
- {
- public ENEMY() { tex = enemy_tex; }
- public override void collide(SPRITE player) // SPRITE waaa
- {
- if (player.GetType() != typeof(PLAYER)) return; // waaa
- player.tex = explosion_tex;
- }
- }
- static Texture2D player_tex, bullet_tex, enemy_tex, explosion_tex;
- static List<SPRITE> sprites = new List<SPRITE>();
- static Random random = new Random();
- static KeyboardState keyboard, last_keyboard;
- static void Main() { new GAME().Run(); }
- GAME()
- {
- GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = 1280;
- graphics.PreferredBackBufferHeight = 720;
- graphics.ApplyChanges();
- IsMouseVisible = true;
- }
- protected override void LoadContent()
- {
- player_tex = Texture2D.FromFile(GraphicsDevice, "player.png");
- bullet_tex = Texture2D.FromFile(GraphicsDevice, "bullet.png");
- enemy_tex = Texture2D.FromFile(GraphicsDevice, "enemy.png");
- explosion_tex = Texture2D.FromFile(GraphicsDevice, "explosion.png");
- for (int i = 0; i < 10; i++)
- {
- ENEMY enemy = new ENEMY();
- enemy.pos = new Vector2(random.Next(600, 1200), random.Next(720));
- sprites.Add(enemy);
- }
- PLAYER player = new PLAYER();
- player.pos = new Vector2(100, 360);
- sprites.Add(player);
- base.LoadContent();
- }
- protected override void Update(GameTime gameTime)
- {
- last_keyboard = keyboard; keyboard = Keyboard.GetState();
- for (int i = 0; i < sprites.Count; i++)
- sprites[i].update();
- for (int i = 0; i < sprites.Count; i++)
- for (int j = i + 1; j < sprites.Count; j++)
- if (sprites[i].colliding(sprites[j]))
- {
- sprites[i].collide(sprites[j]); // how do i make these two lines trigger
- sprites[j].collide(sprites[i]); // the derived classes' ver instead of SPRITE?
- }
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- SpriteBatch sprite_batch = new SpriteBatch(GraphicsDevice);
- sprite_batch.Begin();
- for (int i = 0; i < sprites.Count; i++)
- sprites[i].draw(sprite_batch);
- sprite_batch.End();
- base.Draw(gameTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement