鱼C论坛

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

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

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

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

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

x
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;

  10. namespace 打砖块
  11. {
  12.     public partial class Form_Main : Form
  13.     {
  14.         Thread T;
  15.         Thread O;
  16.         public Form_Main()
  17.         {
  18.             InitializeComponent();
  19.             Control.CheckForIllegalCrossThreadCalls = false;
  20.         }
  21.         bool pd = false;  //判断是否点击滑杆
  22.         int X = 0;   //滑杆上鼠标X值
  23.         //  int speed = 0; //速度
  24.         int o_x = -8;  //球的X坐标
  25.         int o_y = -8;  //球的Y坐标
  26.         //滑杆上鼠标按下
  27.         private void lbl___MouseDown(object sender, MouseEventArgs e)
  28.         {
  29.             pd = true;
  30.             X = e.X;
  31.         }
  32.         //滑杆上鼠标弹起
  33.         private void lbl___MouseUp(object sender, MouseEventArgs e)
  34.         {
  35.             pd = false;
  36.         }
  37.         //滑杆上鼠标移动
  38.         private void lbl___MouseMove(object sender, MouseEventArgs e)
  39.         {
  40.             _shift(e);
  41.         }

  42.         //滑杆运动
  43.         private void _shift(MouseEventArgs e)
  44.         {
  45.             if (pd)
  46.             {

  47.                 if (lbl__.Location.X + lbl__.Width + 1 > panel.Width)
  48.                 {
  49.                     lbl__.Location = new Point(panel.Width-lbl__.Width-1, lbl__.Location.Y);
  50.                     return;
  51.                 }
  52.                 if (lbl__.Location.X - 2 < 0)
  53.                 {
  54.                     lbl__.Location = new Point(lbl__.Location.X + 1, lbl__.Location.Y);
  55.                     return;
  56.                 }
  57.                 if (e.X > X)
  58.                 {
  59.                     lbl__.Location = new Point(lbl__.Location.X + 1, lbl__.Location.Y);
  60.                 }
  61.                 else
  62.                 {
  63.                     lbl__.Location = new Point(lbl__.Location.X - 1, lbl__.Location.Y);
  64.                 }
  65.                 this.Refresh();
  66.             }
  67.         }

  68.         //小球的挡板
  69.         public void stop()
  70.         {
  71.             while (true)
  72.             {
  73.                 if (lbl_O.Location.X - 2 < 0)
  74.                 {
  75.                     //     lbl_O.Location = new Point(lbl_O.Location.X + 1, lbl_O.Location.Y);
  76.                     o_x *= -1;
  77.                 }
  78.                 if (lbl_O.Location.X + lbl_O.Width + 2 > panel.Width)
  79.                 {
  80.                     //      lbl_O.Location = new Point(lbl_O.Location.X - 1, lbl_O.Location.Y);
  81.                     o_x *= -1;
  82.                 }
  83.                 if (lbl_O.Location.Y - 2 < 0)
  84.                 {
  85.                     //      lbl_O.Location = new Point(lbl_O.Location.X, lbl_O.Location.Y + 1);
  86.                     o_y *= -1;
  87.                 }
  88.                 if ((lbl_O.Location.X > lbl__.Location.X && lbl_O.Location.X < lbl__.Location.X + lbl__.Width)
  89.                     && (lbl_O.Location.Y + lbl_O.Height) > (panel.Height - lbl_O.Height))
  90.                 {
  91.                     o_y *= -1;
  92.                 }
  93.                 if ((lbl_O.Location.X < lbl__.Location.X || lbl_O.Location.X > lbl__.Location.X + lbl__.Width)
  94.                     && (lbl_O.Location.Y + lbl_O.Height) > (panel.Height - lbl_O.Height))
  95.                 {
  96.                     lbl_O.Location = new Point(panel.Width / 2, lbl_O.Location.Y - 50);
  97.                     o_x *= -1;
  98.                     o_y *= -1;
  99.                     MessageBox.Show("失败");
  100.                     return;
  101.                 }
  102.                 Thread.Sleep(50);
  103.                 lbl_O.Location = new Point(lbl_O.Location.X + o_x, lbl_O.Location.Y + o_y);
  104.                 this.Refresh();
  105.             }

  106.         }

  107.         //球的闪烁
  108.         private void O_O()
  109.         {
  110.             while (true)
  111.             {
  112.                 Thread.Sleep(200);
  113.                 if (lbl_O.Checked == true)
  114.                 {
  115.                     lbl_O.Checked = false;
  116.                 }
  117.                 else
  118.                 {
  119.                     lbl_O.Checked = true;
  120.                 }
  121.                 this.Refresh();
  122.             }
  123.         }

  124.         private void btn_Go_Click(object sender, EventArgs e)
  125.         {


  126.             //开启进程
  127.             O = new Thread(O_O);
  128.             T = new Thread(stop);
  129.             O.Start();
  130.             T.Start();
  131.         }

  132.         private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
  133.         {
  134.             //结束进程
  135.             if (T != null)
  136.             {
  137.                 T.Abort();
  138.             }
  139.             if (O != null)
  140.             {
  141.                 O.Abort();
  142.             }
  143.         }


  144.         protected override bool ProcessDialogKey(Keys keyData)
  145.         {

  146.             switch (keyData)
  147.             {
  148.                 case Keys.Right:
  149.                     if (lbl__.Location.X + lbl__.Width + 2 > panel.Width)
  150.                     {
  151.                         lbl__.Location = new Point(panel.Width- lbl__.Width, lbl__.Location.Y);
  152.                         break;
  153.                     }
  154.                     lbl__.Location = new Point(lbl__.Location.X + 8, lbl__.Location.Y);
  155.                     break;
  156.                 case Keys.Left:
  157.                     if (lbl__.Location.X - 2 < 0)
  158.                     {
  159.                         lbl__.Location = new Point(0, lbl__.Location.Y);
  160.                         break;
  161.                     }
  162.                     lbl__.Location = new Point(lbl__.Location.X -8 , lbl__.Location.Y);
  163.                     break;
  164.             }
  165.             return true;
  166.         }


  167.     }
  168. }
复制代码
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-4-27 01:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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