我也想要给我一个!!!
[email protected]
[email protected]
解决方案 »
- 各位大哥大姐,帮忙看下这个,为什么不能运行?
- 如何单击按钮滚动图片
- 大哥大姐都来看看啊,java读取oracle中的clob
- 一个jdbc的简单问题在线等答案,答对立刻送分
- java常用包包含在哪些jar文件中
- 学习完J2SE后,该向那些方面发展。
- 如果两个用户使用相同的IP怎么办?
- java.net.socketpermission的问题
- 改错,帮忙呀!
- SQLException: Invalid handle 是怎么回事?
- 我想写两个线程,一个线程实现向一个链表内写入数据,另一个线程实现从这个链表内依次按顺序读取数据并删除,请问我该如何实现??能否给
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.*;
class MyView extends Canvas
//implements KeyListener
{
public boolean Start;
private int Speed; //下落速度
private int shape; //移动图形
private int change; //移动图形变形信息
private int posx; //当前移动图形在画布中的横坐标
private int posy; //当前移动图形在画布中的纵坐标
private int width; //画布的宽
private int height; //画布的高
int i,j;
ThreadSpeedControl threadspeed=new ThreadSpeedControl();
boolean back[][] = new boolean[24][30]; //已经定型的图形层
int unit[][] = new int[4][2]; //移动图形的坐标指示层
/*************************** 初始化 ****************************/
public MyView(int w, int h)
{
Speed = 200;
width = w;
height = h;
Start = true;
reset(); for(i=0;i<24;i++)
for(j=0;j<30;j++)back[i][j]=false;
reshape(0,0,w,h);
}
/********** 移动图形的复位与选择 ********************/
/********** 当移动图形触底固定后,需要生成新的移动图形,在这里进行处理 ****************/
public void reset()
{
posx=12; //posx,posy 指示移动图形是在第一行的中间位置开始下落
posy=0;
change = 0; //
shape = (int)(java.lang.Math.random()*100)%6; //一共有六种移动图形,每次随机生成其中一种
shape = shape * 10;
//System.out.println(shape);
for(i=0;i<4;i++) //移动开始前,将移动坐标指示清零
{
unit[i][0]=0;
unit[i][1]=0;
}
}
public void paint(Graphics g)
{
//JOptionPane.showMessageDialog(null, "tag");
g.setColor(Color.blue);
g.fillRect(10, 10, width, height);
threadspeed.setPriority(6);
threadspeed.start(); //启动时间齿轮
}
/*************************** 初始化 ****************************//*************************** 键盘响应 ****************************/
public boolean keyDown(Event evt, int key)
{
if(key==1006)
left(); //移动图形左移
else if(key == 1007)
right(); //移动图形右移
else if(key == 1005)
down(); //移动图形下移
else if(key == 1004)
changeshape(); //移动图形变形
return false;
}
/********* 画出已经落地固定的图形 ************************/
/******** 已固定的图形无须经常刷新,避免闪屏,在移动图形触底时更新一下即可 ******************/
public void drawback()
{
Graphics g;
g = this.getGraphics();
g.setColor(Color.blue);//背景颜色设为蓝色
g.fillRect(10,10,240,300);
for(i=0;i<24;i++)
{
for(j=0;j<30;j++)
{
if(back[i][j] == true)
{
g.setColor(Color.red);
g.fillRect((i+1)*10,(j+1)*10, 10, 10);
g.setColor(Color.black);
g.drawRect((i+1)*10,(j+1)*10,10,10);
}
}
}
}
/******** 画出移动图形 *****************/
public void drawmove()
{
Graphics g;
g = this.getGraphics();
for(i=0;i<4;i++)
{
g.setColor(Color.red);
g.fillRect((unit[i][0]+1)*10,(unit[i][1]+1)*10,10,10);
g.setColor(Color.black);
g.drawRect((unit[i][0]+1)*10,(unit[i][1]+1)*10,9,9);
} }
/******** 擦除旧的移动图形 ,否则,会留下移动图形的移动轨迹 *****************/
public void erasemove()
{
Graphics g;
g = this.getGraphics();
g.setColor(Color.blue);
for(i=0;i<4;i++)
g.fillRect((unit[i][0]+1)*10,(unit[i][1]+1)*10,10,10);
}
/******** 移动图形左移 *****************/
public void left()
{
drawunit();
for(i=0;i<4;i++)
{
if(back[unit[i][0]-1][unit[i][1]]==true || unit[i][0]-1<0)
return;
}
erasemove();
posx--;
drawunit(); drawmove();
} /******** 移动图形右移 *****************/
public void right()
{
drawunit();
for(i=0;i<4;i++)
{
if(back[unit[i][0]+1][unit[i][1]]==true || unit[i][0]+1>22)
return;
}
erasemove();
posx++;
drawunit(); drawmove();
}
/******** 移动图形下移 *****************/
public void down()
{
drawunit();
for(i=0;i<4;i++)
{
if(back[unit[i][0]][unit[i][1]+1]==true || unit[i][1]+1>28)
{
for(j=0;j<4;j++)back[unit[j][0]][unit[j][1]]=true;
drawback();
judgestate();
reset();
continue;
}
}
erasemove();
posy++; drawunit();
drawmove();
}
/******** 判断是否满格,满格则更新 *****************/
public void judgestate()
{
boolean isover = false;
for(i=0;i<24;i++)
{
if(back[i][1]==true)
{
isover=true;
break;
}
}
if(isover == false)
{
for(j=0;j<30;j++)
{
for(i=0;i<24;i++)
if(back[i][j]==false)break;
if(i==23)
{
for(int l=j;l>0;l--)
for(int k=0;k<24;k++)
back[k][l] = back[k][l-1];
drawback();
}
}
}
else
{
Start = false;
threadspeed.stop();
System.out.println("game over");
}
}
/******** 移动图形变形,由change指示 *****************/
public void changeshape()
{
int oldshape = shape;
int oldchange = change;
int tmpchange;
change++;
change=change%4;
drawunit();
for(i=0;i<4;i++)
{
if(back[unit[i][0]][unit[i][1]]==true || unit[i][1]>28 || unit[i][1]<0 || unit[i][0]<0 || unit[i][0]>22)
{
shape = oldshape;
change= oldchange;
return;
}
} tmpchange = change;
shape = oldshape;
change = oldchange;
drawunit();
erasemove(); change = tmpchange;
drawunit();
drawmove();
}
public void drawunit()
{
switch(shape+change)
{
case 0:
System.out.println("case 0");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx; unit[1][1]=posy+1;
unit[2][0]=posx+1;unit[2][1]=posy;
unit[3][0]=posx+1;unit[3][1]=posy+1;
break;
case 1:
System.out.println("case 1");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx; unit[1][1]=posy+1;
unit[2][0]=posx+1;unit[2][1]=posy;
unit[3][0]=posx+1;unit[3][1]=posy+1;
break;
case 2:
System.out.println("case 2");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx; unit[1][1]=posy+1;
unit[2][0]=posx+1;unit[2][1]=posy;
unit[3][0]=posx+1;unit[3][1]=posy+1;
break;
case 3:
System.out.println("case 3");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx; unit[1][1]=posy+1;
unit[2][0]=posx+1;unit[2][1]=posy;
unit[3][0]=posx+1;unit[3][1]=posy+1;
break;
case 10:
System.out.println("case 10");
for(i=0;i<4;i++)
{
unit[i][0]=posx;
unit[i][1]=posy+i;
}
break;
case 11:
System.out.println("case 11");
for(i=0;i<4;i++)
{
unit[i][0]=posx+i;
unit[i][1]=posy;
}
break;
case 12:
System.out.println("case 12");
for(i=0;i<4;i++)
{
unit[i][0]=posx;
unit[i][1]=posy+i;
}
break;
case 13:
System.out.println("case 13");
for(i=0;i<4;i++)
{
unit[i][0]=posx+i;
unit[i][1]=posy;
}
break;
case 20:
System.out.println("case 20");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx+1;unit[1][1]=posy;
unit[2][0]=posx+1;unit[2][1]=posy+1;
unit[3][0]=posx+1;unit[3][1]=posy+2;
break;
case 21:
System.out.println("case 21");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy+1;
unit[2][0]=posx-1;unit[2][1]=posy+1;
unit[3][0]=posx-2;unit[3][1]=posy+1;
break;
case 22:
System.out.println("case 22");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx-1;unit[1][1]=posy;
unit[2][0]=posx-1;unit[2][1]=posy-1;
unit[3][0]=posx-1;unit[3][1]=posy-2;
break;
case 23:
System.out.println("case 23");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy-1;
unit[2][0]=posx+1;unit[2][1]=posy-1;
unit[3][0]=posx+2;unit[3][1]=posy-1;
break;
case 30:
System.out.println("case 30");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx-1;unit[1][1]=posy;
unit[2][0]=posx-1;unit[2][1]=posy+1;
unit[3][0]=posx-1;unit[3][1]=posy+2;
break;
case 31:
System.out.println("case 31");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx; unit[1][1]=posy-1;
unit[2][0]=posx-1;unit[2][1]=posy-1;
unit[3][0]=posx-2;unit[3][1]=posy-1;
break;
case 32:
System.out.println("case 32");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx+1;unit[1][1]=posy;
unit[2][0]=posx+1;unit[2][1]=posy-1;
unit[3][0]=posx+1;unit[3][1]=posy-2;
break;
case 33:
System.out.println("case 33");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy+1;
unit[2][0]=posx+1;unit[2][1]=posy+1;
unit[3][0]=posx+2;unit[3][1]=posy+1;
break;
case 40:
System.out.println("case 40");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy+1;
unit[2][0]=posx+1;unit[2][1]=posy+1;
unit[3][0]=posx+1;unit[3][1]=posy+2;
break;
case 41:
System.out.println("case 41");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx-1;unit[1][1]=posy;
unit[2][0]=posx-1;unit[2][1]=posy+1;
unit[3][0]=posx-2;unit[3][1]=posy+1;
break;
case 42:
System.out.println("case 42");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy-1;
unit[2][0]=posx-1;unit[2][1]=posy-1;
unit[3][0]=posx-1;unit[3][1]=posy-2;
break;
case 43:
System.out.println("case 43");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx+1;unit[1][1]=posy;
unit[2][0]=posx+1;unit[2][1]=posy-1;
unit[3][0]=posx+2;unit[3][1]=posy-1;
break;
case 50:
System.out.println("case 50");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy+1;
unit[2][0]=posx-1;unit[2][1]=posy+1;
unit[3][0]=posx-1;unit[3][1]=posy+2;
break;
case 51:
System.out.println("case 51");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx-1;unit[1][1]=posy;
unit[2][0]=posx-1;unit[2][1]=posy-1;
unit[3][0]=posx-2;unit[3][1]=posy-1;
break;
case 52:
System.out.println("case 52");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx ;unit[1][1]=posy-1;
unit[2][0]=posx+1;unit[2][1]=posy-1;
unit[3][0]=posx+1;unit[3][1]=posy-2;
break;
case 53:
System.out.println("case 53");
unit[0][0]=posx; unit[0][1]=posy;
unit[1][0]=posx+1;unit[1][1]=posy;
unit[2][0]=posx+1;unit[2][1]=posy+1;
unit[3][0]=posx+2;unit[3][1]=posy+1;
break;
}
}
/*************************** 键盘响应 ****************************//*************************** 速度(时间齿轮) ****************************/
class ThreadSpeedControl extends Thread
{
ThreadSpeedControl(){}
public void run()
{
while(true)
{
if(Start == true)
{
drawunit();
for(i=0;i<4;i++)
{
if(back[unit[i][0]][unit[i][1]+1]==true || unit[i][1]+1>28)
{
for(j=0;j<4;j++)back[unit[j][0]][unit[j][1]]=true;
drawback();
judgestate();
reset();
continue;
}
}
erasemove();
posy++; drawunit();
drawmove();
try
{
sleep(200);
}
catch (InterruptedException e)
{
return;
}
}
}
}
}
/*************************** 速度(时间齿轮) ****************************/}
* Russ.java
* made by Gesheng
* 2004.6.10 --
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;public class Russ extends JPanel
implements ActionListener { boolean ok;
int high;
JLabel score;
int speed;
MyView mv;
private JButton buttonStart;
private class buttonStartHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
mv=new MyView(240,300);
add("Center", mv);
}
} public Russ()
{
super(new BorderLayout());
score = new JLabel("---------Score----------",null,JLabel.CENTER);
buttonStart = new JButton("Start");
buttonStart.addActionListener(new buttonStartHandler());
// mv=new MyView(240,300);
// add("Center", mv);
add(score,BorderLayout.PAGE_START);
add(buttonStart, BorderLayout.AFTER_LAST_LINE);
} /** Handle the key released event from the text field. */
public void actionPerformed(ActionEvent e) {
//Clear the text components.
}
public void prepare()
{
high = 0;
speed = 100;
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window.
JFrame frame = new JFrame("RussDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane.
JComponent newContentPane = new Russ();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane); //Display the window.
frame.pack();
frame.setSize(270,370);
frame.setVisible(true);
} public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//prepare();
createAndShowGUI();
}
});
}
}
http://community.csdn.net/Expert/topic/2692/2692594.xml?temp=.1334955
import java.awt.event.*;
import javax.swing.*;public class JTetrix extends JFrame implements Runnable {
private ImageIcon iconLogo; private JPanel nextPanel, scorePanel, opPanel, gamePanel, hidedOpPanel; private JLabel labLevel, labLine, labScore; private final JTextArea keyfocus; // 键盘的操作焦点,隐藏于游戏画面之后 private JButton btnNew, btnPause, btnQuit; private showNextPiece nextPieceArea; // 预览下一方块 private final gameOpBoard gameOpArea; // 游戏画面 private Container c; private int Level, Score, LineRemoved; // 等级、得分、移除总行数 private double interval; // 暂停时间,用于控制速度 private boolean pause; // 是否暂停 private Thread loopThread; // 游戏回圈执行绪
static final long serialVersionUID = 1; public JTetrix() {
c = getContentPane(); //iconLogo = new ImageIcon(JTetrix.class.getResource("logo.jpg"));
Level = 1;
Score = 0;
LineRemoved = 0; // 预览下一个方块的面版配置
nextPanel = new JPanel();
nextPanel.setLayout(null);
nextPanel.setBorder(BorderFactory.createTitledBorder("下一个"));
nextPanel.setLocation(10, 10);
nextPanel.setSize(150, 120); nextPieceArea = new showNextPiece(5, 6, 15, 15);
nextPieceArea.generateNextPiece(); // 先产生第一片待取
nextPieceArea.setLocation(40, 20);
nextPanel.add(nextPieceArea); // 等级、得分面版配置
scorePanel = new JPanel();
scorePanel.setBorder(BorderFactory.createTitledBorder("等级 / 得分"));
scorePanel.setLocation(10, 140);
scorePanel.setSize(150, 160);
scorePanel.add(new JLabel(" 速度等级 "));
scorePanel.add(labLevel = new JLabel("1", SwingConstants.CENTER));
scorePanel.add(new JLabel(" 移除行数 "));
scorePanel.add(labLine = new JLabel("0", SwingConstants.CENTER));
scorePanel.add(new JLabel(" 目前得分 "));
scorePanel.add(labScore = new JLabel("0", SwingConstants.CENTER)); // 功能面版配置
opPanel = new JPanel();
opPanel.setBorder(BorderFactory.createTitledBorder(""));
opPanel.setLocation(10, 320);
opPanel.setSize(150, 130);
opPanel.add(btnNew = new JButton("开新游戏"));
opPanel.add(btnPause = new JButton("暂停游戏"));
opPanel.add(btnQuit = new JButton("关闭游戏")); // 游戏操作画面面版配置
gameOpArea = new gameOpBoard(10, 22, 18, 18);
gameOpArea.setLocation(20, 20);
gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix()); // 先取出一个方块
nextPieceArea.generateNextPiece(); // 预览窗格产生下一个方块预览 gamePanel = new JPanel();
gamePanel.setLayout(null);
gamePanel.setLocation(180, 20);
gamePanel.setSize(12 * 18, 24 * 18); gamePanel.setBorder(BorderFactory.createTitledBorder(""));
gamePanel.add(gameOpArea); // 隐藏的操作面版配置
hidedOpPanel = new JPanel();
hidedOpPanel.setLayout(new BorderLayout());
hidedOpPanel.setLocation(200, 40);
hidedOpPanel.setSize(10 * 18, 22 * 18);
keyfocus = new JTextArea(); keyfocus.setEditable(false);
hidedOpPanel.add(keyfocus); // 加入面版至视窗
c.setLayout(null);
c.add(nextPanel);
c.add(scorePanel);
c.add(opPanel);
c.add(gamePanel);
c.add(hidedOpPanel); // 键盘事件处理
keyfocus.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT)
gameOpArea.moveRight();
else if (key == KeyEvent.VK_LEFT)
gameOpArea.moveLeft();
else if (key == KeyEvent.VK_UP)
gameOpArea.RotateRL(1);
else if (key == KeyEvent.VK_DOWN)
gameOpArea.RotateRL(0);
else if (key == KeyEvent.VK_SPACE) {
gameOpArea.soonMoveDown();
}
}
}); // 开新游戏
btnNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newGame();
keyfocus.requestFocus();
}
}); // 暂停游戏
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pause = true; JOptionPane.showOptionDialog(null, "程式名称:\n JTetrix v0.1\n"
+ "程式设计:\n younganne\n"
+ "简介:\n 一个用Java写的俄罗斯方块游戏\n", "关于JTetrix",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null); pause = false;
interval = 2000 / Level;
loopThread.interrupt();
keyfocus.requestFocus();
}
}); // 关闭游戏
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}); // 启动游戏回圈执行绪
loopThread = new Thread(this);
loopThread.start(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTetrix v0.1");
setSize(420, 500); setVisible(true);
} // 游戏回圈的执行绪执行对象
public void run() {
while (true) { // 游戏回圈
try {
// 是否暂停
if (pause)
interval = 999999999;
else
interval = 2000 / Level; if (gameOpArea.isUpdated()) {// 由游戏画面的阵列是否更新来判断是否取出下一个
gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());
nextPieceArea.generateNextPiece(); // 更新等级、得分等资讯
Score = gameOpArea.getScore();
labScore.setText("" + Score);
LineRemoved = gameOpArea.getLineRemoved();
labLine.setText("" + LineRemoved);
Level = (int) ((Score + 100) / 100); // 每一百分升级一次
labLevel.setText("" + Level);
} Thread.sleep((int) interval);
gameOpArea.MoveDown(); // 不断下移
} catch (InterruptedException e) {
}
}
} // 开新游戏
public void newGame() {
Level = 1;
Score = 0;
LineRemoved = 0;
labScore.setText("" + Score);
labLine.setText("" + LineRemoved);
labLevel.setText("" + Level); gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix()); // 先取出一个方块
gameOpArea.newState();
nextPieceArea.generateNextPiece(); // 预览窗格产生下一个方块预览
} public static void main(String[] args) {
JTetrix frm = new JTetrix();
}
}// 方块的资料结构与操作方法class TetrixPiece {
private int pieceType; // 方块样式 private int[][] coordinates = new int[4][2]; // 四个方块,记录X与Y public TetrixPiece() {
initialize((int) (Math.random() * 7 + 1));
} public TetrixPiece(int type) {
initialize(type);
} // 向左转动
public void rotateLeft() {
if (pieceType == 5) // 不转动正方形方块
return; int tmp;
for (int i = 0; i < 4; i++) {
tmp = getXCoord(i);
setXCoord(i, getYCoord(i));
setYCoord(i, -tmp);
}
} // 向右转动
public void rotateRight() {
if (pieceType == 5) // 不转动正方形方块
return; int tmp;
for (int i = 0; i < 4; i++) {
tmp = getXCoord(i);
setXCoord(i, -getYCoord(i));
setYCoord(i, tmp);
}
}
public int getType() {
return pieceType;
} // 取得X座标
public int getXCoord(int index) {
return coordinates[index][0];
} // 取得Y座标
public int getYCoord(int index) {
return coordinates[index][1];
} // 取得方块最左边一格的座标
public int getMinX() {
int tmp = coordinates[0][0]; for (int i = 1; i < 4; i++)
if (tmp > coordinates[i][0])
tmp = coordinates[i][0]; return tmp;
} // 取得方块最右边一格的座标
public int getMaxX() {
int tmp = coordinates[0][0]; for (int i = 1; i < 4; i++)
if (tmp < coordinates[i][0])
tmp = coordinates[i][0]; return tmp;
} // 取得方块最上面一格的座标
public int getMinY() {
int tmp = coordinates[0][1]; for (int i = 1; i < 4; i++)
if (tmp > coordinates[i][1])
tmp = coordinates[i][1]; return tmp;
} // 取得方块最下面一格的座标
public int getMaxY() {
int tmp = coordinates[0][1]; for (int i = 1; i < 4; i++)
if (tmp < coordinates[i][1])
tmp = coordinates[i][1]; return tmp;
} // 设定X座标
public void setXCoord(int index, int value) {
coordinates[index][0] = value;
} // 设定Y座标
public void setYCoord(int index, int value) {
coordinates[index][1] = value;
} // 设定XY座标
public void setCoord(int index, int x, int y) {
coordinates[index][0] = x;
coordinates[index][1] = y;
} // 初始方块类型
private void initialize(int type) {
// 七种方块的座标
final int pieceTypes[][][] = {
{ { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } }, { { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } }, { { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } }, { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } }, { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, { { -1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } }, { { 1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } } }; if (type < 1 || type > 7)
type = 1; // 依指定的类型设定方块座标
pieceType = type;
for (int i = 0; i < 4; i++) {
coordinates[i][0] = pieceTypes[type - 1][i][0];
coordinates[i][1] = pieceTypes[type - 1][i][1];
}
}
}// 显示下一个方块的类别class showNextPiece extends JComponent {
private int boardWidth, boardHeight; // 显示的格数 private int gridWidth, gridHeight; // 每格的大小 private TetrixPiece nextPiece; // 下一个方块 private Color pieceColor; // 方块色彩
static final long serialVersionUID = 8; public showNextPiece(int bw, int bh, int gw, int gh) {
boardWidth = bw;
boardHeight = bh;
gridWidth = gw;
gridHeight = gh;
setBackground(Color.black);
setSize(boardWidth * gridWidth, boardHeight * gridHeight);
} // 产生下一个方块
public void generateNextPiece() {
nextPiece = new TetrixPiece();
repaint();
} // 传回下一个方块
public TetrixPiece getNextTetrix() {
return nextPiece;
} // 画格子
public void drawGrid(Graphics g, int x, int y) {
g.setColor(pieceColor);
g.fillRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
g.setColor(Color.lightGray);
g.drawRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
} // 依类型决定色彩并画出方块
public void paint(Graphics g) {
switch (nextPiece.getType()) {
case 1:
pieceColor = Color.red;
break;
case 2:
pieceColor = Color.orange;
break;
case 3:
pieceColor = Color.yellow;
break;
case 4:
pieceColor = Color.green;
break;
case 5:
pieceColor = Color.blue;
break;
case 6:
pieceColor = Color.magenta;
break;
case 7:
pieceColor = Color.gray;
break;
} g.setColor(Color.black);
g.fillRect(0, 0, boardWidth * gridWidth, boardHeight * gridHeight);
for (int i = 0; i < 4; i++) {
drawGrid(g, nextPiece.getXCoord(i) + 2, nextPiece.getYCoord(i) + 2);
}
}
}// 游戏区主类别class gameOpBoard extends JComponent {
private int boardWidth, boardHeight; // 区块格子数目 private int gridWidth, gridHeight; // 格子大小 private TetrixPiece currentPiece; // 现在操作的方块 private int gameBoard[][]; // 游戏的面版阵列,用于记录堆积的方块 private int nClearLines; // 没有方格的行数 private int nLinesRemoved; // 被移除行数 private int score; // 得分 private int xOffset, yOffset; // 依原始TetrixPiece进行位移 private boolean gameover; // Game Over ?
static final long serialVersionUID = 5; public gameOpBoard(int bw, int bh, int gw, int gh) {
boardWidth = bw;
boardHeight = bh;
gridWidth = gw;
gridHeight = gh;
gameBoard = new int[boardWidth][boardHeight]; nClearLines = boardHeight;
score = 0; setBackground(Color.black);
setSize(boardWidth * gridWidth, boardHeight * gridHeight);
} // 设定目前的操作方块
public void setCurrentPiece(TetrixPiece piece) {
xOffset = 5;
yOffset = 1;
currentPiece = piece; // 判断是否结束游戏
int x, y;
for (int i = 0; i < 4; i++) {
x = currentPiece.getXCoord(i);
y = currentPiece.getYCoord(i); if (gameBoard[x + xOffset][y + yOffset] != 0) {
gameover = true;
}
} repaint();
} // 重新开始游戏的新状态
public void newState() {
nClearLines = boardHeight;
score = 0;
gameover = false;
for (int i = 0; i < boardWidth; i++)
for (int j = 0; j < boardHeight; j++)
gameBoard[i][j] = 0;
}
// xStep: 1 表向右 xStep: -1 表向左 xStep: 0 不移动X方向
// yStep: 1 表向下 yStep: 0 不移动Y方向
public boolean canMoveTo(int xStep, int yStep) {
int x, y;
for (int i = 0; i < 4; i++) {
x = currentPiece.getXCoord(i) + xOffset + xStep;
y = currentPiece.getYCoord(i) + yOffset + yStep;
// 是否超出边界
if (x < 0 || x >= boardWidth || y >= boardHeight)
return false;
// 是否已经有方块
if (gameBoard[x][y] != 0)
return false;
} return true;
} // 向左移
public void moveLeft() {
if (canMoveTo(-1, 0))
xOffset--; repaint();
} // 向右移
public void moveRight() {
if (canMoveTo(1, 0))
xOffset++; repaint();
} // 向下移
public void MoveDown() {
if (canMoveTo(0, 1))
yOffset++; repaint();
} // 瞬间下移
public void soonMoveDown() {
while (canMoveTo(0, 1))
yOffset++; score += 3;
repaint();
} // 转动方块
// r: 0 表向左转 r: 1 表示向右转
public void RotateRL(int r) {
TetrixPiece tmp = new TetrixPiece(currentPiece.getType()); for (int i = 0; i < 4; i++) {
tmp.setCoord(i, currentPiece.getXCoord(i), currentPiece
.getYCoord(i));
} if (r == 0)
tmp.rotateLeft();
else
tmp.rotateRight(); // 测试是否可转动
int x, y;
for (int i = 0; i < 4; i++) {
x = tmp.getXCoord(i) + xOffset;
y = tmp.getYCoord(i) + yOffset;
// 超出边界?
if (x < 0 || x >= boardWidth || y >= boardHeight || y < 0)
return;
// 已经有方块?
if (gameBoard[x][y] != 0)
return; } currentPiece = tmp;
repaint();
} // 是否更新游戏画面阵列
public boolean isUpdated() {
if (gameover)
return false; if (canMoveTo(0, 1))
return false; int x, y;
for (int i = 0; i < 4; i++) {
x = currentPiece.getXCoord(i) + xOffset;
y = currentPiece.getYCoord(i) + yOffset;
gameBoard[x][y] = currentPiece.getType();
if (y < nClearLines) {
nClearLines = y;
}
} // 移除完整的行
while (removeFullLines() != 0)
; repaint();
return true;
} // 移除完整的行
public int removeFullLines() {
int i, j, k;
int nFullLines = 0; // 完整行的数目 for (j = boardHeight - 1; j >= nClearLines; j--) {
for (i = 0; i < boardWidth; i++) {
if (gameBoard[i][j] == 0) // 不是完整的一行
break;
} if (i == boardWidth) { // 找到完整的一行
nFullLines = 1; for (k = j - 1; k >= nClearLines; k--) { // 继续找完整行
for (i = 0; i < boardWidth; i++)
if (gameBoard[i][k] == 0) // 不是完整行
break; if (i == boardWidth) { // 找到完整行
nFullLines++;
} else {
for (i = 0; i < boardWidth; i++) { // 下移完整行上面的一行
if (gameBoard[i][k + nFullLines] != gameBoard[i][k]) {
gameBoard[i][k + nFullLines] = gameBoard[i][k];
}
}
}
}
// 更新相关数据
nClearLines = nClearLines + nFullLines;
nLinesRemoved = nLinesRemoved + nFullLines;
score = score + 10 * nFullLines;
}
} // 清除游戏区域阵列移动后的行资料
for (j = 0; j < nClearLines; j++)
for (i = 0; i < boardWidth; i++)
gameBoard[i][j] = 0; return nFullLines;
} // 目前得分
public int getScore() {
return score;
} // 移除总行数
public int getLineRemoved() {
return nLinesRemoved;
} // 绘图 // 取得格子颜色
public Color getGridColor(int type) {
switch (type) {
case 1:
return Color.red;
case 2:
return Color.orange;
case 3:
return Color.yellow;
case 4:
return Color.green;
case 5:
return Color.blue;
case 6:
return Color.magenta;
case 7:
return Color.gray;
} return Color.black;
} // 画格子
// isPiece: true 画格子 isPiece: false 画堆积方块区
public void drawGrid(Graphics g, int x, int y, boolean isPiece) { if (isPiece) // 这是方块
g.setColor(getGridColor(currentPiece.getType()));
else
// 堆积的区域
g.setColor(getGridColor(gameBoard[x][y])); g.fillRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
g.setColor(Color.lightGray);
g.drawRect(x * gridWidth, y * gridHeight, gridWidth, gridHeight);
} public void paint(Graphics g) {
// 清除前一个画面
g.setColor(Color.black);
g.fillRect(0, 0, boardWidth * gridWidth, boardHeight * gridHeight); // 画方块
for (int i = 0; i < 4; i++) {
drawGrid(g, currentPiece.getXCoord(i) + xOffset, currentPiece
.getYCoord(i)
+ yOffset, true);
} // 画堆积方块区
for (int i = 0; i < boardWidth; i++) {
for (int j = 0; j < boardHeight; j++) {
if (gameBoard[i][j] == 0)
continue; drawGrid(g, i, j, false);
}
} // 是否画出Game Over字样
if (gameover) {
g.setFont(new Font("细明体", Font.ITALIC | Font.BOLD, 26));
g.setColor(Color.white);
g.drawString("Game Over", 30, 50);
g.setColor(Color.red);
g.drawString("Game Over", 32, 52); }
}}
[email protected]
[email protected]
[email protected]
[email protected]
能给我一份吗
[email protected]
thank you !!!!!!!!!!
[email protected]
谢谢了!
[email protected]
谢谢了!
这么看太难了吧!!
[email protected]
[email protected]
[email protected]