![基于C#俄羅斯方塊設計_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/6/c0e2a83b-9cbc-4b7c-8dfe-ae631fe28434/c0e2a83b-9cbc-4b7c-8dfe-ae631fe284341.gif)
![基于C#俄羅斯方塊設計_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/6/c0e2a83b-9cbc-4b7c-8dfe-ae631fe28434/c0e2a83b-9cbc-4b7c-8dfe-ae631fe284342.gif)
![基于C#俄羅斯方塊設計_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/6/c0e2a83b-9cbc-4b7c-8dfe-ae631fe28434/c0e2a83b-9cbc-4b7c-8dfe-ae631fe284343.gif)
![基于C#俄羅斯方塊設計_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/6/c0e2a83b-9cbc-4b7c-8dfe-ae631fe28434/c0e2a83b-9cbc-4b7c-8dfe-ae631fe284344.gif)
![基于C#俄羅斯方塊設計_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/6/c0e2a83b-9cbc-4b7c-8dfe-ae631fe28434/c0e2a83b-9cbc-4b7c-8dfe-ae631fe284345.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、此程序主要三個小結講解:1、 設計方塊類(block.cs)2、 設計游戲類(game.cs)3、 設計窗體類(form1.cs)通俗易懂,希望對廣大的學生們都有幫助歡迎下載技術qq 1278263100 郵箱1278263100游戲所需圖片:游戲運行圖:話不多說:設計方塊類(block.cs)程序都有注釋,通俗易懂using System;using System.Collections.Generic;using System.Text;using System.Drawing;/addnamespace 俄羅斯方塊 public class Block private short wid
2、th; private short height; private short top; private short left; private int ID; /方塊部件的ID public int, shape;/存儲方塊部件的形狀,為空白,為有磚塊 public Block()/構造函數(shù) Random randomGenerator = new Random(); int randomBlock = randomGenerator.Next(1, 5);/產(chǎn)生14的數(shù) this.ID = randomBlock; switch (this.ID) case 1: /橫條形 this.Wi
3、dth = 4; this.Height = 1; this.Top = 0; this.Left = 3; shape = new intthis.Width, this.Height; shape0, 0 = 1; shape1, 0 = 1; shape2, 0 = 1; shape3, 0 = 1; break; case 2:/正方形 this.Width = 2; this.Height = 2; this.Top = 0; this.Left = 4; / Creates the new shape for this block. shape = new intthis.Widt
4、h, this.Height; shape0, 0 = 1; shape0, 1 = 1; shape1, 0 = 1;shape1, 1 = 1; break; case 3:/形 this.Width = 3; this.Height = 3; this.Top = 0; this.Left = 4; / Creates the new shape for this block. shape = new intthis.Width, this.Height; shape0, 0 = 1; shape1, 0 = 1; shape2, 0 = 1; shape1, 1 = 1; shape1
5、, 2 = 1; break; case 4:/L形 this.Width = 2; this.Height = 3; this.Top = 0; this.Left = 4; / Creates the new shape for this block. shape = new intthis.Width, this.Height; shape0, 0 = 1; shape0, 1 = 1; shape0, 2 = 1; shape1, 2 = 1; break; public short Width/Width屬性 get return width; set width = value;
6、public short Height/Height屬性 get return height; set height = value; public short Top/Top屬性 get return top; set top = value; public short Left/Left屬性 get return left; set left = value; public void Draw(Graphics g) Image brickImage = Image.FromFile("image/block0.gif"); for (int i = 0; i <
7、 this.Width; i+) for (int j = 0; j < this.Height; j+) if (this.shapei, j = 1)/黑色格子 /得到繪制這個格子的在游戲面板中的矩形區(qū)域 Rectangle rect = new Rectangle(this.Left + i) * Game.BlockImageWidth, (this.Top + j) * Game.BlockImageHeight, Game.BlockImageWidth, Game.BlockImageHeight); g.DrawImage(brickImage, rect); /clas
8、s Block設計游戲類(game.cs)using System;using System.Collections.Generic;using System.Text;using System.Drawing;/addnamespace 俄羅斯方塊 class Game public const int BlockImageWidth = 21;/方磚中每個小方格的大小 public const int BlockImageHeight = 21; public const int PlayingFieldWidth = 10;/游戲面板大小 public const int Playing
9、FieldHeight = 20; private int, pile; /存儲在游戲面板中的所有方磚; private Block currentBlock ;/當前的俄羅斯方塊 private Block nextBlock ;/下一個的俄羅斯方塊 public int score = 0, lines=0; public bool over=false;/游戲是否結束 public Game()/Game類構造函數(shù) pile = new intPlayingFieldWidth , PlayingFieldHeight; ClearPile(); CreateNewBlock();/產(chǎn)生
10、新的俄羅斯方塊 private void ClearPile()/清空游戲面板中的所有方磚 for (int i = 0; i < PlayingFieldWidth ; i+) for (int j = 0; j < PlayingFieldHeight ; j+) pilei, j= 0; private void CreateNewBlock()/產(chǎn)生新的俄羅斯方塊if (this.nextBlock != null)currentBlock = nextBlock;elsecurrentBlock = new Block();nextBlock = new Block();
11、 public void DrawPile(Graphics g) Image brickImage = Image.FromFile("image/block1.gif");/方磚的圖形 for (int i = 0; i < PlayingFieldWidth ; i+) for (int j = 0; j < PlayingFieldHeight ; j+) if (pilei, j = 1) Rectangle rect = new Rectangle(i * BlockImageWidth, j * BlockImageHeight, BlockIma
12、geWidth, BlockImageHeight);/(j - 1) g.DrawImage(brickImage, rect); public void DrawCurrentBlock(Graphics g) if (currentBlock != null)/檢查當前塊是否為空 currentBlock.Draw(g); public void DrawNextBlock(Graphics drawingSurface) if (nextBlock != null) short currentLeft = nextBlock.Left; short currentTop = nextB
13、lock.Top; nextBlock.Left = (short)(6 - nextBlock.Width) / 2); nextBlock.Top = (short)(6 - nextBlock.Height) / 2); nextBlock.Draw(drawingSurface); nextBlock.Left = currentLeft; nextBlock.Top = currentTop; private void MoveBlockToPile()/固定到游戲面板上 for (int i = 0; i < currentBlock.Width; i+) for (int
14、j = 0; j < currentBlock.Height; j+) int fx, fy; fx = currentBlock.Left + i; fy = currentBlock.Top + j; if (currentBlock.shapei, j =1) pilefx, fy = 1; CheckForLines(); if (CheckForGameOver()/檢查游戲是否結束 over = true; public bool DownCurrentBlock() bool hit = false; currentBlock.Top+; if (currentBlock.
15、Top + currentBlock.Height) > PlayingFieldHeight) hit = true;/當前塊觸游戲面板底 else/檢查是否接觸到下一行其他已落方塊 for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.Height; j+) int fx, fy; fx = currentBlock.Left + i; fy = currentBlock.Top + j; if (currentBlock.shapei, j = 1) &&
16、 (pilefx, fy = 1)/(fy + 1) hit = true; if (hit)/觸到其他已落方塊或游戲面板底 currentBlock.Top-; MoveBlockToPile();/固定到游戲面板上 CreateNewBlock(); /產(chǎn)生新的俄羅斯方塊 return hit; public void RotateCurrentBlock()/旋轉方塊 bool canRotate = true; short newWidth = 0; short newHeight = 0; int, newShape; newWidth = currentBlock.Height;
17、newHeight = currentBlock.Width; newShape = new intnewWidth, newHeight; int x,y; if (currentBlock.Left + newWidth) <= Game.PlayingFieldWidth) && (currentBlock.Top + newHeight) < Game.PlayingFieldHeight) for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.H
18、eight; j+) x = (currentBlock.Height - 1) - j); y = i; newShapex, y = currentBlock.shapei, j; if (newShapex, y = 1 && pilex + currentBlock.Left, y + currentBlock.Top = 1) canRotate = false; return;/不能旋轉 if (canRotate) currentBlock.Width = newWidth; currentBlock.Height = newHeight; currentBloc
19、k.shape = newShape; public void MoveCurrentBlockSide(bool left)/左右移動 bool canMove = true; if (left)/左移動 if (currentBlock.Left > 0) for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.Height; j+) int fx, fy; fx = currentBlock.Left + i; fy = (currentBlock.Top + 1) + j
20、; if (currentBlock.shapei, j = 1) && (pile(fx - 1), fy =1) canMove = false; if (canMove) currentBlock.Left-; else/右移動 if (currentBlock.Left + currentBlock.Width) < PlayingFieldWidth) for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.Height; j+) int fx, fy;
21、 fx = currentBlock.Left + i; fy = (currentBlock.Top + 1) + j; if (currentBlock.shapei, j = 1) && (pile(fx + 1), fy=1) canMove = false; if (canMove) currentBlock.Left+; private int CheckForLines()/檢查是否滿行并消去 int numLines = 0; int completeLines = new intPlayingFieldHeight; for (int j = PlayingF
22、ieldHeight-1; j > 0; j-)/j = PlayingFieldHeight bool fullLine = true; for (int i = 0; i < PlayingFieldWidth; i+) if (pilei, j = 0) fullLine = false; break; if (fullLine) numLines+; completeLinesnumLines = j; if (numLines > 0) for (int i = 1; i <= numLines; i+) ClearLine(completeLinesi +
23、(i - 1); score += 5 * (numLines * (numLines + 1); lines += numLines; return numLines; private void ClearLine(int lineNumber) for (int j = lineNumber; j > 0; j-) for (int i = 0; i < PlayingFieldWidth; i+) pilei, j = pilei, (j - 1); for (int i = 0; i < PlayingFieldWidth; i+) pilei, 0 = 0; pub
24、lic bool CheckForGameOver()/檢查游戲是否結束 if (currentBlock.Top = 0) return true; else return false; 設計窗體類(form1.cs)下圖:如果覺得圖片不清楚可以另存為桌面,慢慢研究using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespac
25、e 俄羅斯方塊 public partial class Form1 : Form public Form1() InitializeComponent(); Game game=null; private void button1_Click(object sender, EventArgs e) game= new Game(); pictureBox1.Height = Game.BlockImageHeight * Game.PlayingFieldHeight + 3; pictureBox1.Width = Game.BlockImageWidth * Game.PlayingFi
26、eldWidth+3; pictureBox1.Invalidate();/重畫游戲面板區(qū)域 timer1.Enabled = true; button1.Enabled = false; private void button2_Click(object sender, EventArgs e) if (button2.Text = "暫停游戲") timer1.Enabled = false; button2.Text = "繼續(xù)游戲" else timer1.Enabled = true; button2.Text = "暫停游戲&quo
27、t; private void button3_Click(object sender, EventArgs e) this.Close(); private void pictureBox1_Paint(object sender, PaintEventArgs e) /重畫游戲面板 if (game != null) game.DrawPile(e.Graphics); game.DrawCurrentBlock(e.Graphics); private void pictureBox2_Paint(object sender, PaintEventArgs e) /重畫下一個方塊 if
28、(game != null) game.DrawNextBlock(e.Graphics); private void timer1_Tick(object sender, EventArgs e) if (!game.DownCurrentBlock() pictureBox1.Invalidate();/重畫游戲面板區(qū)域 pictureBox2.Invalidate();/重畫下一個方塊 lblScore.Text = game.score.ToString(); if (game.over = true) timer1.Enabled = false; MessageBox.Show("游戲結束,", "提示"); but
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電競酒店的未來發(fā)展路徑與挑戰(zhàn)
- 2022-2027年中國家禽行業(yè)市場調研及未來發(fā)展趨勢預測報告
- 固定蓋板針布行業(yè)行業(yè)發(fā)展趨勢及投資戰(zhàn)略研究分析報告
- 2024河南日用玻璃制品及玻璃包裝容器制造市場前景及投資研究報告
- 防火涂料檢測報告(一)2025
- 單位租車合同范本
- 2025年裝載機斗齒項目可行性研究報告
- 個人兼職加工合同范本
- 加盟合同范例加盟
- 付全款發(fā)貨合同范本
- 跨領域安檢操作標準化的現(xiàn)狀與挑戰(zhàn)
- 2024年08月香港2024年中國銀行(香港)有限公司校園招考筆試歷年參考題庫附帶答案詳解
- 2025年重慶三峽擔保集團招聘筆試參考題庫含答案解析
- 《快遞運營》課件-項目一 快遞運營認知
- keysight眼圖和抖動噪聲基礎知識與測量方法
- TPU材料項目可行性研究報告寫作參考范文
- 試用期考核合格證明表
- 鍋爐補給水陰陽混床操作步序表
- 2005年第4季度北京住房租賃指導價格
- 醫(yī)療器械GMP計算機軟件確認控制程序
- 蘇教版最新五年級數(shù)學上冊應用題與解決問題專項
評論
0/150
提交評論