Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 59 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Rectangle;
  5. import java.util.Random;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. public class Window extends JPanel {
  11.  
  12.     public int width, height;
  13.  
  14.     public Window(String title, int width, int height) {
  15.         this.width = width;
  16.         this.height = height;
  17.         JFrame frame = new JFrame(title);
  18.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         frame.setSize(width, height);
  20.         frame.setLocationRelativeTo(null);
  21.         frame.setResizable(false);
  22.         frame.setVisible(true);
  23.  
  24.         setPreferredSize(new Dimension(width, height));
  25.         frame.add(this);
  26.         frame.pack();
  27.  
  28.         // Add inputs
  29.         Mouse m = new Mouse();
  30.         addMouseListener(m);
  31.         addMouseMotionListener(m);
  32.         Keys k = new Keys();
  33.         frame.addKeyListener(k);
  34.        
  35.         init();
  36.     }
  37.  
  38.     Random r = new Random();
  39.     Rectangle player, enemy;
  40.    
  41.     public void init() {
  42.         player = new Rectangle(0,0, 50,50);
  43.         enemy = new Rectangle(width/2 - 100, height/2 - 100, 200,200);
  44.        
  45.     }
  46.    
  47.     public void paint(Graphics g) {
  48.         super.paint(g); //Clears the Screen
  49.         setBackground(Color.WHITE);
  50.        
  51.         if(Keys.up)
  52.             player.setLocation(player.x, --player.y);
  53.         if(Keys.down)
  54.             player.setLocation(player.x, ++player.y);
  55.         int d = 0;
  56.         d++;
  57.        
  58.        
  59.         for (d = 0; d <= 1; d++) {
  60.         g.setColor(Color.green);
  61.         g.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  62.             enemy.setLocation(--enemy.x, enemy.y);
  63.             if(enemy.x <= -200) {
  64.                 enemy = new Rectangle(width/2 - 100, height/2 - 100, 200,200);
  65.                 g.setColor(Color.green);
  66.                 g.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
  67.                     enemy.setLocation(--enemy.x, enemy.y);
  68.             }
  69.         }
  70.            
  71.            
  72.        
  73.        
  74.         g.setColor(Color.DARK_GRAY);
  75.         if(player.intersects(enemy))
  76.             g.setColor(Color.red);
  77.        
  78.         g.fillRect(player.x, player.y, player.width, player.height);
  79.        
  80.         try {
  81.             Thread.sleep(3);
  82.         } catch (InterruptedException e) {
  83.             e.printStackTrace();
  84.         }
  85.         repaint();
  86.     }
  87.  
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top