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 = "暂停游戏";
            }
        }

    }
}

 

阅读更多
个人分类: C# Winform
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭