package cn.sxt.game;
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
*Frame�Ǵ���
*/
public class MyGameFrame extends Frame {
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
Plane plane = new Plane(planeImg,250,250);//���÷ɻ��ʼλ��
Shell[] shells = new Shell[50];//�ڵ�
Explode bao ;
Date startTime = new Date();
Date endTime;
int period; //��Ϸ�����ʱ��
@Override
public void paint(Graphics g) { //�Զ������á� g�൱��һֻ����
Color c = g.getColor();
g.drawImage(bg, 0, 0, null);
/**
* g.drawLine(100,100,300,300)����
* g.drawRect()����
* g.drawOval()Բ
* g.fillRect()ʵ�ľ���
* g.setColor()��ɫ
* g.drawString()�ַ�
*
*/
plane.drawSelf(g); //���ɻ�
//�������е��ڵ�
for(int i=0;i<shells.length;i++){
shells[i].draw(g);//�������е��ڵ�
//�ɻ���ڵ�����ײ��⣡����turn ������
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 {
@Override
public void run() {
//������
while(true){
repaint(); //�ػ�
try {
Thread.sleep(40); //1s=1000ms
} 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 WindowAdapter() {
//�����ڲ��࣬��д
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start(); //�����ػ����ڵ��߳�
addKeyListener(new KeyMonitor()); //������Ӽ��̵ļ���
//��ʼ��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 offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//������Ϸ���ڵĿ�Ⱥ߶�
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
最近下载更多
最近浏览更多
3334004690 LV11
2024年6月22日
ClydeSon LV5
2023年12月27日
微信网友_6790966844739584 LV5
2023年12月27日
2871581741 LV1
2023年12月22日
bangyiyang LV2
2023年12月21日
412836806
2023年10月13日
暂无贡献等级
yangxb2 LV10
2023年7月11日
2017143155 LV12
2023年6月26日
啊伟大伟大
2023年6月18日
暂无贡献等级
微信网友_6508798513811456 LV5
2023年6月7日

