C#純代碼實現(xiàn)打字游戲
更新時間:2020年05月14日 08:46:05 作者:不染-何程龍
這篇文章主要介紹了這篇文章主要為大家詳細介紹了C#純代碼實現(xiàn)打字游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#實現(xiàn)打字游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、需求分析
1、界面設計 布局
- 需要哪些內(nèi)容控件
- 容器的概念
- 集合的概念
2、開始游戲
- 字母的生成 26個字母 ASCII碼值
- 字母的載體 控件
- 字母、位置、大小、顏色
- 要求隨機的
- 計時器
3、字母從上往下運動
- TOP變化
- 注意:垃圾回收問題,未消除的字母進行銷毀,釋放資源
- 計時器
4、需要產(chǎn)生對應的從下往上生成字母子彈,打掉字母
- 處理與鍵盤之間的交互,鍵盤相關(guān)事件
- 字母需要轉(zhuǎn)換,ASCII碼值
- 知識點:事件參數(shù):EventArgs e
- 子彈從下往上運動
- 子彈與字母相遇,兩者消失
5、添加動畫效果、音效等
二、代碼實現(xiàn)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//導入媒體命名空間
using System.Media;
namespace PlaneTest_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//初始化panel
Panel game = new Panel();
//初始化pictureBox
PictureBox plane = new PictureBox();
//初始化一個播放器
SoundPlayer sound = new SoundPlayer();
//創(chuàng)建爆炸效果圖
ImageList imageList = new ImageList();
//初始化隨機數(shù)對象
Random ra = new Random();
//添加timer1,控制字母生成
Timer timer1 = new Timer();
//添加timer2,控制字母、子彈運動
Timer timer2 = new Timer();
//添加timer3,控制爆炸效果圖的播放
Timer timer3 = new Timer();
//設置游戲開始按鈕
Button beginBtn = new Button();
//實現(xiàn)記錄分數(shù)
Label result = new Label();
//記錄分數(shù)
int count = 0;
//實現(xiàn)記錄失誤
Label list = new Label();
//記錄失誤
int rex = 0;
private void Form1_Load(object sender, EventArgs e)
{
//設置窗體屬性
this.Text = "天天打字母";
this.Size = new Size(1000,700);
this.CenterToScreen();
//設置game屬性
game.Size = new Size(800,650);
game.BackColor = Color.SkyBlue;
game.BorderStyle = BorderStyle.Fixed3D;
game.Location = new Point(5,5);
this.Controls.Add(game);
//設置plane屬性
plane.Image = Image.FromFile("../../images/plane/plane4.png");
plane.SizeMode = PictureBoxSizeMode.AutoSize;
plane.Location = new Point(game.Width/2-plane.Width/2,game.Height-plane.Height);
plane.Tag = "plane";
game.Controls.Add(plane);
//設置timer1屬性,此計時器控制字母生成
timer1.Tick += Timer1_Tick;
timer1.Interval = 1000;
//設置timer2屬性,此計時器控制字母運動
timer2.Tick += Timer2_Tick;
timer2.Interval = 200;
//添加鍵盤按下事件
this.KeyPress += Form1_KeyPress;
//設置按鈕屬性
beginBtn.Text = "開始游戲";
beginBtn.Size = new Size(100,30);
beginBtn.Location = new Point(game.Width+50, 20);
this.Controls.Add(beginBtn);
//生成按鈕點擊事件
beginBtn.Click += BeginBtn_Click;
//設置記錄分數(shù)label的屬性
result.Size = new Size(50,50);
result.BackColor = Color.White;
result.Location = new Point(beginBtn.Left+ beginBtn.Width/2-result.Width/2, beginBtn.Top+ beginBtn.Height+10);
result.TextAlign = ContentAlignment.MiddleCenter;
result.Font = new Font("楷體", 22);
result.ForeColor = Color.Green;
this.Controls.Add(result);
//設置記錄失誤label的屬性
list.Size = new Size(50, 50);
list.BackColor = Color.White;
list.Location = new Point(beginBtn.Left + beginBtn.Width / 2 - list.Width / 2, result.Top + result.Height + 10);
list.TextAlign = ContentAlignment.MiddleCenter;
list.Font = new Font("楷體", 22);
list.ForeColor = Color.Red;
this.Controls.Add(list);
}
/// <summary>
/// 按鈕點擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeginBtn_Click(object sender, EventArgs e)
{
if (beginBtn.Text == "開始游戲")
{
//開啟計時器
timer1.Start();
timer2.Start();
timer3.Start();
beginBtn.Text = "暫停游戲";
//開啟窗體獲取鍵盤事件的焦點
this.KeyPreview = true;
}
else
{
//關(guān)閉計時器
timer1.Stop();
timer2.Stop();
timer3.Stop();
beginBtn.Text = "開始游戲";
//關(guān)閉窗體獲取鍵盤事件的焦點
this.KeyPreview = false;
}
}
/// <summary>
/// 字母生成
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer1_Tick(object sender, EventArgs e)
{
//初始化label
Label zimu = new Label();
zimu.Text = ((char)ra.Next(97, 123)).ToString();
zimu.ForeColor = Color.FromArgb(ra.Next(255), ra.Next(255), ra.Next(255));
zimu.Font = new Font("楷體",ra.Next(30, 41));
zimu.Location = new Point(ra.Next(game.Width - zimu.Width),0);
zimu.AutoSize = true;
zimu.TextAlign = ContentAlignment.MiddleCenter;
zimu.Tag = "zimu";
game.Controls.Add(zimu);
}
/// <summary>
/// 字母運動
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer2_Tick(object sender, EventArgs e)
{
result.Text = count.ToString();
list.Text = rex.ToString();
//遍歷panel中的控件
foreach (Control item in game.Controls)
{
//判斷控制為label
if (item.GetType().Name == "Label")
{
//判斷其tag為字母的tag
if (item.Tag.ToString() == "zimu" || item.Tag.ToString() == "change")
{
item.Top += 10;
//釋放資源
if (item.Top > plane.Top - 40)
{
rex++;
item.Dispose();
}
}
}
//判斷為PictureBox
if (item.GetType().Name == "PictureBox")
{
//判斷其tag為子彈的tag
if (item.Tag.ToString() == "zidan")
{
item.Top -= 40;
//判斷字母與子彈相遇
//遍歷panel中的所有控件
foreach (Control it in game.Controls)
{
//判斷控件類型
if (it.GetType().Name == "Label")
{
//判斷tag
if (it.Tag.ToString() == "change")
{
//判斷相遇
if (it.Top + it.Height >= item.Top)
{
count++;
//銷毀
item.Dispose();
it.Dispose();
//添加爆炸音效
//設置路徑
sound.SoundLocation = "../../sound/Bomb.wav";
//播放
sound.Play();
//設置爆炸效果圖大小
imageList.ImageSize = new Size(50, 50);
//創(chuàng)建爆炸圖片
PictureBox Bomb = new PictureBox();
Bomb.Size = new Size(50, 50);
Bomb.Tag = 0;
Bomb.Location = new Point(it.Left+it.Width/2-Bomb.Width/2,it.Top+it.Height/2-Bomb.Height/2);
game.Controls.Add(Bomb);
//創(chuàng)建字符串表示路徑
string path;
for (int i = 0; i < 32; i++)
{
if (i<10)
{
path = String.Concat("../../images/Bomb/1000", i.ToString(), ".png");
}
else
{
path = String.Concat("../../images/Bomb/100", i.ToString(), ".png");
}
imageList.Images.Add(Image.FromFile(path));
Bomb.Image = imageList.Images[Convert.ToInt32(Bomb.Tag)];
}
//設置控制爆炸圖播放效果計時器(timer3)的屬性
timer3.Tick += Timer3_Tick;
timer3.Tag = Bomb;
timer3.Interval = 30;
timer3.Start();
}
}
}
}
}
}
}
}
/// <summary>
/// 控制爆炸圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer3_Tick(object sender, EventArgs e)
{
//通過事件發(fā)起者(sender),獲取計時器的tag
Timer timerBox = sender as Timer;
//通過計時器的tag,找到Bomb的tag
PictureBox pictureBox = timerBox.Tag as PictureBox;
//pictureBox的image與imageList的images相關(guān)聯(lián)
pictureBox.Image = imageList.Images[Convert.ToInt32(pictureBox.Tag)];
//給pictureBox的tag+1
pictureBox.Tag = Convert.ToInt32(pictureBox.Tag) + 1;
if (Convert.ToInt32(pictureBox.Tag) >= 31)
{
timerBox.Dispose();
pictureBox.Dispose();
}
}
/// <summary>
/// 鍵盤事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//遍歷panel中的控件
foreach (Control item in game.Controls)
{
//判斷是控件
if (item.GetType().Name == "Label")
{
//判斷其tag是字母
if (item.Tag.ToString() == "zimu")
{
if (item.Text == e.KeyChar.ToString())
{
//改變字母的tag
item.Tag = "change";
//改變plane坐標
plane.Location = new Point(item.Left + item.Width / 2 - plane.Width / 2, game.Height - plane.Height);
//生成子彈
PictureBox zidan = new PictureBox();
zidan.Image = Image.FromFile("../../images/Bullet/Ammo_enemy3.png");
zidan.SizeMode = PictureBoxSizeMode.AutoSize;
zidan.Tag = "zidan";
zidan.Location = new Point(item.Left + item.Width / 2 - zidan.Width / 2, game.Height - plane.Height);
game.Controls.Add(zidan);
//字母與子彈對應,則跳出當前循環(huán)
return;
}
}
}
}
}
}
}
三、運行結(jié)果



更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,也分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn)
RabbitMQ是一個消息的代理器,用于接收和發(fā)送消息,本文主要介紹了C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-12-12
C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能
一直喜歡用Google Translate API進行在線翻譯,但是服務越來越慢這篇文章,所以只能換一個了,主要給大家介紹了關(guān)于C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能的相關(guān)資料,需要的朋友可以參考下2021-06-06
C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁面上
本文主要介紹用C#實現(xiàn)圖片轉(zhuǎn)換成字節(jié)流,字節(jié)流轉(zhuǎn)換成圖片,并根據(jù)圖片路徑返回圖片的字節(jié)流,有需要的朋友可以參考下2015-08-08
UnityShader使用速度映射圖實現(xiàn)運動模糊
這篇文章主要為大家詳細介紹了UnityShader使用速度映射圖實現(xiàn)運動模糊,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02

