相对简单,粗陋,希望大家给点指点。 这算是督促自己发布源码的一个地方,虽然菜鸟,但也得养成好习惯,嘿嘿~
package game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameFace extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
// 相关参数设定:所有全局变量初始化
private ImageIcon[][] Blocks = new ImageIcon[12][22];// 定位
private final int panelWidth = 384 + 32 * 5;// 宽 12*32
private final int panelHight = 672;// 高 22*32
private int x = 192, y = 0;// 初始坐标
private int DELAY = 800;// 延迟和单位图像大小
private final int IMAGE_SIZE = 32;
private Timer timer = new Timer(DELAY, new Down());// 计时器
private ImageIcon Block, Earth;// 元方块
private int score = 0;// 得分
private ImageIcon background;
private Fangkuai[] F = new Fangkuai[2];
// 内部类:方块
public class Fangkuai {
private int xingzhuang[][] = new int[5][5];
public int pengzhuang() {
int i, j;
for (i = 4; i > 0; i--)
for (j = 4; j > 0; j--) {
if (xingzhuang[i][j] != 0)
if ((Blocks[x / 32 + i][y / 32 + j + 1] != null))
return 1;
}
return 0;
}
public void set_xingzhuang(int xx)
{
for (int m = 0; m < 5; m++)
for (int n = 0; n < 5; n++)
xingzhuang[m][n] = 0;
switch (xx) {
case 1:
xingzhuang[1][1] = 1;
xingzhuang[2][1] = 1;
xingzhuang[1][2] = 1;
xingzhuang[2][2] = 1;
break;
case 2:
xingzhuang[1][1] = 1;
xingzhuang[2][1] = 1;
xingzhuang[2][2] = 1;
xingzhuang[2][3] = 1;
break;
case 3:
xingzhuang[2][1] = 1;
xingzhuang[3][1] = 1;
xingzhuang[1][2] = 1;
xingzhuang[2][2] = 1;
break;
case 4:
xingzhuang[2][1] = 1;
xingzhuang[2][2] = 1;
xingzhuang[3][2] = 1;
xingzhuang[4][2] = 1;
break;
case 5:
xingzhuang[2][1] = 1;
xingzhuang[1][2] = 1;
xingzhuang[3][1] = 1;
xingzhuang[2][2] = 1;
break;
case 6:
xingzhuang[2][1] = 1;
xingzhuang[1][2] = 1;
xingzhuang[2][2] = 1;
xingzhuang[3][2] = 1;
break;
case 0:
xingzhuang[2][1] = 1;
xingzhuang[2][2] = 1;
xingzhuang[2][3] = 1;
xingzhuang[2][4] = 1;
break;
default:
break;
}
}
// 旋转方法:理解动态过程;图像模拟;
public void xuanzhuan() {
outer: for (int k = 0; k < 3; k++)
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (x / 32 + j > 11 || x / 32 + j < 0)
break outer;
if (xingzhuang[i][j] != 0 && xingzhuang[j][4 - i] == 0
&& xingzhuang[i][j] != 2) {
xingzhuang[i][j] = 0;
xingzhuang[j][4 - i] = 2;
}
}
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (xingzhuang[i][j] == 2)
xingzhuang[i][j] = 1;
}
}
public Fangkuai(int xxx) {
x = 192;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
xingzhuang[i][j] = 0;
set_xingzhuang(xxx);
}
}// 方块类
// GAMEFACE主类构造函数
public GameFace() {
// 初始化数据
background = new ImageIcon("images/3.gif");
this.setTitle("" + score);
Block = new ImageIcon("images/1.gif");
Earth = new ImageIcon("images/2.gif");
for (int i = 0; i < 12; i++)
Blocks[i][21] = Earth;
for (int i = 0; i < 2; i++) {
F[i] = new Fangkuai(new Random().nextInt(7));
}
GameArea game = new GameArea();
this.add(game);
this.addKeyListener(new Control());
timer.start();
}
// 内部类GameArea:游戏进行区域
private class GameArea extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public GameArea() {
setPreferredSize(new Dimension(panelWidth, panelHight));
setBackground(Color.BLACK);
}
// 绘制
public void paintComponent(Graphics page) {
super.paintComponent(page);
setTitle("" + score);
background.paintIcon(this, page, 0, 0);
// 显示下一个方块
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (F[1].xingzhuang[i][j] != 0) {
Block.paintIcon(this, page, 384 + i * 32, j * 32);
}
// 降落中的方块
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (F[0].xingzhuang[i][j] != 0) {
Block.paintIcon(this, page, x + i * 32, y + j * 32);
}
// 落地的方块
for (int i = 0; i < 12; i++)
for (int j = 0; j < 22; j++) {
if (Blocks[i][j] != null)
Blocks[i][j].paintIcon(this, page, i * 32, j * 32);
}
}
}
// 内部类:下降
public class Down implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 下降速度
if (F[0].pengzhuang() == 0)
y += IMAGE_SIZE;
// 落地判断:碰撞;
else {
timer.stop();
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (F[0].xingzhuang[i][j] != 0)
Blocks[x / 32 + i][y / 32 + j] = Block;
}
y = 0;
F[0] = F[1];
F[1] = new Fangkuai(new Random().nextInt(7));
timer = new Timer(DELAY, new Down());
timer.start();
// 消除方块
for (int j = 21; j > 0; j--) {
int k = 1;
for (int i = 0; i < 12; i++) {
if (Blocks[i][j] == Block)
k *= 1;
else
k *= 0;
}
if (k == 1) {
score += 100;
for (int l = 0; l < 12; l++)
Blocks[l][j] = null;
for (int p = j; p > 0; p--)
for (int q = 0; q < 12; q++)
Blocks[q][p] = Blocks[q][p - 1];
}
}
}
repaint();
}
}
// 内部类:键盘控制
public class Control implements KeyListener {
public void keyPressed(KeyEvent e) {
int test;
if (e.getKeyCode() == KeyEvent.VK_LEFT&&F[0].pengzhuang()==0) {
test = 1;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (F[0].xingzhuang[i][j] != 0 && (x + i * 32 - 32) < 0)
test = 0;
}
if (test == 1) {
x -= IMAGE_SIZE;
repaint();
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT&&F[0].pengzhuang()==0) {
test = 1;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (F[0].xingzhuang[i][j] != 0
&& (i * 32 + x + 32) >= (32 * 12))
test = 0;
}
if (test == 1) {
x += IMAGE_SIZE;
repaint();
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
timer.stop();
timer = new Timer(50, new Down());
timer.start();
}
if (e.getKeyCode() == KeyEvent.VK_UP&&F[0].pengzhuang()==0) {
F[0].xuanzhuan();
repaint();
}
}
public void keyTyped(KeyEvent e) {
;
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
}