VB之俄罗斯方块教程

VB之俄罗斯方块教程

首页休闲益智二百多斤的方块更新时间:2024-06-09

俄罗斯方块是我们大家喜欢玩的一个游戏,但如何编写这个游戏呢,在此小编用Visual Basic个人的设计思路来实现它,供大家参考学习。

设计思路:

我们都知道俄罗斯方块的图形很简单,翻来覆去就那几种,但这些图形如何表现出来呢,小编知道两种方法:

(1),第一种是将方块所在的框架界面看作一个二维表,在这个二维表中每一个方格有两个变量,其中一个变量记录是否有方块停留在上面,另外一个变量记录停留的方块叫什么名字;

(2),第二种是用VB较擅长的图形界面实现,贪吃蛇的主要图形有"Z字形"、"反Z字形"、"7字形"、反7字形"、"T字形"、"田字形"及"长条形"及其这些图形的旋转变换而共构成19种图案,在此,在编写的过程中为使程序简洁化,我们可以用这些图形的交替变换来模拟图形的变化,因此选择第二种方法。

下面接着要说的游戏的操作方法,也很简单,方块的下落由time控件控制右移动与旋转变化由Form的KeyDown中的"↑"、"↓"、"←"、" →"进行操作。

设计步骤:

1,建立如下图所示的窗体界面

2,根据上图,自行设置各控件属性

3,右击 "ImageList1" 控件,右键选择 "属性" 菜单选项,打开如下图所示的对话框,选择 "Images" ,再点 "InsertPicture"插入19张图形素材(素材可以联系小编索取)之后确定返回。

4,建立游戏源程序代码

(1),运行函数

1. Function Get_X_Value()

2. If GetValue(1, 2) Then 'Get X Value

3. If MaxX - MinX >= 2 Then

4. If MaxX - CurX <= 1 Then

5. Adjust_Left = MaxX - 2 - 1

6. Else

7. Adjust_Left = CurX - 1

8. End If

9. Get_X_Value = True

10. Exit Function

11. End If

12. End If

13. Get_X_Value = False

14. End Function

15. Function GetValue(nType As Integer, nWid As Integer)

16. GetCoor

17. On Error Resume Next

18. Dim OKCount, EmptyCount As Integer

19. MinX = Xs(1).cX

20. MaxX = Xs(1).cX

21. MinY = Xs(1).cY

22. MaxY = Xs(1).cY

23. For i = 2 To 4

24. If MinX > Xs(i).cX Then MinX = Xs(i).cX

25. If MaxX < Xs(i).cX Then MaxX = Xs(i).cX

26. If MinY > Xs(i).cY Then MinY = Xs(i).cY

27. If MaxY < Xs(i).cY Then MaxY = Xs(i).cY

28. Next

29. For i = MinX To MaxX

30. For j = MinY To MaxY

31. If Total(i, j) Then

32. GetValue = False

33. Exit Function

34. End If

35. Next

36. Next

37. If nType = 0 Then 'Get Y Value

38. EmptyCount = 0 'Get MinY

39. OKCount = 0

40. For i = MinY - 1 To MinY - (nWid - 1) Step -1

41. For j = MinX To MaxX

42. If Total(j, i) = False Then OKCount = OKCount 1

43. Next

44. If OKCount >= picNow.Width And OKCount >= picNow.Height Then

45. EmptyCount = EmptyCount 1

46. OKCount = 0

47. Else

48. Exit For

49. End If

50. Next

51. MinY = MinY - EmptyCount

52. If MinY < 1 Then MinY = 1

53. EmptyCount = 0 'GetMaxY

54. OKCount = 0

55. For i = MaxY 1 To MaxY nWid - 1

56. For j = MinX To MaxX

57. If Total(j, i) = False Then OKCount = OKCount 1

58. Next

59. If OKCount >= picNow.Width And OKCount >= picNow.Height Then

60. EmptyCount = EmptyCount 1

61. OKCount = 0

62. Else

63. Exit For

64. End If

65. Next

66. MaxY = MaxY EmptyCount

67. If MaxY > 20 Then MaxY = 20

68. Else 'Get X Value

69. EmptyCount = 0 'Get MinX

70. OKCount = 0

71. For i = MinX - 1 To MinX - (nWid - 1) Step -1

72. For j = MinY To MaxY

73. If Total(i, j) = False Then OKCount = OKCount 1

74. Next

75. If OKCount >= picNow.Width And OKCount >= picNow.Height Then

76. EmptyCount = EmptyCount 1

77. OKCount = 0

78. Else

79. Exit For

80. End If

81. Next

82. MinX = MinX - EmptyCount

83. If MinX < 1 Then MinX = 1

84. EmptyCount = 0 'GetMaxX

85. OKCount = 0

86. For i = MaxX 1 To MaxX (nWid - 1)

87. For j = MinY To MaxY

88. If Total(i, j) = False Then OKCount = OKCount 1

89. Next

90. If OKCount >= picNow.Width And OKCount >= picNow.Height Then

91. EmptyCount = EmptyCount 1

92. OKCount = 0

93. Else

94. Exit For

95. End If

96. Next

97. MaxX = MaxX EmptyCount

98. If MaxX > 10 Then MaxX = 10

99. End If

100. GetValue = True

101. End Function

102. Function Get_Y_Value()

103. If GetValue(0, 2) Then 'Get Y Value

104. If MaxY - MinY >= 2 Then

105. If MaxY - (picNow.Top 1) <= 1 Then

106. Adjust_Top = MinY - 1

107. Else

108. Adjust_Top = picNow.Top

109. End If

110. Get_Y_Value = True

111. Exit Function

112. End If

113. End If

114. Get_Y_Value = False

115. End Function

116. Sub Global_Init()

117. '全局初始化

118. picBackGround.Cls

119. imgNext.Picture = LoadPicture("")

120. picNow.Visible = False

121. tmrDrop.Enabled = False

122. End Sub

123. Sub Init()

124. '每个方块的初始化过程

125. picNow.Visible = False

126. tmrDrop.Enabled = False

127. Type_Now = Type_Next

128. picNow.Picture = imgNext.Picture

129. imgNowBackup.Picture = picNow.Picture

130. Sel_Next

131. intRotate = 0

132. picNow.Left = 4

133. picNow.Top = 0

134. picNow.Visible = True

135. tmrDrop.Enabled = True

136. End Sub

137. Sub GetCoor()

138. '获取一个方块的 4 个点的坐标

139. For i = 1 To 4 'init

140. Xs(i).cX = 0

141. Xs(i).cY = 0

142. Xs(i).cZ = False

143. Next

144. CurX = picNow.Left 1

145. Select Case Type_Now

146. Case 1 '长条

147. If intRotate Mod 2 = 1 Then

148. Xs(1).cX = CurX

149. Xs(1).cY = picNow.Top 1

150. Xs(1).cZ = True

151. For i = 2 To 4

152. Xs(i).cX = CurX i - 1

153. Xs(i).cY = picNow.Top 1

154. Xs(i).cZ = True

155. Next

156. Else

157. Xs(1).cX = CurX

158. Xs(1).cY = picNow.Top 4

159. Xs(1).cZ = True

160. For i = 2 To 4

161. Xs(i).cX = CurX

162. Xs(i).cY = picNow.Top i - 1

163. Xs(i).cZ = False

164. Next

165. End If

166. Case 2 'Z字形

167. If intRotate Mod 2 = 1 Then

168. Xs(1).cX = CurX

169. Xs(1).cY = picNow.Top 3

170. Xs(1).cZ = True

171. Xs(2).cX = CurX 1

172. Xs(2).cY = picNow.Top 2

173. Xs(2).cZ = True

174. For i = 3 To 4

175. Xs(i).cX = CurX i - 3

176. Xs(i).cY = picNow.Top 5 - i

177. Xs(i).cZ = False

178. Next

179. Else

180. Xs(1).cX = CurX

181. Xs(1).cY = picNow.Top 1

182. Xs(1).cZ = True

183. Xs(2).cX = CurX 1

184. Xs(2).cY = picNow.Top 2

185. Xs(2).cZ = True

186. Xs(3).cX = CurX 2

187. Xs(3).cY = picNow.Top 2

188. Xs(3).cZ = True

189. Xs(4).cX = CurX 1

190. Xs(4).cY = picNow.Top 1

191. Xs(4).cZ = False

192. End If

193. Case 3 '7字形

194. Select Case intRotate Mod 4

195. Case 0

196. Xs(1).cX = CurX

197. Xs(1).cY = picNow.Top 1

198. Xs(1).cZ = True

199. Xs(2).cX = CurX 1

200. Xs(2).cY = picNow.Top 3

201. Xs(2).cZ = True

202. For i = 3 To 4

203. Xs(i).cX = CurX 1

204. Xs(i).cY = picNow.Top i - 2

205. Xs(i).cZ = False

206. Next

207. Case 1

208. Xs(1).cX = CurX

209. Xs(1).cY = picNow.Top 2

210. Xs(1).cZ = True

211. Xs(2).cX = CurX 1

212. Xs(2).cY = picNow.Top 2

213. Xs(2).cZ = True

214. Xs(3).cX = CurX 2

215. Xs(3).cY = picNow.Top 2

216. Xs(3).cZ = True

217. Xs(4).cX = CurX 2

218. Xs(4).cY = picNow.Top 1

219. Xs(4).cZ = False

220. Case 2

221. Xs(1).cX = CurX

222. Xs(1).cY = picNow.Top 3

223. Xs(1).cZ = True

224. Xs(2).cX = CurX 1

225. Xs(2).cY = picNow.Top 3

226. Xs(2).cZ = True

227. For i = 3 To 4

228. Xs(i).cX = CurX

229. Xs(i).cY = picNow.Top i - 2

230. Xs(i).cZ = False

231. Next

232. Case 3

233. Xs(1).cX = CurX

234. Xs(1).cY = picNow.Top 2

235. Xs(1).cZ = True

236. Xs(2).cX = CurX 1

237. Xs(2).cY = picNow.Top 1

238. Xs(2).cZ = True

239. Xs(3).cX = CurX 2

240. Xs(3).cY = picNow.Top 1

241. Xs(3).cZ = True

242. Xs(4).cX = CurX

243. Xs(4).cY = picNow.Top 1

244. Xs(4).cZ = False

245. End Select

246. Case 4 'T字形

247. Select Case intRotate Mod 4

248. Case 0

249. Xs(1).cX = CurX

250. Xs(1).cY = picNow.Top 2

251. Xs(1).cZ = True

252. Xs(2).cX = CurX 1

253. Xs(2).cY = picNow.Top 2

254. Xs(2).cZ = True

255. Xs(3).cX = CurX 2

256. Xs(3).cY = picNow.Top 2

257. Xs(3).cZ = True

258. Xs(4).cX = CurX 1

259. Xs(4).cY = picNow.Top 1

260. Xs(4).cZ = False

261. Case 1

262. Xs(1).cX = CurX

263. Xs(1).cY = picNow.Top 3

264. Xs(1).cZ = True

265. Xs(2).cX = CurX 1

266. Xs(2).cY = picNow.Top 2

267. Xs(2).cZ = True

268. For i = 3 To 4

269. Xs(i).cX = CurX

270. Xs(i).cY = picNow.Top i - 2

271. Xs(i).cZ = False

272. Next

273. Case 2

274. Xs(1).cX = CurX

275. Xs(1).cY = picNow.Top 1

276. Xs(1).cZ = True

277. Xs(2).cX = CurX 1

278. Xs(2).cY = picNow.Top 2

279. Xs(2).cZ = True

280. Xs(3).cX = CurX 2

281. Xs(3).cY = picNow.Top 1

282. Xs(3).cZ = True

283. Xs(4).cX = CurX 1

284. Xs(4).cY = picNow.Top 1

285. Xs(4).cZ = False

286. Case 3

287. Xs(1).cX = CurX

288. Xs(1).cY = picNow.Top 2

289. Xs(1).cZ = True

290. Xs(2).cX = CurX 1

291. Xs(2).cY = picNow.Top 3

292. Xs(2).cZ = True

293. For i = 3 To 4

294. Xs(i).cX = CurX 1

295. Xs(i).cY = picNow.Top i - 2

296. Xs(i).cZ = False

297. Next

298. End Select

299. Case 5 '反7字形

300. Select Case intRotate Mod 4

301. Case 0

302. Xs(1).cX = CurX

303. Xs(1).cY = picNow.Top 3

304. Xs(1).cZ = True

305. Xs(2).cX = CurX 1

306. Xs(2).cY = picNow.Top 1

307. Xs(2).cZ = True

308. For i = 3 To 4

309. Xs(i).cX = CurX

310. Xs(i).cY = picNow.Top i - 2

311. Xs(i).cZ = False

312. Next

313. Case 1

314. Xs(1).cX = CurX

315. Xs(1).cY = picNow.Top 1

316. Xs(1).cZ = True

317. Xs(2).cX = CurX 1

318. Xs(2).cY = picNow.Top 1

319. Xs(2).cZ = True

320. Xs(3).cX = CurX 2

321. Xs(3).cY = picNow.Top 2

322. Xs(3).cZ = True

323. Xs(4).cX = CurX 2

324. Xs(4).cY = picNow.Top 1

325. Xs(4).cZ = False

326. Case 2

327. Xs(1).cX = CurX

328. Xs(1).cY = picNow.Top 3

329. Xs(1).cZ = True

330. Xs(2).cX = CurX 1

331. Xs(2).cY = picNow.Top 3

332. Xs(2).cZ = True

333. For i = 3 To 4

334. Xs(i).cX = CurX 1

335. Xs(i).cY = picNow.Top i - 2

336. Xs(i).cZ = False

337. Next

338. Case 3

339. Xs(1).cX = CurX

340. Xs(1).cY = picNow.Top 2

341. Xs(1).cZ = True

342. Xs(2).cX = CurX 1

343. Xs(2).cY = picNow.Top 2

344. Xs(2).cZ = True

345. Xs(3).cX = CurX 2

346. Xs(3).cY = picNow.Top 2

347. Xs(3).cZ = True

348. Xs(4).cX = CurX

349. Xs(4).cY = picNow.Top 1

350. Xs(4).cZ = False

351. End Select

352. Case 6 '反Z字形

353. If intRotate Mod 2 = 1 Then

354. Xs(1).cX = CurX

355. Xs(1).cY = picNow.Top 2

356. Xs(1).cZ = True

357. Xs(2).cX = CurX 1

358. Xs(2).cY = picNow.Top 3

359. Xs(2).cZ = True

360. For i = 3 To 4

361. Xs(i).cX = CurX i - 3

362. Xs(i).cY = picNow.Top i - 2

363. Xs(i).cZ = False

364. Next

365. Else

366. Xs(1).cX = CurX

367. Xs(1).cY = picNow.Top 2

368. Xs(1).cZ = True

369. Xs(2).cX = CurX 1

370. Xs(2).cY = picNow.Top 2

371. Xs(2).cZ = True

372. Xs(3).cX = CurX 2

373. Xs(3).cY = picNow.Top 1

374. Xs(3).cZ = True

375. Xs(4).cX = CurX 1

376. Xs(4).cY = picNow.Top 1

377. Xs(4).cZ = False

378. End If

379. Case 7 '田字形

380. Xs(1).cX = CurX

381. Xs(1).cY = picNow.Top 2

382. Xs(1).cZ = True

383. Xs(2).cX = CurX 1

384. Xs(2).cY = picNow.Top 2

385. Xs(2).cZ = True

386. For i = 3 To 4

387. Xs(i).cX = CurX i - 3

388. Xs(i).cY = picNow.Top 1

389. Xs(i).cZ = False

390. Next

391. End Select

392. End Sub

393. Sub Judge_Full()

394. '判断是否堆满

395. R_Value = picNow.Top 1 'MinY

396. rx_value = picNow.Top picNow.Height 'MaxY

397. For i = rx_value To R_Value Step -1

398. If Total(1, i) And Total(2, i) And Total(3, i) And Total(4, i) And Total(5, i) And _

399. Total(6, i) And Total(7, i) And Total(8, i) And Total(9, i) And Total(10, i) Then

400. '如果一行已经堆满,则将此行上面的图象全部向下移动一点

401. k = BitBlt(picBackGround.hDC, 0, 20, 200, (i - 1) * 20, picBackGround.hDC, 0, 0, vbSrcCopy)

402. For j = i To 1 Step -1

403. For k = 1 To 10

404. Total(k, j) = Total(k, j - 1)

405. Next k

406. Next j

407. i = i 1

408. End If

409. Next i

410. '如果目前方块的顶点位置为1 ,则表示全部堆满

411. If picNow.Top <= 1 Then

412. Select Case MsgBox("游戏结束,是否再来一句?", 4 32)

413. Case vbYes

414. For i = 1 To 10

415. For j = 0 To 20

416. Total(i, j) = False

417. Next j

418. Next i

419. CurX = 0

420. picBackGround.Cls

421. '改变 tmrDrop 的 Interval 值即可改变游戏速度

422. tmrDrop.Interval = 1000

423. Sel_Next

424. Init

425. Case Else

426. Global_Init

427. End Select

428. End If

429. End Sub

430. '判断方块能否翻转

431. Function Judge_Rotate()

432. Select Case Type_Now

433. Case 1 '长条

434. If intRotate Mod 2 = 1 Then

435. If GetValue(0, 4) Then 'Get Y Value

436. If MaxY - MinY >= 3 Then

437. Adjust_Top = MinY - 1

438. Judge_Rotate = True

439. Exit Function

440. End If

441. End If

442. Judge_Rotate = False

443. Exit Function

444. Else

445. If GetValue(1, 4) Then 'Get X Value

446. If MaxX - MinX >= 3 Then

447. If MaxX - CurX <= 2 Then

448. Adjust_Left = MaxX - 3 - 1

449. Else

450. If CurX = MinX Then

451. Adjust_Left = CurX - 1

452. Else

453. Adjust_Left = CurX - 1 - 1

454. End If

455. End If

456. Judge_Rotate = True

457. Exit Function

458. End If

459. End If

460. Judge_Rotate = False

461. Exit Function

462. End If

463. Case 2 '2字

464. If intRotate Mod 2 = 0 Then

465. Judge_Rotate = Get_Y_Value

466. Exit Function

467. Else

468. Judge_Rotate = Get_X_Value

469. Exit Function

470. End If

471. Case 3 '7字

472. Select Case intRotate Mod 4

473. Case 0

474. Judge_Rotate = Get_X_Value

475. Exit Function

476. Case 1

477. Judge_Rotate = Get_Y_Value

478. Exit Function

479. Case 2

480. Judge_Rotate = Get_X_Value

481. Exit Function

482. Case 3

483. Judge_Rotate = Get_Y_Value

484. Exit Function

485. End Select

486. Case 4 'T字

487. Select Case intRotate Mod 4

488. Case 0

489. Judge_Rotate = Get_Y_Value

490. Exit Function

491. Case 1

492. Judge_Rotate = Get_X_Value

493. Exit Function

494. Case 2

495. Judge_Rotate = Get_Y_Value

496. Exit Function

497. Case 3

498. Judge_Rotate = Get_X_Value

499. Exit Function

500. End Select

501. Case 5 '反7字

502. Select Case intRotate Mod 4

503. Case 0

504. Judge_Rotate = Get_X_Value

505. Exit Function

506. Case 1

507. Judge_Rotate = Get_Y_Value

508. Exit Function

509. Case 2

510. Judge_Rotate = Get_X_Value

511. Exit Function

512. Case 3

513. Judge_Rotate = Get_Y_Value

514. Exit Function

515. End Select

516. Case 6 '反2字

517. If intRotate Mod 2 = 0 Then

518. Judge_Rotate = Get_Y_Value

519. Exit Function

520. Else

521. Judge_Rotate = Get_X_Value

522. Exit Function

523. End If

524. End Select

525. End Function

526. '判断能否向左移动

527. Function JudgeX_Left()

528. GetCoor

529. For i = 1 To 4

530. On Error Resume Next

531. If Xs(i).cY > 0 Then

532. If Total(Xs(i).cX - 1, Xs(i).cY) Or Xs(i).cX = 0 Then

533. JudgeX_Left = False

534. Exit Function

535. End If

536. End If

537. Next

538. JudgeX_Left = True

539. End Function

540. '判断能否向右移动

541. Function JudgeX_Right()

542. GetCoor

543. For i = 1 To 4

544. On Error Resume Next

545. If Xs(i).cY > 0 Then

546. If Total(Xs(i).cX 1, Xs(i).cY) Or Xs(i).cX = 10 Then

547. JudgeX_Right = False

548. Exit Function

549. End If

550. End If

551. Next

552. JudgeX_Right = True

553. End Function

554. '判断能否向下移动

555. Sub JudgeY()

556. GetCoor

557. For i = 1 To 4

558. If Xs(i).cZ Then

559. On Error Resume Next

560. If Xs(i).cY > 0 Then

561. If Total(Xs(i).cX, Xs(i).cY 1) Or Xs(i).cY = 20 Then

562. '如果不能移动,将4点位置的坐标设置为 True,并将图形固定下来

563. For j = 1 To 4

564. Total(Xs(j).cX, Xs(j).cY) = True

565. Next j

566. picBackGround.PaintPicture picNow.Picture, picNow.Left, picNow.Top, picNow.Width, picNow.Height, , , , , vbSrcAnd

567. Judge_Full

568. If picNow.Visible Then Init

569. Exit Sub

570. End If

571. End If

572. End If

573. Next

574. End Sub

575. Sub Sel_Next()

576. '随机从 7 个放块中选择一个

577. Randomize

578. Type_Next = Int((7 * Rnd) 1)

579. Select Case Type_Next

580. Case 1

581. imgNext.Picture = LoadResPicture(11, 0)

582. Case 2

583. imgNext.Picture = LoadResPicture(13, 0)

584. Case 3

585. imgNext.Picture = LoadResPicture(15, 0)

586. Case 4

587. imgNext.Picture = LoadResPicture(19, 0)

588. Case 5

589. imgNext.Picture = LoadResPicture(23, 0)

590. Case 6

591. imgNext.Picture = LoadResPicture(27, 0)

592. Case 7

593. imgNext.Picture = LoadResPicture(29, 0)

594. End Select

595. imgNext.Move (picNextBackGround.Width - imgNext.Width) \ 2 - 30, (picNextBackGround.Height - imgNext.Height) \ 2 - 30

596. End Sub

(2),键盘控制函数

1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

2. '改变 Case 的 KeyCode 值就可以改变键盘控制按钮

3. Select Case KeyCode

4. Case vbKeyLeft

5. If picNow.Left - 1 >= 0 Then

6. J_Value = JudgeX_Left

7. If J_Value Then

8. picNow.Picture = imgNowBackup.Picture

9. r = BitBlt(picTemp.hDC, 0, 0, picNow.Width * 20, picNow.Height * 20, picBackGround.hDC, (picNow.Left - 1) * 20, picNow.Top * 20, vbSrcCopy)

10. picNow.Left = picNow.Left - 1

11. r = BitBlt(picNow.hDC, 0, 0, picNow.Width * 20, picNow.Height * 20, picTemp.hDC, 0, 0, vbSrcAnd)

12. End If

13. End If

14. Case vbKeyRight

15. If picNow.Left picNow.Width < picBackGround.ScaleWidth Then

16. J_Value = JudgeX_Right

17. If J_Value Then

18. picNow.Picture = imgNowBackup.Picture

19. r = BitBlt(picTemp.hDC, 0, 0, picNow.Width * 20, picNow.Height * 20, picBackGround.hDC, (picNow.Left 1) * 20, picNow.Top * 20, vbSrcCopy)

20. picNow.Left = picNow.Left 1

21. r = BitBlt(picNow.hDC, 0, 0, picNow.Width * 20, picNow.Height * 20, picTemp.hDC, 0, 0, vbSrcAnd)

22. End If

23. End If

24. Case vbKeyDown

25. tmrDrop_Timer

26. Case vbKeyUp

27. If Judge_Rotate Then

28. intRotate = intRotate 1

29. Select Case Type_Now

30. Case 1 '长条

31. If intRotate Mod 2 = 1 Then

32. 'picNow.Picture = LoadResPicture(12, 0)

33. picNow.Picture = ImageList1.ListImages.Item(2).Picture

34. picNow.Top = picNow.Top 3

35. picNow.Left = Adjust_Left

36. Else

37. 'picNow.Picture = LoadResPicture(11, 0)

38. picNow.Picture = ImageList1.ListImages.Item(1).Picture

39. picNow.Top = Adjust_Top

40. picNow.Left = picNow.Left 1

41. End If

42. Case 2 'Z字形

43. If intRotate Mod 2 = 1 Then

44. 'picNow.Picture = LoadResPicture(14, 0)

45. picNow.Picture = ImageList1.ListImages.Item(4).Picture

46. picNow.Top = Adjust_Top

47. Else

48. 'picNow.Picture = LoadResPicture(13, 0)

49. picNow.Picture = ImageList1.ListImages.Item(3).Picture

50. picNow.Top = picNow.Top 1

51. picNow.Left = Adjust_Left

52. End If

53. Case 3 '7字形

54. Select Case intRotate Mod 4

55. Case 0

56. 'picNow.Picture = LoadResPicture(15, 0)

57. picNow.Picture = ImageList1.ListImages.Item(5).Picture

58. picNow.Top = Adjust_Top

59. Case 1

60. 'picNow.Picture = LoadResPicture(16, 0)

61. picNow.Picture = ImageList1.ListImages.Item(6).Picture

62. picNow.Top = picNow.Top 1

63. picNow.Left = Adjust_Left

64. Case 2

65. picNow.Picture = LoadResPicture(17, 0)

66. picNow.Top = Adjust_Top

67. Case 3

68. picNow.Picture = LoadResPicture(18, 0)

69. picNow.Top = picNow.Top 1

70. picNow.Left = Adjust_Left

71. End Select

72. Case 4 'T字形

73. Select Case intRotate Mod 4

74. Case 0

75. picNow.Picture = LoadResPicture(19, 0)

76. picNow.Top = picNow.Top 1

77. picNow.Left = Adjust_Left

78. Case 1

79. picNow.Picture = LoadResPicture(20, 0)

80. picNow.Top = Adjust_Top

81. Case 2

82. picNow.Picture = LoadResPicture(21, 0)

83. picNow.Top = picNow.Top 1

84. picNow.Left = Adjust_Left

85. Case 3

86. picNow.Picture = LoadResPicture(22, 0)

87. picNow.Top = Adjust_Top

88. End Select

89. Case 5 '反7字形

90. Select Case intRotate Mod 4

91. Case 0

92. picNow.Picture = LoadResPicture(23, 0)

93. picNow.Top = Adjust_Top

94. Case 1

95. picNow.Picture = LoadResPicture(24, 0)

96. picNow.Top = picNow.Top 1

97. picNow.Left = Adjust_Left

98. Case 2

99. picNow.Picture = LoadResPicture(25, 0)

100. picNow.Top = Adjust_Top

101. Case 3

102. picNow.Picture = LoadResPicture(26, 0)

103. picNow.Top = picNow.Top 1

104. picNow.Left = Adjust_Left

105. End Select

106. Case 6 '反Z字形

107. If intRotate Mod 2 = 1 Then

108. picNow.Picture = LoadResPicture(28, 0)

109. picNow.Top = Adjust_Top

110. Else

111. picNow.Picture = LoadResPicture(27, 0)

112. picNow.Top = picNow.Top 1

113. picNow.Left = Adjust_Left

114. End If

115. End Select

116. imgNowBackup.Picture = picNow.Picture

117. End If

118. End Select

119. End Sub

(3),ImageList

1. Dim CurX As Integer '目前X坐标

2. Dim Total(10, 20) As Boolean '总体坐标布局 10x20

3. Dim MinX As Integer, MaxX As Integer, MinY As Integer, MaxY As Integer '一个方块的最小 x 坐标,最大 x 坐标,最小 y 坐标,最大 y 坐标

4. Private Type cXs

5. cX As Integer 'x 坐标

6. cY As Integer 'y 坐标

7. cZ As Boolean '判断一个点下面是否是空的

8. End Type

9. Dim Xs(4) As cXs

10. Dim Adjust_Left As Integer '翻转后向左方调整的位置

11. Dim Adjust_Top As Integer '翻转后向上方调整的位置

12. 'BitBlt 函数作用:位操作位图,实现不规则的方块的动作

13. Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long

14. Dim Type_Now As Integer '目前方块的类型

15. Dim Type_Next As Integer '下个方块的类型

16. Dim intRotate As Integer '方块旋转的状态

(4),Hide

1. Private Sub Commannd1_Click()

2. imgNext.Visible = Not (imgNext.Visible)

3. If imgNext.Visible Then

4. Commannd1.Caption = "Hide(&D)"

5. Else

6. Commannd1.Caption = "Show(&S)"

7. End If

8. End Sub

(5),Start

1. Private Sub Command1_Click()

2. '将 10x20 的坐标全部设置为空

3. For i = 1 To 10

4. For j = 0 To 20

5. Total(i, j) = False

6. Next j

7. Next i

8. CurX = 0

9. picBackGround.Cls

10. '改变 tmrDrop 的 Interval 值即可改变游戏速度

11. tmrDrop.Interval = 1000

12. Sel_Next

13. Init

14. End Sub

(6),Finish

1. Private Sub Command2_Click()

2. End

3. End Sub

(7),Keyboard

1. Private Sub Command3_Click()

2. MsgBox "键盘控制方法:" vbCrLf "← 控制方块向左移动;" _

3. vbCrLf "→ 控制方块向右移动;" _

4. vbCrLf "↓ 控制方块向下快速移动;" _

5. vbCrLf "↑ 控制方块的顺时针方向的翻转。", 64, "俄罗斯方块 键盘操作帮助"

6. End Sub

(8),Timer

1. Private Sub tmrDrop_Timer()

2. '方块下落

3. JudgeY

4. picNow.Picture = imgNowBackup.Picture

5. r = BitBlt(picTemp.hDC, 0, 0, picNow.Width * 20, picNow.Height * 20, picBackGround.hDC, picNow.Left * 20, (picNow.Top 1) * 20, vbSrcCopy)

6. picNow.Top = picNow.Top 1

7. r = BitBlt(picNow.hDC, 0, 0, picNow.Width * 20, picNow.Height * 20, picTemp.hDC, 0, 0, vbSrcAnd)

8. DoEvents

9. If picNow.Top picNow.Height > picBackGround.ScaleHeight Then Init

10. End Sub

(9),Label

1. Private Sub Form_Load()

2. Label1.Caption = " 俄罗斯方块 "

3. End Sub

5,按F5键运行程序,如下图所示

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved