-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.pde
155 lines (136 loc) · 3.5 KB
/
Tetris.pde
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
Screen screen, secondaryScreen;
BaseShape activeShape;
BaseShape[] nextShapes;
int score;
PImage imgStars;
void setup() {
screen = new Screen(new PVector(width/10, 100), 300, 10, 20);
//screen.showGrid = false;
secondaryScreen = new Screen(new PVector(6*width/10, 260), 120, 5, 13);
secondaryScreen.showGrid = false;
size(800, 800);
activeShape = randomShape();
nextShapes = new BaseShape[]{randomShape(), randomShape(), randomShape()};
score = 0;
tint(160);
imgStars = loadImage("space.jpeg");
}
void drawNextShapes() {
for (int i=0; i<nextShapes.length; i++) {
BaseShape shape = nextShapes[i];
fill(shape.colour);
shape.loc = new PVector(1, 1 + (i * 4));
shape.draw(secondaryScreen);
}
}
BaseShape randomShape() {
int randNum = (int)random(0, 7); // 7 possible shapes
PVector locVector = new PVector(4, 0); // row 0, col 5
BaseShape newShape;
switch (randNum) {
case 0:
newShape = new Block(locVector);
break;
case 1:
newShape = new TShaped(locVector);
break;
case 2:
newShape = new LShaped(locVector);
break;
case 3:
newShape = new LShapedReversed(locVector);
break;
case 4:
locVector.x -= 1; // translate to the left by 1
newShape = new Straight(locVector);
break;
case 5:
newShape = new LeftZigZag(locVector);
break;
case 6:
newShape = new RightZigZag(locVector);
break;
default:
newShape = new Block(locVector);
break;
}
while (newShape.willCollide(new PVector(0, 1))) {
newShape.loc.y -= 1;
if (newShape.loc.y <= 0) {
endGame();
return newShape;
}
}
return newShape;
}
void draw() {
//background(200);
image(imgStars, 0, 0, imgStars.width*1.2, height);
drawTitle();
strokeWeight(1);
stroke(1);
secondaryScreen.draw();
screen.draw();
displayScore();
drawNextShapes();
activeShape.draw();
activeShape.move();
if (activeShape.isLocked) {
activeShape = nextShapes[0];
activeShape.loc.x = 4;
activeShape.loc.y = 0;
nextShapes[0] = nextShapes[1];
nextShapes[1] = nextShapes[2];
nextShapes[2] = randomShape();
}
}
void drawTitle() {
pushMatrix();
PVector pos = new PVector(width/2, 60);
textSize(48);
textAlign(CENTER);
fill(168, 52, 96, 150);
stroke(5);
fill(256, 256, 100);
text("Tetris :)", pos.x, pos.y);
popMatrix();
}
void displayScore() {
fill(255, 100, 100);
textSize(24);
textAlign(LEFT);
String scoreText = String.format("Score: %d", score);
text(scoreText, screen.loc.x, screen.loc.y - 20);
}
void mousePressed() {
if (mouseButton == LEFT) {
activeShape.rotate(RIGHT);
}
}
void keyPressed() {
if (keyCode == 39 || key == 'd') { // arrow right
activeShape.move(RIGHT);
} else if (keyCode == 37 || key == 'a') { // arrow left
activeShape.move(LEFT);
} else if (keyCode == 40 || key == 's') { // arrow down
activeShape.isSpedUp = true;
score += 1;
} else if (key == 'r' || key == 'w' || keyCode == 38) { // arrow up
activeShape.rotate(RIGHT);
}
}
void keyReleased() {
if (keyCode == 40 || key == 's') {
activeShape.isSpedUp = false;
}
}
void endGame() {
String message = String.format("Game Ended! Your score: %d", score);
fill(60);
rect(0, height/2-60, width, 100);
fill(255, 160, 50, 230);
textSize(48);
textAlign(CENTER);
text(message, width/2, height/2);
frameRate(0);
}