java实现窗口飞机游戏_java如何实现飞机游戏 java实现飞机游戏代码示例

工具类(用来获取图片对象):

package sc.wh.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 sc.wh.game;

public class Constants {

public static int WIDTH = 500;

public static int HEIGHT = 500;

}

所有游戏对象的父类:

package sc.wh.game;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Rectangle;

// 游戏父类

public class GameObject {

Image img; // 创建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 sc.wh.game;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyEvent;

// 飞机类,继承自游戏类,拥有游戏类对应的方法和属性

public class Plane extends GameObject{

// 当有键盘事件时,判断飞机移动的方向

boolean left,right,up,down;

// 飞机移动的速度

int speed = 5;

// 用于判断飞机是否存活

boolean live = true;

// 画飞机的方法,可根据键盘事件设置(left,right...)布尔值实时调整飞机位置

public void drawSelf(Graphics g) {

// 如果飞机存活,才调用画图方法

if(live) {

if(right) {

x += speed;

}

if(left) {

x -= speed;

}

if (up) {

y -= speed;

}

if(down) {

y += speed;

}

if (x<=0) {

x = 0;

}else if(x >= 460) {

x = 460;

}

if(y <= 40) {

y = 40;

}else if(y >= 470) {

y = 470;

}

// 根据位置画图

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

}

}

// 构造方法

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

this.img = img;

this.x = x;

this.y = y;

// 根据img对象的get..方法获取图片大小,用于矩形实现碰撞检测

this.width = img.getWidth(null);

this.height = img.getHeight(null);

}

// 键盘按下时,会调用此方法来设置移动的方向

public void addDirection(KeyEvent e) {

// getKeyCode可以获取按下键盘对应特定的值

switch (e.getKeyCode()) {

case KeyEvent.VK_LEFT:

left = true;

break;

case KeyEvent.VK_RIGHT:

right = true;

break;

case KeyEvent.VK_UP:

up = true;

break;

case KeyEvent.VK_DOWN:

down = true;

break;

}

}

// 键盘松开时,会调用此方法来设置取消移动的方向

public void minusDirection(KeyEvent e) {

// getKeyCode可以获取按下键盘对应特定的值

switch (e.getKeyCode()) {

case KeyEvent.VK_LEFT:

left = false;

break;

case KeyEvent.VK_RIGHT:

right = false;

break;

case KeyEvent.VK_UP:

up = false;

break;

case KeyEvent.VK_DOWN:

down = false;

break;

}

}

}

炮弹类:

package sc.wh.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); // 设置颜色

if(x <= 0|| x >= Constants.WIDTH-width-10) {

degree = Math.PI - degree; // 当碰撞水平地图边界后,反转角度

}

if(y<=40 || y >= Constants.HEIGHT-height) {

degree = -degree; // 当碰撞垂直地图后,反转角度

}

// 填充一个圆作为炮弹

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

// 根据角度设置炮弹移动的位置

x += speed*Math.cos(degree);

y += speed*Math.sin(degree);

g.setColor(c);

}

}

显示爆炸效果的类:

package sc.wh.game;

import java.awt.Graphics;

import java.awt.Image;

public class Explode {

// 记录爆炸位置

double x,y;

// 创建爆炸数组,static保证图片只加载一次

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;

}

}

游戏效果图

6f8ac7316e6ce768cc5f4a4748b196d0.png

ec5fb20a5b47fe300f698d82f9addab6.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值