




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、此程序主要三個小結(jié)講解:1、 設(shè)計方塊類(block.cs)2、 設(shè)計游戲類(game.cs)3、 設(shè)計窗體類(form1.cs)通俗易懂,希望對廣大的學(xué)生們都有幫助歡迎下載技術(shù)qq 1278263100 郵箱1278263100游戲所需圖片:游戲運行圖:話不多說:設(shè)計方塊類(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()/構(gòu)造函數(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設(shè)計游戲類(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 ;/當(dāng)前的俄羅斯方塊 private Block nextBlock ;/下一個的俄羅斯方塊 public int score = 0, lines=0; public bool over=false;/游戲是否結(jié)束 public Game()/Game類構(gòu)造函數(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)/檢查當(dāng)前塊是否為空 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()/檢查游戲是否結(jié)束 over = true; public bool DownCurrentBlock() bool hit = false; currentBlock.Top+; if (currentBlock.
15、Top + currentBlock.Height) > PlayingFieldHeight) hit = true;/當(dāng)前塊觸游戲面板底 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()/旋轉(zhuǎn)方塊 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;/不能旋轉(zhuǎn) 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()/檢查游戲是否結(jié)束 if (currentBlock.Top = 0) return true; else return false; 設(shè)計窗體類(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("游戲結(jié)束,", "提示"); but
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 藝術(shù)品交易中介服務(wù)協(xié)議
- 旅游規(guī)劃與設(shè)計服務(wù)合同
- 隧道機械租賃合同
- 項目團隊月度工作總結(jié)報告書
- 7《一匹出色的馬》教學(xué)設(shè)計-2023-2024學(xué)年二年級下冊語文統(tǒng)編版
- Unit 1 Family Lesson3(教學(xué)設(shè)計)-2024-2025學(xué)年北師大版(三起)(2024)英語三年級上冊
- 第6單元 20陀螺(教學(xué)設(shè)計)2024-2025學(xué)年四年級語文上冊同步教學(xué)(統(tǒng)編版)
- 西寧高鐵u型渠施工方案
- 濟寧鋼質(zhì)入戶門施工方案
- 北京拼接式蓄水池施工方案
- 院感考核表(門-診)
- 建筑工程綜合評估法Ⅱ評標(biāo)辦法
- 鈉離子電池工藝與材料
- 初中語文九年級下冊閱讀理解50篇附答案解析
- 《陶瓷造型工藝》課程標(biāo)準(zhǔn)
- 火電廠各指標(biāo)指標(biāo)解析(最新版)
- 病毒性腦炎患者的護理查房ppt課件
- TPU材料項目可行性研究報告寫作參考范文
- 第二編 債權(quán)總論
- 試用期考核合格證明表
- 常見八種疾病
評論
0/150
提交評論