سلام
اول تصمیم داشتم یک راه حل کامل بر مبنای اصول شی گرایی برایتان ارائه دهم ولی هرچه فکر کردم دیدم شاید کمی برایتان سخت شود...
گمانم کلش یک کلاس باشد (مثلاً همان فرم اصلی برنامه) خیلی خیلی ساده تر باشد و زودتر به جواب نهاییتان برسید.
پیشنهاد میکنم، فعلاً علی الحساب پیشنهاد میکنم کد زیر را گرفته و توابع خالی را تکمیل کنید.
بخش هایی را برایتان تکمیل کرده ام...
در واقع الآن مسئله شما را به چند تابع کوچک شکسته ام که امیدوارم بتوانید این بخش های کوچک ساده را تکمیل کنید.
بخش هایی که باید تکمیل کنید با ... code مشخص شده اند.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1
: Form
{
//__________________________________________________ ______________________
public Form1()
{
this.InitializeComponent();
this.SetStyle(
ControlStyles.ResizeRedraw |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
this.CreateNewRandShape();
this.timer1.Start();
}
//__________________________________________________ ______________________
private const int PLAYGROUND_WIDTH = 10;
private const int PLAYGROUND_HEIGHT = 20;
private const int SHAPE_WIDTH_HEIGHT = 4; //4*4 Square
//__________________________________________________ ______________________
private readonly Random m_Rand = new Random();
private readonly bool[,] m_Playground = new bool[PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT];
private int m_Score = 0;
private bool[,] m_Shape;
private System.Drawing.Point m_ShapePosition;
//__________________________________________________ ______________________
//یکپارچه کردن شکل جاری روی زمین بازی
private void MergeShapeOnPlayground()
{
for (int y = 0; y < SHAPE_WIDTH_HEIGHT; y++)
for (int x = 0; x < SHAPE_WIDTH_HEIGHT; x++)
if (this.m_Shape[x, y]) this.m_Playground[this.m_ShapePosition.X + x, this.m_ShapePosition.Y + y] = true;
}
//__________________________________________________ ______________________
//ساخت رندوم شکل جدید
private void CreateNewRandShape()
{
//top-center of playground
this.m_ShapePosition.Y = 0;
this.m_ShapePosition.X = (PLAYGROUND_WIDTH - SHAPE_WIDTH_HEIGHT) / 2;
switch (this.m_Rand.Next(1, 5))
{
case 1: //vertical line
this.m_Shape = new bool[,] {
{false,false,true,false},
{false,false,true,false},
{false,false,true,false},
{false,false,true,false} };
break;
case 2:
//code...
//this.m_Shape = ...
break;
case 3:
//code...
//this.m_Shape = ...
break;
case 4:
//code...
//this.m_Shape = ...
break;
case 5:
//code...
//this.m_Shape = ...
break;
}
}
//__________________________________________________ ______________________
//چرخش نود درجه ای شکل جاری
private void ShapeRotate90(bool Clockwise)
{
if (!Clockwise)
{
this.ShapeRotate90(true);
this.ShapeRotate90(true);
this.ShapeRotate90(true);
return;
}
//rotate 'm_Shape' 90-degree in clockwise
//code...
}
//__________________________________________________ ______________________
//خط های افقی پرشده را پیدا کرده و ضمن افزایش امتیاز بازیکن ماتریس زمین را با شیفت بازسازی میکند
private void FindAndRemoveHorizontalFillLines()
{
for (int y = 0; y < PLAYGROUND_HEIGHT; y++)
{
bool isfill = true;
for (int x = 0; x < PLAYGROUND_WIDTH; x++)
{
if (!this.m_Playground[x, y])
{
isfill = false;
break;
}
}
if (!isfill) continue;
//find a fill line
this.m_Score++;
//shift up-to-down 'm_Playground' matrix
//code...
}
}
//__________________________________________________ ______________________
//بررسی انکه شکل جاری در نقطه داده شده با ماتریس زمین تداخل و همپوشانی دارد یا نه
private bool HasOverlap()
{
for (int y = 0; y < SHAPE_WIDTH_HEIGHT; y++)
for (int x = 0; x < SHAPE_WIDTH_HEIGHT; x++)
if (this.m_Playground[this.m_ShapePosition.X + x, this.m_ShapePosition.Y + y] && this.m_Shape[x, y])
return true;
return false;
}
//__________________________________________________ ______________________
//سرریز تایمر
private void timer1_Tick(object sender, EventArgs e)
{
this.m_ShapePosition.Y++;
if (this.HasOverlap())
{
this.m_ShapePosition.Y--;
this.MergeShapeOnPlayground();
this.FindAndRemoveHorizontalFillLines();
this.CreateNewRandShape();
if (this.HasOverlap())
{
MessageBox.Show(this, "Game Over!");
this.Close();
}
}
this.Invalidate(); //re-paint
}
//__________________________________________________ ______________________
//تابع ترسیم فرم
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(this.BackColor);
this.OnPaint(e.Graphics, this.m_Playground, Point.Empty);
this.OnPaint(e.Graphics, this.m_Shape, this.m_ShapePosition);
}
//__________________________________________________ ______________________
//ترسیم یک ماتریس در نقطه داده شده
private void OnPaint(Graphics graph, bool[,] shape, Point position)
{
//code...
//graph.FillRectangle(Brushes.Blue, ...)
}
//__________________________________________________ ______________________
}
}