LD13 发表于 2012-2-29 00:24:49

C#_打方块雏形_球与滑杆_多线程

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 打砖块
{
    public partial class Form_Main : Form
    {
      Thread T;
      Thread O;
      public Form_Main()
      {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
      }
      bool pd = false;//判断是否点击滑杆
      int X = 0;   //滑杆上鼠标X值
      //int speed = 0; //速度
      int o_x = -8;//球的X坐标
      int o_y = -8;//球的Y坐标
      //滑杆上鼠标按下
      private void lbl___MouseDown(object sender, MouseEventArgs e)
      {
            pd = true;
            X = e.X;
      }
      //滑杆上鼠标弹起
      private void lbl___MouseUp(object sender, MouseEventArgs e)
      {
            pd = false;
      }
      //滑杆上鼠标移动
      private void lbl___MouseMove(object sender, MouseEventArgs e)
      {
            _shift(e);
      }

      //滑杆运动
      private void _shift(MouseEventArgs e)
      {
            if (pd)
            {

                if (lbl__.Location.X + lbl__.Width + 1 > panel.Width)
                {
                  lbl__.Location = new Point(panel.Width-lbl__.Width-1, lbl__.Location.Y);
                  return;
                }
                if (lbl__.Location.X - 2 < 0)
                {
                  lbl__.Location = new Point(lbl__.Location.X + 1, lbl__.Location.Y);
                  return;
                }
                if (e.X > X)
                {
                  lbl__.Location = new Point(lbl__.Location.X + 1, lbl__.Location.Y);
                }
                else
                {
                  lbl__.Location = new Point(lbl__.Location.X - 1, lbl__.Location.Y);
                }
                this.Refresh();
            }
      }

      //小球的挡板
      public void stop()
      {
            while (true)
            {
                if (lbl_O.Location.X - 2 < 0)
                {
                  //   lbl_O.Location = new Point(lbl_O.Location.X + 1, lbl_O.Location.Y);
                  o_x *= -1;
                }
                if (lbl_O.Location.X + lbl_O.Width + 2 > panel.Width)
                {
                  //      lbl_O.Location = new Point(lbl_O.Location.X - 1, lbl_O.Location.Y);
                  o_x *= -1;
                }
                if (lbl_O.Location.Y - 2 < 0)
                {
                  //      lbl_O.Location = new Point(lbl_O.Location.X, lbl_O.Location.Y + 1);
                  o_y *= -1;
                }
                if ((lbl_O.Location.X > lbl__.Location.X && lbl_O.Location.X < lbl__.Location.X + lbl__.Width)
                  && (lbl_O.Location.Y + lbl_O.Height) > (panel.Height - lbl_O.Height))
                {
                  o_y *= -1;
                }
                if ((lbl_O.Location.X < lbl__.Location.X || lbl_O.Location.X > lbl__.Location.X + lbl__.Width)
                  && (lbl_O.Location.Y + lbl_O.Height) > (panel.Height - lbl_O.Height))
                {
                  lbl_O.Location = new Point(panel.Width / 2, lbl_O.Location.Y - 50);
                  o_x *= -1;
                  o_y *= -1;
                  MessageBox.Show("失败");
                  return;
                }
                Thread.Sleep(50);
                lbl_O.Location = new Point(lbl_O.Location.X + o_x, lbl_O.Location.Y + o_y);
                this.Refresh();
            }

      }

      //球的闪烁
      private void O_O()
      {
            while (true)
            {
                Thread.Sleep(200);
                if (lbl_O.Checked == true)
                {
                  lbl_O.Checked = false;
                }
                else
                {
                  lbl_O.Checked = true;
                }
                this.Refresh();
            }
      }

      private void btn_Go_Click(object sender, EventArgs e)
      {


            //开启进程
            O = new Thread(O_O);
            T = new Thread(stop);
            O.Start();
            T.Start();
      }

      private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
      {
            //结束进程
            if (T != null)
            {
                T.Abort();
            }
            if (O != null)
            {
                O.Abort();
            }
      }


      protected override bool ProcessDialogKey(Keys keyData)
      {

            switch (keyData)
            {
                case Keys.Right:
                  if (lbl__.Location.X + lbl__.Width + 2 > panel.Width)
                  {
                        lbl__.Location = new Point(panel.Width- lbl__.Width, lbl__.Location.Y);
                        break;
                  }
                  lbl__.Location = new Point(lbl__.Location.X + 8, lbl__.Location.Y);
                  break;
                case Keys.Left:
                  if (lbl__.Location.X - 2 < 0)
                  {
                        lbl__.Location = new Point(0, lbl__.Location.Y);
                        break;
                  }
                  lbl__.Location = new Point(lbl__.Location.X -8 , lbl__.Location.Y);
                  break;
            }
            return true;
      }


    }
}
C#源码如下**** Hidden Message *****

/aiq____害__虫/ 发表于 2013-2-1 15:51:20

快快快快快快快快快快

yuanfen123 发表于 2018-4-16 14:52:49

1
页: [1]
查看完整版本: C#_打方块雏形_球与滑杆_多线程