1. 程式人生 > >C# 視窗實現打字遊戲

C# 視窗實現打字遊戲

按照慣例 首先先看一下執行之後的結果圖
這裡寫圖片描述

按相應的鍵值,發射出子彈,實現打字遊戲!

上程式碼

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.Media;

namespace sss
{
    public partial class sssss : Form
    {
        public
sssss() { InitializeComponent(); } SoundPlayer mp = new SoundPlayer("BLAM.WAV"); SoundPlayer bz = new SoundPlayer("GRAY2WEA.WAV"); static int flag2 = 0; //錯誤數 private void panel1_Paint(object sender, PaintEventArgs e) { this
.timer1.Start();//表示開啟移動的時間事件 this.timer2.Start();//開啟隨即造字的時間事件 } //bool isShowBullet = true; private void timer1_Tick(object sender, EventArgs e) { foreach (Control o in this.panel1.Controls) { if (o.GetType().Name == "Label"
)//判斷是否是對應的控制元件 { if ( o.Tag.ToString()=="over" || o.Tag.ToString()=="NoBiu") //判斷是否向字母發射子彈 { o.Top += 3; } else if (o.Tag.ToString() == "Biu") { o.Top -=8; foreach(Control o1 in this.panel1.Controls) { if (o1.GetType().Name == "Label" && o1.Tag.ToString()=="over") { if (o1.Bottom >= o.Top) { o1.Dispose(); o.Dispose(); //加爆炸 PictureBox p = new PictureBox(); p.Size = new Size(100, 100); p.Location = new Point(o1.Left+o1.Width/2-p.Left,o1.Bottom-p.Height/2); this.panel1.Controls.Add(p); Timer t = new Timer(); t.Interval = 10; t.Tag = p; t.Tick += new EventHandler(t_Tick); t.Start(); } } } } } if (o.GetType().Name == "Label") { if (((Label)o).Top >= this.panel1.Height) { ((Label)o).Dispose();//釋放資源 flag2++; this.Tecuo.Text = flag2.ToString(); } } } } int num = 0; void t_Tick(object sender, EventArgs e) { Timer tt = (Timer)sender; PictureBox pp = (PictureBox)tt.Tag; pp.Image = imageList1.Images[num]; num++; if (num == 26) { num = 0; pp.Dispose(); tt.Dispose(); } } Random ran = new Random(); static int flag1 = 0; //總數 int randon; private void timer2_Tick(object sender, EventArgs e) //產生隨機字型 { randon = ran.Next(0,1000); Label la = new Label(); la.AutoSize = true;//自動適應期內容的大小 la.Font = new Font("宋體", ran.Next(30, 70));//隨機產生字型大小 la.Tag = "NoBiu"; la.ForeColor = Color.FromArgb(ran.Next(0, 256), ran.Next(0, 256), ran.Next(0, 256));// la.Location = new Point(ran.Next(0, this.panel1.Width - la.Width), ran.Next(-10, 0)); if (randon % 2 == 0) { la.Text = ((char)ran.Next(97, 123)).ToString(); } else { la.Text = ran.Next(0, 9).ToString(); } this.panel1.Controls.Add(la);//將Label新增到裡面 flag1++; this.number.Text = flag1.ToString(); } private void sssss_Load(object sender, EventArgs e) { // comboBox1.SelectedIndex = comboBox1.SelectedIndex[0]; // comboBox1.SelectedIndex[0] = comboBox1. } static int flag = 0;// ddddd正確數 private void sssss_KeyPress(object sender, KeyPressEventArgs e) { foreach (Control o in this.panel1.Controls) { if (o.GetType().Name == "Label" && o.Tag.ToString()=="NoBiu")// { if (e.KeyChar.ToString().ToLower() == ((Label)o).Text.ToLower())//進行小寫裝換 { flag++; this.score.Text = flag.ToString(); o.Tag = "over"; pictureBox1.Location = new Point(o.Left + o.Width / 2 - pictureBox1.Width / 2, pictureBox1.Top);//移動到相應位置 Label la = new Label(); la.Text = "D"; la.Location = new Point(pictureBox1.Left + pictureBox1.Width / 2, pictureBox1.Top - la.Height); this.panel1.Controls.Add(la); la.Font = new Font("宋體", 8); la.Tag = "Biu"; mp.Play(); return; } } } } private void button1_Click(object sender, EventArgs e) { //this.Close(); Application.Exit(); } private void label2_Click(object sender, EventArgs e) { } private void timer3_Tick(object sender, EventArgs e) //用來自動的選擇難度 { foreach (Control o in this.panel1.Controls) { if (o.GetType().Name == "Label")//判斷是否是對應的控制元件 { ((Label)o).Top += 3; } if (flag1 >= 30) { if (o.GetType().Name == "Label")//判斷是否是對應的控制元件 { ((Label)o).Top += 4; } } else if (flag1 >= 30 && flag1 <= 60) { if (o.GetType().Name == "Label")//判斷是否是對應的控制元件 { ((Label)o).Top += 5; } } else if (flag1 >= 60 && flag1 <= 180) { if (o.GetType().Name == "Label")//判斷是否是對應的控制元件 { ((Label)o).Top += 7; } } else if (flag1 > 180) { if (o.GetType().Name == "Label")//判斷是否是對應的控制元件 { ((Label)o).Top += 10; } } } } private void button3_Click(object sender, EventArgs e) { timer3.Start(); timer1.Stop(); } } }

上面就是整個工程的程式碼!沒有一一講解是由於時間原因,臨近畢業時間比較緊張,還希望諒解!