90坦克第四集


/************************************计时器的代码测试********************************************/
/************************************图形界面测试代码********************************************/
/************************************滑块移动,引入键盘事件**************************************/
/************************************键盘事件引发滑块移动**************************************
/***********可以一边移动,一边发射子弹,子弹可重复发射,但操作起来不流畅********************/

using System;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace game
{
    public class test
    {
        static System.Timers.Timer timer;
        static Label l = new Label();
        static Label pl = new Label();
        static Form f = new Form();
        static int speed = 30;
        static int len = 0,dir = 4,pLife = 0,pdir = 0,plen = 0;
         private static void InitTimer()
        {
            //设置定时间隔(毫秒为单位)
            int interval = 1;
            timer = new System.Timers.Timer(interval);
            //设置执行一次(false)还是一直执行(true)
            timer.AutoReset = true;
            //设置是否执行System.Timers.Timer.Elapsed事件
            timer.Enabled = true;
            //绑定Elapsed事件
            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
        }

           public static void TimerUp(object sender, System.Timers.ElapsedEventArgs e){
            
            int left = l.Left;
            int top = l.Top;
            if(len>0){
                len--;
                switch(dir){
                    case 1:
                        top-=2;
                        break;
                    case 2:
                        top+=2;
                        break;
                    case 3:
                        left-=2;
                        break;
                    case 4:
                        left+=2;
                        break;
                }
                l.Left = left;
                l.Top = top;
            }
            int pleft = 0;
            int ptop = 0;
            if(pLife == 0){
                pleft = left;
                ptop = top;
                pdir = dir;
            }else if(pLife == 1){    
                pleft = pl.Left;
                ptop = pl.Top;
                if(pleft<40||pleft>960){
                    pLife = 0;
                }
                if(ptop<40||ptop>560){
                    pLife = 0;
                }
            }
            Console.Write(pdir);
            if(plen>0){
                if(pLife == 1){
                    plen--;
                    if(plen<=0){
                        if(pLife == 1){
                            plen = 50;
                        }
                    }
                    switch(pdir){
                        case 1:
                            ptop-=2;
                            break;
                        case 2:
                            ptop+=2;
                            break;
                        case 3:
                            pleft-=2;
                            break;
                        case 4:
                            pleft+=2;
                            break;
                    }
                }
            }
            pl.Left = pleft;
            pl.Top = ptop;
            //Console.WriteLine("nnn343=" + n);
        }
        
        public static void test2(){
            InitTimer();
             timer.Start();
        }
        
        public static void test4(){
            new Thread(delegate(){
                f.KeyDown += new KeyEventHandler(keypressed);
            }).Start();
        }
         private static void keypressed(Object o, KeyEventArgs e) {
             
             switch(e.KeyCode.ToString()){
                case "W":
                    len = speed/2;
                    dir = 1;
                    
                    break;
                case "S":
                    len = speed/2;
                    dir = 2;
                    
                    break;
                case "A":
                    len = speed/2;
                    dir = 3;
                    
                    break;
                case "D":
                    len = speed/2;
                    dir = 4;
                    
                    break;
                case "Space":
                    //0是没发射,1是发射了

                    if(pLife == 0){
                        plen = speed/2;
                        pLife = 1;
                    }
                    break;
             }
        }
        static void Main(string[] args)
           {                        
            test3();
            test4();
            test2();
            Console.Read();
        }
        
        public static void test3(){
            new Thread(delegate(){
                f.Height = 600;
                f.Width = 1000;
                f.Location = new Point(400,300);
                f.Show();
                f.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                
                l.Height = 30;
                l.Width = 30;
                l.Top = 300;
                l.Left = 400;
                l.Text = "ddd";
                l.BackColor = Color.Black;
                f.Controls.Add(l);
                
                pl.Height = 5;
                pl.Width = 5;
                pl.Top = 300 + 13;
                pl.Left = 400 + 13;
                pl.Text = "ddd";
                pl.BackColor = Color.Black;
                f.Controls.Add(pl);
                Application.Run(f);
            }).Start();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值