import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.Timer;
class Window extends JFrame {
int window_width = 305, window_height = 450, location_width = 500,
location_height = 130;
static int may_width = 10, may_height = 14;
static int score = 0;
JLabel score_box = new JLabel("得分: " + score);
static int[][] map = new int[may_height][may_width];
static Box[][] wall = new Box[may_height][may_width];
private void initMay() {
for (int i = 0; i < may_height; ++i) {
for (int j = 0; j < may_width; ++j) {
map[i][j] = 0;
wall[i][j] = new Box(j * 30, i * 30);
}
}
}
static void setMay(int width, int height) {
map[height / 30][width / 30] = 1;
}
static void removeMay(int width, int height) {
map[height / 30][width / 30] = 0;
}
JLayeredPane pane = new JLayeredPane();
Window() {
super("俄罗斯方块 ");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点叉时关闭窗口
super.setSize(window_width, window_height);
super.setLocation(location_width, location_height);
super.setResizable(false);
// super.setVisible(true); //窗体显示
pane.setLayout(null);
this.add(pane, BorderLayout.CENTER); // 设置容器
JLabel background = new JLabel();
background.setBounds(0, 0, window_width, window_height);
Icon icon = new ImageIcon("img/b.jpg");
background.setIcon(icon);
pane.add(background, JLayeredPane.DEFAULT_LAYER);
score_box.setBounds(160, 0, 140, 50);
score_box.setFont(new java.awt.Font("Dialog", 1, 20));
score_box.setForeground(Color.red);
pane.add(score_box, JLayeredPane.MODAL_LAYER);
this.validate();
initMay();
for (int i = 0; i < Window.may_height; ++i) {
for (int j = 0; j < Window.may_width; ++j) {
Window.wall[i][j].setVisible(false);
pane.add(Window.wall[i][j], JLayeredPane.PALETTE_LAYER);
}
}
}
void setVisible() {
super.setVisible(true);
}
boolean finishLine(int line) {
int sum = 0;
for (int i = 0; i < 10; ++i) {
if (Window.map[line][i] == 2) {
sum += 1;
}
}
if (sum == 10) {
return true;
} else {
return false;
}
}
}
class Box extends JLabel {
static int box_width = 30;
int location_width = 120, location_height = 0;
Box(int width, int height) {
this.location_width = width;
this.location_height = height;
Icon icon = new ImageIcon("img/c.png");
this.setBounds(location_width, location_height, box_width, box_width);
this.setIcon(icon);
}
Box() {
Icon icon = new ImageIcon("img/c.png");
this.setBounds(location_width, location_height, box_width, box_width);
this.setIcon(icon);
}
}
class BrickOne {
Box[] box = new Box[4];
BrickOne() {
box[0] = new Box();
box[1] = new Box(box[0].location_width, box[0].location_height
+ Box.box_width);
box[2] = new Box(box[1].location_width - Box.box_width,
box[1].location_height);
box[3] = new Box(box[2].location_width, box[2].location_height
- Box.box_width);
for (int i = 0; i < 4; ++i) {
Window.setMay(box[i].location_width, box[i].location_height);
}
}
void move() {
for (int i = 0; i < 4; ++i) {
Window.removeMay(box[i].location_width, box[i].location_height);
}
for (int i = 0; i < 4; ++i) {
box[i].location_height += Box.box_width;
box[i].setBounds(box[i].location_width, box[i].location_height,
Box.box_width, Box.box_width);
}
for (int i = 0; i < 4; ++i) {
Window.setMay(box[i].location_width, box[i].location_height);
}
}
boolean atBottom() {
for (int i = 0; i < 4; ++i) {
if (box[i].location_height == 390) {
return true;
}
}
return false;
}
boolean haveBox() {
for (int i = 0; i < 4; ++i) {
if (Window.map[box[i].location_height / 30 + 1][box[i].location_width / 30] == 2) {
return true;
}
}
return false;
}
boolean haveBox_Now() {
for (int i = 0; i < 4; ++i) {
if (Window.map[box[i].location_height / 30][box[i].location_width / 30] == 2) {
return true;
}
}
return false;
}
void change() {
}
boolean canChange() {
return false;
}
}
class BrickTwo extends BrickOne {
BrickTwo() {
box[0] = new Box();
box[1] = new Box(box[0].location_width + Box.box_width,
box[0].location_height);
box[2] = new Box(box[0].location_width, box[0].location_height
+ Box.box_width);
box[3] = new Box(box[0].location_width - Box.box_width,
box[0].location_height);
}
void change() {
for (int i = 0; i < 4; ++i) {
Window.removeMay(box[i].location_width, box[i].location_height);
}
for (int i = 1; i < 4; ++i) {
if (box[i].location_width >= box[0].location_width
&& box[i].location_height <= box[0].location_height) {
int width_dec = box[i].location_width - box[0].location_width;
int height_dec = box[0].location_height
- box[i].location_height;
box[i].location_width = box[0].location_width + height_dec;
box[i].location_height = box[0].location_height + width_dec;
box[i].setBounds(box[i].location_width, box[i].location_height,
Box.box_width, Box.box_width);
} else if (box[i].location_width >= box[0].location_width
&& box[i].location_height >= box[0].location_height) {
int width_dec = box[i].location_width - box[0].location_width;
int height_dec = box[i].location_height
- box[0].location_height;
box[i].location_width = box[0].location_width - height_dec;
box[i].location_height = box[0].location_height + width_dec;
box[i].setBounds(box[i].location_width, box[i].location_height,
Box.box_width, Box.box_width);
} else if (box[i].location_width <= box[0].location_width
&& box[i].location_height >= box[0].location_height) {
int width_dec = box[0].location_width - box[i].location_width;
int height_dec = box[i].location_height
- box[0].location_height;
box[i].location_width = box[0].location_width - height_dec;
box[i].location_height = box[0].location_height - width_dec;
box[i].setBounds(box[i].location_width, box[i].location_height,
Box.box_width, Box.box_width);
} else {
int width_dec = box[0].location_width - box[i].location_width;
int height_dec = box[0].location_height
- box[i].location_height;
box[i].location_width = box[0].location_width + height_dec;
box[i].location_height = box[0].location_height - width_dec;
box[i].setBounds(box[i].location_width, box[i].location_height,
Box.box_width, Box.box_width);
}
}
for (int i = 0; i < 4; ++i) {
Window.setMay(box[i].location_width, box[i].location_height);
}
}
boolean canChange() {
if (box[0].location_width != 0 && box[0].location_width != 270
&& box[0].location_height != 0 && box[0].location_height != 390) {
if (Window.map[box[0].location_height / 30][box[0].location_width / 30 - 1] != 2
&& Window.map[box[0].location_height / 30][box[0].location_width / 30 + 1] != 2) {
return true;
}
}
return false;
}
}
class BrickThree extends BrickTwo {
BrickThree() {
box[0] = new Box();
box[1] = new Box(box[0].location_width + Box.box_width,
box[0].location_height);
box[2] = new Box(box[0].location_width - Box.box_width,
box[0].location_height);
box[3] = new Box(box[2].location_width, box[2].location_height
+ Box.box_width);
}
}
class BrickFour extends BrickTwo {
BrickFour() {
box[0] = new Box();
box[1] = new Box(box[0].location_width + Box.box_width,
box[0].location_height);
box[2] = new Box(box[1].location_width + Box.box_width,
box[1].location_height);
box[3] = new Box(box[0].location_width - Box.box_width,
box[0].location_height);
}
boolean canChange() {
if (box[0].location_width != 0 && box[0].location_width != 270
&& box[0].location_width != 30 && box[0].location_height != 0
&& box[0].location_height != 390
&& box[0].location_height != 30) {
if (Window.map[box[0].location_height / 30][box[0].location_width / 30 - 1] != 2
&& Window.map[box[0].location_height / 30][box[0].location_width / 30 + 1] != 2) {
return true;
}
}
return false;
}
}
class BrickFive extends BrickTwo {
BrickFive() {
box[0] = new Box();
box[1] = new Box(box[0].location_width + Box.box_width,
box[0].location_height);
box[2] = new Box(box[0].location_width, box[0].location_height
+ Box.box_width);
box[3] = new Box(box[2].location_width - Box.box_width,
box[2].location_height);
}
}
class BrickSix extends BrickTwo {
BrickSix() {
box[0] = new Box();
box[1] = new Box(box[0].location_width + Box.box_width,
box[0].location_height);
box[2] = new Box(box[1].location_width, box[1].location_height
+ Box.box_width);
box[3] = new Box(box[0].location_width - Box.box_width,
box[0].location_height);
}
}
class BrickSeven extends BrickTwo {
BrickSeven() {
box[0] = new Box();
box[1] = new Box(box[0].location_width, box[0].location_height
+ Box.box_width);
box[2] = new Box(box[1].location_width + Box.box_width,
box[1].location_height);
box[3] = new Box(box[0].location_width - Box.box_width,
box[0].location_height);
}
}
class Game implements ActionListener, KeyListener {
int speed = 50, fall_speed = 8, fall_time = 8;
int turn_event = 0;
int brick_num = 0;
Window window = new Window();
BrickOne brick;
Timer timer = new Timer(this.speed, this);
Game() {
brick_num = new Random().nextInt(7) + 1;
switch (brick_num) {
case 1:
brick = new BrickOne();
break;
case 2:
brick = new BrickTwo();
break;
case 3:
brick = new BrickThree();
break;
case 4:
brick = new BrickFour();
break;
case 5:
brick = new BrickFive();
break;
case 6:
brick = new BrickSix();
break;
case 7:
brick = new BrickSeven();
break;
}
for (int i = 0; i < 4; ++i) {
window.pane.add(brick.box[i], JLayeredPane.PALETTE_LAYER);
}
window.setFocusable(true); // 获取焦点(虽然不知道为什么,但是觉得好屌啊 -。-)
}
public void actionPerformed(ActionEvent e) {
if (turn_event != 0) {
switch (turn_event) {
case 1:
for (int i = 0; i < 4; ++i) {
Window.removeMay(brick.box[i].location_width,
brick.box[i].location_height);
}
try {
for (int i = 0; i < 4; ++i) {
brick.box[i].location_width -= Box.box_width;
brick.box[i].setBounds(brick.box[i].location_width,
brick.box[i].location_height, Box.box_width,
Box.box_width);
}
if (brick.haveBox_Now()) {
for (int i = 0; i < 4; ++i) {
brick.box[i].location_width += Box.box_width;
brick.box[i].setBounds(brick.box[i].location_width,
brick.box[i].location_height,
Box.box_width, Box.box_width);
}
}
for (int i = 0; i < 4; ++i) {
Window.setMay(brick.box[i].location_width,
brick.box[i].location_height);
}
} catch (ArrayIndexOutOfBoundsException t) {
for (int i = 0; i < 4; ++i) {
brick.box[i].location_width += Box.box_width;
brick.box[i].setBounds(brick.box[i].location_width,
brick.box[i].location_height, Box.box_width,
Box.box_width);
}
for (int i = 0; i < 4; ++i) {
Window.setMay(brick.box[i].location_width,
brick.box[i].location_height);
}
}
break;
case 2:
for (int i = 0; i < 4; ++i) {
Window.removeMay(brick.box[i].location_width,
brick.box[i].location_height);
}
try {
for (int i = 0; i < 4; ++i) {
brick.box[i].location_width += Box.box_width;
brick.box[i].setBounds(brick.box[i].location_width,
brick.box[i].location_height, Box.box_width,
Box.box_width);
}
if (brick.haveBox_Now()) {
for (int i = 0; i < 4; ++i) {
brick.box[i].location_width -= Box.box_width;
brick.box[i].setBounds(brick.box[i].location_width,
brick.box[i].location_height,
Box.box_width, Box.box_width);
}
}
for (int i = 0; i < 4; ++i) {
Window.setMay(brick.box[i].location_width,
brick.box[i].location_height);
}
} catch (ArrayIndexOutOfBoundsException t) {
for (int i = 0; i < 4; ++i) {
brick.box[i].location_width -= Box.box_width;
brick.box[i].setBounds(brick.box[i].location_width,
brick.box[i].location_height, Box.box_width,
Box.box_width);
}
for (int i = 0; i < 4; ++i) {
Window.setMay(brick.box[i].location_width,
brick.box[i].location_height);
}
}
break;
case 3:
if (brick.canChange()) {
brick.change();
}
break;
case 4:
fall_time = 1;
break;
}
turn_event = 0;
}
if ((--fall_speed) == 0) {
if (brick.atBottom() || brick.haveBox()) {
timer.stop();
for (int i = 0; i < 4; ++i) {
brick.box[i].setVisible(false);
window.pane.remove(brick.box[i]);
}
for (int i = 0; i < 4; ++i) {
Window.map[brick.box[i].location_height / Box.box_width][brick.box[i].location_width
/ Box.box_width] = 2;
Window.wall[brick.box[i].location_height / Box.box_width][brick.box[i].location_width
/ Box.box_width].setVisible(true);
}
for (int i = 13; i >= 0; --i) {
int temp = 0;
if (window.finishLine(i)) {
for (int j = i; j >= 0; --j) {
for (int k = 0; k < 10; ++k) {
if (Window.map[j][k] == 2) {
Window.wall[j][k].setVisible(false);
}
}
}
for (int j = i; j > 0; --j) {
for (int k = 0; k < 10; ++k) {
Window.map[j][k] = Window.map[j - 1][k];
}
}
for (int j = 0; j < 10; ++j) {
Window.map[0][j] = 0;
}
for (int j = i; j >= 0; --j) {
for (int k = 0; k < 10; ++k) {
if (Window.map[j][k] == 2) {
Window.wall[j][k].setVisible(true);
}
}
}
Window.score += 100;
window.score_box.setText("得分: " + Window.score);
temp = 1;
}
if (temp == 1) {
++i;
temp = 0;
}
}
fall_time = (Math.max(8 - Window.score / 1000, 1));
fall_speed = fall_time;
brick_num = new Random().nextInt(7) + 1;
switch (brick_num) {
case 1:
brick = new BrickOne();
break;
case 2:
brick = new BrickTwo();
break;
case 3:
brick = new BrickThree();
break;
case 4:
brick = new BrickFour();
break;
case 5:
brick = new BrickFive();
break;
case 6:
brick = new BrickSix();
break;
case 7:
brick = new BrickSeven();
break;
}
for (int i = 0; i < 4; ++i) {
window.pane.add(brick.box[i], JLayeredPane.PALETTE_LAYER);
}
if (brick.haveBox_Now()) {
JLabel end_background = new JLabel();
end_background.setBounds(0, 0, window.window_width,
window.window_height);
Icon icon = new ImageIcon("img/end_background.jpg");
end_background.setIcon(icon);
window.pane.add(end_background, JLayeredPane.POPUP_LAYER);
JLabel ending_box1 = new JLabel();
ending_box1.setFont(new java.awt.Font("Dialog", 1, 20));
ending_box1.setBounds(60, 150, 180, 25);
window.pane.add(ending_box1, JLayeredPane.DRAG_LAYER);
ending_box1.setText("你的分数是:" + Window.score);
JLabel ending_box2 = new JLabel();
ending_box2.setFont(new java.awt.Font("Dialog", 1, 20));
ending_box2.setBounds(40, 175, 250, 25);
window.pane.add(ending_box2, JLayeredPane.DRAG_LAYER);
double percent = 0;
if (Window.score >= 0 && Window.score <= 1500) {
percent = Window.score * 1.0 / 1500 * 10;
} else if (Window.score > 1500 && Window.score <= 3500) {
percent = (Window.score - 1500) * 1.0 / 2000 * 40 + 10;
} else if (Window.score > 3500 && Window.score <= 5000) {
percent = (Window.score - 3500) * 1.0 / 1500 * 49.9
+ 50;
} else {
percent = 99.9;
}
ending_box2.setText("你打败了"
+ String.format("%.1f%%", percent) + "的网友!");
return;
}
timer = new Timer(speed, this);
timer.start();
return;
}
brick.move();
fall_speed = fall_time;
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
turn_event = 3;
break;
case KeyEvent.VK_RIGHT:
turn_event = 2;
break;
case KeyEvent.VK_DOWN:
turn_event = 4;
break;
case KeyEvent.VK_LEFT:
turn_event = 1;
break;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
void start() {
window.addKeyListener(this);
timer.start();
}
}
public class Tetris {
public static void main(String args[]) {
final Game game = new Game();
final JLabel start_background = new JLabel();
start_background.setBounds(0, 0, game.window.window_width,
game.window.window_height);
Icon icon = new ImageIcon("img/start_background.jpg");
start_background.setIcon(icon);
game.window.pane.add(start_background, JLayeredPane.POPUP_LAYER);
final JButton button1 = new JButton();
Icon icon1 = new ImageIcon("img/button1.jpg");
button1.setIcon(icon1);
final JButton button2 = new JButton();
Icon icon2 = new ImageIcon("img/button2.jpg");
button2.setIcon(icon2);
button1.setBounds(90, 280, 120, 40);
button2.setBounds(90, 340, 120, 40);
game.window.pane.add(button1, JLayeredPane.DRAG_LAYER);
game.window.pane.add(button2, JLayeredPane.DRAG_LAYER);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent b1) {
start_background.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
game.window.pane.remove(start_background);
game.window.pane.remove(button1);
game.window.pane.remove(button2);
game.start();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent b2) {
System.exit(0);
}
});
game.window.setVisible();
// game.start();
}
}
最近下载更多
最近浏览更多
llqq114 LV10
7月30日
武诗惠
6月12日
暂无贡献等级
WFWEVGR LV1
2024年12月27日
luoyanglin LV2
2024年9月14日
hainabian1977 LV2
2024年6月24日
鬼屋报道 LV3
2024年6月1日
aazz123 LV1
2023年11月26日
微信网友_6699076084797440 LV7
2023年10月30日
wangjialiang1 LV17
2023年8月20日
fmj0408 LV1
2023年6月19日

