?? block.java
字號:
// Copyright 2003 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.
import java.util.Random;
import javax.microedition.lcdui.*;
// Blocks try to fly past the base, or crash into it.
// A Block can draw itself.
class Block
{
final static int COLOR = 0xFFFFFF; // WHITE
private final static int MIN_STRENGTH = 0; // bulletStrength
private final static int MAX_STRENGTH = 5; // bulletStrength
private final static int COLLIDE_BORDER = 2; // pixels
private final static Random random = new Random();
private final GameManager gameManager;
private final int xMin;
private final int xMax;
private final int dx;
private final int dxRandom;
private static int dimension = 10; // default height/width in pixels
private int dxVariable = 0;
// A dormant block is one that has been temporarily removed from
// the game, for example before beginning a new life.
// When created, each block is dormant for 1.5 seconds before
// joining the game.
private int dormantTicks = (1500 / GameManager.MILLIS_PER_TICK);
private int bulletHits = 0;
private int bulletStrength = MIN_STRENGTH;
private int x;
private int y;
Block(GameManager gameManager, int xMin, int xMax, int y, int dx)
{
this.gameManager = gameManager;
this.xMin = xMin;
this.xMax = xMax;
this.y = y;
// Set the fixed velocity component, and the default
// variable velocity component (based on the fixed velocity).
// Method varySpeed modifies the variable velocity component based
// on block strength. It modifies the variable component of the
// block speed so that as a block becomes stronger, the variable
// component of speed becomes weaker.
this.dx = dx;
dxRandom = -rand(dx);
varySpeed();
bulletHits = 0;
x = xMax - dimension;
}
private void varySpeed()
{
// Stronger blocks should have a 'tendency' to move
// more slowly than weaker ones move. (Most may move
// somewhat more slowly, but not in all cases.)
dxVariable = ((MAX_STRENGTH - bulletStrength) * dxRandom) /
MAX_STRENGTH;
}
static void setDimension(int d)
{
dimension = d;
}
static int getDimension()
{
return dimension;
}
public void tick()
{
if (dormantTicks > 0)
{
// dormant blocks wait awhile before re-entering the game
dormantTicks--;
}
else
{
// check if we moved off the left game edge
if (x <= xMin)
{
gameManager.doBlockPassed(this);
// become dormant for 3 seconds
dormantTicks = (3000 / GameManager.MILLIS_PER_TICK);
x = xMax - dimension; // initialize re-start x position
if (bulletHits == 0)
{
// If the block flew off canvas without having been shot,
// increase it's strength so it's more difficult to
// destroy in it's next life.
updateStrength();
varySpeed(); // change the variable component of speed
}
else
{
// Reset strength and hits, to become a weak block.
bulletStrength = 0;
bulletHits = 0;
varySpeed(); // change the variable component of speed
}
}
else
{
int gw = gameManager.getCanvas().getWidth();
// The block moves left (dx + dxVariable is negative)
x += (dx + dxVariable);
}
}
}
public void draw(Graphics g)
{
if (dormantTicks == 0)
{
int d = dimension;
int rx = x;
int ry = y;
// Different blocks are drawn for bulletStrength 0 .. 5:
// 0 : dark block with white pixel border, no diagonals
// 1 : dark block with white pixel border, 1 white diagonal line
// 2 : dark block with white pixel border, 2 white diagonal lines
// 3 : white filled block with no diagonals
// 4 : white filled block with one dark diagonal line
// 5 : white filled block with two dark diagonal line
g.setColor(COLOR);
if (bulletStrength < 3)
{
g.drawRect(rx, ry, (d - 1), (d - 1));
if (bulletStrength > 0)
{
g.drawLine((rx + d - 1), ry, rx, (ry + d - 1));
}
if (bulletStrength > 1)
{
g.drawLine(rx, ry, (rx + d - 1), (ry + d - 1));
}
}
else
{
g.fillRect(rx, ry, d, d);
g.setColor(0x000000); // BLACK
if (bulletStrength > 3)
{
g.drawLine((rx + d - 2), (ry + 1), (rx + 1), (ry + d - 2));
}
if (bulletStrength > 4)
{
g.drawLine((rx + 1), (ry + 1), (rx + d - 2), (ry + d - 2));
}
}
}
}
// Increase the bullet strength of the Block up to a maximum strength,
// but reset to initial value if update would be more than the maximum.
void updateStrength()
{
if (bulletStrength <= MAX_STRENGTH)
{
bulletStrength++;
}
else if (bulletStrength > MAX_STRENGTH)
{
bulletStrength = MIN_STRENGTH;
}
}
int getPoints()
{
// The 'points' value of a block is based on its strength
return (bulletStrength + 1);
}
boolean isCollision(Bullet b)
{
boolean overlaps = overlaps2D(x, y, dimension, dimension,
b.getX(), b.getY(), b.getWidth(),
b.getHeight());
return ((dormantTicks == 0) && overlaps);
}
boolean isCollision(Base b)
{
// Using a phone keypad to control the {x, y} position
// of the base may not give the player sufficient fine grained
// control of position as they might like. One way to improve
// this is to make the Block/Base collision detection less sensitive.
//
// The COLLIDE_BORDER parameter allows the collision detection
// to be slightly more forgiving than an exact overlap.
boolean overlaps = overlaps2D((x + COLLIDE_BORDER),
(y + COLLIDE_BORDER),
(dimension - (2 * COLLIDE_BORDER)),
(dimension - (2 * COLLIDE_BORDER)),
b.getX(),
b.getY(),
(b.getWidth() + b.getCannonDimension()),
b.getHeight());
return ((dormantTicks == 0) && overlaps);
}
boolean doBulletCollision()
{
if (++bulletHits > bulletStrength)
{
doExplode();
return true; // block exploded
}
return false;
}
void doExplode()
{
dormantTicks = 30; // Remove from game for 30 ticks
// Re-initialize some block parameters
bulletHits = 0;
x = xMax - dimension;
}
// Do two rectangles overlap?
private static boolean overlaps2D(int x1, int y1, int w1, int h1,
int x2, int y2, int w2, int h2)
{
return overlaps1D(x1, w1, x2, w2) && overlaps1D(y1, h1, y2, h2);
}
// Do two lines overlap?
private static boolean overlaps1D(int p1, int l1, int p2, int l2)
{
return (p1 <= p2) ? ((p1 + l1) >= p2) : (p1 <= (p2 + l2));
}
// Return a positive random-like number
private static int rand(int scale)
{
return (random.nextInt() << 1 >>> 1) % scale;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -