资源描述
.
此程序主要三个小结讲解:
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;//add
namespace 俄罗斯方块
{
public class Block
{
private short width;
private short height;
private short top;
private short left;
private int ID; //方块部件的ID
public int[,] shape; //存储方块部件的形状,0为空白,1为有砖块
public Block()//构造函数
{
Random randomGenerator = new Random();
int randomBlock = randomGenerator.Next(1, 5);//产生1—4的数
this.ID = randomBlock;
switch (this.ID)
{
case 1: //横条形
this.Width = 4;
this.Height = 1;
this.Top = 0;
this.Left = 3;
shape = new int[this.Width, this.Height];
shape[0, 0] = 1; shape[1, 0] = 1;
shape[2, 0] = 1; shape[3, 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 int[this.Width, this.Height];
shape[0, 0] = 1; shape[0, 1] = 1;
shape[1, 0] = 1;shape[1, 1] = 1;
break;
case 3: //T形
this.Width = 3;
this.Height = 3;
this.Top = 0;
this.Left = 4;
// Creates the new shape for this block.
shape = new int[this.Width, this.Height];
shape[0, 0] = 1; shape[1, 0] = 1;
shape[2, 0] = 1; shape[1, 1] = 1;
shape[1, 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 int[this.Width, this.Height];
shape[0, 0] = 1; shape[0, 1] = 1;
shape[0, 2] = 1; shape[1, 2] = 1;
break;
}
}
public short Width//Width属性
{
get
{
return width;
}
set
{
width = value;
}
}
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 < this.Width; i++)
{
for (int j = 0; j < this.Height; j++)
{
if (this.shape[i, j] == 1)//黑色格子
{
//得到绘制这个格子的在游戏面板中的矩形区域
Rectangle rect = new Rectangle((this.Left + i) * Game.BlockImageWidth, (this.Top + j) * Game.BlockImageHeight, Game.BlockImageWidth, Game.BlockImageHeight);
g.DrawImage(brickImage, rect);
}
}
}
}
}//class Block
}
设计游戏类(game.cs)
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;//add
namespace 俄罗斯方块
{
class Game
{
public const int BlockImageWidth = 21;//方砖中每个小方格的大小
public const int BlockImageHeight = 21;
public const int PlayingFieldWidth = 10;//游戏面板大小
public const int PlayingFieldHeight = 20;
private int[,] pile; //存储在游戏面板中的所有方砖;
private Block currentBlock ;//当前的俄罗斯方块
private Block nextBlock ;//下一个的俄罗斯方块
public int score = 0, lines=0;
public bool over=false;//游戏是否结束
public Game()//Game类构造函数
{
pile = new int[PlayingFieldWidth , PlayingFieldHeight];
ClearPile();
CreateNewBlock();//产生新的俄罗斯方块
}
private void ClearPile()//清空游戏面板中的所有方砖
{
for (int i = 0; i < PlayingFieldWidth ; i++)
{
for (int j = 0; j < PlayingFieldHeight ; j++)
{
pile[i, j]= 0;
}
}
}
private void CreateNewBlock()//产生新的俄罗斯方块
{
if (this.nextBlock != null)
{
currentBlock = nextBlock;
}
else
{
currentBlock = new Block();
}
nextBlock = new Block();
}
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 (pile[i, j] == 1)
{
Rectangle rect = new Rectangle(i * BlockImageWidth, j * BlockImageHeight, BlockImageWidth, 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 = nextBlock.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 j = 0; j < currentBlock.Height; j++)
{
int fx, fy;
fx = currentBlock.Left + i;
fy = currentBlock.Top + j;
if (currentBlock.shape[i, j] ==1)
{
pile[fx, fy] = 1;
}
}
}
CheckForLines();
if (CheckForGameOver())//检查游戏是否结束
over = true;
}
public bool DownCurrentBlock()
{
bool hit = false;
currentBlock.Top++;
if ((currentBlock.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.shape[i, j] == 1) && (pile[fx, fy] == 1))//(fy + 1)
{
hit = true;
}
}
}
}
if (hit)//触到其他已落方块或游戏面板底
{
currentBlock.Top--;
MoveBlockToPile();//固定到游戏面板上
CreateNewBlock(); //产生新的俄罗斯方块
}
return hit;
}
public void RotateCurrentBlock()//旋转方块
{
bool canRotate = true;
short newWidth = 0;
short newHeight = 0;
int[,] newShape;
newWidth = currentBlock.Height;
newHeight = currentBlock.Width;
newShape = new int[newWidth, 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.Height; j++)
{
x = ((currentBlock.Height - 1) - j);
y = i;
newShape[x, y] = currentBlock.shape[i, j];
if (newShape[x, y] == 1 && pile[x + currentBlock.Left, y + currentBlock.Top] == 1)
{
canRotate = false; return;//不能旋转 }
}
}
}
if (canRotate)
{
currentBlock.Width = newWidth;
currentBlock.Height = newHeight;
currentBlock.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;
if ((currentBlock.shape[i, 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;
fx = currentBlock.Left + i;
fy = (currentBlock.Top + 1) + j;
if ((currentBlock.shape[i, j] == 1) && (pile[(fx + 1), fy]==1))
{
canMove = false;
}
}
}
if (canMove)
{
currentBlock.Left++;
}
}
}
}
private int CheckForLines()//检查是否满行并消去
{
int numLines = 0;
int[] completeLines = new int[PlayingFieldHeight];
for (int j = PlayingFieldHeight-1; j > 0; j--)//j = PlayingFieldHeight
{
bool fullLine = true;
for (int i = 0; i < PlayingFieldWidth; i++)
{
if (pile[i, j] == 0)
{
fullLine = false;
break;
}
}
if (fullLine)
{
numLines++;
completeLines[numLines] = j;
}
}
if (numLines > 0)
{
for (int i = 1; i <= numLines; i++)
{
ClearLine((completeLines[i] + (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++)
{
pile[i, j] = pile[i, (j - 1)];
}
}
for (int i = 0; i < PlayingFieldWidth; i++)
{
pile[i, 0] = 0;
}
}
public 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;
namespace 俄罗斯方块
{
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.PlayingFieldWidth+3;
pictureBox1.Invalidate();//重画游戏面板区域
timer1.Enabled = true;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "暂停游戏")
{
timer1.Enabled = false; button2.Text = "继续游戏";
}
else
{
timer1.Enabled = true; button2.Text = "暂停游戏";
}
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//重画游戏面板
if (game != null)
{
展开阅读全文