public partial class Form1 : Form
{
private MyRect _currentMyRect;//正在操作的方塊;
private MyRect _nextMyRect;//下一個方塊
private Point startlocation = new Point(GameField.length * 8, 0);
private bool stillRuning = false;
public Form1()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
/// InitGameData();
}
private void Form1_Load(object sender, EventArgs e)
{
GameField.backcolor = pictureBox1.BackColor;
GameField.winHandle = pictureBox1.Handle;
timer1.Interval = 1000;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Activated_1(object sender, EventArgs e)
{
pictureBox1.Invalidate();
Application.DoEvents();
GameField.Redraw();
}
/// <summary>
/// 重寫ProcessDialogKey使鍵盤按下的鍵能夠被識別
/// </summary>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Up || keyData == Keys.Down ||
keyData == Keys.Left || keyData == Keys.Right)
return false;
else
return base.ProcessDialogKey(keyData);
}
/// <summary>
/// 判斷按鍵左右和下是否按下。按下執行,空格鍵執行變換函式。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
while(_currentMyRect.RDown());
break;
case Keys.Left:
_currentMyRect.RLeft();
break;
case Keys.Right:
_currentMyRect.RRight();
break;
case Keys.Up://變形
_currentMyRect.RChange();
break;
case Keys.Space:
timer1.Enabled = !timer1.Enabled;
if (!timer1.Enabled)
MessageBox.Show("暫停");
break;
}
pictureBox1.Focus();
}
/// <summary>
/// 每段時間觸發一次,第一個到達底部,第二個才重繪
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer1_Tick(object sender, EventArgs e)
{
if (!stillRuning)
return;
if (!_currentMyRect.RDown())
{
//游戲結束
if (_currentMyRect.Top() == 0)
{
timer1.Enabled = false;
timer1.Stop();
stillRuning = false;
MessageBox.Show("game over");
}
int eraseLines = GameField.CheckLines();
if (eraseLines > 0)
{
pictureBox1.Invalidate();
Application.DoEvents();
GameField.Redraw();
}
_currentMyRect = new MyRect(startlocation, _nextMyRect._typeShape);
_currentMyRect.Draw(GameField.winHandle);
_nextMyRect = new MyRect(new Point(80, 50), _nextMyRect._typeShape);
//_nextMyRect.Draw(GameField.winHandle);
}
_currentMyRect.RDown();
//this.Invalidate();
Thread.Sleep(1000);
}
/// <summary>
/// 開始按鈕,按下后不斷產生方塊,并且下移方塊。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_start_Click(object sender, EventArgs e)
{
// _list_All = new List<Rentagle>();
if (_currentMyRect== null)
{
_currentMyRect = new MyRect(startlocation, MyRect.TypeShape.Undefined);
_currentMyRect.Draw(GameField.winHandle);
_nextMyRect = new MyRect(new Point(80, 50), MyRect.TypeShape.Undefined);
stillRuning = true;
btn_start.Enabled = false;
// timer1.Enabled = true;
timer1.Start();
}
else
{
timer1.Enabled = true;
}
}
private void stop_Click(object sender, EventArgs e)
{
timer1.Stop();
timer1.Dispose();
//stop.Enabled = false;
btn_start.Enabled = true;
timer1.Enabled = false;
}
}
class Square
{
public Point _point;
public Size _size;
public Color _forecolor;
public Color _backcolor;
public Square(Size size, Color forecolor, Color backcolor)
{
_size = size;
_backcolor = backcolor;
_forecolor = forecolor;
}
public void Draw(IntPtr winHandle)
{
Graphics graphics = Graphics.FromHwnd(winHandle);
GraphicsPath path = new GraphicsPath();
Rectangle rectangle = new Rectangle(_point, _size);
path.AddRectangle(rectangle);
Color[] colors = new Color[] { _backcolor };
PathGradientBrush pb = new PathGradientBrush(path);
pb.CenterColor = _forecolor;
pb.SurroundColors = colors;
graphics.FillPath(pb, path);
}
public void Erase(IntPtr winHandle)
{
Graphics graphics = Graphics.FromHwnd(winHandle);
Rectangle rectangle = new Rectangle(_point, _size);
graphics.FillRectangle(new SolidBrush(GameField.backcolor), rectangle);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Data;
namespace Tetris
{
class MyRect
{
//組成一個圖形的四個小圖形
public Square _square1;
public Square _square2;
public Square _square3;
public Square _square4;
//每個小圖形的寬度
private const int length = GameField.length;
//7種不同的型別
public enum TypeShape
{
Undefined=0,
Squa=1,
Lin=2,
J=3,
L=4,
T=5,
Z=6,
S=7
};
public TypeShape _typeShape;
private static Color forceColor;
private static Color backColor;
//三個不同的方向
public enum TypeDirection
{
Up=1,
Right=2,
Left=4,
Down=3
};
//定義最開始的方向為下,方便后邊使用
public TypeDirection firstDirection = TypeDirection.Down;
/// <summary>
/// 分別畫出不同的圖形,并且畫出圖形的顏色,背景顏色
/// </summary>
/// <param name="thisLoction"></param>
/// <param name="typeShape"></param>
public MyRect(Point thisLoction,TypeShape typeShape)
{
Random random = new Random(DateTime.Now.Millisecond);
typeShape = (TypeShape)random.Next(7);
if (typeShape == TypeShape.Undefined)
{
_typeShape = (TypeShape)(random.Next(7)+ 1);
}
else
_typeShape = typeShape;
forceColor = Color.Blue;
backColor = GameField.backcolor;
Size Ssize = new Size(length, length);
_square1 = new Square(Ssize, forceColor, backColor);
_square2 = new Square(Ssize, forceColor, backColor);
_square3 = new Square(Ssize, forceColor, backColor);
_square4 = new Square(Ssize, forceColor, backColor);
switch (_typeShape)
{
case TypeShape.Squa:
//_square1 = new Square(Ssize, Color.Black, backColor);
_square1._point = new Point(thisLoction.X, thisLoction.Y);
_square2._point = new Point(thisLoction.X+length, thisLoction.Y);
_square3._point = new Point(thisLoction.X, thisLoction.Y+length);
_square4._point = new Point(thisLoction.X+length, thisLoction.Y+length);
break;
case TypeShape.Lin:
_square1._point = new Point(thisLoction.X, thisLoction.Y);
_square2._point = new Point(thisLoction.X+length, thisLoction.Y);
_square3._point = new Point(thisLoction.X+2*length, thisLoction.Y);
_square4._point = new Point(thisLoction.X+3*length, thisLoction.Y);
break;
case TypeShape.J:
_square1._point = new Point(thisLoction.X+length, thisLoction.Y);
_square2._point = new Point(thisLoction.X+length, thisLoction.Y+length);
_square3._point = new Point(thisLoction.X+length, thisLoction.Y+2*length);
_square4._point = new Point(thisLoction.X, thisLoction.Y+2*length);
break;
case TypeShape.L:
_square1._point = new Point(thisLoction.X, thisLoction.Y);
_square2._point = new Point(thisLoction.X, thisLoction.Y+length);
_square3._point = new Point(thisLoction.X, thisLoction.Y+2*length);
_square4._point = new Point(thisLoction.X+length, thisLoction.Y
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/79158.html
標籤:C#
上一篇:微信公眾號頁面經常打不開
