📄 dodger.java
字号:
AudioClip bangAudioClip = Applet.newAudioClip ( bangAudioURL );
dodgerSprite = new DodgerSprite (
dodgerGreenIcon,
dodgerYellowIcon,
dodgerRedIcon,
dodgerBangIcon,
dodgerBoomIcon,
bangAudioClip,
bounds ); animatedComponent.addMouseListener ( dodgerSprite );
animatedComponent.addMouseMotionListener ( dodgerSprite );
animatedComponent.addKeyListener ( dodgerSprite );
} catch ( IOException ex ) { ex.printStackTrace ( ); }
animatedComponent.requestFocus ( );
// obstacles obstacleSprites = new ObstacleSprite [ 0 ];
obstacleExplodeAudioClip = Applet.newAudioClip (
getClass ( ).getClassLoader ( ).getResource (
OBSTACLE_EXPLODE_AUDIO_FILENAME ) );
random = new Random ( );
dodgerBounds = new Rectangle ( );
scoreTextSprite = new TextSprite ( TITLE );
scoreTextSprite.setX ( 20 );
scoreTextSprite.setY ( 20 );
scoreTextSprite.setColor ( Color.RED );
try { DodgerData dodgerData = ( DodgerData ) SerializableLib.load ( LATEST_DATA_FILENAME, BACKUP_DATA_FILENAME, FILE_CONTENTS_SPEC, ( Applet ) this, PERSISTENCE_KEY, ( ClassLoader ) null, ( String ) null ); if ( dodgerData != null ) { highScore = dodgerData.highScore; } } catch ( Exception ex ) { ex.printStackTrace ( ); } }
public void start ( ) { animatedComponent.start ( ); }
public void stop ( ) { animatedComponent.stop ( ); }
public void destroy ( )
//////////////////////////////////////////////////////////////////////
{
try { SerializableLib.save ( new DodgerData ( highScore ), LATEST_DATA_FILENAME, BACKUP_DATA_FILENAME, FILE_CONTENTS_SPEC, ( Applet ) this, PERSISTENCE_KEY ); } catch ( Exception ex ) { ex.printStackTrace ( ); } animatedComponent.destroy ( );
}
//////////////////////////////////////////////////////////////////////
// interface ComponentAnimator methods
//////////////////////////////////////////////////////////////////////
public void update ( JComponent component )
//////////////////////////////////////////////////////////////////////
{
ObstacleSprite [ ] obstacleSprites = this.obstacleSprites;
for ( int i = 0; i < obstacleSprites.length; i++ )
{
obstacleSprites [ i ].update ( component );
}
Shape dodgerCollisionShape = dodgerSprite.getCollisionShape ( );
if ( dodgerCollisionShape != null )
{
Rectangle2D dodgerCollisionRectangle2D
= dodgerCollisionShape.getBounds2D ( );
for ( int i = 0; i < obstacleSprites.length; i++ )
{
Shape obstacleCollisionShape
= obstacleSprites [ i ].getCollisionShape ( );
if ( obstacleCollisionShape != null )
{
if ( obstacleCollisionShape.intersects (
dodgerCollisionRectangle2D ) )
{
dodgerSprite.setHit ( );
obstacleSprites [ i ].setHit ( );
break;
}
}
}
}
dodgerSprite.update ( component );
if ( dodgerSprite.isShooting ( ) )
{
shoot ( );
}
long score = dodgerSprite.getScore ( );
highScore = score > highScore ? score : highScore;
scoreTextSprite.setText (
"High Score: " + highScore + " Score: " + score );
component.repaint ( );
}
public void paint (
JComponent component,
Graphics2D graphics )
//////////////////////////////////////////////////////////////////////
{
graphics.setColor ( BACKGROUND_COLOR );
graphics.fillRect ( 0, 0, bounds.width, bounds.height );
ObstacleSprite [ ] obstacleSprites = this.obstacleSprites;
for ( int i = 0; i < obstacleSprites.length; i++ )
{
obstacleSprites [ i ].paint ( component, graphics );
}
dodgerSprite.paint ( component, graphics );
scoreTextSprite.paint ( component, graphics );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private Image loadAutomaticImage ( String imageFilename, int transparency ) throws IOException ////////////////////////////////////////////////////////////////////// { return ImageLib.loadAutomaticImage ( imageFilename, transparency, animatedComponent, getClass ( ).getClassLoader ( ), null ); }
private void shoot ( )
//////////////////////////////////////////////////////////////////////
{
Rectangle2D shootArea = dodgerSprite.getShootArea ( );
int index = -1;
ObstacleSprite [ ] obstacleSprites = this.obstacleSprites;
for ( int i = 0; i < obstacleSprites.length; i++ )
{
Sprite obstacleSprite = obstacleSprites [ i ];
Shape obstacleCollisionShape
= obstacleSprite.getCollisionShape ( );
if ( obstacleCollisionShape != null )
{
if ( obstacleCollisionShape.intersects ( shootArea ) )
{
if ( ( index < 0 )
|| ( obstacleSprites [ index ].getY ( )
< obstacleSprite.getY ( ) ) )
{
index = i;
}
}
}
}
if ( index > -1 )
{
obstacleSprites [ index ].setHit ( );
}
}
private void resetBounds ( )
//////////////////////////////////////////////////////////////////////
{
dodgerSprite.resetScore ( );
animatedComponent.getBounds ( bounds );
int obstacleCount = ( int ) Math.round (
OBSTACLE_DENSITY * bounds.width * bounds.height );
obstacleSprites = new ObstacleSprite [ obstacleCount ];
for ( int i = 0; i < obstacleSprites.length; i++ )
{
ObstacleSprite obstacleSprite = new ObstacleSprite (
obstacleExplodeAudioClip,
random,
bounds,
PIXELS_PER_FRAME );
obstacleSprite.setY ( Double.POSITIVE_INFINITY );
obstacleSprites [ i ] = obstacleSprite;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -