Ejemplo n.º 1
0
        public Form1()
        {
            InitializeComponent();

            this.display = Display.Create(320, 200, 2, 2, CGAColor.Black);
            this.display.RegisterListener(this);

            ScaledBitmap testSprite = ScaledBitmap.FromBitmap((Bitmap)Bitmap.FromFile("test_sprite.png"));
            Random       rnd        = new Random();

            this.viewPorts = new List <TestViewPort>();
            this.viewPorts.Add(new TestViewPort(0, 0, 200, 150, CGAColor.Cyan, testSprite, 50, rnd));
            this.viewPorts.Add(new TestViewPort(50, 70, 100, 50, CGAColor.Green, testSprite, 50, rnd));
            this.viewPorts.Add(new TestViewPort(70, 80, 100, 50, CGAColor.Red, testSprite, 50, rnd));
            foreach (ViewPort vp in this.viewPorts)
            {
                this.display.RegisterViewPort(vp);
            }
            this.selectedVP = this.viewPorts[2];

            this.formClosing = new AutoResetEvent(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs a TestSimulator object.
        /// </summary>
        /// <param name="width">The width of the simulation field.</param>
        /// <param name="height">The height of the simulation field.</param>
        public TestSimulator(int width, int height, int opCount) : base(0, 0, width, height)
        {
            /// Create the Display and the ViewPort objects that we will use for drawing.
            this.theDisplay = Display.Create(width, height, 1, 1, CGAColor.Black);
            this.theDisplay.RegisterViewPort(this);

            /// Create the player bitmaps for each possible colors.
            Bitmap originalBmp = (Bitmap)Bitmap.FromFile("player.png");

            this.playerBitmaps = new Dictionary <PlayerColor, ScaledBitmap>();
            this.playerBitmaps.Add(PlayerColor.White, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.White));
            this.playerBitmaps[PlayerColor.White].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Red, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Red));
            this.playerBitmaps[PlayerColor.Red].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Blue, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Blue));
            this.playerBitmaps[PlayerColor.Blue].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Green, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Green));
            this.playerBitmaps[PlayerColor.Green].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Yellow, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Yellow));
            this.playerBitmaps[PlayerColor.Yellow].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Cyan, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Cyan));
            this.playerBitmaps[PlayerColor.Cyan].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Orange, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Brown));
            this.playerBitmaps[PlayerColor.Orange].MakeTransparent(CGAColor.LightMagenta);
            this.playerBitmaps.Add(PlayerColor.Magenta, ScaledBitmap.FromBitmap(originalBmp, CGAColor.LightCyan, CGAColor.Magenta));
            this.playerBitmaps[PlayerColor.Magenta].MakeTransparent(CGAColor.LightMagenta);
            originalBmp.Dispose();

            /// Generate disjunct random initial positions.
            List <Rectangle> initPosList = new List <Rectangle>();

            for (int i = 0; i < opCount; i++)
            {
                bool rdy = false;
                while (!rdy)
                {
                    int       x                 = RandomService.DefaultGenerator.Next(width - Player.DIAMETER);
                    int       y                 = RandomService.DefaultGenerator.Next(height - Player.DIAMETER);
                    Rectangle generatedRect     = new Rectangle(x, y, Player.DIAMETER, Player.DIAMETER);
                    bool      intersectionFound = false;
                    foreach (Rectangle r in initPosList)
                    {
                        if (!Rectangle.Intersect(r, generatedRect).IsEmpty)
                        {
                            /// The generated rectangle intersects another rectangle.
                            intersectionFound = true;
                            break;
                        }
                    }
                    if (!intersectionFound)
                    {
                        /// Intersection not found with the other rectangles --> put it into the list.
                        initPosList.Add(generatedRect);
                        rdy = true;
                    }
                }
            }

            /// Create the players.
            this.players = new Player[opCount];
            for (int i = 0; i < opCount; i++)
            {
                this.players[i] = new Player((PlayerColor)i, initPosList[i], this);
            }
        }