Menu



Manage

Study_C# > 8/Form1.cs Lines 116 | 3.6 KB
다운로드

                        using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Rectangle;
        private void MakeBlack_Click(object sender, EventArgs e)
        {
            var x = (pnlMain.Size.Width / 2) - (30 / 2);
            var y = (pnlMain.Size.Height - 40);

            //info
            Rectangle black = new Rectangle(x,y,30,30);

            //view
            Label blackLabel = new Label();
            blackLabel.AutoSize = false;
            blackLabel.Size = new Size(black.Width, black.Height);
            blackLabel.BackColor = Color.Black;

            this.pnlMain.Controls.Add(blackLabel);
            blackLabel.Location = new System.Drawing.Point(black.X,black.Y);
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            black.MoveY(10);
            //blackLable.Location = new System.Drawing.Point(MakeBlack.GetX(), black.GetY());
            SetBlackLocaton();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            black.MoveY(-10);
            //blackLable.Location = new System.Drawing.Point(MakeBlack.GetX(), black.GetY());
            SetBlackLocaton();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            MakeBlack.MoveX(-10);
            if(black.GetX() < 0)
            {
                MakeBlack.MoveX(10);
            }
            SetBlackLocaton();
        }

        private void SetBlackLocaton()
        {
            blackLable.Location = new System.Drawing.Point(MakeBlack.X, black.Y));
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            MakeBlack.MoveX(10);
            if (black.X +black.Width > pnlMain.Size.Width)
            {
                MakeBlack.MoveX(10);
            }
            SetBlackLocaton();
        }
        List<Rectangle> reds = new List<Rectangle>();
        List<Label> redlables = new List<Label>();
        private void btnMakeRed_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                int y = 20 + rand.Next(-2,2); //Next 오차범위 랜덤 쓸때 자주씀
                int x = 20 + (40 * i) + rand.Next(-2, 20);
                Rectangle r = new Rectangle();

                Label l = new Label();
                l.AutoSize = false;
                l.Size = new Size(r.Width, r.Height);
                l.BackColor = Color.Yellow;
                l.ForeColor = Color.Red;
                l.Text = (i + 1).ToString();
                l.TextAlign = ContentAlignment.MiddleCenter;

                pnlMain.Controls.Add(l);
                l.Location = new System.Drawing.Point(r.X, r.Y);

                reds.Add(r);
                redlables.Add(l);
            }
            tmrRed.Enabled = true;
        }

        private void tmrRed_Tick(object sender, EventArgs e)
        {
            for(int i=0; i<reds.Count; i++) //리스트는 카운트로 숫자구함 렝쓰는 배열만
            {
                reds[i].MoveX(rand.Next(-5, 5));
                reds[i].MoveY(rand.Next(0,14));

                redlables[i].Location = new System.Drawing.Point(reds[i].X, reds[i].Y);
            }
        }
    }
}