Advertisement
Flavio1234

Animação de bolas

Sep 16th, 2020
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace ProjetoImagem
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.  
  17.         string pasta_imagens = "";
  18.    
  19.  
  20.         Image img_normal; //Imagem do botão
  21.         Image img_high; // Quando o mouse passa por cima do botão
  22.         Image img_disabled; //Quando o botão está inativo
  23.  
  24.         int largura;
  25.         int altura;
  26.  
  27.  
  28.         List<cl_bola> BOLAS = new List<cl_bola>();
  29.  
  30.      
  31.  
  32.         //=======================================
  33.  
  34.         public Form1()
  35.         {
  36.             InitializeComponent();
  37.  
  38.             pasta_imagens = Application.StartupPath + @"\img\";
  39.  
  40.             //=======================================
  41.  
  42.             //carregamento da imagem do fundo
  43.            //img_fundo = Image.FromFile(pasta_imagens + "foto_1.jpg");
  44.            //pic_teste.BackgroundImage = img_fundo;
  45.  
  46.            img_normal = Image.FromFile(pasta_imagens + "btn_normal.png");
  47.            img_high = Image.FromFile(pasta_imagens + "btn_high.png");
  48.            img_disabled = Image.FromFile(pasta_imagens + "btn_disabled.png");
  49.  
  50.            //Rectangle rect = pic_teste.Bounds;
  51.  
  52.            ////MessageBox.Show(rect.X.ToString() + " - " +
  53.            //                rect.Y.ToString() + " - " +
  54.            //                rect.Width.ToString() + " - " +
  55.            //                rect.Height.ToString() + " - ");
  56.  
  57.  
  58.  
  59.  
  60.            //int largura = img_fundo.Width;
  61.            //int altura = img_fundo.Height;
  62.            //MessageBox.Show(string.Format("Largura: {0} | Altura : {1}", largura, altura));
  63.  
  64.  
  65.            
  66.         }
  67.  
  68.         //=======================================
  69.  
  70.         private void Form1_Load(object sender, EventArgs e)
  71.         {
  72.             btn_teste.BackgroundImage = img_normal;
  73.  
  74.             largura = pic_teste.Width;
  75.             altura = pic_teste.Height;
  76.  
  77.            
  78.        
  79.         }
  80.  
  81.         private void pic_teste_Click(object sender, EventArgs e)
  82.         {
  83.             MessageBox.Show("PictureBox clicada");
  84.         }
  85.  
  86.         private void button1_Click(object sender, EventArgs e)
  87.         {
  88.  
  89.             //ativa ou desativa o btn_teste
  90.             if (btn_teste.Enabled)
  91.                 btn_teste.Enabled = false;
  92.  
  93.             else
  94.                 btn_teste.Enabled = true;
  95.            
  96.  
  97.  
  98.             ////pic_teste.Location = new Point(382, 220);
  99.  
  100.             //Rectangle r_pic =  pic_teste.Bounds;
  101.             //Rectangle r_but = button2.Bounds;
  102.  
  103.             //button2.Location = new Point(r_pic.X + r_pic.Width / 2 - r_but.Width / 2,
  104.             //                             r_pic.Y + r_pic.Height / 2 - r_but.Height / 2);
  105.  
  106.             ////button2.Location = new Point(r.X, r.Y);
  107.  
  108.             ////button2.Location = new Point(r.X, r.Bottom);
  109.         }
  110.  
  111.         private void btn_teste_MouseEnter(object sender, EventArgs e)
  112.         {
  113.             //altera a imagem para high
  114.             btn_teste.BackgroundImage = img_high;
  115.         }
  116.  
  117.         private void btn_teste_MouseLeave(object sender, EventArgs e)
  118.         {
  119.             //regressa ao aspeto noromal do botão
  120.             btn_teste.BackgroundImage = img_normal;
  121.         }
  122.  
  123.         private void btn_teste_Click(object sender, EventArgs e)
  124.         {
  125.  
  126.        
  127.  
  128.             //criar a folha em branco
  129.             //Bitmap imgback = new Bitmap(pic_teste.Width, pic_teste.Height);
  130.             //Graphics desenhador = Graphics.FromImage(imgback);
  131.             //Image bola = Image.FromFile(pasta_imagens + "bola_pequena.png");
  132.  
  133.             //Pen lapis = new Pen(Color.LightGray, 5);
  134.  
  135.             ////atribuir uma cor de fundo da imagem
  136.             //desenhador.Clear(Color.Black);
  137.             //desenhador.DrawImage(bola, new Rectangle(100, 100, 50, 50), new Rectangle(0, 0, 50, 50), GraphicsUnit.Pixel);
  138.  
  139.  
  140.             ////cortar e colar uma porção de imagens
  141.             //desenhador.DrawImage(original, new Rectangle(50, 50, 50, 50), new Rectangle(90, 100, 50, 50), GraphicsUnit.Pixel);
  142.             //desenhador.DrawImage(original, new Rectangle(150, 50, 100, 100), new Rectangle(90, 100, 50, 50), GraphicsUnit.Pixel);
  143.  
  144.  
  145.             //string texto = "Lorem Ipsum é simplesmente uma simulação de texto da indústria tipográfica e de impressos, e vem sendo utilizado desde o século XVI.";
  146.             //Brush cor = new SolidBrush(Color.Black);
  147.             //Font letra = new Font("Ariarl", 12, FontStyle.Regular, GraphicsUnit.Pixel);
  148.             //Rectangle rect = new Rectangle(50, 50, 500, 100);
  149.  
  150.             //StringFormat alinhamento = new StringFormat();
  151.             //alinhamento.Alignment = StringAlignment.Center;
  152.             //alinhamento.LineAlignment = StringAlignment.Center;
  153.  
  154.  
  155.             //desenhador.DrawRectangle(lapis, rect);
  156.             //desenhador.DrawString(texto, letra, cor, rect, alinhamento);
  157.  
  158.  
  159.  
  160.             //string texto = "Ola mundo";
  161.             //Font letra = new Font("Courrier", 40, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Pixel);
  162.  
  163.             //Brush cor_principal = new SolidBrush(Color.FromArgb(200, 180, 0));
  164.             //Brush cor_sombra = new SolidBrush(Color.Black);
  165.  
  166.  
  167.             //Point inicio = new Point(50, 50);
  168.             //Point inicio_sombra = new Point(50, 55);
  169.  
  170.             //desenhador.DrawString(texto, letra, cor_sombra, inicio_sombra);
  171.             //desenhador.DrawString(texto, letra, cor_principal, inicio);
  172.  
  173.             //Pen lapis = new Pen(Color.Black, 3);
  174.             //Pen lapis_vermelho = new Pen(Color.Red);
  175.            
  176.  
  177.             ////desenhar linha quebrada
  178.             //Point[] pontos = new Point[]
  179.             //{
  180.             //    new  Point(50, 50),
  181.             //    new Point(100, 80),
  182.             //    new Point(100, 150)
  183.             //};
  184.  
  185.             //desenhador.DrawLine(lapis, pontos);
  186.  
  187.  
  188.           //  //circuferência
  189.           //desenhador.DrawEllipse(lapis, new Rectangle(100, 100, 200, 200));
  190.           //desenhador.DrawRectangle(lapis_vermelho, new Rectangle(100, 100, 200, 200));
  191.  
  192.             //Brush pincel_1 = new SolidBrush(Color.Red);
  193.             //Brush pincel_2 = new LinearGradientBrush(new Point(150, 50), new Point(250, 50), Color.Red, Color.Yellow);
  194.             //Brush pincel_3 = new SolidBrush(Color.Yellow);
  195.  
  196.             //desenhador.FillRectangle(pincel_1, new Rectangle(50, 50, 100, 250));
  197.             //desenhador.FillRectangle(pincel_2, new Rectangle(150, 50, 100, 250));
  198.             //desenhador.FillRectangle(pincel_3, new Rectangle(250, 50, 100, 250));
  199.  
  200.             //desenhador.FillEllipse(pincel_1, new Rectangle(50, 50, 500, 200));
  201.  
  202.             //Rectangle rec = new Rectangle(50, 50, 300, 200);
  203.             //desenhador.FillRectangle(pincel_3, rec);
  204.             //desenhador.DrawRectangle(lapis, rec);
  205.  
  206.  
  207.             //apresenta a imagem final
  208.            // pic_teste.BackgroundImage = imgback;
  209.  
  210.            
  211.         }
  212.  
  213.         private void Desenhar()
  214.         {
  215.              //criar a folha em branco
  216.            Bitmap imgback = new Bitmap(pic_teste.Width, pic_teste.Height);
  217.             Graphics desenhador = Graphics.FromImage(imgback);
  218.             Image img_bola = Image.FromFile(pasta_imagens + "bola_pequena.png");
  219.  
  220.             Pen lapis = new Pen(Color.LightGray, 5);
  221.  
  222.             //atribuir uma cor de fundo da imagem
  223.             desenhador.Clear(Color.Black);
  224.  
  225.             //ciclo que percorre todas as bolas percorrentes
  226.             foreach (cl_bola bola in BOLAS)
  227.             {
  228.                 bola.Mover(largura, altura);
  229.                 desenhador.DrawImage(img_bola, new Rectangle(bola.x, bola.y, 50, 50), new Rectangle(0, 0, 50, 50), GraphicsUnit.Pixel);
  230.             }
  231.  
  232.          
  233.             //apresenta a imagem final
  234.             pic_teste.BackgroundImage = imgback;
  235.  
  236.         }
  237.  
  238.         private void btn_teste_EnabledChanged(object sender, EventArgs e)
  239.         {
  240.             //define a imagem ativa ou inativa do btn_teste
  241.             if (btn_teste.Enabled)
  242.                 btn_teste.BackgroundImage = img_normal;
  243.             else
  244.                 btn_teste.BackgroundImage = img_disabled;
  245.         }
  246.  
  247.  
  248.         public Pen lapis { get; set; }
  249.  
  250.         private void tempo_Tick(object sender, EventArgs e)
  251.         {
  252.  
  253.             Desenhar();
  254.  
  255.  
  256.  
  257.  
  258.  
  259.             //x += velocidade_x;
  260.             //y += velocidade_y;
  261.  
  262.             ////colisões
  263.             ////detecta colisão do fundo
  264.             //if (y + 50 >= pic_teste.Height)
  265.             //    velocidade_y = -velocidade_y;
  266.             ////detecta colisão no top
  267.             //if (y <= 0)
  268.             //    velocidade_y = -velocidade_y;
  269.             ////detecta colisão a esquerda
  270.             //if (x <= 0)
  271.             //    velocidade_x = -velocidade_x;
  272.             ////detecta colisão da direita
  273.             //if (x + 50 >= pic_teste.Width)
  274.             //    velocidade_x = -velocidade_x;
  275.  
  276.  
  277.  
  278.             //mover a imagem
  279.             //x += 20;
  280.             //Desenhar();
  281.             //if (x >= 770)
  282.             //    tempo.Enabled = false;
  283.            
  284.            
  285.         }
  286.  
  287.         private void button2_Click(object sender, EventArgs e)
  288.         {
  289.             cl_bola b = new cl_bola();
  290.             BOLAS.Add(b);
  291.             tempo.Enabled = true;
  292.          
  293.         }
  294.  
  295.    
  296.     }
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement