資源簡介
飛翔的小鳥java版源碼,請使用eclipse導入運行!-----
代碼片段和文件信息
package?com.tarena.bird;
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.Graphics;
import?java.awt.Graphics2D;
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?java.awt.image.BufferedImage;
import?java.util.Random;
import?javax.imageio.ImageIO;
import?javax.swing.Jframe;
import?javax.swing.JPanel;
?
public?class?BirdGame?extends?JPanel?{
Bird?bird;
Column?column1?column2;?
Ground?ground;
BufferedImage?background;
/**?游戲結束狀態?*/
boolean?gameOver;
BufferedImage?gameOverImage;
/**?游戲開始狀態?*/
boolean?started;
BufferedImage?startImage;
//分數
int?score;
Sound?hitSound;
Sound?flappySound;
Sound?scoreSound;
Sound?startSound;
/**?初始化?BirdGame?的屬性變量?*/
public?BirdGame()?throws?Exception?{
started?=?false;
startImage?=?ImageIO.read(
getClass().getResource(“start.png“));
gameOver?=?false;
gameOverImage?=?ImageIO.read(
getClass().getResource(
“gameover.png“));
score?=?0;
bird?=?new?Bird();
column1?=?new?Column(1);
column2?=?new?Column(2);
ground?=?new?Ground();
background?=?ImageIO.read(
getClass().getResource(“bg.png“));?
hitSound?=?new?Sound(“hit.wav“);
flappySound?=?new?Sound(“flappy.wav“);
scoreSound?=?new?Sound(“score.wav“);
startSound?=?new?Sound(“start.wav“);
}
/**?“重寫(修改)“paint方法實現繪制?*/
public?void?paint(Graphics?g){
g.drawImage(background?0?0?null);
g.drawImage(column1.image?
column1.x-column1.width/2?
column1.y-column1.height/2?null);
g.drawImage(column2.image?
column2.x-column2.width/2?
column2.y-column2.height/2?null);
//在paint方法中添加繪制分數的算法
Font?f?=?new?Font(Font.SANS_SERIF
Font.BOLD?40);
g.setFont(f);
g.drawString(““+score?40?60);
g.setColor(Color.WHITE);
g.drawString(““+score?40-3?60-3);
g.drawImage(ground.image?ground.x?
ground.y?null);
//旋轉(rotate)繪圖坐標系,是API方法
Graphics2D?g2?=?(Graphics2D)g;
g2.rotate(-bird.alpha?bird.x?bird.y);
g.drawImage(bird.image?
bird.x-bird.width/2?
bird.y-bird.height/2?null);
g2.rotate(bird.alpha?bird.x?bird.y);
//在paint方法中添加顯示游戲結束狀態代碼
if(gameOver){
g.drawImage(gameOverImage00null);
}
if(!?started){
g.drawImage(startImage?0?0?null);
}
?
//添加調試的方框
// g.drawRect(bird.x-bird.size/2?
// bird.y-bird.size/2?
// bird.size?bird.size);
// g.drawRect(column1.x-column1.width/2?
// column1.y-column1.height/2?
// column1.width?
// column1.height/2-column1.gap/2);
// g.drawRect(column1.x-column1.width/2?
// column1.y+column1.gap/2?
// column1.width?
// column1.height/2-column1.gap/2);
}//paint方法的結束
//BirdGame中添加方法action()
public?void?action()?throws?Exception?{
MouseListener?l=new?MouseAdapter(){
//Mouse?老鼠?Pressed按下
public?void?mousePressed(
MouseEvent?e){
try{
if(gameOver){
synchronized(BirdGame.this){
評論
共有 條評論