C#純代碼實(shí)現(xiàn)打字游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)打字游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、需求分析
1、界面設(shè)計(jì) 布局
- 需要哪些內(nèi)容控件
- 容器的概念
- 集合的概念
2、開(kāi)始游戲
- 字母的生成 26個(gè)字母 ASCII碼值
- 字母的載體 控件
- 字母、位置、大小、顏色
- 要求隨機(jī)的
- 計(jì)時(shí)器
3、字母從上往下運(yùn)動(dòng)
- TOP變化
- 注意:垃圾回收問(wèn)題,未消除的字母進(jìn)行銷(xiāo)毀,釋放資源
- 計(jì)時(shí)器
4、需要產(chǎn)生對(duì)應(yīng)的從下往上生成字母子彈,打掉字母
- 處理與鍵盤(pán)之間的交互,鍵盤(pán)相關(guān)事件
- 字母需要轉(zhuǎn)換,ASCII碼值
- 知識(shí)點(diǎn):事件參數(shù):EventArgs e
- 子彈從下往上運(yùn)動(dòng)
- 子彈與字母相遇,兩者消失
5、添加動(dòng)畫(huà)效果、音效等
二、代碼實(shí)現(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;
//導(dǎo)入媒體命名空間
using System.Media;
namespace PlaneTest_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//初始化panel
Panel game = new Panel();
//初始化pictureBox
PictureBox plane = new PictureBox();
//初始化一個(gè)播放器
SoundPlayer sound = new SoundPlayer();
//創(chuàng)建爆炸效果圖
ImageList imageList = new ImageList();
//初始化隨機(jī)數(shù)對(duì)象
Random ra = new Random();
//添加timer1,控制字母生成
Timer timer1 = new Timer();
//添加timer2,控制字母、子彈運(yùn)動(dòng)
Timer timer2 = new Timer();
//添加timer3,控制爆炸效果圖的播放
Timer timer3 = new Timer();
//設(shè)置游戲開(kāi)始按鈕
Button beginBtn = new Button();
//實(shí)現(xiàn)記錄分?jǐn)?shù)
Label result = new Label();
//記錄分?jǐn)?shù)
int count = 0;
//實(shí)現(xiàn)記錄失誤
Label list = new Label();
//記錄失誤
int rex = 0;
private void Form1_Load(object sender, EventArgs e)
{
//設(shè)置窗體屬性
this.Text = "天天打字母";
this.Size = new Size(1000,700);
this.CenterToScreen();
//設(shè)置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);
//設(shè)置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);
//設(shè)置timer1屬性,此計(jì)時(shí)器控制字母生成
timer1.Tick += Timer1_Tick;
timer1.Interval = 1000;
//設(shè)置timer2屬性,此計(jì)時(shí)器控制字母運(yùn)動(dòng)
timer2.Tick += Timer2_Tick;
timer2.Interval = 200;
//添加鍵盤(pán)按下事件
this.KeyPress += Form1_KeyPress;
//設(shè)置按鈕屬性
beginBtn.Text = "開(kāi)始游戲";
beginBtn.Size = new Size(100,30);
beginBtn.Location = new Point(game.Width+50, 20);
this.Controls.Add(beginBtn);
//生成按鈕點(diǎn)擊事件
beginBtn.Click += BeginBtn_Click;
//設(shè)置記錄分?jǐn)?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);
//設(shè)置記錄失誤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>
/// 按鈕點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeginBtn_Click(object sender, EventArgs e)
{
if (beginBtn.Text == "開(kāi)始游戲")
{
//開(kāi)啟計(jì)時(shí)器
timer1.Start();
timer2.Start();
timer3.Start();
beginBtn.Text = "暫停游戲";
//開(kāi)啟窗體獲取鍵盤(pán)事件的焦點(diǎn)
this.KeyPreview = true;
}
else
{
//關(guān)閉計(jì)時(shí)器
timer1.Stop();
timer2.Stop();
timer3.Stop();
beginBtn.Text = "開(kāi)始游戲";
//關(guān)閉窗體獲取鍵盤(pán)事件的焦點(diǎ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>
/// 字母運(yùn)動(dòng)
/// </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)
{
//判斷控件類(lèi)型
if (it.GetType().Name == "Label")
{
//判斷tag
if (it.Tag.ToString() == "change")
{
//判斷相遇
if (it.Top + it.Height >= item.Top)
{
count++;
//銷(xiāo)毀
item.Dispose();
it.Dispose();
//添加爆炸音效
//設(shè)置路徑
sound.SoundLocation = "../../sound/Bomb.wav";
//播放
sound.Play();
//設(shè)置爆炸效果圖大小
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)];
}
//設(shè)置控制爆炸圖播放效果計(jì)時(shí)器(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)
{
//通過(guò)事件發(fā)起者(sender),獲取計(jì)時(shí)器的tag
Timer timerBox = sender as Timer;
//通過(guò)計(jì)時(shí)器的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>
/// 鍵盤(pán)事件
/// </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坐標(biāo)
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);
//字母與子彈對(duì)應(yīng),則跳出當(dāng)前循環(huán)
return;
}
}
}
}
}
}
}
三、運(yùn)行結(jié)果



更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專(zhuān)題,也分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#創(chuàng)建Windows服務(wù)的圖文教程
本文主要介紹了C#創(chuàng)建Windows服務(wù)的圖文教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
c# 調(diào)用.bat文件的實(shí)現(xiàn)代碼
c# 調(diào)用.bat文件主要利用了using System.Diagnostics;命名空間,大家可以參考下。2009-06-06
C#使用RabbitMQ發(fā)送和接收消息工具類(lèi)的實(shí)現(xiàn)
RabbitMQ是一個(gè)消息的代理器,用于接收和發(fā)送消息,本文主要介紹了C#使用RabbitMQ發(fā)送和接收消息工具類(lèi)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
C#將Word轉(zhuǎn)換成PDF方法匯總(基于Office和WPS)
這篇文章主要匯總了C#將Word轉(zhuǎn)換成PDF方法,基于Office和WPS的兩種解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
解決用Aspose.Words,在word文檔中創(chuàng)建表格的實(shí)現(xiàn)方法
本篇文章是對(duì)利用Aspose.Words,在word文檔中創(chuàng)建表格的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

