2017年5月30日 星期二

Unit 15A 類別定義與動畫

Unit 15A 類別定義與動畫(5%~10%)
1.      點按滑鼠,在滑鼠點按位置(圓中心)畫圓(3%)
2.      產生連續爆炸圖(3%)
3.      產生爆炸圖兩次(2%)
4.      點按滑鼠,在滑鼠點按位置產生爆炸圖兩次(3%)
5.      兩物體碰撞後產生爆炸圖(3%)
6.      顯示影像
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;

namespace 爆炸01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Image im = Properties.Resources.fire;
        int x, y; //滑鼠點按位置
        Random rd = new Random();
        int w, h; //小圖寬高
        //宣告放小圖的二維陣列
        Rectangle[,] re = new Rectangle[3, 5];
        int r = 0, c = 0; //小圖陣列註標位置
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(8, 8);
            w = im.Width / 5; h = im.Height / 3;
            //切割影像放在二維矩形
            for (int i= 0;i<3;i++)
                for (int j = 0; j < 5; j++)
                    re[i, j] = new Rectangle(j * w, i * h, w, h);

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            e.Graphics.DrawImage(im, 300, 200, re[r,c],
                GraphicsUnit.Pixel);
        }
    }
}



2017年5月23日 星期二

Unit 14A 類別定義與動畫
「類別」可以有很多屬性,有「類別」比較好管理
Ex:球具有圓心、顏色、穿透性(反彈係數)…靜態特性
專案>加入類別 bomb.cs
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;

namespace 類別1
{
    class bomb
    {
        public int x, y, r,mx,my, w, h; //類別成員變數: 圓心、直徑、移動方向、視窗寬高
        public Brush br; //顏色非整數
        public bomb() { } //宣告類別變數,空的建構元constructor
        public bomb(int x1, int y1, int r1, int mx1, int my1,Brush br1, int w1, int h1) //指定初值
        {
            x = x1; y = y1; r = r1; mx=mx1 ; my=my1; br = br1; w = w1; h = h1;//右邊指定給左邊
        }
        public void drawC(Graphics g)//畫圓要有畫布
        {
            g.FillEllipse(br,x-r/2,y-r/2,r,r); //(x,y)圓心
        }
        public void move()
        {
            x=x+mx; y=y+my;//mx:移動的x方向距離
            if (x+r/2>w-3) //碰右牆
                mx=-mx;
            else if (x-r/2<0) //碰左牆
                mx=-mx;
            else if (y+r/2>h) //碰視窗下緣
                my=-my;
            else if (y-r/2<0) //碰視窗上緣
                my=-my;
        }
    }
}

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;

namespace 類別1//主程式
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
            Random rd = new Random(); //定義亂數
           
            bomb[] bo = new bomb[5]; //很多個球用中括號
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(5, 10);
            for (int i=0;i<bo.Length; i++)    //陣列初值for迴圈
                bo[i]=new bomb(rd.Next(50,900), rd.Next(50,550),
                    rd.Next(48,120), rd.Next(-25,25),rd.Next(-25,25),
                    new SolidBrush(Color.FromArgb(rd.Next(99,255),
                        rd.Next(99,255),rd.Next(99,255))),960,640);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            for (int i = 0; i < bo.Length; i++)
                bo[i].drawC(e.Graphics);
                    }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < bo.Length; i++)
                bo[i].move();
            for (int i = 0; i < bo.Length; i++)
                for (int j = i + 1; j < bo.Length;j++ )
                    if (collide(bo[i], bo[j]))
                    {
                        bomb t = new bomb();
                        t.mx = bo[i].mx;
                        t.my = bo[i].my;
                        bo[i].mx = bo[j].my;
                        bo[i].my = bo[j].my;
                        bo[j].mx = t.mx;
                        bo[j].my = t.my;
                    }

                    this.Refresh();
        }
        bool collide(bomb b1, bomb b2) //布林值
        {
            if((b1.x - b1.r/2 < b2.x + b2.r/2 &&
                b1.x + b1.r/2 > b2.x - b2.r/2 &&
                b1.y - b1.r/2 < b2.y + b2.r/2 &&
                b1.y + b1.r/2 > b2.y - b2.r/2))
                return true;
            else
                return false;

        }
    }
}


2017年5月16日 星期二

Unit 13 小影像動畫

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;

namespace 小影像動畫1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int x=300, y=300; //x,y為小影像中心
        int r=0, c=0; //小圖列數行數
        int w, h; //大影像寬高
     
        Image im1 = Properties.Resources.wGirl;
        Rectangle[,] im = new Rectangle[4, 4];
        //設定小影像,im二維陣列,型態先給,四層樓同一個門牌號碼,
        //陣列從0開始,第0,第一,第二,第三...
        //影像變數為屬性.資源.檔案名稱
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 640;
            this.DesktopLocation = new Point(5, 10);
            //設定在桌面位置
           w = im1.Width / 4; h = im1.Height / 4;
            //小圖寬高為大圖寬高/4
            for (int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                im[i,j]=new Rectangle(j*w,i*h,w,h);
            //i為列,j為行,跑兩個for迴圈
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;
            e.Graphics.DrawImage(im1, 64, 64);
            //顯示影像局部矩形區域
            //e.Graphics.DrawImage(im, 左上x,左上y,矩形區域,GraphicsUnit.Pixel)
            e.Graphics.DrawImage(im1, x-r/2, y-c/2,im[r,c],GraphicsUnit.Pixel);
            //e表繪圖畫布變數的指令,
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c++;
            c=c%4;
            //c為0,1,2,3,0,1,2,3
            this.Refresh();
        }

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

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            { r = 1; x = x - 3; }
            else if (e.KeyCode == Keys.Right)
            { r = 2; x = x + 3; }
            if (e.KeyCode == Keys.Up)
            { r = 3; y = y - 3; }
            else if (e.KeyCode == Keys.Down)
            { r = 0; y = y + 3; }
            this.Refresh();
        }
    }
}

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();
        }
    }
}