双人 像素鸟如下:
package Flappybirid_1;
import java.awt.image.BufferedImage;
import java.io.IOException;//发出IO异常的信号
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.imageio.ImageIO;
import javax.swing.*;
public class World extends JFrame{
GamePanel fp;
public class GamePanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 6865748790042152200L;
BufferedImage background;
BufferedImage startImage;
Ground ground;
Column column1;
Column column2;
Bird bird[]=new Bird[2];
int []score=new int[2];
GoldCoin gc1,gc2;
boolean start; //游戏开始的标志位
boolean gameover;
boolean []over=new boolean[2];
boolean overs;
boolean getcoin;
int sign;
int random;
public GamePanel(int l) throws IOException {
sign=l;
overs=true;
background=ImageIO.read(getClass().getResource("/images/白天.png"));
startImage=ImageIO.read(getClass().getResource("/images/上.png"));
column1=new Column(360);
column2=new Column(560);
gc1=new GoldCoin();
gc2=new GoldCoin();
for(int i=0;i<sign;i ) {
bird[i]=new Bird(100,190 i*100,i);
score[i]=0;
}
//初始化地面
ground=new Ground();
}
/*重写父类的pain方法来绘制图形*/
public void paint (Graphics g) {
//绘制背景图片
g.drawImage(background,0,0,null);
if(start==true) {
//绘制开始图片
column1.paint(g);
column2.paint(g);
gc1.paint(g);
gc2.paint(g);
for(int i=0;i<sign;i ) {
bird[i].paint(g);
}
}
if(gameover) {
try {
BufferedImage end=ImageIO.read(getClass().getResource("/images/Over.png"));
g.drawImage(end,0,0,null);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(start==false&&gameover==false) {
//绘制开始图片
g.drawImage(startImage,0,0,null);
}
//绘制地面
for(int i=0;i<sign;i ) {
g.setFont(new Font(Font.MONOSPACED,Font.BOLD,20));
g.setColor(Color.WHITE);
g.drawString("score" i "=" score[i], 30 200*i, 50);
ground.paint(g);
}
}
public void action() throws InterruptedException {
this.setFocusable(true);
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO 自动生成的方法存根
if(arg0.getButton()==MouseEvent.BUTTON1) {
if(gameover==true) {
try{
column1=new Column(360);
column2=new Column(560);
gc1=new GoldCoin();
gc2=new GoldCoin();
for(int i=0;i<sign;i ) {
bird[i]=new Bird(100,190 i*100,i);
score[i]=0;
over[i]=false;
}
//初始化地面
ground=new Ground();
start=false;
gameover=false;
}catch(IOException e1) {
e1.printStackTrace();
}
}
start=true;
if(over[0]==false)
bird[0].flappy();
}
if(arg0.getButton()==MouseEvent.BUTTON3) {
if(sign==2) {
if(over[1]==false)
bird[1].flappy();
}
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
});
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_X) {
if(gameover==true) {
try{
column1=new Column(360);
column2=new Column(560);
gc1=new GoldCoin();
gc2=new GoldCoin();
for(int i=0;i<sign;i ) {
bird[i]=new Bird(100,190 i*40,i);
score[i]=0;
over[i]=false;
}
//初始化地面
ground=new Ground();
start=false;
gameover=false;
}catch(IOException e1) {
e1.printStackTrace();
}
}
start=true;
if(over[0]==false)
bird[0].flappy();
}
if(e.getKeyCode()==KeyEvent.VK_N) {
if(sign==2) {
if(over[1]==false)
bird[1].flappy();
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
});
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO 自动生成的方法存根
while(true) {
overs=true;
if(start==true)
{
column1.step();
column2.step();
if(column1.ran<100&&column1.getcoin==false) {
gc1.step(column1);
}
if(column2.ran<100&&column2.getcoin==false) {
gc2.step(column2);
}
for(int i=0;i<sign;i ) {
bird[i].step();
if(over[i]==false) {
//如果不加这个的话就会出现只要有一只鸟还活着就可以无限飞
over[i]=bird[i].hit(column1,column2,ground);
}
if(bird[i].pass(column1, column2)&&over[i]==false) {
score[i] ;
}
overs=overs&over[i];
if(bird[i].hitCoin(gc1)) {
score[i] =1;
gc1.TrackChange();
column1.getcoin=true;
}
if(bird[i].hitCoin(gc2)) {
score[i] =1;
gc2.TrackChange();
column2.getcoin=true;
}
}
if(overs) {
start=false;
gameover=true;
}
}
ground.step();
repaint();
try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//每个60是重绘一次
}
}
});
t.start();
}
}
World(int l) throws IOException, InterruptedException
{
fp = new GamePanel(l);
setSize(384,448);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(fp);
setVisible(true);
fp.action();
}
}
资源文件列表
FlappyBird_shh4/.classpath , 301
FlappyBird_shh4/.project , 391
FlappyBird_shh4/.settings/org.eclipse.jdt.core.prefs , 598
FlappyBird_shh4/bin/Flappybirid_1/AudioPlayWave.class , 3383
FlappyBird_shh4/bin/Flappybirid_1/Bird.class , 4030
FlappyBird_shh4/bin/Flappybirid_1/Column.class , 1803
FlappyBird_shh4/bin/Flappybirid_1/GoldCoin.class , 1678
FlappyBird_shh4/bin/Flappybirid_1/Ground.class , 1076
FlappyBird_shh4/bin/Flappybirid_1/StartGame.class , 574
FlappyBird_shh4/bin/Flappybirid_1/StartPanel$1.class , 1062
FlappyBird_shh4/bin/Flappybirid_1/StartPanel$2.class , 1062
FlappyBird_shh4/bin/Flappybirid_1/StartPanel.class , 1814
FlappyBird_shh4/bin/Flappybirid_1/World$GamePanel$1.class , 2127
FlappyBird_shh4/bin/Flappybirid_1/World$GamePanel$2.class , 1955
FlappyBird_shh4/bin/Flappybirid_1/World$GamePanel$3.class , 2258
FlappyBird_shh4/bin/Flappybirid_1/World$GamePanel.class , 3683
FlappyBird_shh4/bin/Flappybirid_1/World.class , 840
FlappyBird_shh4/bin/images/ground.png , 3567
FlappyBird_shh4/bin/images/Over.png , 7399
FlappyBird_shh4/bin/images/Pipe_Down.png , 5238
FlappyBird_shh4/bin/images/Pipe_Up.png , 5461
FlappyBird_shh4/bin/images/player0_0.png , 3165
FlappyBird_shh4/bin/images/player0_1.png , 3173
FlappyBird_shh4/bin/images/player0_2.png , 3160
FlappyBird_shh4/bin/images/player0_3.png , 3173
FlappyBird_shh4/bin/images/player0_4.png , 3185
FlappyBird_shh4/bin/images/player0_5.png , 3173
FlappyBird_shh4/bin/images/player0_6.png , 3160
FlappyBird_shh4/bin/images/player0_7.png , 3173
FlappyBird_shh4/bin/images/player1_0.png , 3182
FlappyBird_shh4/bin/images/player1_1.png , 549
FlappyBird_shh4/bin/images/player1_2.png , 3151
FlappyBird_shh4/bin/images/player1_3.png , 3170
FlappyBird_shh4/bin/images/player1_4.png , 3182
FlappyBird_shh4/bin/images/player1_5.png , 3170
FlappyBird_shh4/bin/images/player1_6.png , 3151
FlappyBird_shh4/bin/images/player1_7.png , 3170
FlappyBird_shh4/bin/images/上.png , 73863
FlappyBird_shh4/bin/images/命中.wav , 97034
FlappyBird_shh4/bin/images/白天.png , 34891
FlappyBird_shh4/bin/images/金币.wav , 179978
FlappyBird_shh4/bin/images/金牌.png , 2250
FlappyBird_shh4/bin/images/飞.wav , 60170
FlappyBird_shh4/Image/bg_day.png , 24946
FlappyBird_shh4/Image/bg_night.png , 10109
FlappyBird_shh4/Image/Birds.png , 3275
FlappyBird_shh4/Image/land.png , 1931
FlappyBird_shh4/Image/Over.png , 7399
FlappyBird_shh4/Image/Pipe_Down.png , 5238
FlappyBird_shh4/Image/Pipe_Up.png , 5461
FlappyBird_shh4/Image/player0_0.png , 3165
FlappyBird_shh4/Image/player0_1.png , 3173
FlappyBird_shh4/Image/player0_2.png , 3160
FlappyBird_shh4/Image/player0_3.png , 3173
FlappyBird_shh4/Image/player0_4.png , 3185
FlappyBird_shh4/Image/player0_5.png , 3173
FlappyBird_shh4/Image/player0_6.png , 3160
FlappyBird_shh4/Image/player0_7.png , 3173
FlappyBird_shh4/Image/player1_0.png , 3182
FlappyBird_shh4/Image/player1_2.png , 3151
FlappyBird_shh4/Image/player1_3.png , 3170
FlappyBird_shh4/Image/player1_4.png , 3182
FlappyBird_shh4/Image/player1_5.png , 3170
FlappyBird_shh4/Image/player1_6.png , 3151
FlappyBird_shh4/Image/player1_7.png , 3170
FlappyBird_shh4/src/Flappybirid_1/AudioPlayWave.java , 2574
FlappyBird_shh4/src/Flappybirid_1/Bird.java , 2763
FlappyBird_shh4/src/Flappybirid_1/Column.java , 1133
FlappyBird_shh4/src/Flappybirid_1/GoldCoin.java , 849
FlappyBird_shh4/src/Flappybirid_1/Ground.java , 543
FlappyBird_shh4/src/Flappybirid_1/StartGame.java , 243
FlappyBird_shh4/src/Flappybirid_1/StartPanel.java , 2303
FlappyBird_shh4/src/Flappybirid_1/World.java , 6613
FlappyBird_shh4/src/images/ground.png , 3567
FlappyBird_shh4/src/images/Over.png , 7399
FlappyBird_shh4/src/images/Pipe_Down.png , 5238
FlappyBird_shh4/src/images/Pipe_Up.png , 5461
FlappyBird_shh4/src/images/player0_0.png , 3165
FlappyBird_shh4/src/images/player0_1.png , 3173
FlappyBird_shh4/src/images/player0_2.png , 3160
FlappyBird_shh4/src/images/player0_3.png , 3173
FlappyBird_shh4/src/images/player0_4.png , 3185
FlappyBird_shh4/src/images/player0_5.png , 3173
FlappyBird_shh4/src/images/player0_6.png , 3160
FlappyBird_shh4/src/images/player0_7.png , 3173
FlappyBird_shh4/src/images/player1_0.png , 3182
FlappyBird_shh4/src/images/player1_1.png , 549
FlappyBird_shh4/src/images/player1_2.png , 3151
FlappyBird_shh4/src/images/player1_3.png , 3170
FlappyBird_shh4/src/images/player1_4.png , 3182
FlappyBird_shh4/src/images/player1_5.png , 3170
FlappyBird_shh4/src/images/player1_6.png , 3151
FlappyBird_shh4/src/images/player1_7.png , 3170
FlappyBird_shh4/src/images/上.png , 73863
FlappyBird_shh4/src/images/命中.wav , 97034
FlappyBird_shh4/src/images/白天.png , 34891
FlappyBird_shh4/src/images/金币.wav , 179978
FlappyBird_shh4/src/images/金牌.png , 2250
FlappyBird_shh4/src/images/飞.wav , 60170
