注册账号 登录
小春网 返回首页

wongxp的个人空间 https://wongxp.incn.jp [收藏] [复制] [分享] [RSS]

日志

6.24 -2

已有 221 次阅读2011-6-24 15:27 |个人分类:学习|系统分类:学习

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.Threading;

namespace collision
{
    public partial class Form1 : Form
    {
        //  プレイヤーボール
        Ball pball = new Ball(Color.Red);
        //  敵ボール
        Ball eball = new Ball(Color.Purple);

        //  マウス
        BMouse bm = new BMouse(0, 0);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.UserPaint |
                     ControlStyles.AllPaintingInWmPaint, true);
            this.ClientSize = new Size(400, 400);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

            Thread thread = new Thread(new ThreadStart(ThreadOn));
            thread.Start();
        }

        private void ThreadOn()
        {
            int before = Environment.TickCount;
            int frame = 0;
            int FPS = 60;
            while (!IsDisposed)
            {
                int now = Environment.TickCount;
                int progress = now - before;
                int ideal = (int)(frame * (1000.0f / FPS));
                if (ideal > progress) Thread.Sleep(ideal - progress);
                frame++;
                Invalidate();
                if (progress > 1000)
                {
                    frame = 0;
                    before = now;
                }
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //  プレイヤー座標の更新
            pball.setNewPos();
            //  プレイヤー描画
            pball.draw(e.Graphics);
            //  直線の描画
            if (bm.bClick)
                line(e.Graphics, pball.bcpos, bm.mp, Color.Blue);
        }

        //  直線の描画
        private void line(Graphics g, PointF p1, PointF p2, Color col)
        {
            Pen pen = new Pen(col, 3);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
            g.DrawLine(pen, p1, p2);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                bm.bClick = true;
                bm.mpos = new PointD(e.X, e.Y);
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (bm.bClick)
            {
                bm.mpos = new PointD(e.X, e.Y);
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                bm.bClick = false;
                pball.setNewVelosity(bm.mpos);
            }
        }
    }
    class BMouse
    {
        //  マウスクリックフラグ
        public bool bClick = false;
        public PointD mpos;
        public PointF mp
        {
            set { mpos.X = ((PointF)value).X; mpos.Y = ((PointF)value).Y; }
            get { PointF pf = new PointF((float)mpos.X, (float)mpos.Y); return pf; }
        }
        public BMouse(double x, double y)
        {
            mpos = new PointD(x, y);
        }
    }
    //  引数がDoubleのクラスを生成
    class PointD
    {
        public double X;
        public double Y;
        public PointD(double x,double y)
        {
            X = x;
            Y = y;
        }
        public static PointD operator+ (PointD pos1, PointD pos2)
        {
            PointD pos = new PointD(0,0);
            pos.X = pos1.X + pos2.X;
            pos.Y = pos1.Y + pos2.Y;
            return pos;
        }
    }
    class Ball
    {
        PointD pos = new PointD(0,0);
        PointD mv = new PointD(0,0);
        PointD velosity = new PointD(0.5,0.5);
        Color col;
        public Ball(Color col)
        {
            this.col = col;
        }
        public PointF bpos
        {
            set { pos.X = ((PointF)value).X; pos.Y = ((PointF)value).Y; }
            get { PointF pf = new PointF((float)pos.X, (float)pos.Y); return pf; }
        }
        public PointF bcpos
        {
            get { PointF pf = new PointF((float)pos.X + 16, (float)pos.Y + 16); return pf; }
        }
        public PointF mvs
        {
            set { mv.X = ((PointF)value).X; mv.Y = ((PointF)value).Y; }
            get { PointF pf = new PointF((float)mv.X, (float)mv.Y); return pf; }
        }
        public PointF vel
        {
            set { velosity.X = ((PointF)value).X; velosity.Y = ((PointF)value).Y; }
            get { PointF pf = new PointF((float)velosity.X, (float)velosity.Y); return pf; }
        }
        public void setNewVelosity(PointD mp)
        {
            double xdif = mp.X - pos.X - 16.0;
            double ydif = mp.Y - pos.Y - 16.0;
            xdif /= 5.0;
            ydif /= 5.0;
            if (xdif != 0 || ydif != 0)
            {
                mv.X = xdif;
                mv.Y = ydif;
                double dif =
                    Math.Sqrt(xdif * xdif + ydif * ydif);
                velosity.X = Math.Abs(xdif * 0.5 / dif);
                velosity.Y = Math.Abs(ydif * 0.5 / dif);
            }
        }
        public void setNewPos()
        {
            pos += mv;
            if (mv.X != 0)
            {
                if (mv.X > 0)
                {
                    mv.X -= velosity.X;
                    if (mv.X < 0)
                        mv.X = 0;
                }
                else if (mv.X < 0)
                {
                    mv.X += velosity.X;
                    if (mv.X > 0)
                        mv.X = 0;
                }
            }
            if (mv.Y != 0)
            {
                if (mv.Y > 0)
                {
                    mv.Y -= velosity.Y;
                    if (mv.Y < 0)
                        mv.Y = 0;
                }
                else if (mv.Y < 0)
                {
                    mv.Y += velosity.Y;
                    if (mv.Y > 0)
                        mv.Y = 0;
                }
            }
            if ((pos.X > 400 - 32 && mv.X > 0) || (pos.X < 0 && mv.X < 0))
                mv.X = -mv.X;
            if ((pos.Y > 400 - 32 && mv.Y > 0) || (pos.Y < 0 && mv.Y < 0))
                mv.Y = -mv.Y;
        }
        //  円の描画
        public void draw(Graphics g)
        {
            SolidBrush brush = new SolidBrush(col);
            g.FillEllipse(brush, bpos.X, bpos.Y, 32, 32);
        }
    }
}


悲剧

无聊

震惊

支持

不解

超赞

愤怒

高兴

全部作者的其他最新日志

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册账号

小春网
常务客服微信
微信订阅号
手机客户端
扫一扫,查看更方便! 返回顶部