Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- firstWindow.setTitle("Block Dodger Game!");
- Group root = new Group();
- Scene theScene = new Scene(root);
- firstWindow.setScene(theScene);
- Canvas canvas = new Canvas(550,700);
- root.getChildren().add(canvas);
- ArrayList<String> input = new ArrayList<String>();
- theScene.setOnKeyPressed(
- new EventHandler<KeyEvent>()
- {
- public void handle(KeyEvent e)
- {
- String code = e.getCode().toString();
- if ( !input.contains(code) )
- input.add( code );
- }
- });
- theScene.setOnKeyReleased(
- new EventHandler<KeyEvent>()
- {
- public void handle(KeyEvent e)
- {
- String code = e.getCode().toString();
- input.remove( code );
- }
- });
- GraphicsContext gc = canvas.getGraphicsContext2D();
- Font theFont = Font.font( "Comic Sans MS", FontWeight.BOLD, 24 );
- gc.setFont( theFont );
- gc.setFill( Color.GREEN );
- gc.setStroke( Color.BLACK );
- gc.setLineWidth(1);
- Sprite cursor = new Sprite();
- cursor.setImage("cursor1.png");
- cursor.setPosition(200, 0);
- ArrayList<Sprite> triangleList = new ArrayList<Sprite>();
- for (int i = 0; i < 15; i++)
- {
- Sprite triangle = new Sprite();
- triangle.setImage("triangle1.png");
- double px = 350 * Math.random() + 50;
- double py = 350 * Math.random() + 50;
- triangle.setPosition(px,py);
- triangleList.add( triangle );
- }
- LongValue lastNanoTime = new LongValue( System.nanoTime() );
- IntValue score = new IntValue(0);
- new AnimationTimer()
- {
- public void handle(long currentNanoTime)
- {
- // calculate time since last update.
- double elapsedTime = (currentNanoTime - lastNanoTime.value) / 1000000000.0;
- lastNanoTime.value = currentNanoTime;
- // game logic
- cursor.setVelocity(0,0);
- if (input.contains("LEFT"))
- cursor.addVelocity(-50,0);
- if (input.contains("RIGHT"))
- cursor.addVelocity(50,0);
- if (input.contains("UP"))
- cursor.addVelocity(0,-50);
- if (input.contains("DOWN"))
- cursor.addVelocity(0,50);
- cursor.update(elapsedTime);
- // collision detection
- Iterator<Sprite> triangleIter = triangleList.iterator();
- while ( triangleIter.hasNext() )
- {
- Sprite moneybag = triangleIter.next();
- if ( cursor.intersects(moneybag) )
- {
- triangleIter.remove();
- score.value++;
- }
- }
- // render
- gc.clearRect(0, 0, 512,512);
- cursor.render( gc );
- for (Sprite moneybag : triangleList )
- moneybag.render( gc );
- String pointsText = "Cash: $" + (100 * score.value);
- gc.fillText( pointsText, 360, 36 );
- gc.strokeText( pointsText, 360, 36 );
- }
- }.start();
- firstWindow.show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement