java弹球小游戏代码 java弹球小游戏实验报告( 三 )


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ballApplet extends Applet implements Runnable {
Dimension d;
Font bFont = new Font("Helvetica", Font.BOLD, 24);
Font sFont = new Font("Helvetica", Font.BOLD, 14);
FontMetrics fmSmall, fmBig;
Graphics goff;
Image img;
Thread mThread;
boolean ingame = false;
int ballx, bally, batpos;
int batdpos = 0;
int balldx = 0, balldy = 0, dxval;
int iScroe, ballNum;
boolean[] showbrick;
int brickNumOfLine;
final int bWidth = 5;
final int batW = 20;
final int ballsize = 5;
final int batH = 5;
final int scoreH = 25;
final int brickW = 15;
final int brickH = 8;
final int space = 1;
final int backcol = 0x102040;
final int line = 4;
final int startline = 32;
public void init() {
Graphics g;
d=size();
setBackground(new Color(backcol));
brickNumOfLine = (d.width-2*bWidth)/(brickW +space);
d.width = brickNumOfLine*(brickW+space)+(2*bWidth);
g = getGraphics();
g.setFont(sFont);
fmSmall= g.getFontMetrics();
g.setFont(bFont);
fmBig = g.getFontMetrics();
showbrick = new boolean[brickNumOfLine*line];
this.addKeyListener(new BallGameKeyListener());
GameInit();
}
public void GameInit() {
batpos = (d.width - batW) / 2;
ballx = (d.width - ballsize) / 2;
bally = (d.height - ballsize - scoreH - 2 * bWidth);
iScroe = 0;
ballNum = 3;
dxval = 2;
if (Math.random()0.5)
balldx = dxval;
else
balldx = -dxval;
balldy = -dxval;
batdpos = 0;
InitBricks();
}
public void InitBricks() {
int i;
for (i = 0; iline * brickNumOfLine; i++)
showbrick[i] = true;
}
// public boolean keyDown(Event e, int key) {
//if (ingame) {
//if (key == Event.LEFT)
//batdpos = -4;
//if (key == Event.RIGHT)
//batdpos = 4;
//if (key == Event.ESCAPE)
//ingame = false;
//} else {
//if (key == 's' || key == 'S') {
//ingame = true;
//GameInit();
//}
//}
//return true;
// }
//
// public boolean keyUp(Event e, int key) {
//if (key == Event.LEFT || key == Event.RIGHT)
//batdpos = 0;
//return true;
// }
public void paint(Graphics g) {
if (goff == nulld.width0d.height0) {
img = createImage(d.width, d.height);
goff = img.getGraphics();
}
if (goff == null || img == null)
return;
goff.setColor(new Color(backcol));
goff.fillRect(0, 0, d.width, d.height);
if (ingame)
PlayGame();
else
ShowIntroScreen();
g.drawImage(img, 0, 0, this);
}
public void PlayGame() {
MoveBall();
CheckBat();
CheckBricks();
DrawPlayField();
DrawBricks();
ShowScore();
}
public void ShowIntroScreen() {
MoveBall();
CheckBat();
CheckBricks();
BatDummyMove();
DrawPlayField();
DrawBricks();
ShowScore();
goff.setFont(bFont);
goff.setColor(new Color(96, 128, 255));
String s = "弹球游戏";
goff.drawString(s, (d.width - fmBig.stringWidth(s)) / 2, (d.height
- scoreH - bWidth) / 2 - 20);
goff.setFont(sFont);
goff.setColor(new Color(128, 255, 32));
s = "请按下'S'键开始游戏";
goff.drawString(s, (d.width - fmSmall.stringWidth(s)) / 2, (d.height

推荐阅读