Advertisement
miladinovic87

Untitled

Feb 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5.  
  6. import javax.swing.*;
  7.  
  8. public class Main {
  9.     private static int speed = 1;
  10.  
  11.     public static void gravity() {
  12.         speed = speed + 1; // ubrzava jedan piksel kroz jednu promenu
  13.     }
  14.  
  15.     public static void jump() {
  16.         speed = -14; // speed se stavlja negativni da ide u suprotnom smeru na kratko
  17.         // posto gravity povecava za jedan piksel preci ce iz negativnog u pozitivno
  18.         // posle 14 krugova
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         JFrame f = new JFrame();
  24.         f.setLayout(null);
  25.         f.setTitle("Birdy");
  26.         f.setDefaultCloseOperation(3);
  27.         f.setSize(800, 600);
  28.         f.setVisible(true);
  29.  
  30.         JLayeredPane background = new JLayeredPane();
  31.         background.setBounds(0, 0, 800, 600);
  32.         background.setBackground(new Color(255, 100, 200));
  33.         background.setOpaque(true);
  34.         background.setLayout(null);
  35.         background.setVisible(true);
  36.         f.add(background);
  37.  
  38.         JLabel bird = new JLabel();
  39.         bird.setBounds(100, 100, 50, 50);
  40.         bird.setBackground(new Color(25, 25, 25));
  41.         bird.setOpaque(true);
  42.         background.add(bird);
  43.        
  44.        
  45.         JLabel tube = new JLabel();
  46.  
  47.         f.addKeyListener(new KeyListener() {
  48.             public void keyPressed(KeyEvent e) {
  49.                 if (e.getKeyCode() == 32) {
  50.                     jump();
  51.                 }
  52.             }
  53.  
  54.             public void keyReleased(KeyEvent arg0) {
  55.             }
  56.  
  57.             public void keyTyped(KeyEvent arg0) {
  58.             }
  59.         });
  60.  
  61.         while (true) {
  62.             boolean start = true;
  63.             while (start == true) {
  64.  
  65.                 //set location and change gravity speed
  66.                 bird.setLocation(bird.getX(), bird.getY() + speed);
  67.                 gravity();
  68.                
  69.                 //tubes
  70.  
  71.                 // if it hits top or bottom
  72.                 if (bird.getY() < background.getY() || bird.getY() + bird.getHeight() > background.getHeight()) {
  73.                     System.out.println("OVER");
  74.                     speed = 1; // reset
  75.                     bird.setLocation(100, 100);
  76.                     break; // exit second while
  77.                 }
  78.                
  79.                 //if it hits tubes
  80.  
  81.                 try {
  82.                     Thread.sleep(16); // 1000/60 = ~16 (60fps)
  83.                 } catch (InterruptedException e) {
  84.                 }
  85.                 f.repaint();
  86.             }
  87.  
  88.         }
  89.  
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement