日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

拼图游戏【简单】

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 拼图游戏【简单】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace WindowsFormsApplication7 11 { 12 public partial class Form1 : Form 13 { 14 //聲明一個3*3的二維數組 15 int[,] pos = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; 16 int ar = 0;//數組x軸 17 int ac = 0;//數組y軸 18 int r = 5;//圖片位置x軸值 19 int c = 5;//圖片位置y軸值 20 int i = 0;//第幾個數 21 int hi = 0;//記錄按鍵次數 22 23 public Form1() 24 { 25 InitializeComponent(); 26 27 //運行隨機生成將圖片打亂 28 PiRandom(); 29 } 30 //將圖片打亂順序 31 private void PiRandom() 32 { 33 //定義一個隨機對象 34 Random ran = new Random(); 35 //聲明一個隨機數接收變量 36 int f; 37 //成功第幾次 38 while (i < 8) 39 { 40 //得到隨機數(大于0小于9之間的整數) 41 f = ran.Next(9); 42 43 //查看隨機數的數據類型 44 //label10.Text =Convert.ToString( f.GetType()); 45 46 //判斷不重復 47 if (numNotExists(f) == true && f > 0) 48 { 49 pos[ar, ac] = f; 50 switch (f) 51 { 52 case 1: 53 label1.Location = new Point(r, c); 54 break; 55 case 2: 56 label2.Location = new Point(r, c); 57 break; 58 case 3: 59 label3.Location = new Point(r, c); 60 break; 61 case 4: ; 62 label4.Location = new Point(r, c); 63 break; 64 case 5: 65 label5.Location = new Point(r, c); 66 break; 67 case 6: 68 label6.Location = new Point(r, c); 69 break; 70 case 7: 71 label7.Location = new Point(r, c); 72 break; 73 case 8: ; 74 label8.Location = new Point(r, c); 75 break; 76 } 77 r += 100; 78 ac++; 79 if (r > 300) 80 { 81 r = 5; 82 c += 100; 83 } 84 if (ac > 2) 85 { 86 ac = 0; 87 ar++; ; 88 } 89 i++; 90 } 91 else continue; 92 } 93 label9.Location = new Point(r, c); 94 pos[2, 2] = 9; 95 } 96 //判斷數組中是否有該值 97 private bool numNotExists(int f) 98 { 99 for (int x = 0; x < 3; x++) 100 { 101 for (int y = 0; y < 3; y++) 102 { 103 if (pos[x, y] == f) 104 { 105 return false; 106 } 107 } 108 } 109 return true; 110 } 111 //按鍵按下發生的事件(控制的是labl9這個控件) 112 private void Form1_KeyDown(object sender, KeyEventArgs e) 113 { 114 int temp; 115 //判斷按鍵碼 116 switch ((int)e.KeyCode) 117 { 118 // 119 case 38:// 120 if (ar < 2) 121 { 122 temp = pos[ar, ac]; 123 pos[ar, ac] = pos[ar + 1, ac]; 124 pos[ar + 1, ac] = temp; 125 Swap(pos[ar, ac], temp);//調用交換控件 126 hi++;//按鍵次數增加 127 ar++;//記錄當前0在數組中的位置 128 } 129 break; 130 case 39:// 131 if (ac >0) 132 { 133 temp = pos[ar, ac]; 134 pos[ar, ac] = pos[ar, ac-1]; 135 pos[ar, ac-1] = temp; 136 Swap(pos[ar, ac], temp);//調用交換控件 137 hi++;//按鍵次數增加 138 ac--;//記錄當前0在數組中的位置 139 } 140 break; 141 case 40:// 142 if (ar >0) 143 { 144 temp = pos[ar, ac]; 145 pos[ar, ac] = pos[ar - 1, ac]; 146 pos[ar - 1, ac] = temp; 147 Swap(pos[ar, ac], temp);//調用交換控件 148 hi++;//按鍵次數增加 149 ar--;//記錄當前0在數組中的位置 150 } 151 break; 152 case 37:// 153 if (ac<2) 154 { 155 temp = pos[ar, ac]; 156 pos[ar, ac] = pos[ar, ac + 1]; 157 pos[ar, ac + 1] = temp; 158 Swap(pos[ar, ac], temp);//調用交換控件 159 hi++;//按鍵次數增加 160 ac++;//記錄當前0在數組中的位置 161 } 162 break; 163 } 164 label10.Text = hi.ToString(); 165 166 Boolean b = true; 167 int s = 1; 168 //for查看是否排列完成 169 for (int x = 0; x < 3; x++) 170 { 171 for (int y = 0; y < 3; y++) 172 { 173 if (pos[x, y] != s) 174 { 175 b = false; 176 break ; 177 } 178 s++; 179 } 180 } 181 if(b) 182 { 183 MessageBox.Show("一共移動了"+hi.ToString()+"次!!!","游戲結束"); 184 } 185 } 186 //控件交換 187 private void Swap(int p, int t) 188 { 189 Point temp; 190 switch (p) 191 { 192 case 1: 193 temp = label1.Location; 194 label1.Location = label9.Location; 195 label9.Location = temp; 196 break; 197 case 2: 198 temp = label2.Location; 199 label2.Location = label9.Location; 200 label9.Location = temp; 201 break; 202 case 3: 203 temp = label3.Location; 204 label3.Location = label9.Location; 205 label9.Location = temp; 206 break; 207 case 4: 208 temp = label4.Location; 209 label4.Location = label9.Location; 210 label9.Location = temp; 211 break; 212 case 5: 213 temp = label5.Location; 214 label5.Location = label9.Location; 215 label9.Location = temp; 216 break; 217 case 6: 218 temp = label6.Location; 219 label6.Location = label9.Location; 220 label9.Location = temp; 221 break; 222 case 7: 223 temp = label7.Location; 224 label7.Location = label9.Location; 225 label9.Location = temp; 226 break; 227 case 8: 228 temp = label8.Location; 229 label8.Location = label9.Location; 230 label9.Location = temp; 231 break; 232 } 233 } 234 } 235 }

?【本篇新知識】:

  Point 是表示二維平面中定義的一個點的 x 和 y 坐標的有序對; 

  label9.Location;//控件坐標

轉載于:https://www.cnblogs.com/pengyouqiang88/p/5131682.html

總結

以上是生活随笔為你收集整理的拼图游戏【简单】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。