打飞机源代码0

计应143赵先亮作

package com.ct.www;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

//用来游戏界面和逻辑处理

public class ShootGame extends JPanel {
private Hero hero = new Hero();
private Bullet[] bullets = new Bullet[0];//存放子弹
private FiyingObject[] enemys = {};//存放敌机和小蜜蜂
private int fs = 0;
public static BufferedImage background;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage gameover;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage pause;
public static BufferedImage start;

static {
try {
background = ImageIO.read(ShootGame.class.getResource("background.png"));
airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));
bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
JFrame p = new JFrame();
p.setSize(400, 852);
p.setDefaultCloseOperation(3);
p.setLocationRelativeTo(null);
ShootGame game = new ShootGame();
p.add(game);
p.setVisible(true);
// 开始游戏
game.action();
}

public void paint(Graphics g) {

g.drawImage(background, 0, 0, null);
paintHero(g);
paintBullet(g);
PaintEnemy(g);
painfenshu(g);//方法

}
private void painfenshu(Graphics g) {
g.drawString(fs+""+"分数", 20, 30);

}
private int enterIndex = 0;
private void PaintEnemy(Graphics g) {
for ( int i=0; i<enemys.length;i++){
FiyingObject obj = enemys[i];
g.drawImage(obj.image, obj.x, obj.y, null);
}
// TODO Auto-generated method stub

}

private void paintBullet(Graphics g) {
for(int i=0;i<bullets.length;i++){
Bullet bullet = bullets[i];
g.drawImage(bullet.image, bullet.x, bullet.y, null);
}
// TODO Auto-generated method stub

}

private void paintHero(Graphics g) {// 飞机
g.drawImage(hero.image, hero.x, hero.y, null);

}

public void action() {

MouseAdapter l = new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
// 就是鼠标当前的坐标
int x = e.getX();
int y = e.getY();
hero.moveto(x, y);
repaint();
}

};
// 添加 鼠标 移动 监听
this.addMouseMotionListener(l);
Timer timer = new Timer();
TimerTask task = new TimerTask(){
public void run(){
MoveAction();
shootAction();
enteAction();
repaint();
bangAction();
}
private void bangAction() {
//子弹是否集中
for(int i=0;i<bullets.length;i++){
Bullet bullet = bullets[i];
bang(bullet);
}

}

private void bang(Bullet bullet) {
//子弹是否集中
for(int i=0;i<enemys.length;i++){
FiyingObject obj = enemys[i];
if(obj.shootBy(bullet)){
enemys[i]=enemys[enemys.length-1];
enemys = Arrays.copyOf(enemys, enemys.length-1);
fs+=10;
}
}

}
private int enterIndex = 0;
private void enteAction() {
enterIndex++;
//敌人入场
if(enterIndex%40==0){
FiyingObject obj = getRandomEnemy();
enemys = Arrays.copyOf(enemys, enemys.length+1);
enemys[enemys.length-1]= obj;
}
}

private FiyingObject getRandomEnemy() {
Random random = new Random();
int i =random.nextInt(20);
// switch(i/10)
// {
// case 1:return new AirPlane();
// case 0:return new Bee();
// 
// }
// return null;
if(i<=4){
return new Bee();

}else{
return new AirPlane();
}
}


};
timer.schedule(task,500,10);
}

private void MoveAction() {
//子弹移动
for(int i=0;i<bullets.length;i++){
Bullet bullet = bullets[i];
bullet.move();
}
//控制敌人移动
for(int i=0;i<enemys.length;i++){
FiyingObject obj = enemys[i];
obj.move();
}
hero.move();

}
int shootIndex = 0;

protected void shootAction() {
shootIndex++;
if(shootIndex %10 == 0){
Bullet[] bs = hero.shoot();
// Bullet[] temp=Arrays.copyOf(bs,bs.length);
bullets=Arrays.copyOf(bullets, bullets.length+bs.length);
//src:被复制的数组
//
//吧bs额元素复制到bullet
System.arraycopy(bs, 0, bullets, bullets.length-bs.length, bs.length);
// TODO Auto-generated method stub
}
}
}

package com.ct.www;

import java.awt.image.BufferedImage;

public abstract class FiyingObject {
protected int x;//横坐标
protected int y;//纵坐标
protected int width;//宽
protected int height;//高
protected BufferedImage image;//对应的图片

public abstract void move();
public boolean shootBy(Bullet bullet) {
int x = bullet.x;
int y = bullet.y;
if(x>this.x&&x<this.x+width&&y>this.y&&y<this.y+height){
return true;
}
return false;

}
}

package com.ct.www;

import java.util.Random;

public class AirPlane extends FiyingObject{
public AirPlane(){
image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
x = new Random().nextInt(400-image.getWidth());
y = 0;
}
public void move(){
y += 1;
}
}

package com.ct.www;

import java.util.Random;

public class Bee extends FiyingObject {
public Bee(){
image = ShootGame.bee;
Random random = new Random();
width = image.getWidth();
height = image.getHeight();
x = new Random().nextInt(400-image.getWidth());
y = 0;
}
int xMove = 1;
public void move(){
x+=xMove;
y+=1;
// boolean flag = false;
if(x>=400-image.getWidth()){
xMove = -1;

}else if(x<=0){
xMove = 1;
}

}

}

package com.ct.www;

public class Bullet extends FiyingObject {
public Bullet (int x, int y){
this.x = x;
this.y = y;
this.image = ShootGame.bullet;
}

public void move() {
// TODO Auto-generated method stub
y-=9;
}

}

转载于:https://www.cnblogs.com/zxlizsm/p/5401614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值