欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

FXGL学习2

最编程 2024-07-23 21:38:50
...
public class MainPlay extends GameApplication { protected Entity shooter; protected Entity normalizedZomba; protected Entity bullet; int z_hp=12;//僵尸血条 int s_hp=10;//植物血条 @Override protected void initSettings(GameSettings gameSettings) { gameSettings.setWidth(680); gameSettings.setHeight(480); gameSettings.setVersion("1.0v"); gameSettings.setTitle("植物练习射击僵尸"); } @Override protected void initGame() { getGameWorld().addEntityFactory(new PlantFactory()); shooter=spawn("shooter"); normalizedZomba=spawn("zomba"); normalizedZomba.setPosition(600,100); generate(); } private void generate(){ bullet=spawn("pea"); bullet.setPosition(shooter.getX()+10, shooter.getY()); } @Override protected void initInput() { Input input=getInput(); input.addEventHandler(MouseEvent.MOUSE_CLICKED,event -> { shooter.setPosition(event.getX()-40, event.getY()-20); }); } @Override protected void initPhysics() { getPhysicsWorld().addCollisionHandler(new CollisionHandler(PlantEntity.PLANT,PlantEntity.ZOMBA) { @Override protected void onCollisionBegin(Entity plant, Entity zomba) { s_hp--; if (s_hp<=0){ shooter.removeFromWorld(); } } }); getPhysicsWorld().addCollisionHandler(new CollisionHandler(PlantEntity.BULLET,PlantEntity.ZOMBA) { @Override protected void onCollision(Entity bullet, Entity zomba) { z_hp--; if (z_hp<=0){ zomba.removeFromWorld(); } bullet.removeFromWorld();//开始碰撞后移除bullet } }); } @Override protected void onUpdate(double tpf) { normalizedZomba.translate(new Vec2(-tpf*10,0));//僵尸移动 bullet.translate(new Vec2(6,0));//子弹发射 if (bullet.getX()>getAppWidth()){ bullet.removeFromWorld(); generate(); } } public static void main(String[] args) { launch(args); } }