private void panel1_Paint(object sender, PaintEventArgs e)
{
if (objBox != null)
{
int t = 0;
int tmp = objBox.boxType; Graphics g = panel1.CreateGraphics();
g.Clear(Color.Black); for (int i = 15; i >= 0; i--)
{
t = tmp & 1;
tmp = tmp >> 1;
if (t == 1)
objBox.DrawBoxs(i % 4, i / 4, g);
}
//重绘背景区域
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
if (BackGround[i, j] == 1)
{
g.DrawRectangle(Pens.Wheat, j* ConstData.BOX_SIZE, i* ConstData.BOX_SIZE,
ConstData.BOX_SIZE, ConstData.BOX_SIZE);
g.FillRectangle(Brushes.Green, j * ConstData.BOX_SIZE + 1, i* ConstData.BOX_SIZE + 1,
ConstData.BOX_SIZE - 1, ConstData.BOX_SIZE - 1);
}
}
}
g.Dispose();
}
} private void timer1_Tick(object sender, EventArgs e)
{
if (objBox.OutRange(ConstData.DOWN) && Check(ConstData.DOWN))
{
objBox.yCurrentPos++;
panel1.Invalidate();
} else
{
ChangeBack();
Random rnd = new Random();
if (objBoxPre != null)
{
int n = rnd.Next(0, 6);
int b = Boxes[n, 0];
objBox = objBoxPre;
objBoxPre = new TerrisBox(b);
panel2.Invalidate();
}
else
{
int k = rnd.Next(0, 6);
int m = Boxes[k, 0];
objBox = new TerrisBox(m);
} if (Check(ConstData.NEW) == false)
{
label3.Text = "Game Over!";
button1.Enabled = true;
timer1.Stop();
}
panel1.Invalidate();
}
} private bool Check(int direction)//检查是否有障碍,0:无障碍,1:有障碍
{
bool moveable = true;
int type = objBox.boxType; //获得方块数据
int t = 0;
int x = objBox.xCurrentPos;
int y = objBox.yCurrentPos;
for (int i = 15; i >= 0; i--)
{
t = type & 1;
type = type >> 1; switch (direction)
{
case ConstData.DOWN:
if (t == 1 && BackGround[y + i / 4, x - 1 + i % 4] == 1)
moveable = false;
break;
case ConstData.RIGHT :
if (t == 1 && BackGround[y-1 + i / 4 , x +i%4 ] == 1)
moveable = false;
break;
case ConstData.LEFT :
if (t == 1 && BackGround [ y-1 + i / 4-1 ,x-2+i%4] == 1)
moveable = false;
break;
case ConstData.NEW :
if(t==1&&BackGround[y-1+i/4,x-1+i%4]==1)
moveable=false;
break; }
if (moveable == false) break;
}
return moveable;
} private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Right:
if (objBox.OutRange(ConstData.RIGHT) && Check(ConstData.RIGHT))
{
objBox.xCurrentPos++;
panel1.Invalidate();
}
break;
case Keys.Left:
if (objBox.OutRange(ConstData.LEFT) && Check(ConstData.LEFT))
{
objBox.xCurrentPos--;
panel1.Invalidate();
}
break;
case Keys.Down:
if (objBox.OutRange(ConstData.DOWN) && Check(ConstData.DOWN))
{
objBox.yCurrentPos++;
panel1.Invalidate();
}
break;
default:
break;
}
}
private void ChangeBack( )
{
int type = objBox.boxType;
int t = 0;
int x = objBox.xCurrentPos;
int y = objBox.yCurrentPos;
for (int i = 15; i >=0; i--)
{
t = type & 1;
type = type >> 1;
if (t == 1)
BackGround[y - 1 + i / 4, x - 1 + i % 4] = 1;
}
} private void panel2_Paint(object sender, PaintEventArgs e)
{
if (objBoxPre != null)
{
int t = 0;
int tmp = objBoxPre.boxType;
Graphics g = panel2.CreateGraphics();
g.Clear(Color.Black);
for (int i = 15; i >= 0; i--)
{
t = tmp & 1;
tmp = tmp >> 1;
if (t == 1)
objBoxPre.DrawBoxs(i % 4, i / 4, g);
}
g.Dispose();
}
}
}
}
这个程序 再这不能放全。还是下载下来看看 怎么修改好。。大家都来帮帮忙吧
要求实现 向上方向键旋转 (带上思想跟注释) 修改Panel2的显示问题感谢帮助了。先 等待 如果修改好了 发到邮箱 [email protected] 注明你论坛的名字 好加分!下载地址:我放在了 CSDN的下载频道 下载不需要积分 连接:http://zhu890103.download.csdn.net/ 下载最新的那个资源
2009-4-22的是以前的资源 没实现Panel2的显示的
2009-5-4的是最新的俄罗斯的 资源刚放上可能还没显示。。1楼不知道能不能吧文件放上·····
IShape接口using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;namespace Shapes
{
public enum Direction
{
Up = 0,
Down = 1,
Left = 2,
Right = 3
}; /// <summary>
/// IShape
/// All Shapes should be derived from this
/// </summary>
public interface IShape : IDirection
{
Color Color { get; set; }
Point[] Points { get; }
Point[] LastPoints { get; }
Rectangle[] Rects { get; } Point[] RotatedCoordinate { get; }
Point[] CalRotateCoordinate();
//Direction Direction { get; set; }
//Point CenterPoint { get; }
Size PerSize { get; }
Size ShapeSize { get; set; } void DrawPreview();
void Draw();
void Erasure();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.Core;namespace Shapes
{
public abstract class AbstractShape : IShape
{
#region Fields public event EventHandler Lefted;
public event EventHandler Righted;
public event EventHandler Downed;
public event EventHandler Rotated; Point[] m_Points = null;
Point[] m_RotatedCoor = null;
Rectangle[] m_Rects = null;
Point[] m_LastPoints = null;
Size m_PerSize = new Size(20, 20);
#endregion #region constructors
public AbstractShape()
{
m_Points = new Point[4];
m_Rects = new Rectangle[4];
m_LastPoints = new Point[4];
m_RotatedCoor = new Point[4];
CurrentDirection = Direction.Up;
ShapeSize = new Size(30, 30);
Color = Color.Red;
}
public AbstractShape(Control Owner)
: this()
{
this.Owner = Owner;
}
#endregion #region Properties
public Control Owner
{
get;
set;
}
public Size ShapeSize
{
get;
set;
}
#endregion #region methods protected virtual void OnLefted(EventArgs e)
{
EventHandler eh = Lefted;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnRighted(EventArgs e)
{
EventHandler eh = Righted;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnDowned(EventArgs e)
{
EventHandler eh = Downed;
if (eh != null)
{
eh(this, e);
}
}
protected virtual void OnUped(EventArgs e)
{
EventHandler eh = Rotated;
if (eh != null)
{
eh(this, e);
}
} private void Move(int X, int Y)
{
try
{
for (int i = 0; i < 4; i++)
{
m_LastPoints[i] = m_Points[i]; m_Points[i].X += X;
m_Points[i].Y += Y;
} Erasure();
}
catch (Exception ex)
{
LoggingService.Error(ex);
}
finally
{
Draw();
}
}
#endregion #region IShape Members public Color Color
{
get;
set;
} public Point[] Points
{
get { return m_Points; }
}
public Point[] LastPoints
{
get
{
return m_LastPoints;
}
} //public Point CenterPoint
//{
// get
// {
// int x = 0;
// int y = 0;
// }
//} public Size PerSize
{
get
{
return m_PerSize;
}
} public Rectangle[] Rects
{
get { return m_Rects; }
} public virtual void DrawPreview()
{
Graphics g = Owner.CreateGraphics();
g.FillRectangle(new SolidBrush(Color.Black), 0, 0, Owner.Width, Owner.Height);
int wid = m_PerSize.Width; foreach (Point p in Points)
{
g.FillRectangle(new SolidBrush(Color), (p.X - 2) * m_PerSize.Width - 10, p.Y * m_PerSize.Height + 15, m_PerSize.Width - 1, m_PerSize.Height - 1);
}
g.Dispose();
} public virtual void Draw()
{
Graphics g = Owner.CreateGraphics();
foreach (Point p in Points)
{
g.FillRectangle(new SolidBrush(Color), (p.X - 1) * ShapeSize.Width, p.Y * ShapeSize.Height, ShapeSize.Width - 1, ShapeSize.Height - 1);
}
g.Dispose();
} public virtual void Erasure()
{
Graphics g = Owner.CreateGraphics();
foreach (Point p in m_LastPoints)
{
g.FillRectangle(new SolidBrush(Owner.BackColor), (p.X - 1) * ShapeSize.Width, p.Y * ShapeSize.Height, ShapeSize.Width - 1, ShapeSize.Height - 1);
}
g.Dispose();
}
#endregion #region IDirection Members public Point[] RotatedCoordinate
{
get
{
return m_RotatedCoor;
}
} public Direction CurrentDirection { get; set; } public virtual void Left()
{
Move(-1, 0);
OnLefted(EventArgs.Empty);
} public virtual void Right()
{
Move(1, 0);
OnRighted(EventArgs.Empty);
} public virtual void Down()
{
Move(0, 1);
OnDowned(EventArgs.Empty);
} public virtual void Up()
{
for (int i = 0; i < 4; i++)
{
LastPoints[i] = Points[i];
Points[i] = RotatedCoordinate[i];
}
Erasure();
Draw();
OnUped(EventArgs.Empty);
}
public abstract Point[] CalRotateCoordinate();
#endregion
}
}
.
. . .using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace Shapes
{
/// <summary>
/// .
/// . . .
/// </summary>
class TriangleNormalShape : AbstractShape
{
public TriangleNormalShape(Control Owner)
: base(Owner)
{
Points[0] = new Point(4, 2);//第一个方块的坐标
Points[1] = new Point(5, 1);//第二个
Points[2] = new Point(5, 2);//.
Points[3] = new Point(6, 2);//..可以按此坐标然后在纸上画一下所绘的图形
Color = Color.Yellow;
}
public override Point[] CalRotateCoordinate()//旋转后的坐标,要根据这个来判断是否可以旋转,
{//具体的判断不是本图形考虑的问题,而是游戏的控制器来考虑的
switch (CurrentDirection)
{
case Direction.Up://向上
RotatedCoordinate[0] = Points[1];
RotatedCoordinate[1] = Points[2];
RotatedCoordinate[2] = RotatedCoordinate[1];
RotatedCoordinate[2].Offset(0, 1);
RotatedCoordinate[3] = Points[3];
CurrentDirection = Direction.Down;
break;
case Direction.Down://向下
RotatedCoordinate[0] = RotatedCoordinate[1] = RotatedCoordinate[2] = RotatedCoordinate[3] = Points[1];
RotatedCoordinate[0].Offset(-1, 0);
RotatedCoordinate[2].Offset(0, 1);
RotatedCoordinate[3].Offset(1, 0);
CurrentDirection = Direction.Left;
break;
case Direction.Left://向左
RotatedCoordinate[0] = RotatedCoordinate[1] = RotatedCoordinate[2] = RotatedCoordinate[3] = Points[1];
RotatedCoordinate[0].Offset(-1, 0);
RotatedCoordinate[1].Offset(0, -1);
RotatedCoordinate[3].Offset(0, 1);
CurrentDirection = Direction.Right;
break;
case Direction.Right://向右
RotatedCoordinate[0] = RotatedCoordinate[1] = RotatedCoordinate[2] = RotatedCoordinate[3] = Points[2];
RotatedCoordinate[0].Offset(-1, 0);
RotatedCoordinate[1].Offset(0, -1);
RotatedCoordinate[3].Offset(1, 0);
CurrentDirection = Direction.Up;
break;
}
return RotatedCoordinate;
} }
}