C#實(shí)現(xiàn)俄羅斯方塊
本文實(shí)例為大家分享了C#實(shí)現(xiàn)俄羅斯方塊的具體代碼,供大家參考,具體內(nèi)容如下
1.調(diào)色板代碼
namespace Tetris
{
class Palette
{
private int _width = 15;//畫板寬度
private int _height = 25;//畫板高度
private Color[,] coorArr;//固定磚塊數(shù)組
private Color disapperColor;//背景色
private Graphics gpPalette;//磚塊活動(dòng)畫板
private Graphics gpReady;//下一個(gè)磚塊樣式畫板
private BlockGroup bGroup;//磚塊生產(chǎn)機(jī)制
private Block runBlock;//正在活動(dòng)的磚塊
private Block readyBlock;//下一個(gè)磚塊
private int rectPix;//單元格像
public delegate void IniCountHandle(int _count,int state);//分?jǐn)?shù)變量
public event IniCountHandle CountEvent;
public static int _Count = 0;
static int mark = 100;//靜態(tài)變量衡量過關(guān)的標(biāo)準(zhǔn)
public static int state = 1;
private System.Timers.Timer timerBlock;//定時(shí)器
private static int timeSpan = 800;//定時(shí)器的時(shí)間間隔
public Palette(int x, int y, int pix, Color dColor, Graphics gp, Graphics gr)//構(gòu)造函數(shù)
{
_width = x;
_height = y;
coorArr = new Color[_width, _height];
disapperColor = dColor;
gpPalette = gp;
gpReady = gr;
rectPix = pix;
}
public void Start()//游戲開始
{
state = 1;
_Count = 0;
bGroup = new BlockGroup();
runBlock = bGroup.GetABlock();
runBlock.XPos = _width / 2;
int y = 0;
for (int i = 0; i < runBlock.Length ; i++)//垂直位置
{
if (runBlock[i].Y > y)
{
y = runBlock[i].Y;
}
}
runBlock.YPos = y;
gpPalette.Clear(disapperColor);//清空畫板
runBlock.Paint(gpPalette);//畫運(yùn)行磚塊
Thread.Sleep(20);
readyBlock = bGroup.GetABlock();
readyBlock.XPos = 2;
readyBlock.YPos = 2;
gpReady.Clear(disapperColor);//清空畫板
readyBlock.Paint(gpReady);
//初始化并啟動(dòng)定時(shí)器
timerBlock = new System.Timers.Timer(timeSpan);
timerBlock.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
timerBlock.AutoReset = true;
timerBlock.Start();
}
public void nextstate()
{
PaintBackground(gpPalette);
timerBlock = new System.Timers.Timer(timeSpan);
timerBlock.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
timerBlock.AutoReset = true;
timerBlock.Start();
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
CheckAndOverBlock();
Down();
}
public bool Down()//磚塊下移
{
int xPos = runBlock.XPos;
int yPos = runBlock.YPos + 1;
for (int i = 0; i < runBlock.Length; i++)
{
if (yPos - runBlock[i].Y > _height - 1)//如果超出下邊界則失敗
return false;
if (!coorArr[xPos + runBlock[i].X, yPos - runBlock[i].Y].IsEmpty)//如果下邊有東西擋則失敗
return false;
}
runBlock.erase(gpPalette);//擦除原來位置磚塊
runBlock.YPos++;
runBlock.Paint(gpPalette);
return true;
}
public void Drop()//丟下磚塊
{
timerBlock.Stop();
while (Down()) ;
timerBlock.Start();
}
public void MoveLeft()//向左移動(dòng)
{
int xPos = runBlock.XPos - 1;
int yPos = runBlock.YPos;
for (int i = 0; i < runBlock.Length; i++)
{
if (xPos + runBlock[i].X < 0)
return;
if (!coorArr[xPos + runBlock[i].X, yPos - runBlock[i].Y].IsEmpty)//如果左邊有東西擋則失敗
return;
}
runBlock.erase(gpPalette);
runBlock.XPos--;
runBlock.Paint(gpPalette);
}
public void MoveRight()//向右移動(dòng)
{
int xPos = runBlock.XPos + 1;
int yPos = runBlock.YPos;
for (int i = 0; i < runBlock.Length; i++)
{
if (xPos + runBlock[i].X > _width -1)//如果超出右邊界則失敗
return;
if (!coorArr[xPos + runBlock[i].X, yPos - runBlock[i].Y].IsEmpty)//如果右邊有東西擋則失敗
return;
}
runBlock.erase(gpPalette);
runBlock.XPos++;
runBlock.Paint(gpPalette);
}
public void DeasilRotate()//順時(shí)針旋轉(zhuǎn)
{
for (int i = 0; i < runBlock.Length; i++)
{
int x = runBlock.XPos + runBlock[i].Y;
int y = runBlock.YPos + runBlock[i].X;
if (x < 0 || x > _width - 1)
return;
if (y < 0 || y > _height - 1)
return;
if (!coorArr[x, y].IsEmpty)
return;
}
runBlock.erase(gpPalette);
runBlock.DeasilRotate();
runBlock.Paint(gpPalette);
}
public void ContraRotate()//逆時(shí)針旋轉(zhuǎn)
{
for (int i = 0; i < runBlock.Length; i++)
{
int x = runBlock.XPos - runBlock[i].Y;
int y = runBlock.YPos - runBlock[i].X;
if (x < 0 || x > _width - 1)
return;
if (y < 0 || y > _height - 1)
return;
if (!coorArr[x, y].IsEmpty)
return;
}
runBlock.erase(gpPalette);
runBlock.ContraRotate();
runBlock.Paint(gpPalette);
}
private void PaintBackground(Graphics gp)//重畫畫板背景
{
gp.Clear(Color.Black);//清空畫板
for (int i = 0; i < _height; i++)
{
for (int j = 0; j < _width; j++)
{
if (!coorArr[j, i].IsEmpty)
{
SolidBrush sb = new SolidBrush(coorArr[j, i]);
gp.FillRectangle(sb, j * rectPix + 1,
i * rectPix + 1,
rectPix - 2,
rectPix - 2);
}
}
}
}
public void PaintPalette(Graphics gp)//重畫整個(gè)畫板
{
PaintBackground(gp);//先畫背景
if (runBlock != null)//再畫活動(dòng)的磚塊
{
runBlock.Paint(gp);
}
}
public void PaintReady(Graphics gp)//重畫下一個(gè)磚塊
{
if (readyBlock != null)
{
readyBlock.Paint(gp);
}
}
public void CheckAndOverBlock()//檢查磚塊是否到底
{
bool over = false;
for (int i = 0; i < runBlock.Length; i++)
{
int x = runBlock.XPos + runBlock[i].X;
int y = runBlock.YPos - runBlock[i].Y;
if (y == _height - 1)
{
over = true;
break;
}
if (!coorArr[x, y + 1].IsEmpty)//如果下面有磚塊,則當(dāng)前磚塊結(jié)束
{
over = true;
break;
}
}
if (over)//如果當(dāng)前磚塊已經(jīng)結(jié)束
{
for (int i = 0; i < runBlock.Length; i++)//把當(dāng)前磚塊歸入coordinatearr
{
coorArr[runBlock.XPos + runBlock[i].X, runBlock.YPos - runBlock[i].Y] = runBlock.BlockColor;
}
//檢查是否有滿行現(xiàn)象,如果有,則刪除
CheckAndDelFullRow();
//產(chǎn)生新的磚塊
runBlock = readyBlock;//新的磚塊為準(zhǔn)備好的磚塊
runBlock.XPos = _width / 2;
int y = 0;
for (int i = 0; i < runBlock.Length; i++)//垂直位置
{
if (runBlock[i].Y > y)
{
y = runBlock[i].Y;
}
}
runBlock.YPos = y;
//檢查新產(chǎn)生的磚塊所占用的地方是否已經(jīng)有磚塊,如果有,則游戲結(jié)束
for (int i = 0; i < runBlock.Length; i++)
{
if (!coorArr[runBlock.XPos + runBlock[i].X, runBlock.YPos - runBlock[i].Y].IsEmpty)
{
//游戲結(jié)束
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
gpPalette.DrawString("GAME OVER"+"\r\n"+"得分:" + _Count.ToString()+"\r\n等級(jí):"+state .ToString (),
new Font("Arial Black", 25f),
new SolidBrush(Color.Yellow ),
new RectangleF(0, _height * rectPix / 2 - 100, _width * rectPix, 150),
drawFormat);
timerBlock.Stop();
return;
}
}
runBlock.Paint(gpPalette);
//獲取新的準(zhǔn)備磚塊
readyBlock = bGroup.GetABlock();
readyBlock.XPos = 2;
readyBlock.YPos = 2;
gpReady.Clear(Color.Black);
readyBlock.Paint(gpReady);
}
}
private void CheckAndDelFullRow()//檢查并刪除滿行
{
//找出當(dāng)前磚塊所在行范圍
int lowRow = runBlock.YPos - runBlock[0].Y;//lowRow代表當(dāng)前磚塊的Y軸的最小值
int highRow = lowRow;//highRow代表當(dāng)前磚塊的y軸的最大值
for (int i = 0; i < runBlock.Length;i++ )//找出當(dāng)前磚塊所占行的范圍,放入low,high變量內(nèi)
{
int y = runBlock.YPos - runBlock[i].Y;
if (y < lowRow)
{
lowRow = y;
}
if (y > highRow)
{
highRow = y;
}
}
int count=0;
bool repaint = false;//判斷是否重畫
for (int i = lowRow; i <= highRow; i++)//檢查是否滿行
{
bool rowFull = true;
for (int j = 0; j < _width; j++)
{
if (coorArr[j, i].IsEmpty)//如果有一個(gè)單元格為空,說明這一行不可能為滿行
{
rowFull = false;
break;
}
}
if (rowFull)//如果滿行,則刪除這一行
{
count ++;
repaint = true;//如果刪除,則需重畫
for (int k = i; k > 0; k--)
{
for (int j = 0; j < _width; j++)
{
coorArr[j, k] = coorArr[j, k - 1];
}
}
for (int j = 0; j < _width; j++)//清空第0行
{
coorArr[j, 0] = Color.Empty;
}
}
}
//計(jì)算每滿幾行加的分值
if (count == 1)
{
_Count += 10;
CountEvent(_Count,state);
}
if (count == 2)
{
_Count += 30;
CountEvent(_Count,state);
}
if (count == 3)
{
_Count += 60;
CountEvent(_Count,state );
}
if (count >= 4)
{
_Count += 80;
CountEvent(_Count,state);
}
if (repaint)//重畫
{
PaintBackground(gpPalette);
}
if (_Count >= 1000)//如果通關(guān)初始化為原來的值
{
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
gpPalette.DrawString("O(∩_∩)O"+"\r\n"+"***通關(guān)***"+"\r\n"+"按“開始”按鈕\r\n王者歸來",
new Font("Arial Black", 20f),
new SolidBrush(Color.Red),
new RectangleF(0, _height * rectPix / 2 - 100, _width * rectPix, 120),
drawFormat);
timeSpan = 800;
state = 1;
_Count = 0;
timerBlock.Close();
}
if (_Count >= mark)
{
mark += 100;
timeSpan -= 70;
state++;
CountEvent(_Count ,state);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
gpPalette.DrawString("\tO(∩_∩)O~\r\n***恭喜過關(guān)***\r\n按“下一關(guān)”按鈕\r\n開始新的遠(yuǎn)征",
new Font("Arial Black", 20f),
new SolidBrush(Color.DodgerBlue ),
new RectangleF(0, _height * rectPix / 2 - 80, _width * rectPix, 120),
drawFormat);
timerBlock.Stop();//關(guān)閉計(jì)時(shí)器
}
}
public void Pause()//暫停
{
if (timerBlock.Enabled == true)
{
timerBlock.Enabled = false;
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
gpPalette.DrawString("暫停" + "\r\n" + "得分:" + _Count.ToString(),
new Font("Arial Black", 25f),
new SolidBrush(Color.Aqua),
new RectangleF(0, _height * rectPix / 2 - 100, _width * rectPix, 100),
drawFormat);
}
}
public void EndPause()//結(jié)束暫停
{
if (timerBlock.Enabled == false)
{
timerBlock.Enabled = true;
PaintBackground(gpPalette);
}
}
public void Close()//關(guān)閉
{
timerBlock.Close();
gpPalette.Dispose();//釋放畫布
gpReady.Dispose();
}
}
}
2.保存信息數(shù)組代碼
namespace Tetris
{
class InfoArr
{
private ArrayList info = new ArrayList();//存放多個(gè)BlockInfo累得的數(shù)組
private int _length = 0;//存放Arraylist的長度,以供訪問
public int Length
{
get
{
return _length;
}
}
public BlockInfo this[int index]//索引器,根據(jù)下標(biāo),返回一個(gè)blockinfo的值
{
get
{
return (BlockInfo)info[index];
}
}
public string this[string id]//索引器,根據(jù)一個(gè)字符串的id值下標(biāo),給相應(yīng)id的顏色賦值
{
set
{
if (value == "")
{
return;
}
for (int i = 0; i < info.Count; i++)
{
if (((BlockInfo)info[i]).GetIdStr() == id)
{
try
{
((BlockInfo)info[i]).BColor = Color.FromArgb(Convert.ToInt32(value));
}
catch (System.FormatException)
{
MessageBox.Show("顏色信息錯(cuò)誤!請(qǐng)刪除BlockSet.xml文件,并重新啟動(dòng)程序,很抱歉給您帶來麻煩", "錯(cuò)誤信息",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
}
public BitArray StrToBit(string id)//把字符串轉(zhuǎn)換為bitArray
{
if (id.Length != 25)
{
throw new System.FormatException("磚塊樣式信息不合法!請(qǐng)刪除BlockSet.xml文件,并重新啟動(dòng)程序");
}
BitArray ba = new BitArray(25);
for (int i = 0; i < 25; i++)
{
ba[i] = (id[i] == '0') ? false : true;
}
return ba;
}
public void Add(BitArray id, Color bColor)//添加一個(gè)磚塊信息
{
if (id.Length != 25)
{
throw new System.FormatException("磚塊樣式信息不合法!請(qǐng)刪除blockset.xml文件,并重新啟動(dòng)程序");
}
info.Add(new BlockInfo(id, bColor));//給動(dòng)態(tài)數(shù)組info添加一個(gè)磚塊信息
_length++;//長度加一
}
public void Add(string id, string bColor)
{
Color temp;
if (!(bColor == ""))
{
temp = Color.FromArgb(Convert.ToInt32(bColor));//把字符串轉(zhuǎn)換為顏色類
}
else
{
temp = Color.Empty;
}
info.Add(new BlockInfo(StrToBit(id), temp));//把字符串轉(zhuǎn)換為bitarray類
_length++;
}
}
}
3.方塊信息代碼
namespace Tetris
{
class BlockInfo
{
private BitArray _id;//存放磚塊樣式
private Color _bColor;//存放顏色信息
public BlockInfo(BitArray id, Color bColor)//構(gòu)造函數(shù),給似有函數(shù)變量賦值
{
_id = id;
_bColor = bColor;
}
public BitArray ID
{
get
{
return _id;
}
set
{
_id = value;
}
}
public Color BColor
{
get
{
return _bColor;
}
set
{
_bColor = value;
}
}
public string GetIdStr()
{
StringBuilder s = new StringBuilder(25);
foreach (bool b in _id)
{
s.Append(b ? "1" : "0");
}
return s.ToString();
}
public string GetColorStr()
{
return Convert.ToString(_bColor.ToArgb());
}
}
}
4.方塊組代碼
namespace Tetris
{
class BlockGroup
{
private InfoArr info;//存放所有磚塊樣式信息
private Color disapperColor;//背景色
private int rectPix;//單元格像素
public BlockGroup()//構(gòu)造函數(shù)
{
Config config = new Config();
config.LoadFromXmlFile();
info = new InfoArr();
info = config.Info;
disapperColor = config.BackColor;
rectPix = config.RectPix;
}
public Block GetABlock()//從磚塊組中隨機(jī)抽取一個(gè)磚塊樣式并返回
{
Random rd = new Random();//聲明一個(gè)產(chǎn)生隨機(jī)數(shù)的類
int keyOrder = rd.Next(0, info.Length);//產(chǎn)生一個(gè)隨機(jī)數(shù)
BitArray ba = info[keyOrder].ID;//把抽取出的磚塊樣式賦給BitArray類對(duì)象ba
int struNum = 0;//確定這個(gè)磚塊樣式中被填充方塊的個(gè)數(shù)
foreach (bool b in ba)//需要確定point數(shù)組的長度
{
if (b)
{
struNum++;
}
}
Point[] structArr = new Point[struNum];//新建一個(gè)point數(shù)組,并確定其長度,以創(chuàng)建新的block
int k = 0;
for (int j = 0; j < ba.Length; j++)//用循環(huán)給point數(shù)組structarr賦坐標(biāo)值
{
if (ba[j])
{
structArr[k].X = j / 5 - 2;
structArr[k].Y = 2 - j % 5;
k++;
}
}
return new Block(structArr, info[keyOrder].BColor, disapperColor, rectPix);//創(chuàng)建一個(gè)新磚塊并返回
}
}
5.方塊的基本屬性代碼
namespace Tetris
{
class Block
{
protected Point[] structArr;//存放磚塊組成信息的坐標(biāo)數(shù)組
protected int _xPos;//磚塊中心點(diǎn)所在的X坐標(biāo)
protected int _yPos;//磚塊中心點(diǎn)所在的y坐標(biāo)
protected Color _blockColor;//磚塊顏色
protected Color disapperColor;//擦除顏色
protected int rectPix;//每單元格像素
public Block()//默認(rèn)構(gòu)造函數(shù),聲明此構(gòu)造函數(shù)是為了子類能創(chuàng)建
{
}
public Block(Point[] sa, Color bColor, Color dColor, int pix)
{
//重載構(gòu)造函數(shù),給成員變量賦值
_blockColor = bColor;
disapperColor = dColor;
rectPix = pix;
structArr = sa;
}
public Point this[int index]//索引器,根據(jù)索引訪問磚塊里的小方塊坐標(biāo)
{
get
{
return structArr[index];
}
}
public int Length//屬性,表示structArr的長度
{
get
{
return structArr.Length;
}
}
#region 成員變量相應(yīng)的屬性
public int XPos
{
get
{
return _xPos;
}
set
{
_xPos = value;
}
}
public int YPos
{
get
{
return _yPos;
}
set
{
_yPos = value;
}
}
public Color BlockColor
{
get
{
return _blockColor;
}
}
#endregion
public void DeasilRotate()//順時(shí)針旋轉(zhuǎn)
{
int temp;
for (int i = 0; i < structArr.Length; i++)
{
temp = structArr[i].X;
structArr[i].X = structArr[i].Y;
structArr[i].Y = -temp;
}
}
public void ContraRotate()//逆時(shí)針旋轉(zhuǎn)
{
int temp;
for (int i = 0; i < structArr.Length; i++)
{
temp = structArr[i].X;
structArr[i].X = -structArr[i].Y;
structArr[i].Y = temp;
}
}
private Rectangle PointToRect(Point p)//把坐標(biāo)點(diǎn)轉(zhuǎn)化為畫布的坐標(biāo)值
{
return new Rectangle((_xPos + p.X) * rectPix + 1,
(_yPos - p.Y) * rectPix + 1,
rectPix - 2,
rectPix - 2);
}
public virtual void Paint(Graphics gp)//在指定畫板下繪制磚塊
{
SolidBrush sb = new SolidBrush(_blockColor );
foreach (Point p in structArr)
{
lock (gp)
{
gp.FillRectangle(sb, PointToRect(p));
}
}
}
public void erase(Graphics gp)//擦除矩形
{
SolidBrush sb = new SolidBrush(disapperColor);
foreach (Point p in structArr)
{
lock (gp)
{
gp.FillRectangle(sb, PointToRect(p));
}
}
}
}
}
6.俄羅斯方塊窗體代碼
namespace Tetris
{
public partial class FrmTetris : Form
{
public FrmTetris()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private Palette p;
private Keys downKey;
private Keys dropKey;
private Keys moveLeftKey;
private Keys moveRightKey;
private Keys deasilRotateKey;
private Keys contraRotateKey;
private int paletteWidth;
private int paletteHeight;
private Color paletteColor;
private int rectPix;
private void btnStart_Click(object sender, EventArgs e)
{
lblState.Text = "等級(jí):1";
lblcount.Text = "得分:0";
if (p != null)
{
p.Close();
}
p = new Palette(paletteWidth, paletteHeight, rectPix, paletteColor,
Graphics.FromHwnd(pbRun.Handle),
Graphics.FromHwnd(lblReady.Handle));
p.CountEvent += new Palette.IniCountHandle(p_CountEvent);
p.Start();
}
void p_CountEvent(int _count,int state)
{
lblcount.Text = "得分:"+_count.ToString();
lblState.Text = "等級(jí):" + state.ToString();
}
private void pbRun_Paint(object sender, PaintEventArgs e)
{
if (p != null)
{
p.PaintPalette(e.Graphics);
}
}
private void lblReady_Paint(object sender, PaintEventArgs e)
{
if (p != null)
{
p.PaintReady(e.Graphics);
}
}
private void FrmTetris_Load(object sender, EventArgs e)
{
//讀取xml文件中的參數(shù)配置信息,并依次賦給似有成員變量
Config config = new Config();
config.LoadFromXmlFile();
downKey = config.DownKey;
dropKey = config.DropKey;
moveLeftKey = config.MoveLeftKey;
moveRightKey = config.MoveRightKey;
deasilRotateKey = config.DeasilRotateKey;
contraRotateKey = config.ContraRotateKey;
paletteWidth = config.CoorWidth;
paletteHeight = config.CoorHeight;
paletteColor = config.BackColor;
rectPix = config.RectPix;
//根據(jù)畫板的長度和寬度信息動(dòng)態(tài)改變窗體及畫板的規(guī)格
this.Width = paletteWidth * rectPix + 215;
this.Height = paletteHeight * rectPix + 38;
pbRun.Width = paletteWidth * rectPix;
pbRun.Height = paletteHeight * rectPix;
}
private void FrmTetris_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 32)//屏蔽回車鍵
{
e.Handled = true;
}
if (e.KeyCode == downKey)//下降
{
p.Down();
}
else if (e.KeyCode == dropKey)
{
p.Drop();
}
else if (e.KeyCode == moveLeftKey)
{
p.MoveLeft();
}
else if (e.KeyCode == moveRightKey)
{
p.MoveRight();
}
else if (e.KeyCode == deasilRotateKey)
{
p.DeasilRotate();
}
else if (e.KeyCode == contraRotateKey)
{
p.ContraRotate();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (p == null)
{
return;
}
if (btnPause.Text == "暫停")
{
p.Pause();
btnPause.Text = "繼續(xù)";
}
else
{
p.EndPause();
btnPause.Text = "暫停";
}
}
private void btnConfig_Click(object sender, EventArgs e)
{
if (btnPause.Text == "暫停")
{
btnPause.PerformClick();
}
using (Frmconfig frmconfig= new Frmconfig())
{
frmconfig.ShowDialog();
}
}
private void FrmTetris_FormClosing(object sender, FormClosingEventArgs e)
{
if (p !=null )
{
p.Close ();
}
}
private void button5_Click(object sender, EventArgs e)
{
p.Down();
}
private void button1_Click(object sender, EventArgs e)
{
p.MoveLeft();
}
private void button2_Click(object sender, EventArgs e)
{
p.MoveRight();
}
private void button3_Click(object sender, EventArgs e)
{
p.DeasilRotate();
}
private void button4_Click(object sender, EventArgs e)
{
p.ContraRotate();
}
private void button6_Click(object sender, EventArgs e)
{
p.Drop();
}
private void button7_Click(object sender, EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.AddExtension = true;
ofDialog.CheckFileExists = true;
ofDialog.CheckPathExists = true;
//the next sentence must be in single line
ofDialog.Filter = "MP3文件(*.mp3)|*.mp3|Audio文件(*.avi)|*.avi|VCD文件(*.dat)|*.dat|WAV文件(*.wav)|*.wav|所有文件 (*.*)|*.*";
ofDialog.DefaultExt = "*.mp3";
if (ofDialog.ShowDialog() == DialogResult.OK)
{
this.axWindowsMediaPlayer1.URL = ofDialog.FileName;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime dt = DateTime.Now; //當(dāng)前時(shí)間的實(shí)例;
lbltime.Text = dt.ToString(); //轉(zhuǎn)為string類型 把值交給lbltime的Text屬性;
}
private void button8_Click(object sender, EventArgs e)
{
p.CountEvent += new Palette.IniCountHandle(p_CountEvent);
p.nextstate();
}
}
}
7.游戲設(shè)置窗體代碼
namespace Tetris
{
public partial class Frmconfig : Form
{
public Frmconfig()
{
InitializeComponent();
}
private bool[,] struArr = new bool[5, 5];
private Color blockColor = Color.Red;
private void lblMode_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
gp.Clear(Color.Black);
Pen p = new Pen(Color.White);
for (int i = 31; i < 155; i = i + 31)
gp.DrawLine(p, 1, i, 155, i);
for (int i = 31; i < 155; i = i + 31)
gp.DrawLine(p, i, 1, i, 155);
//填充顏色
SolidBrush s = new SolidBrush(blockColor);
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
if (struArr[x, y])
{
gp.FillRectangle(s, 31 * x + 1, 31 * y + 1, 30, 30);
}
}
}
}
private void lblMode_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
int xPos, yPos;
xPos = e.X / 31;
yPos = e.Y / 31;
struArr[xPos, yPos] = !struArr[xPos, yPos];
bool b = struArr[xPos, yPos];
Graphics gp = lblMode.CreateGraphics();
SolidBrush s = new SolidBrush(b ? blockColor : Color.Black);
gp.FillRectangle(s, 31 * xPos + 1, 31 * yPos + 1, 30, 30);
gp.Dispose();
}
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# 在windows中操作IIS設(shè)置FTP服務(wù)器的示例
這篇文章主要介紹了c# 在windows中操作IIS設(shè)置FTP服務(wù)器的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#實(shí)現(xiàn)排列組合算法完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下2014-09-09
C#利用ASP.NET?Core開發(fā)學(xué)生管理系統(tǒng)詳解
隨著技術(shù)的進(jìn)步,跨平臺(tái)開發(fā)已經(jīng)成為了標(biāo)配,在此大背景下,ASP.NET?Core也應(yīng)運(yùn)而生。本文主要利用ASP.NET?Core開發(fā)一個(gè)學(xué)生管理系統(tǒng),感興趣的可以學(xué)習(xí)一下2022-01-01
基于WPF實(shí)現(xiàn)3D畫廊動(dòng)畫效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)簡單的3D畫廊動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
使用WPF實(shí)現(xiàn)加載動(dòng)畫效果
在應(yīng)用程序加載大量數(shù)據(jù)或執(zhí)行復(fù)雜操作時(shí),為用戶提供一個(gè)良好的加載體驗(yàn)變得至關(guān)重要,加載動(dòng)畫是其中一個(gè)有效的方式,下面我們就來看看如何使用WPF實(shí)現(xiàn)簡單的加載動(dòng)畫效果吧2024-03-03
C#中的隨機(jī)數(shù)函數(shù)Random()
這篇文章介紹了C#生成隨機(jī)數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#?TaskScheduler任務(wù)調(diào)度器的實(shí)現(xiàn)
本文主要介紹了C#?TaskScheduler任務(wù)調(diào)度器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-05-05

