1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| package game;
import javax.imageio.ImageIO; import java.util.*;
import javax.swing.*;
import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.imageio.*; import java.awt.*;
public class BirdGame extends JPanel { Image background; Image startImage; Image overImage; Ground ground; Column column1,column2; Bird bird; int score; int state; public static final int START=0; public static final int RUNNING=1; public static final int GAME_OVER=2; public BirdGame() throws Exception { background = new ImageIcon("./source/bg.png").getImage(); startImage = new ImageIcon("./source/start.png").getImage(); overImage=new ImageIcon("./source/gameover.png").getImage(); ground=new Ground(); column1=new Column(1); column2=new Column(2); bird=new Bird(); score=0; state=0; } public void paint(Graphics g) { g.drawImage(background, 0, 0,null); g.drawImage(ground.image, ground.x, ground.y, 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); 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); 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); switch(state) { case START: g.drawImage(startImage, 0, 0, null); break; case GAME_OVER: g.drawImage(overImage, 0, 0, null); break; } } public void action() throws Exception { MouseListener l=new MouseAdapter() { public void mousePressed(MouseEvent e) { try { switch(state) { case START: state=RUNNING; break; case RUNNING: bird.flappy(); break; case GAME_OVER: column1=new Column(1); column2=new Column(2); bird=new Bird(); score=0; state=START; break; } } catch (Exception ex) { ex.printStackTrace(); } } }; addMouseListener(l); while(true) { switch(state) { case START: bird.fly(); ground.step(); break; case RUNNING: ground.step(); column1.step(); column2.step(); bird.fly(); bird.step();
score++; if(bird.hit(ground)||bird.hit(column1)||bird.hit(column2)) { state=GAME_OVER; } break; } Thread.sleep(1000/60); repaint(); } } public static void main(String[] args) throws Exception { JFrame frame=new JFrame(); BirdGame game=new BirdGame(); frame.add(game); frame.setSize(440,670); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); game.action(); } }
|