Guest User

Untitled

a guest
Feb 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. package pkg3dgraphics;
  2.  
  3. import javafx.scene.canvas.GraphicsContext;
  4. import javafx.scene.paint.Color;
  5.  
  6.  
  7. public class GameObject {
  8. protected String name;
  9. protected int xCoord,yCoord,mySizeX = 25,mySizeY = 25;
  10. public GameObject(){
  11.  
  12. }
  13. public GameObject(String newName, int startXCoord, int startYCoord){
  14. setName(newName);
  15. setX(startXCoord);
  16. setY(startYCoord);
  17. }
  18. public void SetSize(int X, int Y){
  19. mySizeX = X;
  20. mySizeY = Y;
  21. }
  22. public int getXsize(){
  23. return mySizeX;
  24. }
  25. public int getYsize(){
  26. return mySizeY;
  27. }
  28. public String getName(){
  29. return name;
  30. }
  31. public void setName(String newName){
  32. name = newName;
  33. }
  34. public int getX(){
  35. return xCoord;
  36. }
  37. public int getY(){
  38. return yCoord;
  39. }
  40. public void setX(int newXCoord){
  41. xCoord = newXCoord;
  42. }
  43.  
  44. public void setY(int newYCoord){
  45. yCoord = newYCoord;
  46. }
  47. public void moveTo(int X,int Y){
  48. xCoord = X;
  49. yCoord = Y;
  50. }
  51. public void moveBy(int X,int Y){
  52. xCoord +=X;
  53. yCoord +=Y;
  54. }
  55. public void Draw(GraphicsContext currentContext){
  56. currentContext.setFill(Color.GREEN);
  57. currentContext.setStroke(Color.BLUE);
  58. currentContext.fillOval(xCoord,yCoord, mySizeX,mySizeY );
  59.  
  60. }
  61. }
  62.  
  63. package pkg3dgraphics;
  64.  
  65.  
  66. public class Shooter extends GameObject {
  67. public Shooter(){
  68.  
  69. }
  70. public Shooter(String newName, int startXCoord, int startYCoord){
  71. setName(newName);
  72. setX(startXCoord);
  73. setY(startYCoord);
  74. }
  75. public Bullet shoot(String direction){
  76. Bullet newBullet = new Bullet(direction);
  77. return newBullet;
  78. }
  79.  
  80. }
  81.  
  82. package pkg3dgraphics;
  83.  
  84. import javafx.scene.paint.Color;
  85. import javafx.scene.canvas.GraphicsContext;
  86.  
  87. public class Bullet extends GameObject{
  88. String myDirection;
  89. protected int mySpeed = 5,mySizeX = 5,mySizeY = 5;
  90. boolean goNorth,goSouth,goWest,goEast;
  91. protected boolean active = false;
  92. public void setSpeed(int newSpeed){
  93. mySpeed = newSpeed;
  94. }
  95. public void setDirection(String newDirection){
  96. myDirection = newDirection;
  97. active = true;
  98. if ( myDirection == "North" )
  99. goNorth = true;
  100. if ( myDirection == "South" )
  101. goSouth = true;
  102. if ( myDirection == "West" )
  103. goWest = true;
  104. if ( myDirection == "East" )
  105. goEast = true;
  106. }
  107. Bullet(String direction ){
  108. myDirection = direction;
  109. active = true;
  110. if ( myDirection == "North" )
  111. goNorth = true;
  112. if ( myDirection == "South" )
  113. goSouth = true;
  114. if ( myDirection == "West" )
  115. goWest = true;
  116. if ( myDirection == "East" )
  117. goEast = true;
  118. }
  119.  
  120. public void advance(int W,int H,GraphicsContext gc){
  121. if (xCoord <= W && xCoord >= 0 && yCoord <= H && yCoord >= 0 ) {
  122. if (goNorth) moveBy(0,mySpeed);
  123. if (goSouth) moveBy(0,mySpeed);
  124. if (goWest) moveBy(mySpeed,0);
  125. if (goEast) moveBy(mySpeed,0);
  126. }else{
  127. active = false;
  128. Vanish(gc);
  129. goNorth = false;
  130. goSouth = false;
  131. goWest = false;
  132. goEast = false;
  133. }
  134. }
  135. public void Vanish(GraphicsContext gc){
  136. gc.setFill(Color.WHITE);
  137. }
  138. }
  139.  
  140. package pkg3dgraphics;
  141.  
  142. import java.util.Vector;
  143. import javafx.animation.AnimationTimer;
  144. import javafx.application.Application;
  145. import static javafx.application.Application.launch;
  146. import javafx.event.EventHandler;
  147. import javafx.scene.Group;
  148. import javafx.scene.Scene;
  149. import javafx.scene.canvas.Canvas;
  150. import javafx.scene.canvas.GraphicsContext;
  151. import static javafx.scene.input.KeyCode.DOWN;
  152. import static javafx.scene.input.KeyCode.LEFT;
  153. import static javafx.scene.input.KeyCode.RIGHT;
  154. import static javafx.scene.input.KeyCode.SHIFT;
  155. import static javafx.scene.input.KeyCode.UP;
  156. import javafx.scene.input.KeyEvent;
  157. import javafx.stage.Stage;
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. public class Main extends Application {
  165. public static void main(String[] args) {
  166. launch(args);
  167. }
  168.  
  169.  
  170.  
  171. public int WINDOWWIDTH = 600;
  172. public int WINDOWHEIGHT = 400;
  173. boolean goNorth,goSouth,goWest,goEast,running,ShootNorth,ShootSouth,ShootWest,ShootEast;
  174. Shooter player = new Shooter("Player",WINDOWWIDTH/2,WINDOWHEIGHT/2);
  175. Bullet bullets[] = new Bullet[500];
  176. int bulletsSize = 0;
  177. Canvas mainCanvas = new Canvas(WINDOWWIDTH,WINDOWHEIGHT);
  178. GraphicsContext mainContext = mainCanvas.getGraphicsContext2D();
  179. @Override
  180. public void start(Stage primaryStage) {
  181. Group root = new Group();
  182. Canvas mainCanvas = new Canvas(WINDOWWIDTH,WINDOWHEIGHT);
  183. GraphicsContext mainContext = mainCanvas.getGraphicsContext2D();
  184. root.getChildren().add(mainCanvas);
  185.  
  186. Scene scene = new Scene(root,WINDOWWIDTH,WINDOWHEIGHT);
  187. scene.setOnKeyPressed(new EventHandler<KeyEvent> (){
  188. @Override
  189. public void handle(KeyEvent event){
  190. switch(event.getCode()){
  191. case UP: ShootNorth = true; break;
  192. case DOWN: ShootSouth = true; break;
  193. case LEFT: ShootWest = true; break;
  194. case RIGHT: ShootEast = true; break;
  195. case W: goNorth = true; break;
  196. case S: goSouth = true; break;
  197. case A: goWest = true; break;
  198. case D: goEast = true; break;
  199. }
  200. }
  201. });
  202. scene.setOnKeyReleased(new EventHandler <KeyEvent>(){
  203. @Override
  204. public void handle(KeyEvent event){
  205. switch(event.getCode()){
  206. case UP: ShootNorth = false; break;
  207. case DOWN: ShootSouth = false; break;
  208. case LEFT: ShootWest = false; break;
  209. case RIGHT: ShootEast = false; break;
  210. case W: goNorth = false; break;
  211. case S: goSouth = false; break;
  212. case A: goWest = false; break;
  213. case D: goEast = false; break;
  214.  
  215. }
  216. }
  217. });
  218. primaryStage.setScene(scene);
  219. primaryStage.show();
  220. AnimationTimer Timer = new AnimationTimer(){
  221. @Override
  222. public void handle (long now){
  223. int dx = 0, dy = 0;
  224. if (goNorth) dy = -1;
  225. if (goSouth) dy = 1;
  226. if (goWest) dx = -1;
  227. if (goEast) dx = 1;
  228. if (running) { dx *= 3; dy *= 3;}
  229. mainContext.clearRect(0,0,WINDOWWIDTH,WINDOWHEIGHT);
  230. player.moveBy(dx, dy);
  231. CheckShoot();
  232. player.Draw(mainContext);
  233.  
  234. }
  235.  
  236.  
  237. };
  238. Timer.start();
  239.  
  240.  
  241. }
  242.  
  243. public void CheckShoot(){
  244. String direction = null;
  245. int count = 0;
  246. if (ShootNorth)
  247. {
  248. direction = "North";
  249. }
  250. if (ShootSouth)
  251. {
  252. direction = "South";
  253. }
  254. if (ShootWest)
  255. {
  256. direction = "West";
  257. }
  258. if (ShootEast)
  259. {
  260. direction = "East";
  261. }
  262. for (int i = 0; i < bulletsSize; i ++ )
  263. {
  264. if (bullets[i].active = false ){
  265. bullets[i].setDirection(direction);
  266. bullets[i].moveTo(player.getX(),player.getY());
  267. break;
  268. }else count ++;
  269. }
  270. if ( count == bulletsSize ) {
  271. bulletsSize++;
  272. bullets[bulletsSize] = player.shoot(direction);
  273. }
  274.  
  275. }
  276.  
  277. public void advanceAll(){
  278. for (int i = 0; i < bulletsSize; i ++ )
  279. {
  280. bullets[i].advance(WINDOWWIDTH,WINDOWHEIGHT,mainContext);
  281. }
  282. }
  283.  
  284. }
  285.  
  286. Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
  287. at pkg3dgraphics.Main.CheckShoot(Main.java:126)
  288. at pkg3dgraphics.Main$3.handle(Main.java:91)
  289. at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$483(AnimationTimer.java:57)
  290. at java.security.AccessController.doPrivileged(Native Method)
  291. at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
  292. at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
  293. at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
  294. at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
  295. at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
  296. at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
  297. at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  298. at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  299. at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
  300. at java.lang.Thread.run(Thread.java:748)
Add Comment
Please, Sign In to add comment