private void MakeSnack() { while (IfSnackEaten) { // ---- spawnowanie przekasek w "siatce", po ktorej sie porusza waz ------ snack = new SnakeCoordinates((velocity * random.Next(0, (panel1.Width / (int)velocity) - 1)) + (panel1.Width % (int)velocity) / 2, (velocity * random.Next(0, (panel1.Height / (int)velocity) - 1)) + (panel1.Height % (int)velocity) / 2); IfSnackEaten = false; NumbeOfParts(1); for (int i = 0; i < snakePosition.Count; i++) { //sprawdzanie czy przekaska jest wewnatrz węża if ( snack.x + snack.width >= snakePosition[i].x && snack.x <= snakePosition[i].x + snakePosition[i].width && snack.y + snack.heigth >= snakePosition[i].y && snack.y <= snakePosition[i].y + snakePosition[i].heigth ) { IfSnackEaten = true; } } } }
public MyForm() { //----------okno glowne----------------- Text = "Snake"; Width = 800; Height = 600; //Paint += Draw; KeyDown += GetDirection; //---------------panel---------------- panel1.Width = (int)(0.7 * ClientSize.Width); //panel1.width = 885 pikseli panel1.Height = (int)(0.9 * ClientSize.Height); //panel1.heigth = 684 pikseli panel1.Top = (ClientSize.Height - panel1.Height) / 2; panel1.Left = (panel1.Top); //panel1.Left = (ClientSize.Width - panel1.Width) / 2; panel1.BackColor = Color.White; //panel1.TabIndex = 1; //--------------labels-------------- label2.Font = new Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold); label2.AutoSize = true; label2.BackColor = Color.White; label2.Top = (int)(ClientSize.Height * 0.3); label2.Left = ((ClientSize.Width - (panel1.Left + panel1.Width + label1.Width)) / 2) + (panel1.Left + panel1.Width); label2.Text = "Twój Wynik:"; label3.Font = new Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold); label3.Width = ClientSize.Width / 8; label3.BackColor = Color.White; label3.Top = (int)(ClientSize.Height * 0.4); label3.Left = ((ClientSize.Width - (panel1.Left + panel1.Width + label1.Width)) / 2) + (panel1.Left + panel1.Width); label1.Font = new Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold); label1.Text = "Zagraj ponownie"; label1.AutoSize = true; label1.Height += 10; label1.Top = (int)(ClientSize.Height * 0.6); label1.Left = ((ClientSize.Width - (panel1.Left + panel1.Width + label1.Width)) / 2) + panel1.Width; //label1.TabIndex = 4; //label1.Focus(); label1.Visible = false; label1.Click += PlayAgain; label1.BackColor = Color.White; label1.TextAlign = ContentAlignment.MiddleCenter; label2.TextAlign = ContentAlignment.MiddleCenter; label3.TextAlign = ContentAlignment.MiddleCenter; //--------buttons---------- /* * button1.Text = "Zagraj ponownie"; * //button1.Visible = false; * //button1.Enabled = true; * button1.AutoSize = true; * button1.Width += 20; * button1.Height += 10; * button1.Top = (int)(ClientSize.Height * 0.6); * button1.Left = ((ClientSize.Width - (panel1.Left + panel1.Width + button1.Width)) / 2) + (panel1.Left + panel1.Width); * button1.TabIndex = 4; * button1.Focus(); * button1.Click += PlayAgain; */ //----------dodawanie elementow----------------- Controls.Add(label1); Controls.Add(label2); Controls.Add(label3); //Controls.Add(button1); Controls.Add(panel1); //AutoValidate = Enabled; //Focus(); //ResumeLayout(false); //PerformLayout(); //------------inicjacja elementow gry------------- snakePosition.Add(new SnakeCoordinates(((Convert.ToInt16(panel1.Width / (2 * velocity))) * velocity) + ((panel1.Width % velocity) / 2), ((Convert.ToInt16(panel1.Height / (2 * velocity))) * velocity) + ((panel1.Height % velocity) / 2))); snakePreviousPosition.Add(new SnakeCoordinates()); snack = new SnakeCoordinates(-50, -50, 20, 20); NumbeOfParts(3); //--------------------timer----------------- timer2.Enabled = true; timer2.Interval = 100; timer2.Tick += timerTick; }