日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java小游戏飞机大战,java飞机大战小游戏

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java小游戏飞机大战,java飞机大战小游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

package cn.sxt.game;

import java.awt.*;

import java.awt.event.*;

import java.util.Date;

import javax.swing.JFrame;

import javax.xml.crypto.Data;

public class MyGameFrame extends JFrame {

//導入圖片

Image planeImg = GameUtil.getImage("images/plane.png");

Image bj = GameUtil.getImage("images/bg.jpg");

Plane plane = new Plane(planeImg,250,250);

//生成炮彈

Shell[] shells = new Shell[20];

//爆炸對象

Explode bao;

//起始時間對象

Date startTime = new Date();

Date endTime;

//計算時間

int period;

public void paint(Graphics g) {

//保存當前顏色

Color c = g.getColor();

g.drawImage(bj, 0, 0, null);

//飛機移動

plane.drawSelf(g);

//數組循環生成炮彈

for(int i=0;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.yellow);

//改變字體大小

Font f=new Font("宋體", Font.BOLD, 30);

g.setFont(f);

g.drawString("游戲時間:"+period+"秒", 200,300);

}

}

g.setColor(c);

}

//窗口重畫

class PaintThread extends Thread{

@Override

public void run() {

while(true) {

repaint();

try {

Thread.sleep(40);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

//鍵盤監聽

class KeyMonitor extends KeyAdapter{

@Override

public void keyPressed(KeyEvent e) {

plane.addDirection(e);

}

@Override

public void keyReleased(KeyEvent e) {

plane.minusDirection(e);

}

}

public void launchFrame() {

this.setTitle("飛機大戰");

//窗口設置為可見

this.setVisible(true);

this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);

this.setLocation(300,300);

this.addWindowListener(new WindowListener() {

@Override

public void windowOpened(WindowEvent arg0) {

}

@Override

public void windowIconified(WindowEvent arg0) {

}

@Override

public void windowDeiconified(WindowEvent arg0) {

}

@Override

public void windowDeactivated(WindowEvent arg0) {

}

@Override

public void windowClosing(WindowEvent arg0) {

System.exit(0);

}

@Override

public void windowClosed(WindowEvent arg0) {

}

@Override

public void windowActivated(WindowEvent arg0) {

}

});

new PaintThread().start();

addKeyListener(new KeyMonitor());

for(int i=0;i

shells[i]=new Shell();

}

}

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(500,500);

Graphics gOff = offScreenImage.getGraphics();

paint(gOff);

g.drawImage(offScreenImage, 0, 0, null);

}

}

package cn.sxt.game;

public class Constant {

public static final int GAME_WIDTH=570;

public static final int GAME_HEIGHT=600;

}

package cn.sxt.game;

import java.awt.Graphics;

import java.awt.Image;

public class Explode {

double x,y;

static Image[]imgs=new Image[16];

static {

for(int i=0;i<16;i++) {

imgs[i]= GameUtil.getImage("images/explode/e"+(i+1)+".gif");

imgs[i].getWidth(null);

}

}

int count;

public void draw(Graphics g) {

if(count<=15) {

g.drawImage(imgs[count], (int)x,(int) y, null);

count++;

}

}

public Explode(double x,double y) {

this.x=x;

this.y=y;

}

}

package cn.sxt.game;

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 void drawSelf(Graphics g) {

g.drawImage(img, (int)x, (int)y, null);

}

public GameObject(Image img, double x, double y, int speed, int width, int height) {

super();

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) {

super();

this.img = img;

this.x = x;

this.y = y;

}

public GameObject() {

}

public Rectangle getRect() {

return new Rectangle((int)x,(int)y,width,height);

}

}

package cn.sxt.game;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

public class GameUtil {

private GameUtil() {

}

public static Image getImage(String path) {

BufferedImage bi = null;

try {

URL u = GameUtil.class.getClassLoader().getResource(path);

bi = ImageIO.read(u);

}catch(IOException e) {

e.printStackTrace();

}

return bi;

}

}

package cn.sxt.game;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyEvent;

public class Plane extends GameObject{

boolean left,up,right,down,fire;

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;

}

}

}

public Plane(Image img,double x,double y) {

this.img=img;

this.x=x;

this.y=y;

this.speed=10;

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;

case KeyEvent.VK_W:

fire = 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;

}

}

private void drawfire(Graphics g) {

if(fire) {

Color c = g.getColor();

g.setColor(Color.red);

for(int i=(int)y;i

g.fillOval((int)x, (int)y, width, height);

}

g.getColor();

}

}

}

package cn.sxt.game;

import java.awt.Color;

import java.awt.Graphics;

//炮彈類

public class Shell extends GameObject{

double degree;

public Shell() {

x=200;

y=200;

width=10;

height=10;

speed=3;

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_WIDTH-width) {

degree=Math.PI-degree;

}

if(y<30||y>Constant.GAME_HEIGHT-height) {

degree=-degree;

}

g.getColor();

}

}

標簽:java,img,int,awt,大戰,小游戲,import,public

來源: https://blog.csdn.net/weixin_45675353/article/details/106615531

總結

以上是生活随笔為你收集整理的java小游戏飞机大战,java飞机大战小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。