-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.cs
164 lines (129 loc) · 5.43 KB
/
Tetris.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Media;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Tetris {
public class Tetris : Form {
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
public const int BLOCK_SIZE = 25;
public const int SCREEN_WIDTH = 10;
public const int SCREEN_HEIGHT = 20;
private static System.Threading.Timer timer;
private static Tetris instance;
private GUI gui;
private Random rand;
private Screen screen;
private Piece curPiece;
private Piece nextPiece;
private Label lineCounter;
private Label gameOverLabel;
private int linesRemoved = 0;
private bool gameOver = false;
public Tetris() {
InitializeComponent();
gui = new GUI(SCREEN_WIDTH, SCREEN_HEIGHT, BLOCK_SIZE);
rand = new Random();
screen = new Screen(SCREEN_WIDTH, SCREEN_HEIGHT);
AddLabels();
}
private void InitializeComponent() {
this.DoubleBuffered = true;
this.Size = new Size(SCREEN_WIDTH * BLOCK_SIZE + 200, SCREEN_HEIGHT * BLOCK_SIZE + 100);
this.BackColor = System.Drawing.Color.White;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Tetris_Paint);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Tetris_KeyDown);
}
private void AddLabels() {
lineCounter = new Label();
lineCounter.Location = new Point(345, 100);
lineCounter.Font = new Font(FontFamily.GenericSansSerif, 18);
this.Controls.Add(lineCounter);
Label countLabel = new Label();
countLabel.Location = new Point(305, 80);
countLabel.Size = new Size(200, 50);
countLabel.Text = "LINE COUNT";
countLabel.Font = new Font(FontFamily.GenericSansSerif, 12);
this.Controls.Add(countLabel);
Label nextLabel = new Label();
nextLabel.Location = new Point(330, 190);
nextLabel.Text = "NEXT";
nextLabel.Font = new Font(FontFamily.GenericSansSerif, 12);
this.Controls.Add(nextLabel);
gameOverLabel = new Label();
gameOverLabel.Location = new Point(85, 200);
gameOverLabel.Size = new Size(200, 300);
gameOverLabel.Font = new Font(FontFamily.GenericSansSerif, 32);
gameOverLabel.BackColor = Color.Transparent;
this.Controls.Add(gameOverLabel);
}
private Piece randPiece() {
return Piece.GetPiece(rand.Next(0, 7));
}
private void UpdateGame() {
if(gameOver) return;
if(curPiece == null) {
if(nextPiece == null) // start of game
nextPiece = randPiece();
curPiece = nextPiece;
curPiece.pos = new Point(3, -1);
if(curPiece.Overlaps(screen)) { // game over
gameOver = true;
gameOverLabel.Text = "GAME OVER";
}
nextPiece = randPiece();
} else {
bool stop = curPiece.MoveDown(screen);
if(stop) { // piece hit bottom
screen.AddPiece(curPiece);
linesRemoved += screen.RemoveFullRows();
curPiece = null;
}
}
}
public void Tetris_KeyDown(object sender, KeyEventArgs e) {
if(curPiece == null) return;
switch(e.KeyCode) {
case Keys.A:
curPiece.MoveLeft(screen);
break;
case Keys.D:
curPiece.MoveRight(screen);
break;
case Keys.W:
curPiece.Rotate(screen);
break;
case Keys.Space:
curPiece.SnapDown(screen);
break;
}
Invalidate();
}
public void Tetris_Paint(object sender, PaintEventArgs e) {
Graphics graphics = e.Graphics;
gui.DrawLayout(graphics);
gui.DrawScreen(graphics, screen, 30, 30);
if(curPiece != null)
gui.DrawPiece(graphics, curPiece, 30, 30);
if(nextPiece != null)
gui.DrawPiece(graphics, nextPiece, 310, 200);
lineCounter.Text = linesRemoved.ToString();
}
private static void Redraw(Object stateInfo) {
instance.UpdateGame();
instance.Invalidate();
}
public static int Main() {
AttachConsole(-1);
//SoundPlayer player = new SoundPlayer("Tetris_quiet.wav");
//player.PlayLooping();
instance = new Tetris();
timer = new System.Threading.Timer(Tetris.Redraw, new AutoResetEvent(false), 0, 100);
Application.Run(instance);
return 0;
}
}
}