⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boing.java

📁 简单易懂的弹球小游戏
💻 JAVA
字号:
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.applet.*; 
import java.net.*; 


public class Boing extends Applet implements Runnable,MouseMotionListener { 
public static Color bgColor=Color.black,ballColor=Color.blue; 
public static Color paddleColor=Color.red,brickColor=Color.yellow; 
public static Color fgColor=Color.white; 
public AudioClip sound,booing,pop,win; 
public static AudioClip spring,endgame,loose,bgClip; 
public Thread t; 


public void init () { 


try { 
URL u = getCodeBase(); 
booing = getAudioClip(new URL(u,"spring.au")); 
pop = getAudioClip(new URL(u,"pop.au")); 
spring = getAudioClip(new URL(u,"boing.au")); 
endgame = getAudioClip(new URL(u,"Fart.au")); 
win = getAudioClip(new URL(u,"win.au")); 
loose = getAudioClip(new URL(u,"Lose.au")); 
bgClip = getAudioClip(new URL(u,"Stamp.au")); 
/* 
booing = getAudioClip(new URL("file:///d:/boing/spring.au")); 
pop = getAudioClip(new URL("file:///d:/boing/pop.au")); 
spring = getAudioClip(new URL("file:///d:/boing/boing.au")); 
endgame = getAudioClip(new URL("file:///d:/boing/Fart.au")); 
win = getAudioClip(new URL("file:///d:/boing/win.au")); 
loose = getAudioClip(new URL("file:///d:/boing/Lose.au")); 
bgClip = getAudioClip(new URL("file:///d:/boing/Stamp.au")); 
booing = getAudioClip(new URL("http://www.geocities.com/ali_ezad/spring.au")); 
pop = getAudioClip(new URL("http://www.geocities.com/ali_ezad/pop.au")); 
spring = getAudioClip(new URL("http://www.geocities.com/ali_ezad/boing.au")); 
endgame = getAudioClip(new URL("http://www.geocities.com/ali_ezad/Fart.au")); 
*/ 
} catch (Exception e) {} 
sound = null; 
bgClip.loop(); 
} 


public void start () { 


if (t == null) { 
t = new Thread(this); 
t.start(); 


} else { 
t.resume(); 
} 
} 


public void stop () { 


if (t != null) { 
t.suspend(); 
} 
} 


public void destroy () { 


if (t != null) { 
t.stop(); 
t = null; 
} 
} 
public Vector targets; 
public Ball myBall; 
public Paddle myPaddle; 
public int counter; 
public int declutter,paddleStateMonitor; 
public Label info; 
public boolean paddleHit; 


public Boing () { 
setBackground(bgColor); 
setForeground(fgColor); 
setLayout(null); 
declutter = 0; 
paddleStateMonitor = 0; 
paddleHit = false; 
info = new Label("BOING by Ali Ezad Abbas"); 
targets = new Vector(); 


for (int i=0;i<15;i++) { 


for (int j=0;j<10;j++) { 
targets.addElement(new Brick(i*20,30+(j*6),19,5,brickColor)); 
} 
} 
counter = targets.size(); 
int x = Math.abs(new Random().nextInt()%250); 
myPaddle = new Paddle (x); 
targets.addElement(myPaddle); 
//myBall = new Ball(150,424); 
myBall = new Ball(x,274); 
addMouseMotionListener(this); 
add(info); 
info.setBounds(70,5,140,20); 
} 


public void update (Graphics g) { 


if (declutter >= 100) { 
g.setColor(bgColor); 
//g.fillRect(0,0,300,450); 
g.fillRect(0,0,300,300); 
declutter = 0; 
} 
paint(g); 
} 


public void paint (Graphics g) { 


for (int i=0;i<targets.size();i++) { 
((Target)(targets.elementAt(i))).draw(g); 
} 
myBall.draw(g); 
} 


public void run () { 
myBall.startMoving(); 


while (myBall.isNotOut()&&counter!=0) { 
myBall.move(); 
updateGameStatus(); 
declutter++; 
repaint(); 


try { 
Thread.sleep(5); 
} catch (Exception e) {} 
} 


if (counter == 0) { 
bgClip.stop(); 
win.play(); 
} 
} 


public void updateGameStatus () { 


if (paddleStateMonitor > 50 && paddleHit) { 
paddleHit = false; 


for (int i=0;i<targets.size();i++) { 
Target t = (Target)(targets.elementAt(i)); 


if (t.isThePaddle()) { 
t.notifyIt(); 
} 
} 
} 


for (int i=0;i<targets.size();i++) { 
Target t = (Target)(targets.elementAt(i)); 
Rectangle targetArea = t.getArea(); 
Rectangle ballArea = myBall.r; 


if (ballArea.intersects(targetArea)&&t.getActive()) { 
myBall.rebound(); 
t.hasBeenHit(); 
sound = booing; 


if (t.isABrick()) { 
counter--; 
sound = pop; 
} 


if (t.isThePaddle()) { 
t.notifyIt(); 
paddleStateMonitor = 0; 
paddleHit = true; 
repaint(); 
//t.notifyIt(); 
} 
sound.play(); 
} 
} 
paddleStateMonitor++; 
} 


public void mouseMoved (MouseEvent e) { 
myPaddle.setX(e.getX()-25); 
repaint(); 
} 
public void mouseDragged (MouseEvent e) {} 
public void mousePressed (MouseEvent e) {} 
} 

///////////////////////////////////////////////////// 
interface Target { 
abstract void hasBeenHit (); 
abstract void draw (Graphics g); 
abstract Rectangle getArea (); 
abstract boolean getActive (); 
abstract boolean isABrick (); 
abstract boolean isThePaddle (); 
abstract void notifyIt (); 
} 

///////////////////////////////////// 
class Brick implements Target { 
public static Color bgc = Boing.bgColor; 
public Rectangle r; 
public Color color; 
public boolean active; 


public Brick (int a,int b, int c, int d, Color e) { 
r = new Rectangle(a,b,c,d); 
color = e; 
active = true; 
} 


public boolean isABrick () { 
return true; 
} 


public boolean isThePaddle () { 
return false; 
} 


public Rectangle getArea () { 
return r; 
} 


public void delete () { 
color = bgc; 
} 


public void draw (Graphics g) { 
g.setColor(color); 
g.fillRect(r.x,r.y,r.width,r.height); 
} 


public void hasBeenHit () { 
delete(); 
active = false; 
} 


public boolean getActive () { 
return active; 
} 
public void notifyIt () {} 
} 

/////////////////////////////////////////////////// 
class Paddle implements Target { 
public Rectangle r; 
public int previousX; 
public boolean notified; 


public Paddle (int a) { 
//r = new Rectangle(a,439,50,10); 
r = new Rectangle(a,289,50,10); 
previousX = 0; 
notified = false; 
} 


public boolean isABrick () { 
return false; 
} 


public boolean isThePaddle () { 
return true; 
} 


public Rectangle getArea () { 
return r; 
} 


public void setX (int a) { 
previousX = r.x; 
r.x = a; 
} 


public void draw (Graphics g) { 
g.setColor(Boing.bgColor); 
g.fillRect(previousX,r.y,r.width,r.height); 
g.drawLine(previousX+(r.width/3),r.y-r.height,previousX+2*(r.width/3),r.y-r.height); 
g.drawLine(previousX+(r.width/3),r.y,previousX+2*(r.width/3),r.y-r.height); 
g.drawLine(previousX+(r.width/3),r.y-r.height,previousX+2*(r.width/3),r.y); 
g.setColor(Boing.paddleColor); 
g.fillRect(r.x,r.y,r.width,r.height); 


if (notified) { 
g.setColor(Color.green); 
g.drawLine(r.x+(r.width/3),r.y-r.height,r.x+2*(r.width/3),r.y-r.height); 
g.drawLine(r.x+(r.width/3),r.y,r.x+2*(r.width/3),r.y-r.height); 
g.drawLine(r.x+(r.width/3),r.y-r.height,r.x+2*(r.width/3),r.y); 
} 
} 
public void hasBeenHit () {} 


public boolean getActive () { 
return true; 
} 


public void notifyIt () { 
notified = !notified; 
} 
} 

///////////////////////////////////////////// 
class Ball { 
public static Color bgc = Boing.bgColor, bc = Boing.ballColor; 
public Rectangle r; 
public int previousX, previousY, dx, dy; 
public Color color; 
public boolean ballIsOut; 


public Ball (int a, int b) { 
r = new Rectangle (a,b,15,15); 
color = bc; 
previousX = a; 
previousY = b; 
dx = dy = 0; 
ballIsOut = false; 
} 


public void startMoving () { 
dx = dy = 1; 
} 


public void move () { 
previousX = r.x; 
previousY = r.y; 
r.x += dx; 
r.y += dy; 
checkBounds(); 
} 


public void checkBounds () { 


if (r.x<0||r.x+15>300) { 
dx = -1 * dx; 
Boing.spring.play(); 
} 


if (r.y<10) { 
dy = -1 * dy; 
Boing.spring.play(); 
} 
//if (r.y+15>450) { -150 from everyth 
// ing 


if (r.y+15>300) { 
ballIsOut = true; 
Boing.bgClip.stop(); 
Boing.endgame.play(); 
Boing.loose.play(); 
} 
} 


public boolean isNotOut () { 
return !ballIsOut; 
} 


public void rebound () { 
dy = -1 * dy; 
} 


public void draw (Graphics g) { 
g.setColor(bgc); 
g.fillOval(previousX,previousY,r.width,r.height); 
g.setColor(bc); 
g.fillOval(r.x,r.y,r.width,r.height); 
} 
} 

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -