package com.wireless.terris;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.util.Random;
import javax.swing.*;
public class Terris extends JFrame implements Runnable, KeyListener {
private short xOffSet = 2, yOffSet = 0, blockType = (short) Math.round(Math.random() * 6), blockRotation = 0, blockColor = (short) Math.round(Math.random() * 5);
private short matrix[][] = new short[21][10], cube[][] = new short[4][4];//
private Random random = new Random();
private short block[][][][] = {{{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 1, 0, 0 } },/* l */{ { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } } },/*-*/{{ { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 },{ 0, 0, 0, 0 } },/* z */{ { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 },{ 0, 1, 0, 0 } } },/* z| */{{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 },{ 0, 0, 0, 0 } },/* xz */{ { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 },{ 0, 0, 0, 0 } } },/* xz| */{ { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } } },/** []*/{{ { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 },{ 0, 0, 0, 0 } },{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 },{ 0, 0, 0, 0 } },{ { 1, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } } },/* f */{{ { 1, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },{ { 0, 0, 1, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } },{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 },{ 0, 0, 0, 0 } },{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 },{ 0, 0, 0, 0 } } },/* xf */ {{ { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } },{ { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },{ { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } } } };/* t */
private Image[] images = {new ImageIcon("D:/Java/eclipse/workspace/Terris/src/img/Red.gif").getImage(),new ImageIcon("D:/Java/eclipse/workspace/Terris/src/img/Blue.gif").getImage(),new ImageIcon(("D:/Java/eclipse/workspace/Terris/src/img/Pink.gif")).getImage(),new ImageIcon(("D:/Java/eclipse/workspace/Terris/src/img/BBlue.gif")).getImage(),new ImageIcon(("D:/Java/eclipse/workspace/Terris/src/img/Orange.gif")).getImage(),new ImageIcon(("D:/Java/eclipse/workspace/Terris/src/img/Green.gif")).getImage(),new ImageIcon("D:/Java/eclipse/workspace/Terris/src/img/Red.gif").getImage()};
private boolean isPlaying = true;
public Terris() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screenSize.width / 2 - 150 / 2,screenSize.height / 2 - 375 / 2);
setSize(160, 335);
setVisible(true);
createBufferStrategy(2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
}
private void update() {
BufferStrategy strategy = this.getBufferStrategy();
if (!strategy.contentsLost())
strategy.show();
Toolkit.getDefaultToolkit().sync();
}
public void paint(Graphics g) {
Graphics tg = this.getBufferStrategy().getDrawGraphics();
tg.fillRect(5, 30, 150, 340);
for (int i = 0; i < 21; i++)
for (int j = 0; j < 10; j++) {
if (matrix[i][j] != 0)
tg.drawImage(images[matrix[i][j]], j * 15 + 5, i * 15 + 15,null);
if (i < 4 && j < 4&& block[blockType][blockRotation][i][j] != 0)
tg.drawImage(images[blockColor + 1],((j + xOffSet) * 15) + 5,((i + yOffSet) * 15) + 15, null);
}
update();
}
public static void main(String[] args) {
new Thread(new Terris()).start();
}
public void run() {
while (isPlaying)
try {
if (check(0, 0, 0, 1))
yOffSet += 1;
else {
if (yOffSet == 0) {
isPlaying = false;
repaint();
continue;
}
freezeAndNew();
}
repaint();
Thread.sleep(600);
} catch (InterruptedException e) {
}
}
private boolean check(int left, int right, int up, int down) {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (((xOffSet + j - left + right < 0 || xOffSet + j - left+ right >= 10) && block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0)|| ((yOffSet + i + down >= 21) && block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0)|| (block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0 && matrix[yOffSet+ i + down][xOffSet + j - left + right] != 0))
return false;
return true;
}
private void freezeAndNew() {
boolean[] clear = new boolean[4];
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++)
if (block[blockType][blockRotation][i][j] != 0)
matrix[i + yOffSet][j + xOffSet] = (short) (blockColor + 1);
clear[i]=i + yOffSet>=matrix.length?false:(matrix[i + yOffSet][0]!=0&&matrix[i + yOffSet][1]!=0&&matrix[i + yOffSet][2]!=0&&matrix[i + yOffSet][3]!=0&&matrix[i + yOffSet][4]!=0&&matrix[i + yOffSet][5]!=0&&matrix[i + yOffSet][6]!=0&&matrix[i + yOffSet][7]!=0&&matrix[i + yOffSet][8]!=0&&matrix[i + yOffSet][9]!=0);
}
for(int i=0;i<clear.length;i++){
if(clear[i]){
for(int j=yOffSet+i;j>0;j--){
matrix[j]=matrix[j-1];
}
}
}
yOffSet = blockRotation = 0;
xOffSet = 2;
blockType = (short) Math.round(Math.random() * 6);
blockRotation = (short) Math.round((Math.random() * (block[blockType].length - 1)));
blockColor = (short) Math.round(Math.random() * 5);
}
public void keyPressed(KeyEvent e) {// 38-上 40-下 37-左 39-右
if ((e.getKeyCode() == 65 || e.getKeyCode() == 37) && check(1, 0, 0, 0)) {// left
xOffSet--;
} else if ((e.getKeyCode() == 68 || e.getKeyCode() == 39)&& check(0, 1, 0, 0)) {// right
xOffSet++;
} else if ((e.getKeyCode() == 87 || e.getKeyCode() == 38)&& check(0, 0, 1, 0)) {// up
blockRotation = (short) ((blockRotation + 1) >= block[blockType].length ? 0: (blockRotation + 1));
} else if ((e.getKeyCode() == 83 || e.getKeyCode() == 40)&& check(0, 0, 0, 1)) {// down
if (check(0, 0, 0, 1))
yOffSet += 1;
else
freezeAndNew();
}
repaint();
System.out.println(e.getKeyCode());
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
转载请注明出处http://wireless.javaeye.com/blog/595321