⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 俄罗斯方块java.txt

📁 俄罗斯方块java代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/* 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 retriev
ing 
* graphics information such as string metrics, not for actual real-time drawin
g
* 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");
add(icon = new Icon(image, 1));
icon.reshape(172, 170, 36, 36);

image = getImage(getCodeBase(), "anticlockwise.gif");
add(icon = new Icon(image, 2));
icon.reshape(216, 170, 36, 36);

image = getImage(getCodeBase(), "down.gif");
add(icon = new Icon(image, 3));
icon.reshape(260, 170, 36, 36);

image = getImage(getCodeBase(), "clockwise.gif");
add(icon = new Icon(image, 4));
icon.reshape(304, 170, 36, 36);

image = getImage(getCodeBase(), "right.gif");
add(icon = new Icon(image, 5));
icon.reshape(348, 170, 36, 36);

image = getImage(getCodeBase(), "JTetris.gif");
add(picture = new Picture(image));
picture.reshape(192, 210, 180, 139);

add(button1 = new Button("New Game"));
button1.setBackground(new Color(128, 128, 128));
button1.move(180, 8);
button1.setSize(new Dimension(96, 32));

add(button2 = new Button("Pause"));
button2.setBackground(new Color(128, 128, 128));
button2.move(284, 8);
button2.setSize(new Dimension(96, 32));

add(score = new TextField("Score: 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"))
board.newGame();
if (((String)object).equals("Pause"))
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 = fa
lse;
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 } }, // Squ
are
{ { 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][i][1] >= 2
6
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle][i][
0]][CurrentBlockY + 1 + BlockInfo[CurrentBlockShape][CurrentBlockAngle][i][1]]
 != 0)
{ // Piece hits bottom/some other piece.
i = -1;
while (i++ < 3)
{ // Store piece on Board.
BoardArray[CurrentBlockX + BlockInfo[CurrentBlockShape][CurrentBlockAngle][i][
0]][CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle][i][1]] = C
urrentBlockColour;
if (CurrentBlockY + BlockInfo[CurrentBlockShape][CurrentBlockAngle][i][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[i][row] == 0) 
{
i = 99;
break;
}

if (i == 0)
colour = BoardArray[i][row];
else
if (colour != BoardArray[i][row]) colour = 0;
}

if (i == 99) continue;

j = row++;

while (--j > 0)
{
i = -1;

while (i++ < 9)
BoardArray[i][j + 1] = BoardArray[i][j];
}

score += ((colour == 0) ? 100 : 1000);
getParent().action(new Event(this, score, null), this);
forceredraw = true;
repaint();
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -