Advertisement
JackHoughton00

Fixed Second Window

Jun 13th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. firstWindow.setTitle("Block Dodger Game!");
  2.  
  3. Group root = new Group();
  4. Scene theScene = new Scene(root);
  5. firstWindow.setScene(theScene);
  6.  
  7. Canvas canvas = new Canvas(550,700);
  8. root.getChildren().add(canvas);
  9.  
  10. ArrayList<String> input = new ArrayList<String>();
  11.  
  12. theScene.setOnKeyPressed(
  13. new EventHandler<KeyEvent>()
  14. {
  15. public void handle(KeyEvent e)
  16. {
  17. String code = e.getCode().toString();
  18. if ( !input.contains(code) )
  19. input.add( code );
  20. }
  21. });
  22. theScene.setOnKeyReleased(
  23. new EventHandler<KeyEvent>()
  24. {
  25. public void handle(KeyEvent e)
  26. {
  27. String code = e.getCode().toString();
  28. input.remove( code );
  29. }
  30. });
  31.  
  32. GraphicsContext gc = canvas.getGraphicsContext2D();
  33.  
  34. Font theFont = Font.font( "Comic Sans MS", FontWeight.BOLD, 24 );
  35. gc.setFont( theFont );
  36. gc.setFill( Color.GREEN );
  37. gc.setStroke( Color.BLACK );
  38. gc.setLineWidth(1);
  39.  
  40. Sprite cursor = new Sprite();
  41. cursor.setImage("cursor1.png");
  42. cursor.setPosition(200, 0);
  43.  
  44. ArrayList<Sprite> triangleList = new ArrayList<Sprite>();
  45.  
  46. for (int i = 0; i < 15; i++)
  47. {
  48. Sprite triangle = new Sprite();
  49. triangle.setImage("triangle1.png");
  50. double px = 350 * Math.random() + 50;
  51. double py = 350 * Math.random() + 50;
  52. triangle.setPosition(px,py);
  53. triangleList.add( triangle );
  54. }
  55.  
  56. LongValue lastNanoTime = new LongValue( System.nanoTime() );
  57.  
  58. IntValue score = new IntValue(0);
  59.  
  60. new AnimationTimer()
  61. {
  62. public void handle(long currentNanoTime)
  63. {
  64. // calculate time since last update.
  65. double elapsedTime = (currentNanoTime - lastNanoTime.value) / 1000000000.0;
  66. lastNanoTime.value = currentNanoTime;
  67.  
  68. // game logic
  69.  
  70. cursor.setVelocity(0,0);
  71. if (input.contains("LEFT"))
  72. cursor.addVelocity(-50,0);
  73. if (input.contains("RIGHT"))
  74. cursor.addVelocity(50,0);
  75. if (input.contains("UP"))
  76. cursor.addVelocity(0,-50);
  77. if (input.contains("DOWN"))
  78. cursor.addVelocity(0,50);
  79.  
  80. cursor.update(elapsedTime);
  81.  
  82. // collision detection
  83.  
  84. Iterator<Sprite> triangleIter = triangleList.iterator();
  85. while ( triangleIter.hasNext() )
  86. {
  87. Sprite moneybag = triangleIter.next();
  88. if ( cursor.intersects(moneybag) )
  89. {
  90. triangleIter.remove();
  91. score.value++;
  92. }
  93. }
  94.  
  95. // render
  96.  
  97. gc.clearRect(0, 0, 512,512);
  98. cursor.render( gc );
  99.  
  100. for (Sprite moneybag : triangleList )
  101. moneybag.render( gc );
  102.  
  103. String pointsText = "Cash: $" + (100 * score.value);
  104. gc.fillText( pointsText, 360, 36 );
  105. gc.strokeText( pointsText, 360, 36 );
  106. }
  107. }.start();
  108.  
  109. firstWindow.show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement