大家好,欢迎来到IT知识分享网。
package 飞机小游戏; //如果觉得好的话,给一点积分哦,太痛苦了,平时下一点资料都得需要 import java.awt.Color; import java.awt.Font; 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; import javax.swing.JFrame;//解决了图片闪烁问题 import javax.xml.crypto.Data; import org.omg.CORBA.PRIVATE_MEMBER; //窗口类 //飞机小游戏主窗口 public class MyGameFrame extends JFrame {
Image bgImage=GameUtil.getImage("images/bg.jpg");//地球 Image pImage=GameUtil.getImage("images/plane.png");//飞机 Plane plane=new Plane(pImage,250,250);//飞机对象 //int pImageX=250; //int pImageY=250; //Shell shell=new Shell();//炮弹类 Shell shells[]=new Shell[50]; Explode bao;//爆炸对象 Date startTime=new Date();//开始时间 Date endTime;//结束时间 int chixu;//游戏持续时间 @Override public void paint(Graphics g) {
//自动被调用,g相当于画笔 Color color=g.getColor();//获取当前字体 /* // 画图形 super.paint(g); Color color=g.getColor(); Font font=g.getFont(); g.setColor(Color.BLUE);//画笔颜色 g.drawLine(100, 100, 300, 300);//线 g.drawRect(100, 100, 300, 300);//矩形 g.drawOval(100,100, 300, 300);//团员 g.fillRect(100, 100, 40, 40);//实心矩形 g.drawImage(ball, 0, 0, null);//画图片 g.setColor(Color.red); g.setFont(new Font("宋体",Font.BOLD,50));//字体 g.drawString("我在画图形", 200, 200);//文字 g.setColor(color); */ g.drawImage(bgImage, 0, 0, null);//画地球位置 /* g.drawImage(pImage, pImageX, pImageY, null);//画飞机位置 pImageX++;//飞机移动 */ plane.drawSelf(g);//画飞机 //shell.draw(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();//结束时间 chixu=(int)((endTime.getTime()-startTime.getTime())/1000);//持续时间 } bao.draw(g); } if(!plane.live)//计算时间,没有碰撞 {
g.setColor(Color.red);//字颜色颜色 Font font=new Font("宋体", Font.BOLD, 50); g.setFont(font);//改字体 g.drawString("持续时间是:"+chixu+"秒", (int)plane.x, (int)plane.y);//画持续时间 } } g.setColor(color);//回复原先字体 } //反复的重画窗口,实现移动 class PaintThread extends Thread {
@Override public void run() {
// TODO Auto-generated method stub super.run(); while(true) {
repaint();//重画 try {
Thread.sleep(40);//停止时间 } catch (InterruptedException e) {
e.printStackTrace(); } } } } //键盘监听的内部类 class KeMonitor extends KeyAdapter {
@Override public void keyReleased(KeyEvent e) {
//抬起那个键 super.keyTyped(e); //System.out.println("抬起"+e.getKeyCode());//获取抬起的那个键 plane.minusDirection(e); } @Override public void keyPressed(KeyEvent e) {
//按下那个键 super.keyPressed(e); //System.out.println("按下"+e.getKeyCode());//获取按下的那个键 plane.addDirection(e); } } //初始化窗口 public void launchFrame() {
this.setTitle("王硕作品");//创建 this.setVisible(true);//显示 this.setSize(Constant.GAME_WIDE, Constant.GAME_HEIGHT);//大小 this.setLocation(300, 200);//位置 this.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) {
//关闭时候操作 // TODO Auto-generated method stub System.exit(0);//结束虚拟 } }); new PaintThread().start();//启动线程 addKeyListener(new KeMonitor());//给窗口添加键盘监听 //初始化50个炮弹 for(int i=0;i<shells.length;i++) {
shells[i]=new Shell(); } } //入口 public static void main(String[] args) {
MyGameFrame f=new MyGameFrame(); f.launchFrame(); } //双缓冲解决闪烁问题,在内存里先画一个图片 private Image offScrImage=null; public void update(Graphics g) {
if(offScrImage==null) offScrImage=this.createImage(Constant.GAME_WIDE, Constant.GAME_HEIGHT); Graphics gOffGraphics=offScrImage.getGraphics(); paint(gOffGraphics); g.drawImage(offScrImage, 0, 0, null); } } package 飞机小游戏; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; //飞机类 public class Plane extends GameObject{
boolean shang,xia,zuo,you; int speed=10; boolean live=true; public void drawSelf(Graphics g) {
if(live)//判断碰撞 {
g.drawImage(img, (int)x, (int)y, null); if(zuo) x-=speed;//增加减少常量 if(you) x+=speed; if(shang) y-=speed; if(xia) y+=speed; } else {
} } 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: zuo=true; break; case KeyEvent.VK_UP: shang=true; break; case KeyEvent.VK_RIGHT: you=true; break; case KeyEvent.VK_DOWN: xia=true; break; } } //按下某个键,取消相应方向 public void minusDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT: zuo=false; break; case KeyEvent.VK_UP: shang=false; break; case KeyEvent.VK_RIGHT: you=false; break; case KeyEvent.VK_DOWN: xia=false; break; } } } package 飞机小游戏; import java.awt.Color; import java.awt.Graphics; //炮弹类 public class Shell extends GameObject {
double dergree;//随机角度 public Shell() {
x=200; y=200; width=10; height=10; speed=2;//移动速度 dergree=Math.random()*Math.PI;//随机角度[0~2pi] } public void draw(Graphics g) {
Color color=g.getColor(); g.setColor(Color.YELLOW);//点 g.fillOval((int)x, (int)y, width, height);//实心点 //沿任意角度去飞 x+=speed*Math.cos(dergree); y+=speed*Math.sin(dergree); if(x<0||x>Constant.GAME_WIDE-width) dergree=Math.PI-dergree; if(y<30||y>Constant.GAME_HEIGHT-height) dergree=-dergree; g.setColor(color); } } package 飞机小游戏; //工具类 //返回图像对象 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 url=GameUtil.class.getClassLoader().getResource(path); bi=ImageIO.read(url); } catch (IOException e) {
e.printStackTrace(); } return bi; } } package 飞机小游戏; //游戏物体的父类,解决物体的位置、边缘距离、图片、速度 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() {
super(); } //返回物体所在矩形,便于后续碰撞检测 public Rectangle getRect() {
return new Rectangle((int)x, (int)y,width,height); } } package 飞机小游戏; //爆炸类 import java.awt.FontFormatException; import java.awt.Graphics; import java.awt.Image; public class Explode {
double x,y; static Image images[]=new Image[16]; static {
for(int i=0;i<16;i++)//加载图形 {
images[i]=GameUtil.getImage("images/explode/e"+(i+1)+".gif"); images[i].getWidth(null); } } int count; public void draw(Graphics g) {
if(count<=15) {
g.drawImage(images[count], (int)x, (int)y, null);//画图形 count++; } } public Explode(double x, double y) {
super(); this.x = x; this.y = y; } } package 飞机小游戏; //常量类 public class Constant {
public static final int GAME_WIDE=570;//宽 public static final int GAME_HEIGHT=613;//高 }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/104148.html