[java]代码库
package taiyangxi;
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.util.ArrayList;
import java.util.Date;
import javax.print.attribute.standard.PrinterInfo;
import cn.bjsxt.solar.MyFrame;
import cn.bjsxt.solar.ganeutil;
//飞机游戏的主窗口
public class solarframe extends MyFrame {
Image bg= ganeutil.getImage("tupian/123.jpg");
plane p=new plane("tupian/5.jpg",50,50);
ArrayList bulletList =new ArrayList();
Date starttime;
Date endtime;
explode bao;
//画图片
public void paint(Graphics g) {
g.drawImage(bg, 0, 0, null);
p.draw(g);
for(int i=0;i<bulletList.size();i++) {
Bullet b=(Bullet)bulletList.get(i);
b.drew(g);
//检测子弹与飞机的碰撞
boolean peng=b.getRect().intersects(p.getRect());
if(peng) {
p.setLive(false);//飞机死掉
endtime=new Date();
//爆炸一次
if(bao==null)
{bao=new explode(p.x, p.y);}
bao.draw(g);
break;
}
}
if(!p.isLive()) {
int period =(int)((endtime.getTime()-starttime.getTime())/1000);
PrinterInfo(g,"时间:"+period+"秒",20,120,260,Color.white);
switch (period/5) {
case 0:
case 1:
PrinterInfo(g, "菜鸟",50, 100, 200, Color.white);
break;
case 2:
PrinterInfo(g, "小鸟",50, 100, 200, Color.white);
break;
case 3:
PrinterInfo(g, "大鸟",50, 100, 200, Color.white);
break;
case 4:
PrinterInfo(g, "鸟王",50, 100, 200, Color.yellow);
break;
default:
PrinterInfo(g, "鸟祖",50, 100, 200, Color.blue);
break;
}
}
PrinterInfo(g, "分数:100", 10, 50, 50, Color.yellow);
}
//在窗口上打印信息
private void PrinterInfo(Graphics g,String str,int size,int x,int y,Color color) {
// TODO Auto-generated method stub
Color c=g.getColor();
g.setColor(color);
Font f=new Font("宋体", Font.BOLD,size);
g.setFont(f);
g.drawString(str, x,y);
g.setColor(c);
}
//启动窗口
public static void main(String[] args) {
new solarframe().launchFrame();
}
public void launchFrame() {
//直接调用父类的方法
super.launchFrame();
//键盘监听
addKeyListener(new KeyMonitor());
//生成一堆子弹
for(int i=0;i<20;i++) {
Bullet b=new Bullet();
bulletList.add(b);
}
starttime =new Date();
}
//定义为内部类,可以使用外部类的普通属性
//飞机的移动方向
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
//显示按键上下左右 System.out.println("按下"+e.getKeyCode());
p.addDirection(e);
}
//@Override
public void keyReleased(KeyEvent e) {
p.minusDirection(e);
}
}
}
[代码运行效果截图]
初级程序员
by: 云代码会员 发表于:2018-08-13 08:59:19 顶(2) | 踩(2) 回复
好,好好,好好,好
网友回复
回复无言独雨 : 完整嘛?
顶(0) 踩(0) 2021-03-05 08:34:31
回复评论