コード例 #1
0
ファイル: MapCreator.cs プロジェクト: malionde/CSharpMazeGame
        public void CreateMap(ucGameField field, List <BoxInfo> infoList)
        {
            int w = field.Size.Width;
            int h = field.Size.Height;

            int maxX = 0;
            int maxY = 0;

            StringBuilder builder = new StringBuilder();

            // Maksimum satır ve sütün sayısı bul
            for (int i = 0; i < infoList.Count; i++)
            {
                var b = infoList[i];
                if (b.X > maxX)
                {
                    maxX = b.X;
                }
                if (b.Y > maxY)
                {
                    maxY = b.Y;
                }

                //builder.AppendLine(b.ToString());
            }

            //Satır Yüksekliği Yükseklik / Maks satır sayısı
            //Sütün yüksekliği Genişlik / Maks Sütun sayısı
            satirYuk = h / (maxY + 1);
            sutunGen = w / (maxX + 1);

            // MaxY boyunca Döngü çalıştır
            for (int i = 0; i <= maxY; i++)
            {
                //maxX boyunca döngü çalıştır
                //Döngü boyunca ucBox ları yaz
                for (int j = 0; j <= maxX; j++)
                {
                    var info = infoList.Where(z => z.X == j && z.Y == i).FirstOrDefault();

                    if (info == null)
                    {
                        throw new Exception("WTF!");
                    }

                    // ucBox oluştur, boyut ver, konum belirle
                    // Getcolor func ile renk ver
                    ucBox b = new ucBox();
                    b.Size      = new Size(sutunGen, satirYuk);
                    b.Location  = new Point(sutunGen * j, satirYuk * i);
                    b.BackColor = GetColor(info.C);

                    field.Controls.Add(b);
                    //Button btnAdd = new Button();
                    // btnAdd.Location = new System.Drawing.Point(25, 25);

                    // if (!=0)
                    // this.btnAdd.BackColor = Color.Red;
                }
            }


            //File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"test.txt"), builder.ToString());

            // box yap
            // ekle
        }
コード例 #2
0
ファイル: frmMain.cs プロジェクト: malionde/CSharpMazeGame
        public void MovePlayer(object sender, EventArgs e)
        {
            counter++;
            lbl_Counter.Text = counter.ToString();

            // Yönü bul
            // Gidilecek yönü kontrol et

            // eski
            //var box = infoList.Where(i => i.X == playerPosition.X && i.Y == playerPosition.Y).FirstOrDefault();
            //if (box == null) throw new Exception("WTF");

            ucBox old = boxList[playerPosition.Y, playerPosition.X];

            // bunu eskiline getir

            //Hızı eski haline çevir
            timer.Interval = 300;

            int x     = playerPosition.X;
            int y     = playerPosition.Y;
            int old_x = playerPosition.X;
            int old_y = playerPosition.Y;

            switch (playa.Yon)
            {
            case Direction.Top:
                x = x - 1;
                break;

            case Direction.Right:
                y = y + 1;
                break;

            case Direction.Down:
                x = x + 1;
                break;

            case Direction.Left:
                y = y - 1;
                break;

            case Direction.Stop:
                playerPosition.X = 0;
                playerPosition.Y = 0;
                //Duvara çarptığında bir tepki ver
                break;

            default:
                break;
            }


            // yeni
            try
            {
                ucBox yeni = boxList[y, x];

                if (yeni.MapChar == 'F' || playerPosition.X == maxY)

                {
                    timer.Interval = 100000;

                    DialogResult result = MessageBox.Show("You Won!!" + "\n" + "Your Soccer is :" + (health * 100 - (counter * 5)) + "\n" + "Do you wanna next game? ", "You Won!", MessageBoxButtons.YesNoCancel);
                    if (result == DialogResult.Yes)
                    {
                        NewMap();
                    }
                    else if (result == DialogResult.No)

                    {
                        Environment.Exit(0);
                    }
                    else if (result == DialogResult.Cancel)
                    {
                        //code for Cancel
                    }
                }
                else if (yeni.MapChar == '0' || playerPosition.X >= maxY || playerPosition.Y >= maxX)
                {
                    health--;
                    //Eğer can düştüyse Mesajı göster ve diğer hamle yapılana kadar oyunu yavaşlat.
                    //Tekrar tekrar duvara çar
                    timer.Interval = 1400;
                    AutoClosingMessageBox.Show("You hit the wall!" + "\n" + "Your health is :" + health, "Warning!", 1300);

                    #region Duvara carptıktan sonrası
                    // Duvara sürekli çarpmasını engelle
                    if (playa.Yon == Direction.Down)
                    {
                        playa.Yon = Direction.Top;
                    }
                    else if (playa.Yon == Direction.Left)
                    {
                        playa.Yon = Direction.Right;
                    }
                    else if (playa.Yon == Direction.Right)
                    {
                        playa.Yon = Direction.Left;
                    }
                    else
                    {
                        playa.Yon = Direction.Down;
                    }

                    timer.Interval = 500;
                    #endregion

                    if (health == 0)
                    {
                        timer.Interval = 1000000;

                        DialogResult result = MessageBox.Show("You lost the game!" + "\n" + "Do you wanna new game? ", "Game is over!", MessageBoxButtons.YesNoCancel);
                        if (result == DialogResult.Yes)
                        {
                            EndGame();
                        }
                        else if (result == DialogResult.No)

                        {
                            Environment.Exit(0);
                        }
                        else if (result == DialogResult.Cancel)
                        {
                        }
                    }
                }



                //else if ( playerPosition.X < 0 || playerPosition.Y < 0 || playerPosition.X >= maxY-2 || playerPosition.Y >= maxX-1 )
                //    EndGame();
                else
                {
                    playerPosition.X = x;
                    playerPosition.Y = y;

                    var temp = yeni.BackColor;
                    yeni.BackColor = Color.IndianRed;
                    old.BackColor  = temp;
                    old.SetPicture(null);

                    yeni.SetPicture(GetPlayerImage());
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }