前段时间收到某老师的鄙视,决心做一个俄罗斯方块小游戏,其实一直都想过这个游戏,还是有一定的思路的,但是真正轮到我亲手去写,过程上还是有所曲折的,但是也正是因为这样,也还是有颇多收获的。
作为一个菜鸟,我写这个差不多用了一个星期的时间,查了很多东西,在这个过程中也学到了很多。
首先就是写界面还是手写为好,不要依赖myelipese那种可视化的插件。拖来拖去,看着简单,但是如果你写的东西不是那种很简单的,我的意思是说应付老师的东西,还是自己写的好,不然你写到最后,那些插件会把你的代码搞得乱七八糟,我一开始就是用那些插件拖出来的界面,但是后来写的写的是在是难以忍受,于是就直接上代码写了。
再者我的收获就是要考虑的长远些。记得开始写方块整行消除的时候,考虑了很久没有想到很好的方法,因为我的方块本来就是贴图贴上去的,我要消除根本不可能说将一张图片的某一部分消除,如果真要这么做,我想我就误入歧途了。于是我重新将方块变成一个一个的小格子贴上去,如果消除整行,只要根据地图将一行的小格子消除,将上面的格子顺移下来就好。
最后就是自己的成就感了。第一次写了一个游戏,虽然过程有很多困难,虽然现在这个游戏还不完善,但是还是可以算作一个俄罗斯方块了。
我的俄罗斯方块目前为v1.1版,待更新的功能有:
1、 每次下落方块的变换功能;
2、 游戏暂停功能,继续功能;
3、 游戏调节难度功能;
4、 游戏背景音乐;
5、 游戏积分排行榜;
目前我就想到这几个功能还没有实现,过两天继续写。
俄罗斯方块v1.1
MainFrame.java
package com.Tetris;
import java.awt.Color;
import java.awt.HeadlessException;
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.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainFrame main = new MainFrame();
main.setVisible(true);
}
ImageIcon Cube = new ImageIcon("src/Cube.png");
ImageIcon Strinp = new ImageIcon("src/Strip.png");
ImageIcon TuImage = new ImageIcon("src/tu.png");
ImageIcon xImage = new ImageIcon("src/x.png");
public myPanel gamePanel;
public JPanel bottomPanel;
public JPanel topPanel;
public JLabel topLable;
public int x = 150, y = 25;
public boolean isStart = false;
public boolean Pause = false;
JLabel newlabel = new JLabel();
public boolean isFalling = false;
int index = 0;
Timer newtime = new Timer(400, new ReboundListener());
public MainFrame() throws HeadlessException {
super();
// TODO Auto-generated constructor stub
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("俄罗斯方块 v1.1 _wz");
this.setLayout(null);
initgamePanel();
initbottomPanel();
inittopPanel();
this.add(topPanel);
this.add(gamePanel);
this.add(bottomPanel);
this.setBounds(400, 200, 355, 480);
this.setResizable(false);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_1)
if (!isStart) {
myTimerStart();
isStart = true;
isFalling = true;
}
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if (isFalling)
switch (index) {
case 0:
if (x < 300)
x += 25;
break;
case 1:
if (x < 250)
x += 25;
break;
case 2:
if (x < 300)
x += 25;
break;
case 3:
if (x < 300)
x += 25;
break;
default:
break;
}
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if (isFalling)
if (x > 0)
x -= 25;
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
}
public void inittopPanel() {
topPanel = new JPanel();
topPanel.setLayout(null);
topPanel.setBounds(0, 0, 350, 30);
topPanel.setBackground(Color.YELLOW);
topLable = new JLabel("数字键1开始。 得分:0");
topLable.setBounds(0, 0, 300, 30);
topLable.setForeground(Color.red);
topPanel.add(topLable);
}
public void initgamePanel() {
gamePanel = new myPanel();
gamePanel.setLayout(null);
gamePanel.setBounds(0, 25, 350, 400);
gamePanel.add(newlabel);
gamePanel.setBackground(new java.awt.Color(51, 204, 255));
}
public void initbottomPanel() {
bottomPanel = new JPanel();
bottomPanel.setBounds(0, 425, 350, 30);
bottomPanel.setBackground(Color.BLACK);
}
class ReboundListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
switch (index) {
case 0:
if (y < 350)
if (gamePanel.map.isCovered(x, y + 50)
&& gamePanel.map.isCovered(x + 25, y + 50)) {
y += 25;
newlabel.setBounds(x, y, 50, 50);
} else
isFalling = false;
else
isFalling = false;
break;
case 1:
if (y < 375)
if (gamePanel.map.isCovered(x, y + 25)
&& gamePanel.map.isCovered(x + 25, y + 25)
&& gamePanel.map.isCovered(x + 50, y + 25)
&& gamePanel.map.isCovered(x + 75, y + 25)
) {
y += 25;
newlabel.setBounds(x, y, 100, 25);
} else
isFalling = false;
else
isFalling = false;
break;
case 2:
if (y < 325)
if (gamePanel.map.isCovered(x, y + 50)
&& gamePanel.map.isCovered(x + 25, y + 75)) {
y += 25;
newlabel.setBounds(x, y, 50, 75);
} else
isFalling = false;
else
isFalling = false;
break;
case 3:
if (y < 325)
if (gamePanel.map.isCovered(x, y + 50)
&& gamePanel.map.isCovered(x + 25, y + 75)) {
y += 25;
newlabel.setBounds(x, y, 50, 75);
} else
isFalling = false;
else
isFalling = false;
break;
default:
break;
}
if (!isFalling) {// 画方块
upDateScore();
myTimerStop();
gamePanel.drawMyImage(x, y, index);
Initxy();
}
}
}
private void myTimerStop() {
newtime.stop();
}
private void myTimerStart() {
newtime.start();
Random indexRandon = new Random();
index = indexRandon.nextInt(4);
switch (index) {
case 0:
newlabel.setIcon(Cube);
newlabel.setBounds(x, y, 50, 50);
break;
case 1:
newlabel.setIcon(Strinp);
newlabel.setBounds(x, y, 100, 25);
break;
case 2:
newlabel.setIcon(TuImage);
newlabel.setBounds(x, y, 50, 75);
break;
case 3:
newlabel.setIcon(xImage);
newlabel.setBounds(x, y, 50, 75);
break;
default:
break;
}
}
private void upDateScore() {
topLable.setText("数字键1开始。 得分:" + gamePanel.score);
}
private void Initxy() {
x = 150;
y = 25;
isFalling = true;
myTimerStart();
isLose(150, 25);
}
private void isLose(int x, int y) {
if (!gamePanel.map.isCovered(x, y)) {
myTimerStop();
JDialog dia = new JDialog(this, "提示", true);
dia.setBounds(150, 200, 200, 100);
JLabel la = new JLabel("您失败了");
dia.add(la);
la.setLocation(100, 50);
dia.setVisible(true);
}
}
}
Map.java
package com.Tetris;
public class Map {
int x = 0;
int y = 0;
public int[][] map = { // x14
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // x17
};
public Map() {
super();
};
public void setMapTrue(int index, int x1, int y1) {
x = (x1 / 25);
y = (y1 / 25);
switch (index) {
case 0:
map[y][x] = 1;
map[y][x + 1] = 1;
map[y + 1][x] = 1;
map[y + 1][x + 1] = 1;
break;
case 1:
map[y][x] = 1;
map[y][x + 1] = 1;
map[y][x + 2] = 1;
map[y][x + 3] = 1;
break;
default:
break;
}
}
public void setMapFalse(int w) {
for (int i = 0; i < 14; i++)
map[w / 25][i] = 0;
}
public boolean isCovered(int x1, int y1) {
x = x1 / 25;
y = y1 / 25;
if (map[y][x] == 0)// 未被覆盖
return true;
else
return false;
}
}
myPanel.java
package com.Tetris;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class myPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
ImageIcon Diamonds = new ImageIcon("src/Diamonds.png");
Map map = new Map();
int x = 0;
int y = 0;
int score=0;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
int temp = 0;
boolean isLineFull = true;
int isLineFullIndex = 0;
for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j < 14; j++) {
temp += map.map[i][j];
}
if (temp == 14) {
isLineFull = false;
isLineFullIndex = i;
break;
}
temp = 0;
}
if (!isLineFull) {
score+=10;
for (int i = isLineFullIndex; i > 0; i--) {
for (int j = 0; j < 14; j++) {
map.map[i][j] = map.map[i - 1][j];
}
}
for (int i = 0; i < 14; i++) {
map.map[0][i] = 0;
}
}
for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j < 14; j++) {
if (map.map[i][j] == 1)
g.drawImage(Diamonds.getImage(), j * 25, i * 25, null);
}
}
}
public void drawMyImage(int x1, int y1, int index) {
x = (x1 / 25);
y = (y1 / 25);
if (index == 0) {
map.map[y][x] = 1;
map.map[y + 1][x] = 1;
map.map[y][x + 1] = 1;
map.map[y + 1][x + 1] = 1;
}
if (index == 1) {// ****
map.map[y][x] = 1;
map.map[y][x + 1] = 1;
map.map[y][x + 2] = 1;
map.map[y][x + 3] = 1;
}
if (index == 2) {
// -*
// **
// -*
map.map[y][x + 1] = 1;
map.map[y + 1][x] = 1;
map.map[y + 1][x + 1] = 1;
map.map[y + 2][x + 1] = 1;
}
if (index == 3) {
// *
// **
// -*
map.map[y][x] = 1;
map.map[y + 1][x] = 1;
map.map[y + 1][x + 1] = 1;
map.map[y + 2][x + 1] = 1;
}
repaint();
}
}