namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool monsterIsDead = false;
        int currentScore = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void clicked(object sender, EventArgs e)
        {
        }

        private void pushButton_Click(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (collide(label1, label2))
            {
                monsterIsDead = true;
                currentScore++;
                label3.Text = "Score : " + currentScore;
            }

            if (monsterIsDead)
            {
                replaceMonster(this.label2);
                monsterIsDead = false;
            }
            else
            {
                moveMonster(this.label2);
            }
        }

        private bool collide(Label label1, Label label2)
        {
            if ( label1.Location.X + label1.Size.Width >= label2.Location.X && label1.Location.Y + label1.Size.Height >= label2.Location.Y ) {
                return true;
            }

            return false;
        }

        private void moveMonster(Label label)
        {
            Random rand = new Random();
            int moveX = rand.Next(-4, 4);
            int moveY = rand.Next(-4, 4);
            label.SetBounds(label.Location.X + moveX, label.Location.Y + moveY, label.Size.Width, label.Size.Height);
        }

        private void replaceMonster(Label label)
        {
            Random rand = new Random();
            int respownX = rand.Next(this.Size.Width);
            int respownY = rand.Next(this.Size.Height);
            label.SetBounds(respownX, respownY, label.Size.Width, label.Size.Height);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                label1.SetBounds(label1.Location.X, label1.Location.Y - 5, label1.Size.Width, label1.Size.Height);
            }
            else if (e.KeyCode == Keys.Down)
            {
                label1.SetBounds(label1.Location.X, label1.Location.Y + 5, label1.Size.Width, label1.Size.Height);
            }
            else if (e.KeyCode == Keys.Left)
            {
                label1.SetBounds(label1.Location.X - 5, label1.Location.Y, label1.Size.Width, label1.Size.Height);
            }
            else if (e.KeyCode == Keys.Right)
            {
                label1.SetBounds(label1.Location.X + 5, label1.Location.Y, label1.Size.Width, label1.Size.Height);
            }
        }
    }
}

Retrieved from http://wiki.zeropage.org/wiki.php/데블스캠프2012/넷째날/묻지마Csharp/서민관
last modified 2021-02-07 05:29:15