java对键盘回车的侦听_关于键盘侦听器,请教下大佬们

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package JSD1806HYQ;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Graphics;

import java.util.Arrays;

import java.util.Date;

import java.util.Random;

import java.util.Timer;

import java.util.TimerTask;

import java.awt.image.BufferedImage;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

/** 世界 */

public class World extends JPanel implements KeyListener{

public static final int WIDTH = 800;

public static final int HEIGHT = 1000;

public static final int updistance=30;//java窗口中上边距为30个像素;

public static final int downdistance=8;//java窗口中下边距左右编剧为8个像素;

private static final long serialVersionUID = 1L;

private Boss zzlz = new Boss();

private Sky sky = new Sky();

private Hero hero = new Hero();

private FlyingObject[] enemies = {}; //敌人数组

private Bullet[] bullets = {}; //子弹数组

private DrBullet[] drbullets = {};

Date startTime;//界面开始的时间

Date startTime1;//游戏开始的时间

Date endTime;//飞机死亡的时间

Date nowTime;//现在的时间

Date collisionTime=new Date();

Date collisionTimeNow;

long timefirst=24625;

long now_startTime;//开始到现在的时间

int enterNum=0;

boolean bigbullet=true;

boolean boss=false;//判断是否是boss关

boolean flag = false;//表示游戏是否开始

public static BufferedImage start;

public static BufferedImage pause;

public static BufferedImage gameover;

static{

start = FlyingObject.loadImage("start.png");

pause = FlyingObject.loadImage("pause.png");

gameover = FlyingObject.loadImage("gameover.png");

}

public FlyingObject nextOne(){

Random rand = new Random();

int type = rand.nextInt(20);

if(type < 4){

return new Bee();

}else if(type < 12){

return new Airplane();

}else {

return new BigAirplane();

}

}

int flyEnterIndex = 0;

public void enterAction(){

flyEnterIndex++;

if(flyEnterIndex%40==0){

FlyingObject obj = nextOne();

enemies = Arrays.copyOf(enemies,enemies.length+1);

enemies[enemies.length-1] = obj;

}

}

public void stepAction(){

zzlz.step();

sky.step();

for(int i=0;i

enemies[i].step();

}

for(int i=0;i

bullets[i].step();

}

}

int shootIndex = 0;

public void shootAction(){

shootIndex++;

if(shootIndex%30==0){

Bullet[] bs = hero.shoot();

bullets = Arrays.copyOf(bullets,bullets.length+bs.length);

System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);

}

}

private int score = 0;

public void bangAction(){

for(int i=0;i

Bullet b = bullets[i];

for(int j=0;j

FlyingObject f = enemies[j];

if(b.isLife() && f.isLife() && b.hit(f)){

b.goDead();

f.goDead();

if(f instanceof Enemy){

Enemy e = (Enemy)f;

score += e.getScore();

}

if(f instanceof Award){

Award a = (Award)f;

int type = a.getAwardType();

switch(type){

case Award.DOUBLE_FIRE:

hero.addDoubleFire();

break;

case Award.LIFE:

hero.addLife();

break;

}

}

}

}

}

}

public void outOfBoundsAction(){

int index = 0;

FlyingObject[] enemyLives = new FlyingObject[enemies.length];

for(int i=0;i

FlyingObject e = enemies[i];

if(!e.outOfBounds() && !e.isRemove()){

enemyLives[index++] = e;

}

}

enemies = Arrays.copyOf(enemyLives,index);

index = 0;

Bullet[] bulletLives = new Bullet[bullets.length];

for(int i=0;i

Bullet b = bullets[i];

if(!b.outOfBounds() && !b.isRemove()){

bulletLives[index++] = b;

}

}

bullets = Arrays.copyOf(bulletLives,index);

}

public void hitAction(){

for(int i=0;i

FlyingObject f = enemies[i];

if(f.isLife() && hero.hit(f)){

f.goDead();

hero.subtractLife();

hero.clearDoubleFire();

break;

}

}

}

public void checkGameOverAction(){

if(hero.getLife()<=0){

flag = false;

}

}

int num = 1;

public void action(){ //启动执行

Timer timer = new Timer();

int intervel = 10;

timer.schedule(new TimerTask(){

public void run(){

if(flag==true){

enterAction();

stepAction();

shootAction();

outOfBoundsAction();

bangAction();

hitAction();

checkGameOverAction();

}

repaint();

}

},intervel,intervel);

}

public void paint(Graphics g){

if(now_startTime>1000){

System.out.println(now_startTime);

boss=true;

zzlz.paintObject(g);

}

sky.paintObject(g);

hero.paintObject(g);

for(int i=0;i

enemies[i].paintObject(g);

}

for(int i=0;i

bullets[i].paintObject(g);

}

for(int i=0;i

drbullets[i].paintObject(g);

}

g.drawString("SCORE: "+score,10,25);

g.drawString("LIFE: "+hero.getLife(),10,45);

}

public static void main(String[] args) {

JFrame frame = new JFrame();

World world = new World();

frame.add(world);

frame.addKeyListener(world);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(WIDTH, HEIGHT);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

world.action();

}

@Override

public void keyTyped(KeyEvent e) {

}

@Override

public void keyPressed(KeyEvent e) {

hero.addDirection(e);

if(e.getKeyCode()==KeyEvent.VK_ENTER){

flag = true;

}

}

@Override

public void keyReleased(KeyEvent e) {

hero.minusDirection(e);

if(e.getKeyCode()==KeyEvent.VK_ENTER){

enterNum++;

if(enterNum==1){

startTime=new Date();//游戏开始的时间

System.out.println(startTime);

}

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值