Регистрация не е нужна, освен при създаване на тема в "Задача на седмицата".

Анимация

Анимация

Мнениеот Гост » 26 Фев 2021, 09:35

Здравейте, трябва ми код за падане на ябълки от дърво и възстановяването им. Кодът трябва да бъде за C# (windows form application)
Прикачени файлове
6BDA6FB7-F2B4-4B01-AD20-045B412EB544.jpeg
6BDA6FB7-F2B4-4B01-AD20-045B412EB544.jpeg (87.92 KiB) Прегледано 802 пъти
Гост
 

Re: Анимация

Мнениеот Добромир Глухаров » 03 Мар 2021, 20:06

Код: Избери целия код
/*
 * Created by SharpDevelop.
 * User: Добромир
 * Date: 3.3.2021 г.
 * Time: 17:55
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Animation_of_apple_tree
{
   /// <summary>
   /// Description of MainForm.
   /// </summary>
   public partial class MainForm : Form
   {
      Random rnd;
      Point[] apple;
      bool clash;
      Timer tmr;
      public MainForm()
      {
         //
         // The InitializeComponent() call is required for Windows Forms designer support.
         //
         InitializeComponent();
         
         //
         // TODO: Add constructor code after the InitializeComponent() call.
         //
         rnd = new Random();
         apple = new Point[5];
         LoadApples();
         tmr = new Timer();
         tmr.Enabled = true;
         tmr.Interval = 700;
         tmr.Tick += new EventHandler(Falling);
      }
      void MainFormPaint(object sender, PaintEventArgs e)
      {
         e.Graphics.FillRectangle(Brushes.Tan, 130, 150, 40, 140);
         e.Graphics.DrawRectangle(Pens.Brown, 130, 150, 40, 140);
         e.Graphics.FillEllipse(Brushes.LightGreen, 65, 10, 170, 150);
         e.Graphics.DrawEllipse(Pens.Brown, 65, 10, 170, 150);
         for(byte i = 0; i < 5; i++)
            e.Graphics.FillEllipse(Brushes.Red, apple[i].X - 12, apple[i].Y - 12, 24, 24);
      }
      void MainFormKeyPress(object sender, KeyPressEventArgs e)
      {
         if(e.KeyChar == (char)27) Application.Exit();
      }
      void LoadApples()
      {
         for(byte i = 0; i < 5; i++)
         {
            do
            {
               double R = 75.0 * rnd.NextDouble(),
               angle = 2.0 * Math.PI * rnd.NextDouble();
               apple[i] = new Point(150 + (int)(R * Math.Cos(angle)),
                                  85 + (int)(R * Math.Sin(angle)));
               clash = false;
               for(byte j = 0; j < i; j++)
                  if((apple[i].X - apple[j].X) * (apple[i].X - apple[j].X) +
                     (apple[i].Y - apple[j].Y) * (apple[i].Y - apple[j].Y) < 700)
                     clash = true;
            }
            while(clash);
         }
      }
      void Falling(object sender, EventArgs e)
      {
         int i = 0;
         while(i < 5 && apple[i].Y > 275) i++;
         if(i < 5)
            apple[i].Y += 12;
         else
            LoadApples();
         this.Invalidate();
      }
   }
}


Animation of apple tree.zip
(39.92 KiB) 134 пъти
Аватар
Добромир Глухаров
Математик
 
Мнения: 2080
Регистриран на: 11 Яну 2010, 13:23
Рейтинг: 2178

Re: Анимация

Мнениеот Добромир Глухаров » 03 Мар 2021, 22:56

Впрочем ябълките не трябва да падат с постоянна скорост, а равноускорително, така, че се налагат леки изменения:

Код: Избери целия код
/*
 * Created by SharpDevelop.
 * User: Добромир
 * Date: 3.3.2021 г.
 * Time: 17:55
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Animation_of_apple_tree
{
   /// <summary>
   /// Description of MainForm.
   /// </summary>
   public partial class MainForm : Form
   {
      Random rnd;
      Point[] apple;
      bool clash;
      Timer tmr;
      byte numberOfFallingApple;
      int fallingVelocity;
      public MainForm()
      {
         //
         // The InitializeComponent() call is required for Windows Forms designer support.
         //
         InitializeComponent();
         
         //
         // TODO: Add constructor code after the InitializeComponent() call.
         //
         rnd = new Random();
         apple = new Point[5];
         LoadApples();
         tmr = new Timer();
         tmr.Enabled = true;
         tmr.Interval = 700;
         tmr.Tick += new EventHandler(Falling);
         numberOfFallingApple = 0;
         fallingVelocity = 0;
      }
      void MainFormPaint(object sender, PaintEventArgs e)
      {
         e.Graphics.FillRectangle(Brushes.Tan, 130, 150, 40, 140);
         e.Graphics.DrawRectangle(Pens.Brown, 130, 150, 40, 140);
         e.Graphics.FillEllipse(Brushes.LightGreen, 65, 10, 170, 150);
         e.Graphics.DrawEllipse(Pens.Brown, 65, 10, 170, 150);
         for(byte i = 0; i < 5; i++)
            e.Graphics.FillEllipse(Brushes.Red, apple[i].X - 12, apple[i].Y - 12, 24, 24);
      }
      void MainFormKeyPress(object sender, KeyPressEventArgs e)
      {
         if(e.KeyChar == (char)27) Application.Exit();
      }
      void LoadApples()
      {
         for(byte i = 0; i < 5; i++)
         {
            do
            {
               double R = 75.0 * rnd.NextDouble(),
               angle = 2.0 * Math.PI * rnd.NextDouble();
               apple[i] = new Point(150 + (int)(R * Math.Cos(angle)),
                                  85 + (int)(R * Math.Sin(angle)));
               clash = false;
               for(byte j = 0; j < i; j++)
                  if((apple[i].X - apple[j].X) * (apple[i].X - apple[j].X) +
                     (apple[i].Y - apple[j].Y) * (apple[i].Y - apple[j].Y) < 700)
                     clash = true;
            }
            while(clash);
         }
      }
      void Falling(object sender, EventArgs e)
      {
         apple[numberOfFallingApple].Y += fallingVelocity;
         fallingVelocity++;
         if(apple[numberOfFallingApple].Y > 260)
         {
            numberOfFallingApple++;
            fallingVelocity = 0;
            if(numberOfFallingApple > 4)
            {
               numberOfFallingApple = 0;
               LoadApples();
            }
         }
         this.Invalidate();
      }
   }
}


Animation of apple tree.zip
(40.1 KiB) 98 пъти
Аватар
Добромир Глухаров
Математик
 
Мнения: 2080
Регистриран на: 11 Яну 2010, 13:23
Рейтинг: 2178


Назад към C#, Java



Кой е на линия

Регистрирани потребители: Google [Bot]

Форум за математика(архив)