Java零基础学习记录09(飞机躲避炮弹游戏实现)
生活随笔
收集整理的這篇文章主要介紹了
Java零基础学习记录09(飞机躲避炮弹游戏实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**
飛機躲炮彈游戲實現
**
import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Date;/*** 飛機游戲的主窗口類* @author一個垃圾程序員的咆哮* * */ public class MyGameFrame extends Frame{Image bg = GameUtil.getImage("\\images\\bg.jpg");Image planeImage = GameUtil.getImage("\\images\\plane.png");Plane plane = new Plane(planeImage,250,250);Shell[] shells = new Shell[50];Explode bao;Date startTime = new Date();Date endTime;int period;//游戲持續的時間public void paint(Graphics g) {//自動被調用,g相當于一支畫筆Color c = g.getColor();g.drawImage(bg,0,0, null);plane.drawSelf(g);//畫出所有的炮彈for(int i = 0;i<shells.length;i++) {shells[i].draw(g); boolean peng = shells[i].getRect().intersects(plane.getRect());//子彈和飛機碰撞if(peng) {plane.live = false;if(bao == null) {//如果爆炸對象為空,則創建對象bao = new Explode(plane.x, plane.y);endTime = new Date();period =(int)((endTime.getTime() - startTime.getTime())/1000);}bao.draw(g);}//計時功能,給出提示if(!plane.live) {g.setColor(Color.red);Font f = new Font("宋體",Font.BOLD,50);g.setFont(f);g.drawString("時間:"+period+"秒",(int)plane.x,(int)plane.y);}}g.setColor(c);}//定義一個重畫窗口的線程類,是一個內部類,反復重畫窗口class PaintThread extends Thread{public void run() {while (true) {repaint();//重畫窗口try {Thread.sleep(40);//1s = 1000ms}catch (InterruptedException e) {e.printStackTrace();}}}}class KeyMonitor extends KeyAdapter{@Overridepublic void keyPressed(KeyEvent e) {plane.addDirection(e);}@Overridepublic void keyReleased(KeyEvent e) {plane.minusDirection(e);}}/*** 初始化窗口* */public void launchFrame() {this.setTitle("一個垃圾程序員的咆哮!");//給游戲添加標題this.setSize(Constant.GAME_WIDH,Constant.GAME_HEIGHT);//給窗口設置大小this.setLocation(300,300);//給窗口設置位置。this.setVisible(true);//窗口原本是不可見的,設置讓他可見this.addWindowListener(new WindowAdapter() {//監聽器@Overridepublic void windowClosing(WindowEvent e) { //窗口關閉,點擊小紅叉時,真正的關閉程序System.exit(0);//0表示正常結束,-1表示異常結束}});new PaintThread().start();//啟動重畫窗口的線程addKeyListener(new KeyMonitor());//給窗口增加鍵盤的監聽//初始化50個炮彈for(int i = 0;i<shells.length;i++) {shells[i] = new Shell();}new PaintThread().start();}public static void main(String[] args) {MyGameFrame f = new MyGameFrame();//創建一個窗口對象f.launchFrame();}private Image offScreenImage = null;public void update(Graphics g) {if(offScreenImage == null)offScreenImage = this.createImage(Constant.GAME_WIDH,Constant.GAME_HEIGHT);//這是游戲窗口的寬度和高度Graphics gOff = offScreenImage.getGraphics();paint(gOff);g.drawImage(offScreenImage, 0, 0, null); } }/*** 游戲物體的父類,所有游戲對象都具有相同的屬性* @author 一個垃圾程序員的咆哮**/import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; public class GameObject {Image img;double x, y;int speed;int width, height;public GameObject(Image img, double x, double y, int speed, int width, int height) {this.img = img;this.x = x;this.y = y;this.speed = speed;this.width = width;this.height = height;}public GameObject(Image img, double x, double y) {this.img = img;this.x = x;this.y = y;}public GameObject() {}/*** 返回物體所在的矩形,便于后續的碰撞檢測* @return*/public Rectangle getRect() {return new Rectangle((int)x,(int)y,width,height);}public void drawSelf(Graphics g) {g.drawImage(img, (int)x, (int)y, null);} }import java.awt.Image; import java.awt.image.BufferedImage; import java.net.URL;import javax.imageio.ImageIO;public class GameUtil {private GameUtil() {//該類為工具類,不允許創建該類的對象}/*** 返回path路徑的圖片* @param path* @return*/public static Image getImage(String path) {BufferedImage bi = null;try {URL u = GameUtil.class.getClassLoader().getResource(path);bi = ImageIO.read(u);}catch (Exception e) {e.printStackTrace();}return bi;} }import java.awt.Image; import java.awt.image.BufferedImage; import java.net.URL;import javax.imageio.ImageIO;public class GameUtil {private GameUtil() {//該類為工具類,不允許創建該類的對象}/*** 返回path路徑的圖片* @param path* @return*/public static Image getImage(String path) {BufferedImage bi = null;try {URL u = GameUtil.class.getClassLoader().getResource(path);bi = ImageIO.read(u);}catch (Exception e) {e.printStackTrace();}return bi;} }public class Constant { //常量類private Constant() {}public static final int GAME_WIDH = 500;public static final int GAME_HEIGHT = 500; }import java.awt.Color; import java.awt.Graphics; /*** 炮彈類* @author 一個垃圾程序員的咆哮*/ public class Shell extends GameObject{double degree;public Shell() {x = 200;y = 200;width = 10;height = 10;speed = 2;degree = Math.random()*Math.PI*2;}public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.YELLOW);g.fillOval((int)x, (int)y, width, height);//炮彈沿著任意方向飛x += speed*Math.cos(degree);y += speed*Math.sin(degree);if(x<0||x>Constant.GAME_WIDH - width) {degree = Math.PI - degree;}if(y<30||y>Constant.GAME_HEIGHT - height) {degree = - degree;}g.setColor(c); } }import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; public class Plane extends GameObject {int speed = 5;boolean left, up, right, down;boolean live = true;public void drawSelf(Graphics g) {if(live) {g.drawImage(img, (int) x, (int) y, null);if(left) {x -= speed;}if(right) {x += speed;}if(up) {y -= speed;}if (down) {y +=speed;}}else {//死了} }public Plane(Image img, double x, double y) {this.img = img;this.x = x;this.y = y;this.speed = 3;this.width = img.getWidth(null);this.height = img.getHeight(null); }// 按下某個鍵,增加相應的方向 public void addDirection(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_LEFT:left = true;break;case KeyEvent.VK_UP:up = true;break;case KeyEvent.VK_RIGHT:right = true;break;case KeyEvent.VK_DOWN:down = true;break;} }// 按下某個鍵,取消相應的方向 public void minusDirection(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_LEFT:left = false;break;case KeyEvent.VK_UP:up = false;break;case KeyEvent.VK_RIGHT:right = false;break;case KeyEvent.VK_DOWN:down = false;break;} }}
**
用到的圖片以及源代碼:
**
鏈接:https://pan.baidu.com/s/11LusTxbsJj0ZAr2aPz-Reg
提取碼:l45p
總結
以上是生活随笔為你收集整理的Java零基础学习记录09(飞机躲避炮弹游戏实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 确定sw1开关信号输入端口_MEMS光学
- 下一篇: opencv之伪彩变换