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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
package com.sumit.plajava;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TetrisGame extends JPanel{
private final Point[][][] myPoint = {
{
//I
{new Point(0,1), new Point(1,1), new Point(2,1), new Point(3,1)},
{new Point(1,0), new Point(1,1), new Point(1,2), new Point(1,3)},
{new Point(0,1), new Point(1,1), new Point(2,1), new Point(3,1)},
{new Point(1,0), new Point(1,1), new Point(1,2), new Point(1,3)}
},
{
//J
{new Point(0,1), new Point(1,1), new Point(2,1), new Point(2,0)},
{new Point(1,0), new Point(1,1), new Point(1,2), new Point(2,2)},
{new Point(0,1), new Point(1,1), new Point(2,1), new Point(0,2)},
{new Point(1,0), new Point(1,1), new Point(1,2), new Point(1,3)}
},
{
//L
{new Point(0,1), new Point(1,1), new Point(2,1), new Point(2,0)},
{new Point(1,0), new Point(1,1), new Point(1,2), new Point(2,2)},
{new Point(0,1), new Point(1,1), new Point(2,1), new Point(0,2)},
{new Point(1,0), new Point(1,1), new Point(1,2), new Point(2,0)}
},
{
//0
{new Point(0,0), new Point(0,1), new Point(01,0), new Point(1,1)},
{new Point(0,0), new Point(0,1), new Point(01,0), new Point(1,1)},
{new Point(0,0), new Point(0,1), new Point(01,0), new Point(1,1)},
{new Point(0,0), new Point(0,1), new Point(01,0), new Point(1,1)}
},
};
private final Color[] myColor = {Color.CYAN, Color.magenta, Color.orange, Color.YELLOW, Color.black, Color.PINK, Color.red};
private Point pt;
private int currentPiece;
private int rotation;
private ArrayList<Integer> nextPiece = new ArrayList<Integer>();
private long score;
private Color[][] well;
private void init() {
well = new Color[12][24];
for(int i = 0; i < 12; i++) {
for(int j = 0; j < 23; j++) {
if(i == 0 || i == 11 || i == 22) {
well[i][j] = Color.PINK;
}else {
well[i][j] = Color.black;
}
}
}
newPiece();
}
public void newPiece() {
pt = new Point(5,2);
rotation = 0;
if(nextPiece.isEmpty()) {
Collections.addAll(nextPiece, 0,1,2,3);
Collections.shuffle(nextPiece);
}
}
private boolean collidesAt(int x, int y, int rotation) {
for(Point p: myPoint[currentPiece][rotation]) {
if(well[p.x+x][p.y+y] != Color.black) {
return true;
}
}
return false;
}
private void rotate(int i) {
int newRotation=(rotation + i)%4;
if(newRotation < 0) {
newRotation = 3;
}
if(!collidesAt(pt.x, pt.y, newRotation)) {
rotation = newRotation;
}
repaint();
}
public void move(int i) {
if(!collidesAt(pt.x, pt.y, rotation)) {
pt.x += i;
}
repaint();
}
public void drop() {
if(!collidesAt(pt.x, pt.y, rotation)) {
pt.x += 1;
}else {
fixtoWell();
}
repaint();
}
public void fixtoWell() {
for(Point p: myPoint[currentPiece][rotation]) {
well[pt.x + p.x][pt.y + p.y] = myColor[currentPiece];
}
clearRows();
newPiece();
}
public void deleteRow(int row) {
for(int j = row - 1; j > 0; j--) {
for(int i = 1; i < 11; i++) {
well[i][j + 1] = well [i][j];
}
}
}
public void clearRows() {
boolean gap;
int numClear = 0;
for(int j = 21; j > 0; j--) {
gap = false;
for(int i = 1; i < 11; i++) {
if(well[i][j] == Color.black) {
gap = true;
break;
}
}
if(!gap) {
deleteRow(numClear);
j+=1;
numClear+=1;
}
}
switch(numClear) {
case 1 :
score+=100;
break;
case 2 :
score+=300;
break;
case 3 :
score+=500;
break;
case 4 :
score+=800;
break;
}
}
private void drawPiece(Graphics g) {
g.setColor(myColor[currentPiece]);
for(Point p: myPoint[currentPiece][rotation]) {
g.fillRect((p.x + pt.x) *26, (pt.y + p.y) * 26, 25, 25);
}
}
public void paintComponent(Graphics g) {
g.fillRect(0, 0, 26 * 12, 26 * 23);
for(int i = 0; i < 23; i++) {
for(int j = 0; j < 23; j++) {
g.setColor(well[i][j]);
g.fillRect(26*i, 26*j, 25, 25);
}
}
g.setColor(Color.WHITE);
g.drawString("Score is :"+score, 19*12, 25);
drawPiece(g);
}
//main
public static void main(String[] args) {
JFrame f = new JFrame("MyGame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12*26+10, 26*23+25);
f.setVisible(true);
final TetrisGame game = new TetrisGame();
game.init();
f.add(game);
f.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
game.rotate(-1);
break;
case KeyEvent.VK_DOWN:
game.rotate(+1);
break;
case KeyEvent.VK_LEFT:
game.move(-1);
break;
case KeyEvent.VK_SPACE:
game.drop();
game.score+=1;
break;
}
}
});
new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
game.drop();
}
}
}.start();
}
} |
Partager