




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、此程序主要三個(gè)小結(jié)講解:1、 設(shè)計(jì)方塊類(lèi)(block.cs)2、 設(shè)計(jì)游戲類(lèi)(game.cs)3、 設(shè)計(jì)窗體類(lèi)(form1.cs)通俗易懂,希望對(duì)廣大的學(xué)生們都有幫助歡迎下載技術(shù)qq 1278263100 郵箱1278263100游戲所需圖片:游戲運(yùn)行圖:話不多說(shuō):設(shè)計(jì)方塊類(lèi)(block.cs)程序都有注釋?zhuān)ㄋ滓锥畊sing 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;/存儲(chǔ)方塊部件的形狀,為空白,為有磚塊 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)/黑色格子 /得到繪制這個(gè)格子的在游戲面板中的矩形區(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è)計(jì)游戲類(lèi)(game.cs)using System;using System.Collections.Generic;using System.Text;using System.Drawing;/addnamespace 俄羅斯方塊 class Game public const int BlockImageWidth = 21;/方磚中每個(gè)小方格的大小 public const int BlockImageHeight = 21; public const int PlayingFieldWidth = 10;/游戲面板大小 public const int Playing
9、FieldHeight = 20; private int, pile; /存儲(chǔ)在游戲面板中的所有方磚; private Block currentBlock ;/當(dāng)前的俄羅斯方塊 private Block nextBlock ;/下一個(gè)的俄羅斯方塊 public int score = 0, lines=0; public bool over=false;/游戲是否結(jié)束 public Game()/Game類(lèi)構(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)/左右移動(dòng) bool canMove = true; if (left)/左移動(dòng) 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/右移動(dòng) 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è)計(jì)窗體類(lèi)(form1.cs)下圖:如果覺(jué)得圖片不清楚可以另存為桌面,慢慢研究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();/重畫(huà)游戲面板區(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) /重畫(huà)游戲面板 if (game != null) game.DrawPile(e.Graphics); game.DrawCurrentBlock(e.Graphics); private void pictureBox2_Paint(object sender, PaintEventArgs e) /重畫(huà)下一個(gè)方塊 if
28、(game != null) game.DrawNextBlock(e.Graphics); private void timer1_Tick(object sender, EventArgs e) if (!game.DownCurrentBlock() pictureBox1.Invalidate();/重畫(huà)游戲面板區(qū)域 pictureBox2.Invalidate();/重畫(huà)下一個(gè)方塊 lblScore.Text = game.score.ToString(); if (game.over = true) timer1.Enabled = false; MessageBox.Show("游戲結(jié)束,", "提示"); but
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 臍橙種植合同協(xié)議書(shū)范本
- 體育場(chǎng)塑膠跑道材料的選擇
- 河北承德市雙灤區(qū)圣泉高級(jí)中學(xué)2024-2025學(xué)年高二下學(xué)期4月份月考數(shù)學(xué)試卷(解析)
- 2025年冷氣(N2)推進(jìn)系統(tǒng)合作協(xié)議書(shū)
- 2025年橡膠零件、附件項(xiàng)目建議書(shū)
- 護(hù)理各項(xiàng)小治療操作規(guī)范
- 商業(yè)空間高端定制化精裝修設(shè)計(jì)與施工合同
- 無(wú)人機(jī)土方測(cè)量與施工圖預(yù)算編制合作協(xié)議
- 金融創(chuàng)新企業(yè)股權(quán)分紅激勵(lì)與風(fēng)險(xiǎn)控制協(xié)議
- 美妝專(zhuān)區(qū)品牌合作經(jīng)營(yíng)與區(qū)域市場(chǎng)拓展合同
- 小區(qū)安全排查
- 中國(guó)典籍英譯概述課件
- 【MOOC】保險(xiǎn)學(xué)概論-中央財(cái)經(jīng)大學(xué) 中國(guó)大學(xué)慕課MOOC答案
- 【MOOC】航空發(fā)動(dòng)機(jī)結(jié)構(gòu)分析與設(shè)計(jì)-南京航空航天大學(xué) 中國(guó)大學(xué)慕課MOOC答案
- 紅旅賽道未來(lái)規(guī)劃
- GIS安裝標(biāo)準(zhǔn)化作業(yè)指導(dǎo)書(shū)
- 帶電作業(yè)施工方案
- 宏定義與跨平臺(tái)開(kāi)發(fā)
- 腰椎病護(hù)理措施
- 社保費(fèi)扣費(fèi)協(xié)議書(shū)范文范本下載
- 2024年全國(guó)寄生蟲(chóng)病防治技能競(jìng)賽備賽試題庫(kù)-上(血吸蟲(chóng)病、瘧疾)
評(píng)論
0/150
提交評(píng)論