📄 gamescreen.java
字号:
// Copyright 2002 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
package example.fruitmachine;
import javax.microedition.lcdui.*;
import java.io.IOException;
class GameScreen
extends Canvas
implements CommandListener, Runnable, HttpPosterListener
{
private final FruitMachineMIDlet midlet;
private final HttpPoster httpPoster;
private final Account account;
private final Display display;
private final Command changeBetCommand;
private final Command buyCreditCommand;
private final Command spinCommand;
private final Command quitCommand;
private static final int BAR = 0;
private static final int BELL = 1;
private static final int ORANGE = 2;
private static final int LEMON = 3;
private static final int PLUM = 4;
private static final int CHERRY = 5;
private Image[] images = new Image[6];
private final Image holdImage;
private Image[] holdButtonImages = new Image[3];
private static final int wheelValues[] =
{
BAR, ORANGE, LEMON, PLUM, CHERRY,
BELL, ORANGE, LEMON, PLUM, CHERRY,
BELL, ORANGE, LEMON, PLUM, CHERRY,
BELL, ORANGE, LEMON, PLUM, CHERRY
};
private static final int NUM_WHEELS = 3;
private static final int VALUES_PER_WHEEL = wheelValues.length;
private boolean hold[] = new boolean[NUM_WHEELS]; // initially false
private int wheelPosition[] = new int[NUM_WHEELS];
private int fasterTicks[] = new int[NUM_WHEELS];
private int slowerTicks[] = new int[NUM_WHEELS];
private int spindownTicks[] = new int[NUM_WHEELS];
private static final int spinSpeed[] = {8, 9, 7}; // pixels/tick:full speed
private static final int TICK_TIME_MILLIS = 100;
private static final int SPINDOWN_STEP = 4; // ticks at each slower speed
// if not a new spin, can't increase bet AND hold, so have to
// record previous bet
private boolean newSpin = true;
private int previousBet;
private static final int IMAGE_SPACING = 2; // between wheels on screen
private static final int BORDER_SPACING = 2; // between wheels & border
private static final int BORDER_THICKNESS = 2; // thickness of box
private static final int HOLD_SPACING = 2; // between box & hold icons
private int imageWidth;
private int imageHeight;
private int wheelHeight;
// state of animation thread
private volatile boolean aborting = false; // set true to exit animation
private volatile boolean spinning = false;
private boolean spinningFreely = false;
private final Thread animationThread;
// state of UI
private volatile boolean readyForInput = true;
private boolean quitting = false;
// if we're animating the slowing down of the wheels because the server
// has told us whether it was a win or loss, we save the information here
private boolean wasWin;
private int winAmount;
private int updatedCredit;
GameScreen(FruitMachineMIDlet midlet,
HttpPoster httpPoster,
Account account,
Display display,
Image barImage,
Image bellImage,
Image orangeImage,
Image lemonImage,
Image plumImage,
Image cherryImage,
Image holdImage,
Image hold1Image,
Image hold2Image,
Image hold3Image)
{
this.midlet = midlet;
this.httpPoster = httpPoster;
this.account = account;
this.display = display;
images[BAR] = barImage;
images[BELL] = bellImage;
images[ORANGE] = orangeImage;
images[LEMON] = lemonImage;
images[PLUM] = plumImage;
images[CHERRY] = cherryImage;
this.holdImage = holdImage;
holdButtonImages[0] = hold1Image;
holdButtonImages[1] = hold2Image;
holdButtonImages[2] = hold3Image;
imageWidth = barImage.getWidth(); // assume all same size !
imageHeight = barImage.getHeight();
wheelHeight = wheelValues.length * imageHeight;
changeBetCommand = new Command("Change Bet", Command.SCREEN, 3);
addCommand(changeBetCommand);
buyCreditCommand = new Command("Buy Credit", Command.SCREEN, 3);
addCommand(buyCreditCommand);
spinCommand = new Command("Spin", Command.SCREEN, 1);
addCommand(spinCommand);
// don't make this Command.QUIT, since we don't want it winning over
// "Spin" !
quitCommand = new Command("Quit", Command.SCREEN, 4);
addCommand(quitCommand);
setInvalidEndPositions();
setCommandListener(this);
animationThread = new Thread(this);
animationThread.start(); // initially just waits for a notification
}
// this method is used by our animation thread
public void run()
{
while (!aborting)
{
while (!spinning && !aborting)
{
try
{
synchronized (this)
{
wait();
}
}
catch (InterruptedException e)
{
}
}
if (!aborting)
{
// we're animating; spin one step and sleep a while
spin();
repaint(0, 0, getWidth(), getHeight());
try
{
Thread.currentThread().sleep(TICK_TIME_MILLIS);
}
catch (InterruptedException e)
{
}
}
}
}
private void spin()
{
boolean endOfSpin = true;
// this should not happen at the same time as the updates of
// these values by other means
synchronized (wheelPosition)
{
for (int i = 0; i < NUM_WHEELS; ++i)
{
if (! hold[i])
{
if (spinningFreely)
{
endOfSpin = false;
stepWheel(i, spinSpeed[i]);
}
else if (fasterTicks[i] > 0)
{
endOfSpin = false;
stepWheel(i, spinSpeed[i]);
fasterTicks[i]--;
}
else if (slowerTicks[i] > 0)
{
endOfSpin = false;
stepWheel(i, spinSpeed[i] - 1);
slowerTicks[i]--;
}
else if (spindownTicks[i] > 0)
{
endOfSpin = false;
stepWheel(i,
(spindownTicks[i] + SPINDOWN_STEP - 1) /
SPINDOWN_STEP);
spindownTicks[i]--;
}
}
}
}
if (endOfSpin)
{
spinning = false;
account.setCredit(updatedCredit);
if (wasWin)
{
clearHolds();
newSpin = true;
WinScreen.showWin(winAmount);
}
else
{
newSpin = false;
previousBet = account.getCurrentBet();
}
readyForInput = true;
}
}
private void stepWheel(int i, int step)
{
wheelPosition[i] = (wheelPosition[i] + step) % wheelHeight;
}
// for tidying up only
public void abort()
{
aborting = true;
synchronized (this)
{
notify(); // wake up our animation thread and kill it
}
}
// called by the UI thread
public void paint(Graphics g)
{
int width = getWidth();
int height = getHeight();
g.setClip(0, 0, width, height);
// fill background with black
g.setColor(0, 0, 0);
g.fillRect(0, 0, width, height);
// draw a box in yellow
int wheelBoxWidth = imageWidth * NUM_WHEELS +
IMAGE_SPACING * (NUM_WHEELS - 1);
int maxWheelBoxHeight = height - BORDER_SPACING * 2
- BORDER_THICKNESS * 2
- HOLD_SPACING - holdImage.getHeight() - 2;
int wheelBoxHeight = imageHeight * 2;
if (wheelBoxHeight > maxWheelBoxHeight)
{
wheelBoxHeight = maxWheelBoxHeight; // might have to shrink it
}
int borderBoxWidth = wheelBoxWidth +
BORDER_SPACING * 2 +
BORDER_THICKNESS * 2;
int borderBoxHeight = wheelBoxHeight +
BORDER_SPACING * 2 +
BORDER_THICKNESS * 2;
int rectLeft = (width - borderBoxWidth) / 2;
int rectTop = (height - borderBoxHeight
- HOLD_SPACING - holdImage.getHeight()) / 2;
g.setColor(255, 255, 0);
for (int i = 0; i < BORDER_THICKNESS; ++i)
{
g.drawRect(rectLeft + i,
rectTop + i,
borderBoxWidth - 2 * i - 1,
borderBoxHeight - 2 * i - 1);
}
// draw hold signs if in use
int holdTop = rectTop + borderBoxHeight + HOLD_SPACING;
int holdLeft = rectLeft + BORDER_THICKNESS + BORDER_SPACING;
g.setColor(255, 255, 255);
for (int i = 0; i < NUM_WHEELS; ++ i)
{
int signLeft = holdLeft + i * (imageWidth + IMAGE_SPACING);
if (hold[i])
{
g.drawImage(holdImage,
signLeft,
holdTop,
Graphics.TOP | Graphics.LEFT);
}
else
{
g.drawImage(holdButtonImages[i],
signLeft,
holdTop,
Graphics.TOP | Graphics.LEFT);
}
}
// draw wheels
int wheelBoxLeft = rectLeft + BORDER_THICKNESS + BORDER_SPACING;
int wheelBoxTop = rectTop + BORDER_THICKNESS + BORDER_SPACING;
int wheelImageTop = wheelBoxTop - (imageHeight - wheelBoxHeight/2);
g.setClip(wheelBoxLeft, wheelBoxTop, wheelBoxWidth, wheelBoxHeight);
for (int i = 0; i < NUM_WHEELS; ++i)
{
int centrePosition = wheelPosition[i] + imageHeight / 2;
int wheelLeft = wheelBoxLeft + i * (imageWidth + IMAGE_SPACING);
int centreImageNumber =
(centrePosition / imageHeight) % VALUES_PER_WHEEL;
int centreImageTopOffset = wheelImageTop +
centrePosition % imageHeight;
g.drawImage(images[wheelValues[centreImageNumber]],
wheelLeft,
centreImageTopOffset,
Graphics.TOP | Graphics.LEFT);
int topImageNumber = (centreImageNumber + 1) %
wheelValues.length;
int topImageTopOffset = centreImageTopOffset - imageHeight;
g.drawImage(images[wheelValues[topImageNumber]],
wheelLeft,
topImageTopOffset,
Graphics.TOP | Graphics.LEFT);
int bottomImageNumber =
(centreImageNumber + wheelValues.length - 1) %
wheelValues.length;
int bottomImageTopOffset = centreImageTopOffset + imageHeight;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -