public void run() {
while (killme != null) {
try {
Thread.sleep(Statistics.GetGameSpeed());
}
catch (InterruptedException e){} if (!GamePaused) {
if (FallingShape != null) {
if (FallingShape.CheckIfShapeFits(PlayField, 0, 1, false)) {
ChangeShapePosition(0, 1, false);
}
else {
play (SOUND_DROP);
FallingShape.PlaceInPlayField(PlayField);
myPlayFieldCanvas.RepaintPlayField();
Statistics.AddScore(FallingShape.GetValue());
CheckForFullLines();
GetNextRandomShape();
}
} myPlayFieldCanvas.repaint(FallingShape);
}
} killme = null;
} public void CheckForFullLines() {
int Lines = 0;
int x, y, yc;
boolean Gap; for (y = 0; y < rows; y++) {
Gap = false;
for (x = 0; x < cols; x++) {
if (PlayField[x][y] == Color.black)
Gap = true;
} if (!Gap) {
Lines++; for (yc = y - 1; yc >= 0; yc--) {
for (x = 0; x < cols; x++) {
PlayField[x][yc + 1] = PlayField[x][yc];
}
} for (x = 0; x < cols; x++) { // Delete top row.
PlayField[x][0] = Color.black;
}
}
} if (Lines > 0) {
play (SOUND_LINE);
myPlayFieldCanvas.RepaintPlayField();
} Statistics.AddLines(Lines);
} public boolean action(Event evt, Object arg) {
if ("About".equals(arg)) {
GamePaused = true;
myPlayFieldCanvas.About();
} if ("Pause / Resume".equals(arg)) {
if (GamePaused) {
myPlayFieldCanvas.ShowAboutBox = false;
myPlayFieldCanvas.RepaintPlayField();
} GamePaused = !GamePaused;
} return true;
} public synchronized boolean handleEvent(Event e) {
int xChange = 0;
int yChange = 0;
boolean Rotate = false; switch (e.id) {
case Event.ACTION_EVENT:
return action(e, e.arg);
case Event.KEY_ACTION:
case Event.KEY_PRESS:
switch (e.key) {
case 'a':
case 'A': // Move block to left
xChange--;
break; case 'd':
case 'D': // Move block to right
xChange++;
break;
case 's':
case 'S': // Move block down
yChange++;
break; case 'w':
case 'W': // Rotate block
Rotate = true;
break; default:
return false;
}
break; default:
return false;
} ChangeShapePosition (xChange, yChange, Rotate); return true;
} public void ChangeShapePosition(int xChange, int yChange, boolean Rotate) {
while (!FallingShape.IsReady()) ;
if (FallingShape.CheckIfShapeFits(PlayField, xChange, yChange, Rotate)) {
FallingShape.ChangePosition(xChange, yChange, Rotate);
myPlayFieldCanvas.repaint(FallingShape);
}
}
}class StatisticsCanvas extends Canvas implements ImageObserver {
public static final Color textColor = Color.black;
public static final int MaxLevel = 9;
public static final int myFontHeight = 16; protected Font BlocksFont;
protected FontMetrics BlocksFontMetrics;
protected int Level, Lines, Score;
protected Shape NextShape = null; public void InitNewGame() {
Level = Lines = Score = 0;
} public void GameOver() {
} public int GetGameSpeed() {
switch (Level) {
case 0: return 700;
case 1: return 600;
case 2: return 500;
case 3: return 400;
case 4: return 350;
case 5: return 300;
case 6: return 250;
case 7: return 200;
case 8: return 150;
case 9: return 100;
default: return 100;
}
} public StatisticsCanvas() {
reshape(0, 0, 100, 100);
BlocksFont = new Font("TimesRoman",Font.PLAIN,20);
setFont(BlocksFont);
BlocksFontMetrics = getFontMetrics(BlocksFont); InitNewGame();
} public void AddScore(int s) {
Score += s;
repaint();
} public void AddLines(int ln) {
switch (ln) {
case 1:
AddScore (10);
break;
case 2:
AddScore (20);
break;
case 3:
AddScore (30);
break;
case 4:
AddScore (40);
break;
} Lines += ln;
if (Lines > (10 * (Level + 1))) AddLevel();
repaint();
}
Blocks.play (Blocks.SOUND_NEXTLEVEL);
if (Level < MaxLevel) Level++;
repaint();
} public void DisplayNextShape(Shape s) {
NextShape = s;
repaint();
} public void paint(Graphics g) {
g.setColor(textColor);
g.drawString("Level: " + Level, 0, myFontHeight);
g.drawString("Lines: " + Lines, 0, myFontHeight * 3);
g.drawString("Score: " + Score, 0, myFontHeight * 5);
g.drawString("Next:", 0, myFontHeight * 7); if (NextShape != null) {
NextShape.DisplayAbs(g, 10, myFontHeight * 7 + 10);
}
}
}class PlayFieldCanvas extends Canvas implements ImageObserver {
public static final int fh1 = 17, fh2 = 17;
public static final int BorderWidth = 5;
public static final Color BorderColor = Color.blue;
protected Shape FallingShape = null; boolean FallingShapeNeedRepaint = false;
boolean PlayFieldNeedRepaint = false;
boolean DiscardGame = false;
boolean ShowAboutBox = false; protected Font BlocksFont1, BlocksFont2; Color PlayField[][] = null; public void About() {
ShowAboutBox = true;
repaint();
} public void GameOver() {
DiscardGame = true;
repaint();
} public void RepaintPlayField() {
PlayFieldNeedRepaint = true;
repaint();
} public void SetPlayField(Color pv[][]) {
PlayField = pv;
} public PlayFieldCanvas() {
BlocksFont1 = new Font("TimesRoman",Font.BOLD,20);
FontMetrics BlocksFontMetrics1 = getFontMetrics(BlocksFont1);
BlocksFont2 = new Font("TimesRoman",Font.PLAIN,14);
FontMetrics BlocksFontMetrics2 = getFontMetrics(BlocksFont2);
reshape(0, 0, Blocks.ElementSize * Blocks.cols + BorderWidth * 2, Blocks.ElementSize * Blocks.rows + BorderWidth );
} public void repaint(Shape Shp) {
FallingShape = Shp;
FallingShapeNeedRepaint = true;
repaint();
} public void DrawLines (Graphics g, int y1, int y2) {
for (int y = y1 * Blocks.ElementSize; y < y2 * Blocks.ElementSize; y++) {
g.drawLine (BorderWidth, y, BorderWidth + Blocks.cols * Blocks.ElementSize, y);
}
} public void GraphicsEffect(Graphics g, int y1, int y2) {
for (int l = 0; l < 10; l++) {
g.setColor(Color.red);
DrawLines (g, y1, y2);
g.setColor(Color.green);
DrawLines (g, y1, y2);
g.setColor(Color.blue);
DrawLines (g, y1, y2);
g.setColor(Color.black);
DrawLines (g, y1, y2);
}
} public void DiscardIt(Graphics g) {
DiscardGame = false;
GraphicsEffect (g, 0, Blocks.rows);
} public void DisplayAboutBox(Graphics g)
{
int y = 1;
// Clear background
g.setColor(Color.black);
g.fillRect (BorderWidth, 0, Blocks.ElementSize * Blocks.cols, Blocks.ElementSize * Blocks.rows);
g.setFont(BlocksFont1);
g.setColor(Color.red);
g.drawString("BLOCKS", 20, fh1);
g.setFont(BlocksFont2);
g.setColor(Color.cyan);
g.drawString("v 1.01", BorderWidth + 110, fh1);
g.drawString("Copyright (c) 1996", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("Iwan van Rienen. The", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("program is postcard", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("ware. If you like it", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("buy a stamp and", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("a postcard and send : ", BorderWidth + 2, fh1 + fh2 * y++); g.setColor(Color.green);
g.setColor(Color.yellow);
g.drawString("it to Iwan van Rienen", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("J. Maetsuyckerstr. ", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("145 2593 ZG The Hague", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("The Netherlands", BorderWidth + 2, fh1 + fh2 * y++);
g.setColor(Color.cyan);
g.drawString("This source is free", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("Visit my homepage at", BorderWidth + 2, fh1 + fh2 * y++);
g.setColor(Color.green);
g.drawString("http://www.bart.nl/", BorderWidth + 2, fh1 + fh2 * y++);
g.drawString("E-mail to [email protected]", BorderWidth + 2, fh1 + fh2 * y++);
} public void update(Graphics g) {
if (DiscardGame) {
DiscardIt(g);
} if (PlayFieldNeedRepaint) {
DrawPlayField(g);
} DrawFallingShape(g); if (ShowAboutBox) {
DisplayAboutBox(g);
}
} public void paint(Graphics g) {
if (ShowAboutBox) {
DisplayAboutBox(g);
}
else {
DrawPlayField(g);
FallingShapeNeedRepaint = true;
DrawFallingShape(g);
}
} public void DrawFallingShape(Graphics g) {
if (FallingShapeNeedRepaint && FallingShape != null) {
FallingShape.hide(g, BorderWidth); while (!FallingShape.IsReady()) ;
FallingShape.Display(g, BorderWidth);
FallingShapeNeedRepaint = false;
}
} public void DrawPlayField(Graphics g) {
int x, y; g.setColor(BorderColor);
// Draw left border
g.fillRect (0, 0, BorderWidth, Blocks.ElementSize * Blocks.rows);
// Draw right border
g.fillRect (Blocks.ElementSize * Blocks.cols + BorderWidth, 0, BorderWidth, Blocks.ElementSize * Blocks.rows);
// Draw bottom border
g.fillRect (0, Blocks.ElementSize * Blocks.rows, Blocks.ElementSize * Blocks.cols + BorderWidth * 2, BorderWidth); for (x = 0; x < Blocks.cols; x++) {
for (y = 0; y < Blocks.rows; y++) {
if (PlayField[x][y] != Color.black) {
g.setColor(PlayField[x][y]);
g.fillRect(BorderWidth + x * Blocks.ElementSize + 1, y * Blocks.ElementSize + 1, Blocks.ElementSize - 2, Blocks.ElementSize - 2);
g.setColor(Color.white);
g.drawRect(BorderWidth + x * Blocks.ElementSize, y * Blocks.ElementSize, Blocks.ElementSize - 1, Blocks.ElementSize - 1);
}
else {
g.setColor(Color.black);
g.fillRect(BorderWidth + x * Blocks.ElementSize, y * Blocks.ElementSize, Blocks.ElementSize, Blocks.ElementSize);
}
}
} PlayFieldNeedRepaint = false;
}
}
protected int x, y;
int oldX, oldY;
protected int xInShape, yInShape;
protected int OriginalX, OriginalY;
protected int OriginalXInShape, OriginalYInShape;
protected Color clr;
protected boolean ErasePossible; public Element(int xPos, int yPos, Color c) {
ErasePossible = false;
xInShape = OriginalXInShape = xPos;
yInShape = OriginalYInShape = yPos;
x = OriginalX = xPos + Blocks.cols / 2 - (Blocks.MaxElement + 1)/ 2;
y = OriginalY = yPos;
clr = c;
} public void Init() {
ErasePossible = false; x = OriginalX; y = OriginalY; xInShape = OriginalXInShape;
yInShape = OriginalYInShape;
} public void hide (Graphics g, int xOffs, int yOffs) {
if (ErasePossible) {
int Size = Blocks.ElementSize; g.setColor(Color.black);
g.fillRect(xOffs + oldX * Size, yOffs + oldY * Size, Size, Size); ErasePossible = false;
}
} public void Display (Graphics g, int xOffs, int yOffs) {
int Size = Blocks.ElementSize; g.setColor(clr);
g.fill3DRect(xOffs + x * Size + 1, yOffs + y * Size + 1, Size - 2, Size - 2,false);
g.setColor(Color.white);
g.draw3DRect(xOffs + x * Size, yOffs + y * Size, Size - 1, Size - 1,true); oldX = x;
oldY = y; ErasePossible = true;
} public void DisplayAbs (Graphics g, int xOffs, int yOffs) {
int Size = Blocks.ElementSize; g.setColor(clr);
g.fill3DRect(xOffs + OriginalXInShape * Size + 1, yOffs + OriginalYInShape * Size + 1, Size - 2, Size - 2,false);
g.setColor(Color.white);
g.draw3DRect(xOffs + OriginalXInShape * Size, yOffs + OriginalYInShape * Size, Size - 1, Size - 1,true);
} public boolean CheckIfElementFits(Color PlayField[][],
int xOffs, int yOffs, boolean Rotate) {
if (Rotate) {
xOffs += Blocks.MaxElement - yInShape - xInShape;
yOffs += xInShape - yInShape;
} if (x + xOffs < 0 || x + xOffs >= Blocks.cols) return false;
if (y + yOffs >= Blocks.rows ) return false;
if (PlayField[x + xOffs][y + yOffs] != Color.black) return false; return true;
} public void ChangeElementPosition(int xOffs, int yOffs, boolean Rotate) {
if (Rotate) {
xOffs += Blocks.MaxElement - yInShape - xInShape;
yOffs += xInShape - yInShape; int tempxInShape = xInShape; xInShape = Blocks.MaxElement - yInShape;
yInShape = tempxInShape;
} x += xOffs;
y += yOffs;
} public int GetXPos() { return x; }
public int GetYPos() { return y; }
public Color GetColor() { return clr; }
}class Shape {
protected Vector Elements;
protected int Value;
protected boolean DrawReady = true; public Shape() {
DrawReady = true;
} public void Init() {
DrawReady = true;
Element WalkElement; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement = (Element) Elements.elementAt(ix);
WalkElement.Init();
}
} public Shape(int a, int b, int c, int d, Color clr, int v) {
Value = v;
Elements = new Vector(); AddElements(0, a, clr);
AddElements(1, b, clr);
AddElements(2, c, clr);
AddElements(3, d, clr); Init();
} protected void AddElements (int row, int a, Color clr) {
if ((a & 0xf000) > 0) Elements.addElement (new Element(0, row, clr));
if ((a & 0x0f00) > 0) Elements.addElement (new Element(1, row, clr));
if ((a & 0x00f0) > 0) Elements.addElement (new Element(2, row, clr));
if ((a & 0x000f) > 0) Elements.addElement (new Element(3, row, clr));
} public void hide (Graphics g, int xOffs) {
Element WalkElement; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement = (Element) Elements.elementAt(ix);
WalkElement.hide(g, xOffs, 0);
}
} public void Display (Graphics g, int xOffs) {
Element WalkElement;
DrawReady = false; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement = (Element) Elements.elementAt(ix);
WalkElement.Display(g, xOffs, 0);
} DrawReady = true;
} public void DisplayAbs (Graphics g, int xAbs, int yAbs) { Element WalkElement; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement = (Element) Elements.elementAt(ix);
WalkElement.DisplayAbs(g, xAbs, yAbs);
}
} public boolean CheckIfShapeFits(Color PlayField[][], int xOffs, int yOffs,
boolean Rotate) { Element WalkElement; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement= (Element) Elements.elementAt(ix);
if (!WalkElement.CheckIfElementFits
(PlayField, xOffs, yOffs, Rotate)) return false;
} return true;
} public void ChangePosition(int xOffs, int yOffs, boolean Rotate) {
Element WalkElement; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement = (Element) Elements.elementAt(ix);
WalkElement.ChangeElementPosition(xOffs, yOffs, Rotate);
}
} public void PlaceInPlayField(Color PlayField[][]) {
Element WalkElement; for (int ix = 0; ix < Elements.size(); ix++) {
WalkElement= (Element) Elements.elementAt(ix);
PlayField[WalkElement.GetXPos()]
[WalkElement.GetYPos()] = WalkElement.GetColor();
}
} public int GetValue() { return Value; }
public boolean IsReady() { return DrawReady; }
}
这是一个俄罗斯方块代码,我想加上速度选择标签 怎么用JComboBox加上,谢谢。解决给100
然后你对JComboBox家事件处理程序
对每个速度值选项相应的设置速度
帖出来这么多代码^_^
不过你因该发到J2ME去.我也不懂
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.util.Vector;public class Blocks extends java.applet.Applet implements Runnable {
public static final int cols = 10;
public static final int rows = 20;
public static final int ElementSize = 28;
public static final int MaxElement = 3;
public static final Color BackGroundColor = Color.black;
public static final int SOUND_DROP = 0;
public static final int SOUND_GAMEOVER = 1;
public static final int SOUND_NEXTLEVEL = 2;
public static final int SOUND_LINE = 3; PlayFieldCanvas myPlayFieldCanvas;
StatisticsCanvas Statistics;
Thread killme = null;
Vector ShapeSet;
Shape FallingShape = null;
Shape NextShape = null;
Color PlayField[][];
static AudioClip sounds[]; public boolean GamePaused = true; //===========================================================
public boolean forceSpeed = false;
public int speed = 700;
//=========================================================== public void init() {
setLayout(new BorderLayout());
Panel grid = new Panel();
grid.setLayout(new GridLayout(0, 2));
add("Center", grid); myPlayFieldCanvas = new PlayFieldCanvas();
grid.add(myPlayFieldCanvas); Statistics = new StatisticsCanvas();
grid.add(Statistics); Panel p = new Panel();
p.setLayout(new FlowLayout());
add("East", p);
/*
p.add(new Button("About"));
p.add(new Button("Pause / Resume"));
*/
//===========================================================
Control con = new Control(); myPlayFieldCanvas.addKeyListener(con);
Statistics.addKeyListener(con); JButton about = new JButton("About");
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GamePaused = true;
myPlayFieldCanvas.About();
}
});
p.add(about); JButton pauseResume = new JButton("Pause / Resume");
pauseResume.addKeyListener(con);
pauseResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (GamePaused) {
myPlayFieldCanvas.ShowAboutBox = false;
myPlayFieldCanvas.RepaintPlayField();
}
GamePaused = !GamePaused;
}
});
p.add(pauseResume); String[] speeds = {"Speed 0", "Speed 1", "Speed 2", "Speed 3",
"Speed 4", "Speed 5", "Speed 6", "Speed 7",
"Speed 8", "Speed 9", "Default Speed", };
final JComboBox speedChooser = new JComboBox(speeds);
speedChooser.setSelectedItem(speeds[10]);
speedChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int choice = speedChooser.getSelectedIndex(); GamePaused = true;
forceSpeed = true;
switch (choice) {
case 0:
speed = 700;
break;
case 1:
speed = 600;
break;
case 2:
speed = 500;
break;
case 3:
speed = 400;
break;
case 4:
speed = 350;
break;
case 5:
speed = 300;
break;
case 6:
speed = 250;
break;
case 7:
speed = 200;
break;
case 8:
speed = 150;
break;
case 9:
speed = 100;
break;
default:
forceSpeed = false;
}
}
});
p.add(speedChooser);
//===========================================================
ShapeSet = new Vector();
ShapeSet.addElement(new Shape(0x0000, 0x0FF0,
0x0FF0, 0x0000, Color.blue, 3));
ShapeSet.addElement(new Shape(0x0F00, 0x0F00,
0x0FF0, 0x0000, Color.yellow, 5));
ShapeSet.addElement(new Shape(0x00F0, 0x00F0,
0x0FF0, 0x0000, Color.pink, 5));
ShapeSet.addElement(new Shape(0x0000, 0x0F00,
0xFFF0, 0x0000, Color.green, 4));
ShapeSet.addElement(new Shape(0x0F00, 0x0F00,
0x0F00, 0x0F00, Color.red, 4));
ShapeSet.addElement(new Shape(0x0F00, 0x0FF0,
0x00F0, 0x0000, Color.magenta, 4));
ShapeSet.addElement(new Shape(0x00F0, 0x0FF0,
0x0F00, 0x0000, Color.orange, 4));
sounds = new AudioClip[4];
sounds[0] = getAudioClip(getCodeBase(), "audio/drop.au");
sounds[1] = getAudioClip(getCodeBase(), "audio/gameover.au");
sounds[2] = getAudioClip(getCodeBase(), "audio/nextlevel.au");
sounds[3] = getAudioClip(getCodeBase(), "audio/line.au");
PlayField = new Color[cols][rows];
myPlayFieldCanvas.SetPlayField(PlayField); InitNewGame(); GetNextRandomShape();
} public static void play(int n) {
if (sounds[n] != null) {
sounds[n].play();
}
} public void InitNewGame() {
ClearPlayField();
Statistics.InitNewGame();
} public void ClearPlayField() {
int x, y; for (x = 0; x < cols; x++) {
for (y = 0; y < rows; y++) {
PlayField[x][y] = Color.black;
}
}
} public int GetRandomShapeNr() {
int ShapeNr; do {
ShapeNr = (int) (Math.random() * ShapeSet.size());
} while (ShapeNr >= ShapeSet.size()); return ShapeNr;
} public void GetNextRandomShape() {
if (FallingShape == null) {
FallingShape = (Shape) ShapeSet.elementAt(GetRandomShapeNr());
} else {
FallingShape = NextShape;
} FallingShape.Init(); if (!FallingShape.CheckIfShapeFits(PlayField, 0, 0, false)) {
GameOver();
} NextShape = (Shape) ShapeSet.elementAt(GetRandomShapeNr()); Statistics.DisplayNextShape(NextShape);
}
public void GameOver() {
play(SOUND_GAMEOVER);
myPlayFieldCanvas.GameOver();
Statistics.GameOver();
InitNewGame();
} public void start() {
if (killme == null) {
killme = new Thread(this);
killme.start();
}
} public void stop() {
killme = null;
} public void run() {
while (killme != null) {
try {
/*
Thread.sleep(Statistics.GetGameSpeed());
*/
//===========================================================
if (forceSpeed) {
Thread.sleep(speed);
} else {
Thread.sleep(Statistics.GetGameSpeed());
}
//===========================================================
} catch (InterruptedException e) {
} if (!GamePaused) {
if (FallingShape != null) {
if (FallingShape.CheckIfShapeFits(PlayField, 0, 1, false)) {
ChangeShapePosition(0, 1, false);
} else {
play(SOUND_DROP);
FallingShape.PlaceInPlayField(PlayField);
myPlayFieldCanvas.RepaintPlayField();
Statistics.AddScore(FallingShape.GetValue());
CheckForFullLines();
GetNextRandomShape();
}
} myPlayFieldCanvas.repaint(FallingShape);
}
} killme = null;
} public void CheckForFullLines() {
int Lines = 0;
int x, y, yc;
boolean Gap; for (y = 0; y < rows; y++) {
Gap = false;
for (x = 0; x < cols; x++) {
if (PlayField[x][y] == Color.black)
Gap = true;
} if (!Gap) {
Lines++; for (yc = y - 1; yc >= 0; yc--) {
for (x = 0; x < cols; x++) {
PlayField[x][yc + 1] = PlayField[x][yc];
}
} for (x = 0; x < cols; x++) { // Delete top row.
PlayField[x][0] = Color.black;
}
}
} if (Lines > 0) {
play(SOUND_LINE);
myPlayFieldCanvas.RepaintPlayField();
} Statistics.AddLines(Lines);
} /*public boolean action(Event evt, Object arg) {
if ("About".equals(arg)) {
GamePaused = true;
myPlayFieldCanvas.About();
} if ("Pause / Resume".equals(arg)) {
if (GamePaused) {
myPlayFieldCanvas.ShowAboutBox = false;
myPlayFieldCanvas.RepaintPlayField();
} GamePaused = !GamePaused;
} return true;
} public synchronized boolean handleEvent(Event e) {
int xChange = 0;
int yChange = 0;
boolean Rotate = false; switch (e.id) {
case Event.ACTION_EVENT:
return action(e, e.arg); case Event.KEY_ACTION: case Event.KEY_PRESS:
switch (e.key) {
case 'a':
case 'A': // Move block to left
xChange--;
break; case 'd':
case 'D': // Move block to right
xChange++;
break; case 's':
case 'S': // Move block down
yChange++;
break; case 'w':
case 'W': // Rotate block
Rotate = true;
break; default:
return false;
}
break; default:
return false;
} ChangeShapePosition(xChange, yChange, Rotate); return true;
}*/ public void ChangeShapePosition(int xChange, int yChange, boolean Rotate) {
while (!FallingShape.IsReady()) ;
if (FallingShape.CheckIfShapeFits(PlayField, xChange, yChange, Rotate)) {
FallingShape.ChangePosition(xChange, yChange, Rotate);
myPlayFieldCanvas.repaint(FallingShape);
}
} //===========================================================
private class Control implements KeyListener {
public void keyPressed(KeyEvent e) {
if (GamePaused) {
return;
} int xChange = 0;
int yChange = 0;
boolean Rotate = false; switch (e.getKeyChar()) {
case 'a':
case 'A': // Move block to left
xChange--;
break;
case 'd':
case 'D': // Move block to right
xChange++;
break;
case 's':
case 'S': // Move block down
yChange++;
break;
case 'w':
case 'W': // Rotate block
Rotate = true;
break;
default:
} ChangeShapePosition(xChange, yChange, Rotate);
} public void keyReleased(KeyEvent e) {
} public void keyTyped(KeyEvent e) {
}
}
//===========================================================
}