C#键击游戏

  闲来无事,利用空余时间写了个C#的N小的姑且算做游戏吧,主要功能就是:移动下方方块,按字母A-Z发送子弹打击下落的物体,当物体与子弹相撞且字母相等时,相应下落物体消失,同时+1分游戏分!功能很简单,希望博友多多提意见,改进!

源码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.IO;
namespace WinGameTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int pnlTopRightX = 0;//面板右上角X坐标
        private int count = 0;//分数
        private SoundPlayer play = new SoundPlayer();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.pnlTopRightX = this.pnl.Size.Width;

        }

        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="x">声音类别</param>
        private void PlayWav(int x)
        {
            Stream file;
            if (x == 1)
                file = WinGameTest.Properties.Resources._1;
            else if (x == 2)
                file = WinGameTest.Properties.Resources._2;
            else
                file = WinGameTest.Properties.Resources.prop;
            play.Stream = file;
            play.LoadAsync();
            play.Play();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.NumPad4) //向左
            {
                if (this.btn.Location.X > 0)
                {
                    Int32 temp = this.btn.Location.X;
                    temp -= 5;
                    this.btn.Location = new Point(temp, this.btn.Location.Y);
                }

            }
            else if (e.KeyData == Keys.NumPad6) //向右
            {
                if (this.btn.Location.X < this.pnlTopRightX - this.btn.Width)
                {
                    Int32 temp = this.btn.Location.X;
                    temp += 5;
                    this.btn.Location = new Point(temp, this.btn.Location.Y);
                }
            }
            else if (e.KeyValue >= 65 && e.KeyValue <= 90) //攻击
            {
                Point point = new Point(this.btn.Location.X, this.btn.Location.Y - this.btn.Height);
                GetButton(point, Color.Blue, "mov", e.KeyData.ToString());
                PlayWav(1);
            }
        }

        /// <summary>
        /// 生成button
        /// </summary>
        /// <param name="p">坐标位置</param>
        /// <param name="c">背景颜色</param>
        /// <param name="tag">Tag</param>
        /// <param name="txt">文不值</param>
        private void GetButton(Point p, Color c, string tag, string txt)
        {
            Button button = new Button();
            button.Size = this.btn.Size;
            button.BackColor = c;
            button.Location = p;
            button.Tag = tag;
            button.Text = txt;
            button.Enabled = false;
            button.FlatStyle = FlatStyle.Flat;
            this.pnl.Controls.Add(button);
        }

        //移动按键激发出的button(移动子弹)
        private void timer_Tick(object sender, EventArgs e)
        {
            foreach (Control con in this.pnl.Controls)
            {
                if (con.Tag != null)
                {
                    if (con.Tag.ToString() != "mov")
                        continue;
                    Button Btemp = (Button)con;
                    if (Btemp.Location.Y > 0)
                    {
                        Int32 temp = Btemp.Location.Y;
                        temp -= this.tbN.Value;
                        Btemp.Location = new Point(Btemp.Location.X, temp);
                    }
                    else
                    {
                        this.pnl.Controls.Remove(con);
                    }
                }
            }
            //Console.WriteLine(this.pnl.Controls.Count);
        }

        //自动生成Button
        private void Atimer_Tick(object sender, EventArgs e)
        {
            Random ran = new Random();
            int x = ran.Next(0, this.pnlTopRightX - this.btn.Width);
            int y = this.btn.Height;
            char value = Convert.ToChar(ran.Next(65, 91));
            Point p = new Point(x, y);
            GetButton(p, Color.Yellow, "uf", value.ToString());

        }

        //判断条件
        private void Ctimer_Tick(object sender, EventArgs e)
        {
            List<Control> Nlist = new List<Control>();
            List<Control> Mlist = new List<Control>();
            foreach (Control Ncon in this.pnl.Controls)
            {
                if (Ncon.Tag != null)
                {
                    if (Ncon.Tag.ToString() != "mov")
                        continue;
                    Button Ntemp = (Button)Ncon;
                    foreach (Control Mcon in this.pnl.Controls)
                    {
                        if (Mcon.Tag != null)
                        {
                            if (Mcon.Tag.ToString() != "uf")
                                continue;
                            Button Mtemp = (Button)Mcon;
                            if (Ntemp.Location.Y <= Mtemp.Location.Y + Mtemp.Height)//物体相碰条件
                            {
                                if (Ntemp.Location.X >= Mtemp.Location.X && Ntemp.Location.X <= Mtemp.Location.X + Mtemp.Size.Width    //物体相碰条件续
                                    || Mtemp.Location.X >= Ntemp.Location.X && Mtemp.Location.X <= Ntemp.Location.X + Ntemp.Size.Width)
                                {
                                    if (Ntemp.Text.Equals(Mtemp.Text)) //判断文本相等
                                    {
                                        this.count++;
                                        Nlist.Add(Ncon);
                                        Mlist.Add(Mcon);
                                        PlayWav(2);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            this.lbl.Text = this.count.ToString();
            //移除控件
            ClearControl(Nlist);
            ClearControl(Mlist);

        }

        /// <summary>
        /// 向下移动(移动障碍物)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Mtimer_Tick(object sender, EventArgs e)
        {
            foreach (Control con in this.pnl.Controls)
            {
                if (con.Tag != null)
                {
                    if (con.Tag.ToString() != "uf")
                        continue;
                    Button Btemp = (Button)con;
                    if (Btemp.Location.Y + Btemp.Height < this.pnl.Height - this.btn.Height)
                    {
                        Int32 temp = Btemp.Location.Y;
                        temp += this.tbM.Value;
                        Btemp.Location = new Point(Btemp.Location.X, temp);
                    }
                    else
                    {
                        IsStartGameState(false);
                        this.btnStop.Enabled = false;
                        MessageBox.Show("游戏结束!");
                    }
                }
            }
        }

        /// <summary>
        /// 是否启用当前进度同时启用键盘操作
        /// </summary>
        /// <param name="flag">true为启用,false为终止</param>
        private void IsStartGameState(bool flag)
        {
            this.timer.Enabled = flag;
            this.Atimer.Enabled = flag;
            this.Ctimer.Enabled = flag;
            this.Mtimer.Enabled = flag;
            this.KeyPreview = flag;
        }

        //重新开始
        private void btnRestart_Click(object sender, EventArgs e)
        {
            PlayWav(3);
            if (this.btnRestart.Text == "开始游戏")
            {
                IsStartGameState(true);
                this.btnStop.Enabled = true;
                this.btnRestart.Text = "重新开始";
            }
            else
            {
                //终止当前进度同时禁用键盘操作
                IsStartGameState(false);
                //预处理
                this.tbN.Value = 30;
                this.tbM.Value = 10;
                this.count = 0;
                this.lbl.Text = "0";
                this.btnStop.Text = "暂停游戏";
                this.btnStop.Enabled = true;
                //移除除方块外的所有控件
                List<Control> list = new List<Control>();
                foreach (Control con in this.pnl.Controls)
                {
                    if (con.Tag != null)
                    {
                        list.Add(con);
                    }
                }
                ClearControl(list);
                //重新载入
                //将方块放入画布中央
                this.btn.Location = new Point(this.pnl.Width / 2, this.btn.Location.Y);
                //开启
                IsStartGameState(true);
            }
        }

        /// <summary>
        /// 移除包含在list列表里的所有控件
        /// </summary>
        /// <param name="list">控件列表</param>
        private void ClearControl(List<Control> list)
        {
            if (list.Count == 0)
                return;
            for (int i = 0; i < list.Count; i++)
            {
                this.pnl.Controls.Remove(list[i]);
            }
        }

        //暂停游戏
        private void btnStop_Click(object sender, EventArgs e)
        {
            if (this.btnStop.Text == "暂停游戏")
            {
                //终止当前进度同时禁用键盘操作
                IsStartGameState(false);
                this.btnStop.Text = "继续游戏";
            }
            else
            {
                //开启
                IsStartGameState(true);
                this.btnStop.Text = "暂停游戏";
            }
        }

    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值