c语言sscanf函数的用法是什么
283
2022-11-04
基于Java制作一个好玩的打飞机游戏
目录1.效果图2.项目整体构造3.主类代码展示4.飞机类代码展示5.炮弹类代码展示6.爆炸类代码展示
1.效果图
2.项目整体构造
3.主类代码展示
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);
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.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.shttp://leep(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[i] = new Shell(); } } 4.飞机类代码展示 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; //y = y-speed; } if(down){ y += speed; } }else{ } } public Plane(Image img, double x, double y){ this.img = img; this.x = x; this.y = y; this.speed = 3; 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; } } 5.炮弹类代码展示 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; } 6.爆炸类代码展示 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; }
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.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.shttp://leep(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[i] = new Shell(); } } 4.飞机类代码展示 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; //y = y-speed; } if(down){ y += speed; } }else{ } } public Plane(Image img, double x, double y){ this.img = img; this.x = x; this.y = y; this.speed = 3; 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; } } 5.炮弹类代码展示 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; } 6.爆炸类代码展示 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; }
shells[i] = new Shell();
}
}
4.飞机类代码展示
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; //y = y-speed;
}
if(down){
y += speed;
}
}else{
}
}
public Plane(Image img, double x, double y){
this.img = img;
this.x = x;
this.y = y;
this.speed = 3;
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;
}
}
5.炮弹类代码展示
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;
}
6.爆炸类代码展示
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;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~