2017年5月2日 星期二

Unit 11 按鍵

Unit 11A 按鍵控制
作業
按左右鍵會增加或減少5
按上下可以增加或減少砲管r長度3
按空白鍵可以發射直徑為10的砲彈,發射口為砲管口(x,y),
0.1x位置為x+r*cos(a),y位置為y-r*sin(a)+0.5*10*t*t,其中t為累計時間

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; //擴增定義,線型
namespace 按鍵01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //宣告變數,畫筆顏色,亂數
        int x1 = 100, y1 = 600, x2, y2, a=90, r1=60, r2,p=0,q=0;
        int x3 = 300, y3 = 300, r3 = 100;
        double t = 0.1;
        Pen pe1, pe2; //線型顏色線寬樣式
        Random rd = new Random();

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 1024; this.Height = 640;
            pe1 = new Pen(Color.Purple, 10);
            //最前面新增using System.Drawing.Drawing2D;
            //線結束端點.EndCap樣式,起始端點.StartCap
            pe1.EndCap = LineCap.SquareAnchor;//Drawing2D
            pe1.StartCap = LineCap.SquareAnchor;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
                        //.FillRectangle(Brushes.色彩, 左上x, 左上y, , )
            e.Graphics.FillRectangle(Brushes.Gold, x1 - 60, y1 - 20, 60 * 2, 30);
            e.Graphics.FillRectangle(Brushes.Goldenrod, x1 - 30, y1 - 40, 30 * 2, 20);
            //1=1*3.1416/180 弳度
            x2 = x1+(int)(r1 * Math.Cos(a * Math.PI / 180)); //用三角函數算,轉換成整數
            y2 = y1-50-(int)(r1 * Math.Sin(a * Math.PI / 180));
            e.Graphics.DrawLine(pe1, x1, y1 - 50, x2, y2);
            e.Graphics.FillEllipse(Brushes.Black,x2-5+p,y2-5-q,10,10);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //e.KeyCode:按鍵碼
            if (e.KeyCode == Keys.Left)
                a=a+10;
            else if(e.KeyCode==Keys.Right)
               a = a-10;
            if (e.KeyCode == Keys.Up)
            r1 = r1 + 3;
            else if (e.KeyCode == Keys.Down)
            r1 = r1 - 3;
            if (e.KeyCode == Keys.Space)
                timer1.Enabled = true;
            this.Refresh();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            p = x2 + (int)(r1 * Math.Cos(a * Math.PI / 180));
            q = y2 -50- (int)(r1 * Math.Sin(a * Math.PI / 180)+0.5*10*t*t) ;
            t += 0.1;
            this.Refresh();
        }
    }
}


沒有留言:

張貼留言