Regalo navideño – Segunda Parte

Bueno, lo prometido es deuda, así que acá va el código del regalito navideño.

Primero, la clase Sprite, la cual maneja los… sprites! … del juego.

Esto quiere decir que cada fantasma, o burbuja, o pacman esta contenido en un objeto de este tipo. Creo que es mucho más facil de administrar, tratando de automatizar cada uno de los personajes.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace GhostAttack
{
public class Sprite
{
private List<Rectangle> _spritePosition;
    private short _spritePointer;
    private short _totalSprite;
    private Bitmap _fullPicture;
    private int _x;
    private int _y;
    private System.Drawing.Imaging.ImageAttributes _imageAttributes;

    public Sprite(Image spritePicture)
{
_spritePointer = – 1;
_totalSprite = – 1;
_spritePosition = new List<Rectangle>();
_fullPicture = new Bitmap(spritePicture);
_x = 0;
_y = 0;
_imageAttributes = new System.Drawing.Imaging.ImageAttributes();
_imageAttributes.SetColorKey(Color.Magenta, Color.Magenta);
_imageAttributes.ClearColorMatrix();
}

    public int X
{
get { return _x; }
set { _x = value; }
}

    public int Y
{
get { return _y; }
set { _y = value; }
}

    public Rectangle AddSpriteFrame
{
set
{
_spritePosition.Add(value);
_totalSprite++;
}
}

    public void MoveNext()
{
_spritePointer++;
if (_spritePointer > _totalSprite) _spritePointer = 0;
}

    public void DrawAndMoveNext(Graphics sourceFace)
{
this.MoveNext();
Draw(sourceFace);
}

    public void Draw(Graphics sourceFace)
{
sourceFace.DrawImage(_fullPicture, new Rectangle(_x, _y,
_spritePosition[_spritePointer].Width,
_spritePosition[_spritePointer].Height),
_spritePosition[_spritePointer].X,
_spritePosition[_spritePointer].Y,
_spritePosition[_spritePointer].Width,
_spritePosition[_spritePointer].Height, GraphicsUnit.Pixel, _imageAttributes);
}

    public void DrawAt(Graphics sourceFace, int Index)
{
sourceFace.DrawImage(_fullPicture, new Rectangle(_x, _y,
_spritePosition[Index].Width,
_spritePosition[Index].Height),
_spritePosition[Index].X,
_spritePosition[Index].Y,
_spritePosition[Index].Width,
_spritePosition[Index].Height, GraphicsUnit.Pixel, _imageAttributes)
}
}
}

Dentro de un formulario, debemos declarar los objetos que representen todo el juego:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GhostAttack
{
    public partial class Form1 : Form
    {

        private Sprite _blueGhost;
        private Sprite _redGhost;
        private Sprite _orangeGhost;
        private Sprite _pinkGhost;
        private Sprite _swimPack;
        private Sprite _background;
        private int _speedCounter = 1;
        private Sprite[] _bubblesSprites;
        private Sprite[] _numbersSprites;
        private int _score = 0;
        private Bitmap _sourceFace = new Bitmap(388, 279, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
        private Timer _mainProcess = new Timer();
        private int _movementInterval = 0;
        private int _speedInterval = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _mainProcess.Tick += new EventHandler(_mainProcess_Tick);
            _mainProcess.Interval = 20;

            Bitmap _fullImage = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GhostAttack.allgraphics2.bmp"));
            Bitmap _backImage = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GhostAttack.background.jpg"));
            this.pictureBox2.Image = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GhostAttack.xmass.jpg"));

            _blueGhost = new Sprite(_fullImage);
            _redGhost = new Sprite(_fullImage);
            _orangeGhost = new Sprite(_fullImage);
            _pinkGhost = new Sprite(_fullImage);
            _swimPack = new Sprite(_fullImage);
            _background = new Sprite(_backImage);

            _numbersSprites = new Sprite[] { new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage)};
            _numbersSprites[0].AddSpriteFrame = new Rectangle(1, 417, 14, 14);
            _numbersSprites[1].AddSpriteFrame = new Rectangle(16, 417, 14, 14);
            _numbersSprites[2].AddSpriteFrame = new Rectangle(33, 417, 14, 14);
            _numbersSprites[3].AddSpriteFrame = new Rectangle(50, 417, 14, 14);
            _numbersSprites[4].AddSpriteFrame = new Rectangle(64, 417, 14, 14);
            _numbersSprites[5].AddSpriteFrame = new Rectangle(81, 417, 14, 14);
            _numbersSprites[6].AddSpriteFrame = new Rectangle(97, 417, 14, 14);
            _numbersSprites[7].AddSpriteFrame = new Rectangle(113, 417, 14, 14);
            _numbersSprites[8].AddSpriteFrame = new Rectangle(129, 417, 14, 14);
            _numbersSprites[9].AddSpriteFrame = new Rectangle(145, 417, 14, 14);

            _bubblesSprites = new Sprite[] { new Sprite(_fullImage),
                                         new Sprite(_fullImage),
                                         new Sprite(_fullImage)/*,
                                         new Sprite(_fullImage)*/};

            _bubblesSprites[0].AddSpriteFrame = new Rectangle(225, 417, 13, 13);
            _bubblesSprites[1].AddSpriteFrame = new Rectangle(225, 417, 13, 13);
            _bubblesSprites[2].AddSpriteFrame = new Rectangle(225, 417, 13, 13);
            //_bubblesSprites[3].AddSpriteFrame = new Rectangle(225, 417, 13, 13);

            Random randomNumber = new Random();

            int rndNum = randomNumber.Next(1, 388);
            _bubblesSprites[0].X = rndNum;
            rndNum = randomNumber.Next(279, 388);
            _bubblesSprites[0].Y = rndNum;

            rndNum = randomNumber.Next(1, 388);
            _bubblesSprites[1].X = rndNum;
            rndNum = randomNumber.Next(279, 388);
            _bubblesSprites[1].Y = rndNum;

            rndNum = randomNumber.Next(1, 388);
            _bubblesSprites[2].X = rndNum;
            rndNum = randomNumber.Next(279, 388);
            _bubblesSprites[2].Y = rndNum;

            //rndNum = randomNumber.Next(1, 388);
            //_bubblesSprites[3].X = rndNum;
            //rndNum = randomNumber.Next(279, 388);
            //_bubblesSprites[3].Y = rndNum;

            _background.X = 0;
            _background.Y = 0;
            _background.AddSpriteFrame = new Rectangle(0, 0, 388, 279);

            _swimPack.X = 150;
            _swimPack.Y = 120;
            _swimPack.AddSpriteFrame = new Rectangle(6, 135, 28, 23);
            _swimPack.AddSpriteFrame = new Rectangle(38, 134, 28, 22);
            _swimPack.AddSpriteFrame = new Rectangle(70, 135, 28, 23);
            _swimPack.AddSpriteFrame = new Rectangle(102, 135, 28, 23);
            _swimPack.AddSpriteFrame = new Rectangle(134, 136, 28, 23);
            _swimPack.AddSpriteFrame = new Rectangle(165, 138, 28, 23);
            _swimPack.AddSpriteFrame = new Rectangle(198, 137, 28, 23);
            _swimPack.AddSpriteFrame = new Rectangle(230, 135, 28, 23);

            _blueGhost.X = randomNumber.Next(-30, 0);
            _blueGhost.Y = randomNumber.Next(0, 279);
            _blueGhost.AddSpriteFrame = new Rectangle(36, 3, 23, 26);
            _blueGhost.AddSpriteFrame = new Rectangle(67, 3, 23, 26);

            _redGhost.X = randomNumber.Next(-30, 0);
            _redGhost.Y = randomNumber.Next(0, 279);
            _redGhost.AddSpriteFrame = new Rectangle(420, 3, 23, 26);
            _redGhost.AddSpriteFrame = new Rectangle(451, 3, 23, 26);

            _orangeGhost.X = randomNumber.Next(-30, 0);
            _orangeGhost.Y = randomNumber.Next(0, 279);
            _orangeGhost.AddSpriteFrame = new Rectangle(292, 3, 23, 26);
            _orangeGhost.AddSpriteFrame = new Rectangle(323, 3, 23, 26);

            _pinkGhost.X = randomNumber.Next(-30, 0);
            _pinkGhost.Y = randomNumber.Next(0, 279);
            _pinkGhost.AddSpriteFrame = new Rectangle(164, 3, 23, 26);
            _pinkGhost.AddSpriteFrame = new Rectangle(195, 3, 23, 26);

            Graphics _tempGraphic = Graphics.FromImage(_sourceFace);

            _background.DrawAndMoveNext(_tempGraphic);
            _blueGhost.DrawAndMoveNext(_tempGraphic);
            _redGhost.DrawAndMoveNext(_tempGraphic);
            _orangeGhost.DrawAndMoveNext(_tempGraphic);
            _pinkGhost.DrawAndMoveNext(_tempGraphic);
            _swimPack.DrawAndMoveNext(_tempGraphic);
            _bubblesSprites[0].DrawAndMoveNext(_tempGraphic);
            _bubblesSprites[1].DrawAndMoveNext(_tempGraphic);
            _bubblesSprites[2].DrawAndMoveNext(_tempGraphic);
            //_bubblesSprites[3].DrawAndMoveNext(_tempGraphic);

            this.pictureBox1.Image = _sourceFace;

            _mainProcess.Enabled = true;
        }

        private void _mainProcess_Tick(Object sender, System.EventArgs e)
        {
            Graphics _tempGraphic = Graphics.FromImage(_sourceFace);
            _tempGraphic.Clear(Color.Black);

            CheckCollitions();

            this._background.Draw(_tempGraphic);

            _bubblesSprites[0].Y–;
            _bubblesSprites[1].Y–;
            _bubblesSprites[2].Y–;
            //_bubblesSprites[3].Y–;

            _bubblesSprites[0].Draw(_tempGraphic);
            _bubblesSprites[1].Draw(_tempGraphic);
            _bubblesSprites[2].Draw(_tempGraphic);
            //_bubblesSprites[3].Draw(_tempGraphic);

            this._blueGhost.X += 3 + _speedCounter;
            this._redGhost.X += 4 + _speedCounter;
            this._orangeGhost.X += 2 + _speedCounter;
            this._pinkGhost.X += 5 + _speedCounter;

            if (_movementInterval < (_mainProcess.Interval * 2))
            {
                this._blueGhost.Draw(_tempGraphic);
                this._redGhost.Draw(_tempGraphic);
                this._orangeGhost.Draw(_tempGraphic);
                this._pinkGhost.Draw(_tempGraphic);
                this._swimPack.Draw(_tempGraphic);
            }
            else
            {
                _movementInterval = 0;
                this._blueGhost.DrawAndMoveNext(_tempGraphic);
                this._redGhost.DrawAndMoveNext(_tempGraphic);
                this._orangeGhost.DrawAndMoveNext(_tempGraphic);
                this._pinkGhost.DrawAndMoveNext(_tempGraphic);
                this._swimPack.DrawAndMoveNext(_tempGraphic);
            }

            DrawScore(_tempGraphic);

            this.pictureBox1.Image = _sourceFace;

            foreach (Sprite _bubble in _bubblesSprites)
            {
                if (_bubble.Y <= -15)
                {
                    Random randomNumber = new Random();
                    _bubble.X = randomNumber.Next(1, 388);
                    _bubble.Y = randomNumber.Next(279, 388);
                }
            }

            if (_blueGhost.X > 400)
            {
                Random randomNumber = new Random();
                _blueGhost.X = randomNumber.Next(-30, 0);
                _blueGhost.Y = randomNumber.Next(0, 279);
            }

            if (_redGhost.X > 400)
            {
                Random randomNumber = new Random();
                _redGhost.X = randomNumber.Next(-30, 0);
                _redGhost.Y = randomNumber.Next(0, 279);
            }

            if (_orangeGhost.X > 400)
            {
                Random randomNumber = new Random();
                _orangeGhost.X = randomNumber.Next(-30, 0);
                _orangeGhost.Y = randomNumber.Next(0, 279);
            }

            if (_pinkGhost.X > 400)
            {
                Random randomNumber = new Random();
                _pinkGhost.X = randomNumber.Next(-30, 0);
                _pinkGhost.Y = randomNumber.Next(0, 279);
            }

            if (_speedInterval > (_mainProcess.Interval * 50))
            {
                _speedInterval = 0;
                _speedCounter++;
            }

            _movementInterval += _mainProcess.Interval;
            _speedInterval += _mainProcess.Interval;
            _score++;
        }

        private void CheckCollitions()
        {
            Rectangle _intersect = Rectangle.Intersect(
                new Rectangle(_orangeGhost.X, _orangeGhost.Y, 23, 26),
                new Rectangle(_swimPack.X, _swimPack.Y, 28, 23));
            if ((_intersect != Rectangle.Empty) && (_intersect.Height > 0) && (_intersect.Width > 0))
                _mainProcess.Enabled = false;

            _intersect = Rectangle.Intersect(
                new Rectangle(_redGhost.X, _redGhost.Y, 23, 26),
                new Rectangle(_swimPack.X, _swimPack.Y, 28, 23));
            if ((_intersect != Rectangle.Empty) && (_intersect.Height > 0) && (_intersect.Width > 0))
                _mainProcess.Enabled = false;

            _intersect = Rectangle.Intersect(
                new Rectangle(_blueGhost.X, _blueGhost.Y, 23, 26),
                new Rectangle(_swimPack.X, _swimPack.Y, 28, 23));
            if ((_intersect != Rectangle.Empty) && (_intersect.Height > 0) && (_intersect.Width > 0))
                _mainProcess.Enabled = false;

            _intersect = Rectangle.Intersect(
               new Rectangle(_pinkGhost.X, _pinkGhost.Y, 23, 26),
               new Rectangle(_swimPack.X, _swimPack.Y, 28, 23));
            if ((_intersect != Rectangle.Empty) && (_intersect.Height > 0) && (_intersect.Width > 0))
                _mainProcess.Enabled = false;

            if (_score > 50000) _mainProcess.Enabled = false;

            if (!_mainProcess.Enabled) this.pictureBox2.Visible = true;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this._swimPack.X = e.X;
            this._swimPack.Y = e.Y;
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            Cursor.Hide();
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            Cursor.Show();
        }

        private void DrawScore(Graphics _sourceFace)
        {
            string _localScore = _score.ToString();
            int _count = 0;
            for (int i = (_localScore.Length – 1); i >= 0; i–)
            {
                int _temp;
                Int32.TryParse(_localScore.Substring(i, 1),out _temp);
                _numbersSprites[_temp].Y = 5;
                _numbersSprites[_temp].X = 370 – (14 * _count);
                _numbersSprites[_temp].DrawAndMoveNext(_sourceFace);
                _count++;
            }
        }
    }
}

Adjuntas, las imágenes del juego.

allgraphics xmass background



Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s