Advertisement
Liambro64

Untitled

Nov 24th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.Random;
  5. public class KeyBidings extends JFrame {
  6.     int x = 0;
  7.     int y = 0;
  8.     int hits = 0;
  9.     int y2 = 0;
  10.     int x2 = 1000-40;
  11.     int score = 0;
  12.     int score1 = 0;
  13.     int timer = 0;
  14.     String scoreString1 = Integer.toString(score);
  15.     String scoreString2 = Integer.toString(score1);
  16.     int ovalX = 475;
  17.     int ovalY = 225;
  18.     int ovalSpeedY = -50;
  19.     int ovalSpeedX = -50;
  20.     DrawPanel drawPanel = new DrawPanel();
  21.  
  22.     public KeyBidings(){
  23.         Action upAction = new AbstractAction(){
  24.             public void actionPerformed(ActionEvent e) {
  25.                 y2 -=10;
  26.             }
  27.         };
  28.     Action downAction = new AbstractAction(){
  29.         public void actionPerformed(ActionEvent e) {
  30.         y2 +=10;
  31.         }
  32.        
  33.     };
  34.         Action wAction = new AbstractAction(){
  35.             public void actionPerformed(ActionEvent e) {
  36.                 y -=10;
  37.             }
  38.         };
  39.     Action sAction = new AbstractAction(){
  40.         public void actionPerformed(ActionEvent e) {
  41.         y +=10;
  42.         }
  43.        
  44.     };
  45.     drawPanel.repaint();
  46.             InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
  47.             ActionMap actionMap = drawPanel.getActionMap();
  48.  
  49.         inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
  50.         actionMap.put("downAction", downAction);
  51.         inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
  52.         actionMap.put("upAction", upAction);
  53.  
  54.         inputMap.put(KeyStroke.getKeyStroke("S"), "sAction");
  55.         actionMap.put("sAction", sAction);
  56.         inputMap.put(KeyStroke.getKeyStroke("W"), "wAction");
  57.         actionMap.put("wAction", wAction);
  58.  
  59.         add(drawPanel);
  60.  
  61.         pack();
  62.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  63.         setLocationRelativeTo(null);
  64.         setVisible(true);
  65.     }
  66.  
  67.     private class DrawPanel extends JPanel {
  68.  
  69.  
  70.         protected void paintComponent(Graphics g) {
  71.             Random randy = new Random();
  72.         int upperbound =(getWidth()/10) + 1;
  73.         int changer = 0 - (getWidth()/10);
  74.         int changer2 = 0 + (getWidth()/10);
  75.             int randomVar = randy.nextInt(upperbound);
  76.             super.paintComponent(g);
  77.         if ((ovalX)/100 < getWidth()-40 && (ovalX+4000)/100 > getWidth()-40 && ovalY/100 < y2+250 && ovalY/100 > y2) {
  78.         ovalSpeedX = changer2*((hits+5)/5);
  79.         ovalSpeedY = randomVar-changer2;
  80.         randomVar = randy.nextInt(upperbound);
  81.         hits += 1;
  82.         }
  83.         if ((ovalX)/100 > x && (ovalX-400)/100 < x+40 && ovalY/100 < y+250 && ovalY/100 > y) {
  84.         ovalSpeedX = changer*((hits+5)/5);
  85.         ovalSpeedY = randomVar-changer2;
  86.         randomVar = randy.nextInt(upperbound);
  87.         hits += 1;
  88.         }
  89.         if ((ovalY+4000)/100 > getHeight()) {
  90.         ovalSpeedY = (ovalSpeedY-ovalSpeedY-ovalSpeedY);
  91.         }
  92.         if ((ovalY-250)/100 < 0) {
  93.         ovalSpeedY = (ovalSpeedY-ovalSpeedY-ovalSpeedY);
  94.         }
  95.          if ((ovalX+250)/100 > getWidth()){
  96.         score += 1;
  97.         scoreString1 = Integer.toString(score);
  98.         ovalY = (getHeight()/2)*100;
  99.         ovalX = (getWidth()/2)*100;
  100.         hits = 0;
  101.         ovalSpeedX = changer2*((hits+5)/5);
  102.         }
  103.         if ((ovalX+4000)/100<0) {
  104.         score1 += 1;
  105.         scoreString2 = Integer.toString(score1);
  106.         ovalY = (getHeight()/2)*100;
  107.         ovalX = (getWidth()/2)*100;
  108.         hits = 0;
  109.         ovalSpeedX = changer*((hits+5)/5);
  110.         }
  111.             g.setColor(Color.BLACK);
  112.         ovalX = ovalX - ovalSpeedX;
  113.         ovalY = ovalY - ovalSpeedY;
  114.             g.fillRect(0, 0, getWidth(), getHeight());
  115.             g.setColor(Color.WHITE);
  116.             g.fillRect(x, y, 40, 250);
  117.             g.fillRect(getWidth()-40, y2, 40, 250);
  118.         drawPanel.repaint();
  119.         g.fillOval(ovalX/100, ovalY/100, 50, 50);
  120.         g.drawString(scoreString1, 200, 50);
  121.         g.drawString(scoreString2, getWidth()-200, 50);
  122.        
  123.         }
  124.  
  125.         public Dimension getPreferredSize() {
  126.             return new Dimension(1000, 500);
  127.         }
  128.     }
  129.  
  130.     public static void main(String[] args) {
  131.         EventQueue.invokeLater(new Runnable(){
  132.             public void run(){
  133.                 new KeyBidings();
  134.             }
  135.         });
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement