C#的winform拼数字游戏
生活随笔
收集整理的這篇文章主要介紹了
C#的winform拼数字游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#的winform拼數字游戲
聲明:閱讀了別人的代碼學習修改而來,增加了美觀度和游戲樂趣。(作者出處忘了不好意思)
程序截圖
關鍵代碼
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;namespace PingNumberGame {public partial class MainForm : Form {public MainForm() {InitializeComponent();btnGiveup.Visible = false;}Label[,] arrLbl = new Label[3, 3];int unRow = 0, unCol = 0;bool playing = false;const int maxWarning = 5;string[] warning ={"你簡直弱爆了","小學沒畢業(yè)吧你","天吶,什么人吶","真為你的智商感到捉急","你走開我有傻逼恐懼癥"};public void move(object sender) {if(!playing) {return;}int row = ((Label)sender).Top / 120;int col = ((Label)sender).Left / 120;if(Math.Abs(row - unRow) + Math.Abs(col - unCol) == 1) {string temp = arrLbl[unRow, unCol].Text;arrLbl[unRow, unCol].Text = arrLbl[row, col].Text;arrLbl[row, col].Text = temp;arrLbl[unRow, unCol].Visible = true;arrLbl[row, col].Visible = false;unRow = row;unCol = col;}for(int i = 0; i < 9; i++) {if(arrLbl[i / 3, i % 3].Text != Convert.ToString(i + 1))break;if(i == 8) {arrLbl[unRow, unCol].Visible = true;playing = false;MessageBox.Show("恭喜你通過了游戲!", "祝賀", MessageBoxButtons.OK, MessageBoxIcon.Information);btnGiveup.Visible = false;btnPlay.Visible = true;}}}private void label1_Click(object sender, EventArgs e) {move(sender);}private void label2_Click(object sender, EventArgs e) {move(sender);}private void label3_Click(object sender, EventArgs e) {move(sender);}private void label4_Click(object sender, EventArgs e) {move(sender);}private void label5_Click(object sender, EventArgs e) {move(sender);}private void label6_Click(object sender, EventArgs e) {move(sender);}private void label7_Click(object sender, EventArgs e) {move(sender);}private void label8_Click(object sender, EventArgs e) {move(sender);}private void label9_Click(object sender, EventArgs e) {move(sender);}private void btnPlay_Click(object sender, EventArgs e) {arrLbl[0, 0] = label1;arrLbl[0, 1] = label2;arrLbl[0, 2] = label3;arrLbl[1, 0] = label4;arrLbl[1, 1] = label5;arrLbl[1, 2] = label6;arrLbl[2, 0] = label7;arrLbl[2, 1] = label8;arrLbl[2, 2] = label9;arrLbl[unRow, unCol].Visible = true;int[] arrNum = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };Random rm = new Random();for(int i = 0; i < 8; i++) {int rmNum = rm.Next(i, 9);int temp = arrNum[i];arrNum[i] = arrNum[rmNum];arrNum[rmNum] = temp;}for(int i = 0; i < 9; i++) {arrLbl[i / 3, i % 3].Text = arrNum[i].ToString();}int cover = rm.Next(0, 9);unRow = cover / 3;unCol = cover % 3;arrLbl[unRow, unCol].Visible = false;playing = true;btnGiveup.Visible = true;btnPlay.Visible = false;}private void btnGiveup_Click(object sender, EventArgs e) {Random rm=new Random();int num = rm.Next(0, maxWarning);MessageBox.Show(warning[num], "鄙視", MessageBoxButtons.OK, MessageBoxIcon.Warning);btnPlay.Visible = true;btnGiveup.Visible = false;}} }完整工程
度盤下載
轉載于:https://www.cnblogs.com/wsine/p/4298032.html
總結
以上是生活随笔為你收集整理的C#的winform拼数字游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷-小鱼的游泳时间-洛谷的第一个任务
- 下一篇: 微软职位内部推荐-Senior BSP