Java源码共享―俄罗斯方块 /* JTetris.java
Janada家拿大 · ) · 2003-07-17
Java源码共享―俄罗斯方块
/* JTetris.java 1.0.0
* Copyright (C) 1998 Ryan O'Connell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Notes:
* 1) I use bit windows to code in. It'll look odd in 80 columns.
* 2) In Netscape, (Not in appletviewer) getGraphics is only useful for retrieving
* graphics information such as string metrics, not for actual real-time drawing
* work. To get a valid Grapics handle, you need to call repaint().
* Hence kludgeflag...
*
* To Do:
* 1) Need 7 gif files named: left.gif, right.gif, down.gif, JTeris.gif
buttons.gif, clockwise.gif, anticlockwise.gif,
* 2)Stop the game from running before graphics have been loaded in.
* 3) Fix game-paused redraw bug
* 4) To run in browser, add this to webpage:
*/
import java.awt.*;
import java.awt.Image;
import java.awt.image.*;
import java.net.*;
public class JTetris extends java.applet.Applet
{ // Creates the Board and buttons then just sits there responding to events...
Thread DropperThread = null;
Board board;
TextField score;
DisplayPiece piece;
public void init()
{ // Creates pretty buttons.
Icon icon;
Picture picture;
Button button1, button2;
Image image;
ImageFilter imagefilter;
setLayout(null);
this.setBackground(new Color(0, 0, 0));
image = getImage(getCodeBase(), "left.gif"<img src="images/smilies/wink.gif" alt="眨眼" border="0">;
add(icon = new Icon(image, 1));
icon.reshape(172, 170, 36, 36);
image = getImage(getCodeBase(), "anticlockwise.gif"<img src="images/smilies/wink.gif" alt="眨眼" border="0">;
add(icon = new Icon(image, 2));
icon.reshape(216, 170, 36, 36);
image = getImage(getCodeBase(), "down.gif"<img src="images/smilies/wink.gif" alt="眨眼" border="0">;
add(icon = new Icon(image, 3));
icon.reshape(260, 170, 36, 36);
image = getImage(getCodeBase(), "clockwise.gif"<img src="images/smilies/wink.gif" alt="眨眼" border="0">;
add(icon = new Icon(image, 4));
icon.reshape(304, 170, 36, 36);
image = getImage(getCodeBase(), "right.gif"<img src="images/smilies/wink.gif" alt="眨眼" border="0">;
add(icon = new Icon(image, 5));
icon.reshape(348, 170, 36, 36);
image = getImage(getCodeBase(), "JTetris.gif"<img src="images/smilies/wink.gif" alt="眨眼" border="0">;
add(picture = new Picture(image));
picture.reshape(192, 210, 180, 139);
add(button1 = new Button("New Game"<img src="images/smilies/wink.gif" alt="眨眼" border="0">
;
button1.setBackground(new Color(128, 128, 128));
button1.move(180, 8);
button1.setSize(new Dimension(96, 32));
add(button2 = new Button("Pause"<img src="images/smilies/wink.gif" alt="眨眼" border="0">
;
button2.setBackground(new Color(128, 128, 128));
button2.move(284, 8);
button2.setSize(new Dimension(96, 32));
add(score = new TextField("Score: 0"<img src="images/smilies/wink.gif" alt="眨眼" border="0">
;
score.setBackground(new Color(128, 128, 128));
score.setEditable(false);
score.move(232, 48);
score.resize(new Dimension(96, 32));
add(board = new Board());
board.reshape(24, 8, 120, 324);
board.start();
add(piece = new DisplayPiece(board));
piece.reshape(246, 88, 68, 68);
}
public void paint(Graphics g)
{ // Redraw border.
int x, y;
g.setColor(new Color(0, 128, 255));
for (y = 8;
y <= 332;
y += 12)
g.fill3DRect(12, y, 12, 12, true);
for (y = 8;
y <= 332;
y += 12)
g.fill3DRect(144, y, 12, 12, true);
for (x = 24;
x <= 132;
x += 12)
g.fill3DRect(x, 332, 12, 12, true);
}
public boolean action(Event event, Object object)
{ // Responds to Button/Icon clicks
Icon icon;
if (event.target instanceof Icon)
{
// Has an icon has finished loading?
icon = (Icon) event.target;
if (icon.getNumber() == 1)
board.moveLeft();
if (icon.getNumber() == 2)
board.rotateAntiClockwise();
if (icon.getNumber() == 3)
board.drop();
if (icon.getNumber() == 4)
board.rotateClockwise();
if (icon.getNumber() == 5)
board.moveRight();
return true;
}
if (event.target instanceof Button)
{
if (((String)object).equals("New Game"<img src="images/smilies/wink.gif" alt="眨眼" border="0">
board.newGame();
if (((String)object).equals("Pause"<img src="images/smilies/wink.gif" alt="眨眼" border="0">
board.pause();
return true;
}
if (event.target instanceof Board)
{
if (event.id == -1)
{
piece.repaint();
return true;
}
score.setText("Score: " + event.id);
return true;
}
return false;
}
}
class Board extends Panel implements Runnable
{ // All the real work is done in here.
int BoardArray[][];
Color Colours[];
boolean boardinitialized = false, StillRunning = false;
boolean Paused = false, kludgeflag = false, forceredraw = false, GameOver = false;
int CurrentBlockX = 0, CurrentBlockY = 0, CurrentBlockColour = 0;
int CurrentBlockShape, CurrentBlockAngle;
int NextBlockX = 0, NextBlockY = 0;
int NextBlockColour = 0, NextBlockShape, NextBlockAngle;
int LastX, LastY, LastAngle, score = 0, delay = 500;
// { X, Y } information relative to CurrentBlock[XY].
// BlockInfo[Shape][Angle, +ve clockwise][BlockNumber][X/Y];
int BlockInfo[][][][] = { { { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, // Square
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } } },
{ { { 0, 0 }, {-1, 0 }, { 1, 0 }, { 0,-1 } }, // T
{ { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0,-1 } },
{ { 0, 0 }, { 0, 1 }, { 1, 0 }, {-1, 0 } },
{ { 0, 0 }, { 0, 1 }, { 0,-1 }, {-1, 0 } } },
{ { { 0, 0 }, {-1, 0 }, { 1, 0 }, { 2, 0 } }, //
{ { 0, 0 }, { 0,-1 }, { 0, 1 }, { 0, 2 } },
{ { 0, 0 }, {-1, 0 }, { 1, 0 }, { 2, 0 } },
{ { 0, 0 }, { 0,-1 }, { 0, 1 }, { 0, 2 } } },
{ { { 0, 0 }, { 0,-1 }, { 0, 1 }, { 1, 1 } }, // L
{ { 0, 0 }, { 1, 0 }, {-1, 0 }, {-1, 1 } },
{ { 0, 0 }, { 0, 1 }, { 0,-1 }, {-1,-1 } },
{ { 0, 0 }, {-1, 0 }, { 1, 0 }, { 1,-1 } } },
{ { { 0, 0 }, { 0,-1 }, { 0, 1 }, {-1, 1 } }, // Inverted L
{ { 0, 0 }, { 1, 0 }, {-1, 0 }, {-1,-1 } },
{ { 0, 0 }, { 0, 1 }, { 0,-1 }, { 1,-1 } },
{ { 0, 0 }, {-1, 0 }, { 1, 0 }, { 1, 1 } } },
{ { { 0, 0 }, {-1, 0 }, { 0,-1 }, { 1,-1 } }, // S
{ { 0, 0 }, { 0,-1 }, { 1, 0 }, { 1, 1 } },
{ { 0, 0 }, {-1, 0 }, { 0,-1 }, { 1,-1 } },
{ { 0, 0 }, { 0,-1 }, { 1, 0 }, { 1, 1 } } },
{ { { 0, 0 }, { 1, 0 }, { 0,-1 }, {-1,-1 } }, // Z
{ { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1,-1 } },
{ { 0, 0 }, { 1, 0 }, { 0,-1 }, {-1,-1 } },
{ { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1,-1 } } } };
public void start()
{
StillRunning = true;
(new Thread(this)).start();
}
public void stop()
{
StillRunning = false;
}
public void init()
{
setFont(new Font("TimesRoman", Font.BOLD, 20));
setBackground(new Color(0, 0, 0));
}
public void run()
{ // Keeps the game ticking over.
int i;
while (StillRunning)
{
try { // Controls game speed.
Thread.sleep(delay);
}
catch (InterruptedException e) {}
if (!StillRunning) break;
if (boardinitialized)
{
if (Paused) continue;
if (CurrentBlockColour == 0)
{ // Make a new piece.
if (NextBlockColour == 0) makeNewPiece();
CurrentBlockColour = NextBlockColour;
CurrentBlockX = NextBlockX;
CurrentBlockY = NextBlockY;
CurrentBlockShape = NextBlockShape;
CurrentBlockAngle = NextBlockAngle;
makeNewPiece();
}
else
{ // Drop the current piece by one place.
i = -1;
while (i++ < 3)
if (CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1] >= 26
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]][CurrentBlockY + 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]] != 0)
{ // Piece hits bottom/some other piece.
i = -1;
while (i++ < 3)
{ // Store piece on Board.
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]][CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]] = CurrentBlockColour;
if (CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1] < 3)
{
Paused = true;
GameOver = true;
forceredraw = true;
}
}
CheckRow();
score += 10;
getParent().action(new Event(this, score, null), this);
if (delay > 80) delay -= 2;
CurrentBlockColour = 0; // Get new piece next turn.
LastX = LastY = LastAngle = -1; // Don't delete current piece on next dopaint.
forceredraw = true;
break;
}
CurrentBlockY++;
}
dopaint();
}
else
repaint();
}
}
public void CheckRow()
{ // Looks at all the rows and deletes full ones.
int i, row = 27, colour = 0, j;
while (row-- > 0)
{
i = -1;
while (i++ < 9)
{
if (BoardArray
[row] == 0)
{
i = 99;
break;
}
if (i == 0)
colour = BoardArray
[row];
else
if (colour != BoardArray
[row]) colour = 0;
}
if (i == 99) continue;
j = row++;
while (--j > 0)
{
i = -1;
while (i++ < 9)
BoardArray
[j + 1] = BoardArray
[j];
}
score += ((colour == 0) ? 100 : 1000);
getParent().action(new Event(this, score, null), this);
forceredraw = true;
repaint();
}
}
public void pause()
{ // What happens when you click the button.
if (GameOver) return;
Paused = !Paused;
forceredraw = true; // Force complete repaint.
repaint();
}
public void newGame()
{ // What happens when you click the button.
int x, y;
if (boardinitialized)
{
for (x = 0; x < 10; x++)
for (y = 0; y < 27; y++)
BoardArray[x][y] = 0;
LastX = LastY = LastAngle = -1;
GameOver = Paused = kludgeflag = false;
delay = 500;
forceredraw = true;
}
CurrentBlockColour = NextBlockColour = score = 0;
getParent().action(new Event(this, score, null), this);
repaint();
}
public void moveRight()
{ // What happens when you click the button.
int i = -1;
if (boardinitialized && !Paused)
{
while (i++ < 3)
if (CurrentBlockX + 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0] > 9
BoardArray[CurrentBlockX + 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]][CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]] != 0)
return;
CurrentBlockX++;
dopaint();
}
}
public void moveLeft()
{ // What happens when you click the button.
int i = -1;
if (boardinitialized && !Paused)
{
while (i++ < 3)
if (CurrentBlockX - 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0] < 0
BoardArray[CurrentBlockX - 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]][CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]] != 0)
return;
CurrentBlockX--;
dopaint();
}
}
public void rotateClockwise()
{ // What happens when you click the button.
int i, NewBlockAngle;
if (boardinitialized && !Paused)
{
NewBlockAngle = CurrentBlockAngle + 1;
if (NewBlockAngle == 4) NewBlockAngle = 0;
i = -1;
while (i++ < 3)
if (CurrentBlockX + BlockInfo[CurrentBlockShape][NewBlockAngle]
[0] > 9
CurrentBlockX + BlockInfo[CurrentBlockShape][NewBlockAngle]
[0] < 0
CurrentBlockY + BlockInfo[CurrentBlockShape][NewBlockAngle]
[1] >= 27
CurrentBlockY + BlockInfo[CurrentBlockShape][NewBlockAngle]
[1] < 0
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][NewBlockAngle]
[0]][CurrentBlockY + BlockInfo[CurrentBlockShape][NewBlockAngle]
[1]] != 0)
return;
CurrentBlockAngle = NewBlockAngle;
dopaint();
}
}
public void rotateAntiClockwise()
{ // What happens when you click the button.
int i, NewBlockAngle;
if (boardinitialized && !Paused)
{
NewBlockAngle = CurrentBlockAngle - 1;
if (NewBlockAngle == -1) NewBlockAngle = 3;
i = -1;
while (i++ < 3)
if (CurrentBlockX + BlockInfo[CurrentBlockShape][NewBlockAngle]
[0] > 9
CurrentBlockX + BlockInfo[CurrentBlockShape][NewBlockAngle]
[0] < 0
CurrentBlockY + BlockInfo[CurrentBlockShape][NewBlockAngle]
[1] >= 27
CurrentBlockY + BlockInfo[CurrentBlockShape][NewBlockAngle]
[1] < 0
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][NewBlockAngle]
[0]][CurrentBlockY + BlockInfo[CurrentBlockShape][NewBlockAngle]
[1]] != 0)
return;
CurrentBlockAngle = NewBlockAngle;
dopaint();
}
}
public void drop()
{ // What happens when you click the drop button.
int i;
if (boardinitialized && !Paused)
{
while (true)
{
i = -1;
while (i++ < 3)
if (CurrentBlockY + 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1] >= 27
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]][CurrentBlockY + 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]] != 0)
{
dopaint();
return;
}
CurrentBlockY++;
score += 1;
}
}
}
public void update(Graphics g)
{ // Background is *not* blanked on entry to update. (As opposed to paint)
int x, y, i;
if (!boardinitialized)
{ // Can't do this in init(). Java bug or feature?
boardinitialized = true;
BoardArray = new int[10][27];
Colours = new Color[7];
for (x = 0; x < 10; x++)
for (y = 0; y < 27; y++)
BoardArray[x][y] = 0;
Colours[0] = new Color(0, 0, 0);
Colours[1] = new Color(128, 0, 0);
Colours[2] = new Color(0, 128, 0);
Colours[3] = new Color(0, 0, 128);
Colours[4] = new Color(128, 128, 0);
Colours[5] = new Color(128, 0, 128);
Colours[6] = new Color(0, 128, 128);
LastX = LastY = LastAngle = -1;
kludgeflag = forceredraw = false;
}
if (kludgeflag && !forceredraw)
{ // See Notes.
kludgeflag = false;
realdopaint(g);
return;
}
kludgeflag = forceredraw = false;
g.setColor(new Color(0, 0, 0));
g.fillRect(0, 0, 120, 324); // Blank background.
if (Paused)
{ // Display messages if appropriate.
String Over = "Game Over!", Pause = "Game Paused";
g.setColor(new Color(255, 255, 255));
g.drawString(GameOver ? Over : Pause,
60 - (getGraphics().getFontMetrics().stringWidth(GameOver ? Over : Pause) / 2), 20);
if (!GameOver) return;
}
for (x = 0; x < 10; x++)
for (y = 0; y < 27; y++)
{ // Redraw static pieces
if (BoardArray[x][y] != 0)
{
g.setColor(Colours[BoardArray[x][y]]);
g.fill3DRect(x * 12, y * 12, 12, 12, true);
}
}
if (CurrentBlockColour != 0)
{ // Redraw Current Blocks
g.setColor(Colours[CurrentBlockColour]);
i = -1;
while (i++ < 3)
g.fill3DRect((CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]) * 12, (CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]) * 12, 12, 12, true);
}
g.dispose();
}
public void dopaint()
{ // Calls realdopaint via repaint/update to get a valid Graphics handle. See Notes.
kludgeflag = true;
repaint();
}
public void realdopaint(Graphics g)
{ // Erases blocks from previous position and redraws them in their new position.
int x, y, i;
g.setColor(new Color(0, 0, 0));
if (LastX != -1)
{
g.setColor(Colours[0]);
i = -1;
while (i++ < 3)
g.fill3DRect((LastX + BlockInfo[CurrentBlockShape][LastAngle]
[0]) * 12, (LastY + BlockInfo[CurrentBlockShape][LastAngle]
[1]) * 12, 12, 12, true);
}
if (CurrentBlockColour != 0)
{
LastX = CurrentBlockX;
LastY = CurrentBlockY;
LastAngle = CurrentBlockAngle;
g.setColor(Colours[CurrentBlockColour]);
i = -1;
while (i++ < 3)
g.fill3DRect((CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[0]) * 12, (CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle]
[1]) * 12, 12, 12, true);
}
g.dispose();
}
public Color getPieceColour()
{
return(Colours[NextBlockColour]);
}
public int getPieceX(int i)
{
return(BlockInfo[NextBlockShape][NextBlockAngle]
[0]);
}
public int getPieceY(int i)
{
return(BlockInfo[NextBlockShape][NextBlockAngle]
[1]);
}
void makeNewPiece()
{
NextBlockColour = (int) (Math.random() * 6) + 1;
if (NextBlockColour == 7) NextBlockColour = 1;
NextBlockX = (int) (Math.random() * 4) + 3;
if (NextBlockX == 8) NextBlockX = 0;
NextBlockY = 1;
NextBlockShape = (int) (Math.random() * 7);
if (NextBlockShape == 7) NextBlockShape = 0;
NextBlockAngle = (int) (Math.random() * 4);
if (NextBlockAngle == 4) NextBlockAngle = 0;
getParent().action(new Event(this, -1, null), this);
}
}
class Icon extends Panel
{ // Could add 3D click effect here.
int iconnumber = 0;
Image icon;
public Icon(Image image, int num)
{
icon = image;
iconnumber = num;
}
public void paint(Graphics g)
{
if (icon != null)
g.drawImage(icon, 0, 0, this);
}
public int getNumber()
{
return iconnumber;
}
public boolean mouseDown(Event evt, int x, int y)
{
getParent().action(evt, this);
return true;
}
}
class Picture extends Panel
{
Image icon;
public Picture(Image image)
{
icon = image;
}
public void paint(Graphics g)
{
if (icon != null)
g.drawImage(icon, 0, 0, this);
}
}
class DisplayPiece extends Panel
{
Board board;
public DisplayPiece(Board b)
{
board = b;
}
public void paint(Graphics g)
{
g.setColor(new Color(128, 128, 128));
g.fill3DRect(0, 0, 68, 68, true);
int i = -1;
if (board instanceof Board)
while (i++ < 3)
{
g.setColor(board.getPieceColour());
g.fill3DRect(28 + board.getPieceX(i) * 12,
28 + board.getPieceY(i) * 12, 12, 12, true);
}
}
}
Janada
@ 2003-07-15 08:46
引用(Trackback1)
评论
在写源代码时最好把代码写入[code]标签或者至少不要解析表情符号
Color
(
Thanks!
[code]janada[/code]
Janada
(
发表评论
用户名:
Email:
主页:
http://
最后更新
北京村民怒罢庸官
五条蛀虫私分国资657万
联想,我看不到希望!
五大职业十年薪酬变迁一览
动员全党 贯彻新思路
去年上半年8000名贪官外逃
给现代都市人画像
2003经典语录
刘德华不明白为何挨告
亚洲八国地区爆发禽流感示意图
原始快照:web.archive.org