下面是一个老兄写的俄罗斯方块的类,请教Source数组为什么要那样初始化?tempit[i,0]=temp[i*2]-'0';中的temp[i*2]-'0'又是什么意思?

rerli 2003-10-09 05:25:20
elos.cs:
using System;

namespace MyELOS
{
public class Elos
{

private int itx, ity; //参考坐标
private int rndX, rndY, trndX, trndY; //得到资源的参数
private bool[,] map = new bool[22,12]; //地图
private string[,] source = new string[11,4];//所有方块资源
private int[,] it = new int[4,2]; //当前方块的形状
private int countnumber, nextnum; //总分,一个方块下落的次数
private Random rnd = new Random();

public Elos()
{
//方块资源: | # 7 L S Z |-
source[0,0]="21222324";source[0,1]="12223242";source[0,2]="21222324";source[0,3]="12223242";
source[1,0]="12132223";source[1,1]="12132223";source[1,2]="12132223";source[1,3]="12132223";
source[2,0]="21222332";source[2,1]="11213122";source[2,2]="21222312";source[2,3]="21122232";
source[3,0]="21313233";source[3,1]="31122232";source[3,2]="21222333";source[3,3]="11213112";
source[4,0]="21222333";source[4,1]="11213112";source[4,2]="21313233";source[4,3]="31122232";
source[5,0]="11122223";source[5,1]="12212231";source[5,2]="11122223";source[5,3]="12212231";
source[6,0]="12132122";source[6,1]="11212232";source[6,2]="12132122";source[6,3]="11212232";
}

public void do_chang(int x, int y)
{ //移动
int i;
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=false;
ity+=y;itx+=x; //参照点位置移动
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=true;
if(y==1)++nextnum; //下落次数
}

public bool will_chang(int x, int y)
{ //测试能否移动
int i,tempitx,tempity;
bool falg=false;
tempitx=itx; tempity=ity;
tempitx+=x; tempity+=y;
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=false;
for(i=0;i<4;i++)
{
if (tempitx+it[i,0]<1 || tempitx+it[i,0]>=11) falg=true; //X越界
if (tempity+it[i,1]<1 || tempity+it[i,1]>=21) falg=true; //Y越界
if (map[it[i,1]+tempity,it[i,0]+tempitx]) falg=true; //有阻碍物
}
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=true;
if(falg)return false;
return true;
}

public void do_up()
{ //变形
int i;
string temp;
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=false;
if(++trndY>=4)trndY=0;
temp = source[trndX,trndY];
for(i=0;i<4;i++)
{
it[i,0]=temp[i*2]-'0';
it[i,1]=temp[(i+1)*2-1]-'0';
map[it[i,1]+ity,it[i,0]+itx]=true;
}
}

public bool will_up()
{ //测试能否变形
int i,temprndY;
bool falg=false;
int[,] tempit = new int[4,2];
string temp;
temprndY=rndY;
if(++temprndY>=4)temprndY=0;
temp=source[rndX,temprndY];//获取下一个方块资源
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=false;//先使当前方块消失
//虚变形
for(i=0;i<4;i++)
{
tempit[i,0]=temp[i*2]-'0';
tempit[i,1]=temp[(i+1)*2-1]-'0';
if (itx+tempit[i,0]<=0 || itx+tempit[i,0]>=11) falg=true; //X越界
if (ity+tempit[i,1]<=0 || ity+tempit[i,1]>=21) falg=true; //Y越界
if (map[tempit[i,1]+ity,tempit[i,0]+itx]) falg=true; //有阻碍物
}
for(i=0;i<4;i++)map[it[i,1]+ity,it[i,0]+itx]=true;//还原消失方块
if(falg)return false;
return true;
}

public void do_ini()
{ //每个方块的初使化
string temp;
itx=3;ity=0; //参考坐标值
nextnum=0; //下落次数初使化
trndX=rndX;
trndY=rndY;
rndX = rnd.Next(0,6);
rndY = rnd.Next(0,3);
temp=source[trndX,trndY];
for(int i=0;i<4;i++)
{
it[i,0]=temp[i*2]-'0';
it[i,1]=temp[(i+1)*2-1]-'0';
map[it[i,1]+ity,it[i,0]+itx]=true;
}
}

public void reset()
{ //游戏开始//初使化地图
int i,j;
for(i=0;i<=21;i++)
for(j=0;j<=11;j++)
map[i,j]=true;
for(i=1;i<=20;i++)
for(j=1;j<=10;j++)
map[i,j]=false;
countnumber=0;
rndX = rnd.Next(0,6);
rndY = rnd.Next(0,3);
do_ini(); //第一个方块初使化
}

public void do_cancle()
{ //消去
int i,j,n,k,l,c=0;
for(i=20;i>=1;i--)
{
n=0;
for(j=1;j<=10;j++)if(map[i,j])++n;//检查是否满行
if(n==10)
{
for(k=1;k<=10;k++)map[i,k]=false;//消去一行
for(k=i;k>1;k--) //消去行的上部下移
for(l=1;l<=10;l++)
map[k,l]=map[k-1,l];
for(k=1;k<=10;k++)map[1,k]=false;
i++;c++;
}
}
switch(c)
{ //按消的行得分
case 1:countnumber+=1;break;
case 2:countnumber+=3;break;
case 3:countnumber+=7;break;
case 4:countnumber+=15;break;
}
}

public string Map()
{ //返回地图的值
string ReturnMap="";
for(int i=1;i<=20;i++)
for(int j=1;j<=10;j++)
if(map[i,j])ReturnMap=ReturnMap+'■';
else ReturnMap=ReturnMap+'□';
return ReturnMap;
}

public string next(){return source[rndX,rndY];} //返回下一个要显示的方块
public int count(){ return countnumber; } //得分

public bool isGameover()
{ //游戏是否结束
if(nextnum==0)return true;
return false;
}

}
}

demo.cs:
结合下面的代码可以执行演示。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyELOS
{
/// <summary>
/// demo 的摘要说明。
/// </summary>
public class demo : System.Windows.Forms.Form
{
private Timer ttimer1 = new Timer();
private Elos elosEx = new Elos();
private RichTextBox map = new RichTextBox();

public static int Main(string[] args)
{
Application.Run(new demo());
return 0;
}

public demo()
{
this.Height = 350;
this.Width = 130;
map.Width = 135;
map.Height = 340;
this.Controls.Add(map);
ttimer1.Interval = 500;
ttimer1.Enabled = true;
ttimer1.Tick += new System.EventHandler(this.ttimer1_Tick);
map.KeyDown += new KeyEventHandler(map_KeyDown);
elosEx.reset();
}

protected void ttimer1_Tick (object sender, System.EventArgs e)
{

if(elosEx.will_chang(0,1))
{
elosEx.do_chang(0,1);
draw();
}
else
if(elosEx.isGameover()==false)
{
elosEx.do_cancle();
elosEx.do_ini();
}
else
ttimer1.Enabled = false;
this.Text = elosEx.count().ToString();
}

public void draw()
{
map.Text = elosEx.Map();
}

private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "demo";

}

private void map_KeyDown(Object sender, KeyEventArgs k)
{
switch( (int)k.KeyCode )
{
case 37:if(elosEx.will_chang(-1,0)) elosEx.do_chang(-1,0); break;
case 38:if(elosEx.will_up()) elosEx.do_up(); break;
case 39:if(elosEx.will_chang(1,0)) elosEx.do_chang(1,0); break;
case 40:if(elosEx.will_chang(0,1)) elosEx.do_chang(0,1); break;
case 113:elosEx.reset(); break;
}
draw();
}

}
}
...全文
141 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbcb 2003-10-10
  • 打赏
  • 举报
回复
了解
noremorse 2003-10-09
  • 打赏
  • 举报
回复
1。初始化source是为了设置7种方块的在四个方向的形状。

2。temp是string类型的,temp[n]是char类型的,两个char相减返回在字符表中相差的距离。如:
int i = '2' - '0'; // i = 2
minajo21 2003-10-09
  • 打赏
  • 举报
回复
太长了,不想看了...

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧