دانلود سورس بازی تتریس Tetris در سی شارپ C#

این پروژه که با استفاده از زبان برنامه نویسی سی شارپ طراحی شده است و تتریس نام دارد ، در واقع یک توپ است که به صورت شناور در صفحه حرکت می کند و هدف ها را می خورد.
توپ با برخورد با دیواره ها و یا شیئ متحرک کف صفحه انعکاس پیدا می کند . و در جهت مخالف تغییر مسیر می دهد.
سورس کد این برنامه را می توانید در پایین صفحه دانلود کنید.
نحوه کار برنامه :
برنامه ابتدا هدف ها را توسط دو حلقه به صورت جدول مانند ایجاد می کند. سپس با کلیک کاربر توپ حرکت می کند و هدف ها را می خورد . توپ در هنگام حرکت با استفاده از دو متغیر تغییر جهت ، یکی در جهت افقی و یکی در جهت عمودی حرکت می کند و این متغیرها می توانند مقدار مثبت یا منفی به خود بگیرند.
هنگامی که مختصات توپ با هدفی برابر می شود هدف محو می شود و امتیاز افزایش می یابد.
این هم کد برنامه :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace game_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool startgame = false;
int score = 1000,balls=0;
private void panel1_Click(object sender, EventArgs e)
{
if (!startgame)
{
createfoods();
startgame = true;
timer1.Enabled = true;
label3.Visible = false;
}
ball.Location = new Point(216, 182);
speedx = 0; speedy = 10;
ballmove.Enabled = true;
}
int speedx=0,speedy = 8;
PictureBox[] food = new PictureBox[30];
int X = 25, Y = 5;
private void createfoods()
{
int counter=0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
food[counter] = new PictureBox();
food[counter].Size = new Size(15, 15);
food[counter].Location = new Point(X, Y);
food[counter].Parent = panel1;
food[counter].Tag = counter;
food[counter].BackgroundImage = pfood.Image;
food[counter].BackgroundImageLayout = pfood.BackgroundImageLayout;
//food[counter].Click += new EventHandler(Btn_Click);// به رویداد کلیک همه دکمه ها این تابع اضافه میشه:Btn_Click
counter++;
X += 78;
Application.DoEvents();
Thread.Sleep(40);
}
X = 25;
Y += 40;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (!gmove.Enabled)
{
if (e.KeyValue == 39)
{
speedg = 1;
}
else if (e.KeyValue == 37)
{
speedg = -1;
}
gmove.Enabled = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
gmove.Enabled = false;
}
int speedg = 0;
private void gmove_Tick(object sender, EventArgs e)
{
if (speedg < 0 && speedg >-10) speedg--;
else if (speedg > 0 && speedg <10) speedg++;
if(ground.Location.X>0 && ground.Location.X <360)
ground.Location = new Point(ground.Location.X +speedg, ground.Location.Y);
if(ground.Location.X>=360)
ground.Location = new Point(359, ground.Location.Y);
if (ground.Location.X <=0)
ground.Location = new Point(1, ground.Location.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
// createfoods();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
score--;
label2.Text = "امتیاز : " + score.ToString();
}
private void ballmove_Tick(object sender, EventArgs e)
{
ball.Location = new Point(ball.Location.X + speedx, ball.Location.Y + speedy);
Application.DoEvents();
//check foods--------------------------
int fx1 = 0, fx2 = 0, fy1 = 0,fy2 = 0;
for (int i = 0; i < 30; i++)
{
fx1 = food[i].Location.X;
fx2 = food[i].Location.X + ball.Size.Width;
fy1 = food[i].Location.Y;
fy2 = food[i].Location.Y + ball.Size.Height;
if (food[i].Visible)
{
if ((fx1 >= ball.Location.X && fx1 <= ball.Location.X + ball.Size.Width) || (fx2 >= ball.Location.X && fx2 <= ball.Location.X + ball.Size.Width))
{
if ((fy1 >= ball.Location.Y && fy1 <= ball.Location.Y + ball.Size.Height) || (fy2 >= ball.Location.Y && fy2 <= ball.Location.Y + ball.Size.Height))
{
food[i].Visible = false;
balls++;
label1.Text = "توپ : " + balls.ToString();
if (balls == 30)
{
ballmove.Enabled = false;
timer1.Enabled = false;
}
speedy *= -1;
}
}
}
}
//check around-------------------------
if (ball.Location.Y + ball.Size.Height == ground.Location.Y)
{
int ball_loc = ball.Location.X + ball.Size.Width / 2;
int ball_loc1 = ball.Location.X + 1;
int ball_loc2 = ball.Location.X + ball.Size.Width - 1;
int ground_loc = ground.Location.X + ground.Size.Width;
if ((ball_loc1 >= ground.Location.X && ball_loc1 <= ground_loc) || (ball_loc2 >= ground.Location.X && ball_loc2 <= ground_loc))
{
speedy *= -1;
ground_loc = ground.Location.X + ground.Size.Width / 2;
if (ball_loc > ground_loc + ground.Size.Width / 4)
speedx = 4;
else if (ball_loc > ground_loc + 2)
speedx = 2;
else if (ball_loc < ground_loc - ground.Size.Width / 4)
speedx = -4;
else if (ball_loc < ground_loc - 2)
speedx = -2;
else
speedx = 0;
}
else
{
for (int i = 0; i < 6; i++)
{
ball.Location = new Point(ball.Location.X + speedx, ball.Location.Y + speedy);
}
ballmove.Enabled = false;
}
}
if (ball.Location.Y <= 0)
speedy *= -1;
if (ball.Location.X <= 0)
speedx *= -1;
if (ball.Location.X >= 425)
speedx *= -1;
}
private void label3_Click(object sender, EventArgs e)
{
panel1_Click(sender, e);
}
private void label4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.sourcecodes.ir");
}
}
}
هم اکنون می توانید سورس این پروژه را به صورت رایگان دانلود کنید
حجم فایل :849 KB
خیلی عالی بود ، ممنون از سایت خوبتون
لطفا لینک دانلود را بررسی کنید.با تشکر.
سلام ، بررسی انجام شد ، مشکلی یافت نشد ، با تشکر.
یه سایت عالی محشر فوقالعاده هست ممنون