鱼C论坛

 找回密码
 立即注册
查看: 3166|回复: 2

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

[复制链接]
发表于 2012-2-29 00:24:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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#源码如下
游客,如果您要查看本帖隐藏内容请回复

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-2-1 15:51:20 | 显示全部楼层
快快快快快快快快快快
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2018-4-16 14:52:49 From FishC Mobile | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 09:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表