Advertisement
Guest User

Throwntogether

a guest
Dec 21st, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import javafx.animation.AnimationTimer;
  2. import javafx.application.Application;
  3. import javafx.event.EventHandler;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.image.Image;
  7. import javafx.scene.image.ImageView;
  8. import javafx.scene.input.KeyCode;
  9. import javafx.scene.input.KeyEvent;
  10. import javafx.scene.paint.Color;
  11. import javafx.stage.Stage;
  12. import javafx.stage.StageStyle;
  13. import java.awt.Dimension;
  14. import java.awt.Toolkit;
  15.  
  16.  
  17. public class Constructor extends Application{
  18.    
  19.     Image player, shot;
  20.     static Dimension screen = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
  21.     static int wid = screen.width;
  22.     static int hei = screen.height;
  23.     static boolean up, down, left, right, rotleft , rotright;
  24.     static double x = (wid/2)-109;
  25.     static double y = (hei/1.5)-132;
  26.     static double velx = 0, vely = 0, velx2 = 0, vely2 = 0;
  27.     static double forspeed = 0, sidespeed = 0;
  28.     static int rotat = 0;
  29.    
  30.     public void load(){
  31.         player = new Image("res/sprite.png");
  32.     }
  33.  
  34.     @Override
  35.     public void start(final Stage frame) throws Exception{
  36.        
  37.         load();
  38.        
  39.         frame.setTitle("DEFAULT");
  40.         frame.initStyle(StageStyle.UNDECORATED);
  41.        
  42.         Group root = new Group();      
  43.         final ImageView ship = new ImageView();
  44.         ship.setImage(player); 
  45.         root.getChildren().add(ship);
  46.        
  47.         frame.setScene(new Scene(root,  wid,  hei, Color.BLACK));
  48.        
  49.         frame.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>(){
  50.             public void handle(KeyEvent key) {
  51.                   if(key.getCode()==KeyCode.W)
  52.                        up = true;
  53.                   if(key.getCode()==KeyCode.S)
  54.                        down = true;
  55.                   if(key.getCode()==KeyCode.Q)
  56.                        left = true;
  57.                   if(key.getCode()==KeyCode.E)
  58.                        right = true;
  59.                   if(key.getCode()==KeyCode.A)
  60.                        rotleft = true;
  61.                   if(key.getCode()==KeyCode.D)
  62.                        rotright = true;
  63.                  
  64.             }
  65.         });
  66.         frame.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>(){
  67.             public void handle(KeyEvent key) {
  68.                   if(key.getCode()==KeyCode.ESCAPE)
  69.                   {
  70.                       frame.close();
  71.                       System.exit(0);  
  72.                   }
  73.                   if(key.getCode()==KeyCode.W)
  74.                        up = false;
  75.                   if(key.getCode()==KeyCode.S)
  76.                        down = false;
  77.                   if(key.getCode()==KeyCode.Q)
  78.                        left = false;
  79.                   if(key.getCode()==KeyCode.E)
  80.                        right = false;
  81.                   if(key.getCode()==KeyCode.A)
  82.                        rotleft = false;
  83.                   if(key.getCode()==KeyCode.D)
  84.                        rotright = false;
  85.                  
  86.             }
  87.         });
  88.         frame.setAlwaysOnTop(true);
  89.         frame.setHeight(hei);
  90.         frame.setWidth(wid);
  91.         frame.setResizable(false);
  92.         frame.setFullScreen(true);
  93.         frame.show();
  94.        
  95.          new AnimationTimer() {
  96.                 @Override
  97.                 public void handle(long now) {
  98.                    
  99.                     gameloop();
  100.                     ship.setTranslateX(x);
  101.                     ship.setTranslateY(y);
  102.                     ship.setRotate(rotat);
  103.                 }
  104.             }.start();
  105.     }
  106.    
  107.         public static void gameloop(){
  108.         if(Shared.up)
  109.             forspeed += 1;
  110.         if(Shared.down)
  111.             forspeed -= 1;
  112.         if(Shared.right)
  113.             sidespeed += 1;
  114.         if(Shared.left)
  115.             sidespeed -= 1;
  116.        
  117.         if(Shared.rotleft)
  118.             rotat -=3;
  119.         if(Shared.rotright)
  120.             rotat +=3;
  121.        
  122.         velx = Math.cos(Math.toRadians(rotat-90))*forspeed + Math.cos(Math.toRadians(rotat))*sidespeed;
  123.         vely = Math.sin(Math.toRadians(rotat-90))*forspeed + Math.sin(Math.toRadians(rotat))*sidespeed;
  124.        
  125.         if(!Shared.up && !Shared.down)
  126.         {
  127.             if(forspeed > 0)
  128.                 forspeed -= 0.2;
  129.             else if (forspeed < 0)
  130.                 forspeed += 0.2;
  131.         }
  132.         if(!Shared.right && !Shared.left)
  133.         {
  134.             if(sidespeed > 0)
  135.                 sidespeed -= 0.2;
  136.             else if (sidespeed < 0)
  137.                 sidespeed += 0.2;
  138.         }
  139.        
  140.         x += velx;
  141.         y += vely;
  142.        
  143.         screencolisions();
  144.     }
  145.  
  146.     private static void screencolisions() {
  147.         // LEFT RIGHT
  148.         if(x < 0)
  149.         {
  150.             x = 0;
  151.             sidespeed = 0;
  152.         }
  153.         else if (x+218 > Shared.wid)
  154.         {
  155.             x = Shared.wid-218;
  156.             sidespeed = 0;
  157.         }
  158.        
  159.         // UP DOWN
  160.         if(y < 0)
  161.         {
  162.             y = 0;
  163.             forspeed = 0;
  164.         }
  165.         else if (y+164 > Shared.hei)
  166.         {
  167.             y = Shared.hei-164;
  168.             forspeed = 0;
  169.         }
  170.        
  171.     }
  172.    
  173.     public static void main(String[] args){
  174.         Application.launch(args);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement