Tetris on C # in 100 lines

 
3r3-31. Recently I had an idea - to write a simple game in the minimum number of lines. My choice fell on Tetris. In this article I will describe my code.
 
 
To begin with, it is worth noting that in my implementation I included only the basic features: 3r3333368.  
 
 
movement of figures left /right;
 
the fall of the figures;
 
turn figures;
 
deleting filled figures;
 
ending the game.
 
 
So, first add a PictureBox to the form and create a timer.
 
 
Also, the game will need:
 
 
public const int width = 1? height = 2? k = 15; //Size of the field and size of the cell in pixels
public int[,]shape = new int[2, 4]; //Array for storing the falling figure (for each block 2 coordinates are[0, i]And[1, i]
Public int[,]Field = new int[width, height]; //Array to store the field
Public Bitmap bitfield = new Bitmap (k * (width + 1) + ? k * (height + 3) + 1);
Public Graphics gr; //To draw a field on the PictureBox 3r33382.
3rr3359. 3r33368.  
Fill in the margin:
 
 
for (int i = 0; i < width; i++)
field[i, height - 1]= 1;
for (int i = 0; i < height; i++) {
field[0, i]= 1;
field[width - 1, i]= 1;
}

 
Fill the shape:
 
 
public void SetShape () {
Random x = new Random (DateTime.Now.Millisecond);
switch (x.Next (7)) {//Randomly choose 1 out of 7 possible figures 3r33382. case 0: shape = new int[,]{{? ? ? 5}, {? ? ? 8}}; break;
case 1: shape = new int[,]{{? ? ? 3}, {? ? ? 9}}; break;
case 2: shape = new int[,]{{? ? ? 4}, {? ? ? 9}}; break;
case 3: shape = new int[,]{{? ? ? 4}, {? ? ? 7}}; break;
case 4: shape = new int[,]{{? ? ? 4}, {? ? ? 9}}; break;
case 5: shape = new int[,]{{? ? ? 4}, {? ? ? 7}}; break;
case 6: shape = new int[,]{{? ? ? 4}, {? ? ? 9}}; break;
}
}

 
The procedure that draws a “glass” in PictureBox:
 
 
public void FillField () {
gr.Clear (Color.Black); //Clear the field
for (int i = 0; i < width; i++)
for (int j = 0; j 3r33272. if (field[i, j]== 1) {//If the cell of the field exists
gr.FillRectangle (Brushes.Green, i * k, j * k, k, k); //Draw a small square in this place,
gr.DrawRectangle (Pens.Black, i * k, j * k, k, k);
} 3rr3382. for (int i = 0 ; i < 4; i++){ //Рисуем падающую фигуру
gr.FillRectangle (Brushes.Red, shape[1, i]* k, shape[0, i]* k, k, k);
gr.DrawRectangle (Pens.Black, shape[1, i]* k, shape[0, i]* k , k, k);
}
FieldPictureBox.Image = bitfield;
}
3r33358. 3rr3359. 3r3368.  
I first move the figures, and then check whether such an option is possible or not. If there is an error in some place (the figure has gone beyond the bounds of the field or superimposed on the figure already lying in the field), the figure returns to its previous place. For this, I wrote a function that returns true if there was an error on the field or false if there is no error: 3r33333.  
 
public bool FindMistake () {
for (int i = 0; i < 4; i++)
if (shape[1, i]> = width || shape[0, i]> = height ||
shape[1, i]<= 0 || shape[0, i]<= 0 ||
field[shape[1, i]., shape[0, i]]== 1)
return true;
return false;
}
3r3r588. 3r33359. 3rr3368.  
Now proceed to the movement of the figures. Create a KeyDown event in the constructor.
 
 
Move left:
 
 
switch (e.KeyCode) {
case Keys.A:
for (int i = 0; i < 4; i++)
shape[1, i]-; //First we shift the coordinates of all the pieces of the figure 1 left on the axis OX
if (FindMistake ()) //If after that an error
for (int i = 0; i 3r33286. shape[1, i]++; //Return the figure back 1 to the right
break;
} 3r33333. 3r3358. 3r33359. 3r3333368.  
Similarly, moving right:
 
 
case Keys.D:
for (int i = 0; i < 4; i++)
shape[1, i]++;
if (FindMistake ())
for (int i = 0; i 3r33r. 3349. shape[1, i]-;
break; 3r33382.
-;
break; 3r33382.
-;
break; 3r33382.
-;
break; 3r33382.
-;
break; 3r33382.
 
The flipping of the figure is a little more complicated: 3r3333368.  
 
case Keys.W:
var shapeT = new int[2, 4];
Array.Copy (shape, shapeT, shape.Length); //Create a copy of the figure, so that if there is an error on the field after the coup, do not turn it back, but simply restore the copy 3r3382. int maxx = ? maxy = 0;
for (int i = 0; i < 4; i++){
if (shape[0, i]> maxy)
maxy = shape[0, i];
if (shape[1, i]> maxx) 3rr3382. maxx = shape[1, i]; 3rrn33r33r.???. The maximum coordinates of the figure's value are in X and Y. . = maxx - (3 - (maxy - temp)) + 1;
} //Turn the figure. I found these formulas after careful study of the drawn square with the figure before and after the coup.
if (FindMistake ()) 3r38282.Array.Copy (shapeT, shape, shape.Length); 3r3333382 break;
3r33333.
3r3333368  
Now it remains only to add a drop of figures and the removal of lines. All this will happen at the TimerTick event:
 
 
private void TickTimer_Tick (object sender, System.EventArgs e) {
if (field[8, 3]== 1)
Environment.Exit (0); //If the cell of the field where the figures appear on, complete the program.
for (int i = 0; i 3r3r-3286. shape[0, i]++; //Move the figure down
if (FindMistake ()) {
for (int i = 0; i 3rr3349. field[shape[1, i], --shape[0, i].]++;
SetShape ();
} //If an error was found, move the figure up 1 cell, save it to the field array and create a new figure 3r3382. For (int i = height - 2; i> 2 ; i -) {
var cross = (from t in Enumerable.Range (? field.GetLength (0)). Select (j => field[j, i]) .ToArray () where t == 1 select t) .Count (); //Number of filled fields in the row
If (cross == width)
For (int k = i; k> 1; k--) 3r33382. For (int l = 1; l < width - 1; l++)
field[l, k]= field[l, k - 1];
} //Checking for fullness with a row, if there are rows in which all cells are filled, move all rows that are above the harvested line 1 down
FillField (); //Redraw the field
}

 
Finally, when creating a form, you need to call: 3r3333368.  
 
SetShape ();

 
The resulting code:
 
 
using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
namespace LittleTetris {
public partial class Form1: Form {
public const int width = 1? height = 2? k = 15;
public int[,]shape = new int[2, 4];
public int[,]field = new int[width, height];
public Bitmap bitfield = new Bitmap (k * (width + 1) + ? k * (height + 3) + 1);
public Graphics gr;
public Form1 () {
InitializeComponent ();
gr = Graphics.FromImage (bitfield);
for (int i = 0; i 3-3r3271. field[i, height - 1]= 1;
for (int i = 0; i < height; i++) {
field[0, i]= 1; 3r3382. field[width - 1, i]= 1;
} 3r3333382. ;
}
Public void FillField () {
Gr.Clear (Color.Black);
For (int i = 0; i < width; i++)
For (int j = 0; j 3r33232. If) field ([i, j]== 1) {
Gr.FillRectangle (Brushes.Green, i * k, j * k, k, k);
Gr.DrawRectangle (Pens.Black, i * k, j * k, k, k) ;
}
For (int i = 0; i < 4; i++){
Gr.FillRectangle (Brushes.Red, shape[1, i]* K, shape[0, i]* K, k, k);
Gr.DrawRectangle (Pens.Black , shape[1, i]* k, shape[0, i]* k, k, k); 3r3382.} 3r3r???. FieldPictureBox.Image = bitfield;
}
private void TickTimer_Tick (object sender, System.EventArgs e) {
if (field[8, 3]== 1)
Environment.Exit (0);
for (int i = 0; i < 4; i++)
shape[0, i]++;
for (int i = height - 2; i> 2; i -) {
var cross = (from t in Enumerable.Range (0 , field.GetLength (0)). Select (j => field[j, i]) .ToArray () where t == 1 select t) .Count ();
if (cross == width)
for (int k = i; k> 1; k--)
  for (int l = 1; l < width - 1; l++)
field[l, k]= field[l, k - 1]; ++;
SetShape ();}
FillField ();
}
Private void Form1KeyDown (object sender, KeyEventArgs e) {
Switch (e.KeyCode) {3r3333382. for (int i = 0; i < 4; i++)
shape[1, i]-;
if (FindMistake ())
for (int i = 0; i 3rr3349. shape[1, i]++;
break; 3r33333. case Keys .D:
For (int i = 0; i < 4; i++)
Shape[1, i]++;
If (FindMistake ())
for (int i = 0; i < 4; i++)
shape[1, i]-;
break;
case Keys.W:
var shapeT = new int[2, 4];
Array.Copy (shape, shapeT, shape.Length);
int maxx = ? maxy = 0;
for (int i = 0; i < 4; i++){
if (shape[0, i]> maxy)
maxy = shape[0, i];
if (int i = 0; i 3r33232. int temp = shape[0, i];
shape[0, i]= maxy - (maxx - shape[1, i]) - 1;
shape[1, i]= maxx - (3 - (maxy - temp) + 1; 3r33382.} 3r33382. If (FindMistake ()) 3r33382. Array.Copy (shapeT, shape, shape.Length);
Break; 3r33382.}
}
Public void SetShape () {3r38233). Random x = new Random (DateTime.Now.Millisecond);
Switch (x.Next (7)) {
Case 0: shape = new int w2w2w 285. {{? ? ? 5}, {? ? ? 8}}; break;
case 1: shape = new int[,]{{? ? ? 3}, {? ? ? 9}}; break;
case 2: shape = new int[,]{{? ? ? 4}, {? ? ? 9}}; break;
case 3: shape = new int[,]{{? ? ? 4}, {? ? ? 7}}; break;
case 4: shape = new int[,]{{? ? ? 4}, {? ? ? 9}}; break;
case 5: shape = new int[,]{{? ? ? 4}, {? ? ? 7}}; break;
case 6: shape = new int[,]{{? ? ? 4}, {? ? ? 9}}; break;
}
}
public bool FindMistake () {
for (int i = 0; i < 4; i++)
if (shape[1, i]> = width || shape[0, i]> = height ||
shape[1, i]<= 0 || shape[0, i]<= 0 ||
field[shape[1, i]., shape[0, i]]== 1)
return true;
return false;
}
}
} 3r38282. 3r3588. 3r33359. 3r3333368.  
As a result, except for lines with a closing bracket and a painted condition in FindMistake, it turns out 95 lines.
 
 
Here is a screenshot of the running program:
 
 
Tetris on C # in 100 lines
+ 0 -

Comments 88

Offline
asd
asd 4 March 2019 13:02
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.advertising services

Offline
asd
asd 14 March 2019 12:55
Thanks for sharing us.mpu freiburg

Offline
sohail khatri
sohail khatri 24 March 2019 16:44
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for freeiç mimarlık taban puanları devlet üniversiteleri


Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.beden eğitimi öğretmenliği taban puanları
Offline
sohail khatri
sohail khatri 24 March 2019 20:53
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.ukrayna odessa


I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up.avrupa eğitim danışmanlığı


Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.silah mühendisliği bölümleri


Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.denizcilik

Offline
sohail khatri
sohail khatri 25 March 2019 11:48
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.yurtdışı üniversite bölümleri


Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with ourkiev


Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.diş hekimliği taban puanları


This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. inşaat mühendisliği uzaktan eğitim veren üniversiteler
Offline
asd
asd 26 March 2019 17:21
Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.Nerf


Offline
sohail khatri
sohail khatri 28 March 2019 17:19
Thanks, that was a really cool read!Lera Blog
Offline
sohail khatri
sohail khatri 30 March 2019 16:10
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.SPSS help for thesis
Offline
sohail khatri
sohail khatri 31 March 2019 16:01
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.keyword page checker


Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!used test equipment auction



Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.used  lab equipment auction
Offline
sohail khatri
sohail khatri 31 March 2019 20:32
I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.used  testing equipment for sale
Offline
sohail khatri
sohail khatri 1 April 2019 16:38
I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.infervour
Offline
sohail khatri
sohail khatri 3 April 2019 20:34
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.hapcheta

Offline
sohail khatri
sohail khatri 8 April 2019 12:09
Thanks, that was a really cool read!خرید بک لینک طبیعی
Offline
sohail khatri
sohail khatri 8 April 2019 21:13
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
Best Sandwich Maker in India
Offline
sohail khatri
sohail khatri 10 April 2019 17:26
Thanks, that was a really cool read!naked cakes


What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very muchvariancetv
Offline
sohail khatri
sohail khatri 14 April 2019 16:14
It is a great website.. The Design looks very good.. Keep working like that!.AboutNutra
Offline
fuzail faisal
fuzail faisal 17 April 2019 10:43
Great job  for publishing such a beneficial web site. Your web log isn’t only useful but  it is additionally really creative too. טיסות  לברלין
Offline
ASD
ASD 5 May 2019 16:56
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.כרתים פארק מים



I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.נופש  ביוון הכל כלול עם ילדים


I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.מלונות  באנטליה הכל כלול
Offline
sohail khatri
sohail khatri 6 May 2019 13:41
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.used testing equipment for sale



Thank you for taking the time to publish this information very useful!used test equipment auction

Offline
sohail khatri
sohail khatri 7 May 2019 12:28
A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post.ثبت آگهی

Offline
sohail khatri
sohail khatri 9 May 2019 12:38
This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform!تالار عروسی
Offline
sohail khatri
sohail khatri 9 May 2019 14:58
Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.netgear  orbi rbk50 refurbished


Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job!Can you buy Rohypnol Online
Offline
sohail khatri
sohail khatri 11 May 2019 11:42
This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform!streaming vf

Offline
Lopigo
Lopigo 12 May 2019 11:12
This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post.how quickly does radon mitigation work


It is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!diy radon mitigation system sump pump


Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on.radon test canisters
Offline
sohail khatri
sohail khatri 13 May 2019 12:22
I read that Post and got it fine and informative.Fake IDS


Offline
sohail khatri
sohail khatri 13 May 2019 14:14
I adore your websites way of raising the awareness on your readers.sump pump cover radon mitigation
Offline
sohail khatri
sohail khatri 17 May 2019 13:09
This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform!Bluehost India
Offline
sohail khatri
sohail khatri 17 May 2019 18:44
That is really nice to hear. thank you for the update and good luck.CBD Online


That is really nice to hear. thank you for the update and good luck.Massage in London

Offline
sohail khatri
sohail khatri 17 May 2019 22:25
That is really nice to hear. thank you for the update and good luck.
third eye opener


That is really nice to hear. thank you for the update and good luck.
Tantric Massage in London



That is really nice to hear. thank you for the update and good luck.Massage in London

Offline
sohail khatri
sohail khatri 19 May 2019 12:03
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.Driving School


Add comment