资源简介
飞翔的小鸟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){
相关资源
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
- [免费]java实现有障碍物的贪吃蛇游戏
- java Servlet投票实例
- 操作系统作业 (pv,作业管理,等5个
- 基于C/S架构考试系统(Java)
评论
共有 条评论