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;

        }
    }
}


沒有留言:

張貼留言