Java Reference
In-Depth Information
26
pong = new Sprite(Image.createImage("/pong.png"));
27
pongWidth = pong.getWidth();
28
pongHeight = pong.getHeight();
29
pongX = (getWidth() - pongWidth) >> 1;
30
pongY = getHeight() - 20 - pongHeight;
31
pong.setPosition(pongX, pongY);
32
tonePlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
33
tonePlayer.realize();
34
toneControl = (ToneControl) tonePlayer.getControl("ToneControl");
35
toneControl.setSequence(soundSeq);
36
layerManager = new LayerManager();
37
layerManager.append(pong);
38
}
39
40
public void start() {
41
Thread thread = new Thread(this);
42
thread.start();
43
running = true;
44
startTime = System.currentTimeMillis();
45
lastGenTime = startTime;
46
}
47
48
public void run() {
// the game loop
49
while(running) {
50
try {
51
generateGhosts();
52
movePong();
53
moveGhosts();
54
draw();
55
flushGraphics();
56
Thread.sleep(50);
}
57
catch(Exception e) {}
58
}
59
}
60
61
62
private void draw() {
63
graphics.setColor(0x000000);
64
graphics.fillRect(0, 0, getWidth(), getHeight());
65
graphics.setColor(0xff0000);
66
graphics.setFont(font);
67
graphics.drawString("" + points, getWidth()>>1, 10,
Graphics.HCENTER |
Graphics.TOP);
68
layerManager.paint(graphics, 0, 0);
69
}
70
71
// every second there's an 80% chance of generating new ghosts
72
private void generateGhosts() {
73
long elapsed = System.currentTimeMillis() - lastGenTime;
74
if(elapsed > 1000L) {
75
int i = genRandom(1, 10);
76
if(i <= 8) {
77
int numGhosts = genRandom(1, 4);
78
for(int g = 0; g < numGhosts; g++) {
79
Sprite ghost = new Sprite(ghostImage);
80
int ghostX = genRandom(0, getWidth() - ghost.getWidth());
81
ghost.setPosition(ghostX, 0);
 
Search WWH ::




Custom Search