Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.Random;
- public class KeyBidings extends JFrame {
- int x = 0;
- int y = 0;
- int hits = 0;
- int y2 = 0;
- int x2 = 1000-40;
- int score = 0;
- int score1 = 0;
- int timer = 0;
- String scoreString1 = Integer.toString(score);
- String scoreString2 = Integer.toString(score1);
- int ovalX = 475;
- int ovalY = 225;
- int ovalSpeedY = -50;
- int ovalSpeedX = -50;
- DrawPanel drawPanel = new DrawPanel();
- public KeyBidings(){
- Action upAction = new AbstractAction(){
- public void actionPerformed(ActionEvent e) {
- y2 -=10;
- }
- };
- Action downAction = new AbstractAction(){
- public void actionPerformed(ActionEvent e) {
- y2 +=10;
- }
- };
- Action wAction = new AbstractAction(){
- public void actionPerformed(ActionEvent e) {
- y -=10;
- }
- };
- Action sAction = new AbstractAction(){
- public void actionPerformed(ActionEvent e) {
- y +=10;
- }
- };
- drawPanel.repaint();
- InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
- ActionMap actionMap = drawPanel.getActionMap();
- inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
- actionMap.put("downAction", downAction);
- inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
- actionMap.put("upAction", upAction);
- inputMap.put(KeyStroke.getKeyStroke("S"), "sAction");
- actionMap.put("sAction", sAction);
- inputMap.put(KeyStroke.getKeyStroke("W"), "wAction");
- actionMap.put("wAction", wAction);
- add(drawPanel);
- pack();
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setVisible(true);
- }
- private class DrawPanel extends JPanel {
- protected void paintComponent(Graphics g) {
- Random randy = new Random();
- int upperbound =(getWidth()/10) + 1;
- int changer = 0 - (getWidth()/10);
- int changer2 = 0 + (getWidth()/10);
- int randomVar = randy.nextInt(upperbound);
- super.paintComponent(g);
- if ((ovalX)/100 < getWidth()-40 && (ovalX+4000)/100 > getWidth()-40 && ovalY/100 < y2+250 && ovalY/100 > y2) {
- ovalSpeedX = changer2*((hits+5)/5);
- ovalSpeedY = randomVar-changer2;
- randomVar = randy.nextInt(upperbound);
- hits += 1;
- }
- if ((ovalX)/100 > x && (ovalX-400)/100 < x+40 && ovalY/100 < y+250 && ovalY/100 > y) {
- ovalSpeedX = changer*((hits+5)/5);
- ovalSpeedY = randomVar-changer2;
- randomVar = randy.nextInt(upperbound);
- hits += 1;
- }
- if ((ovalY+4000)/100 > getHeight()) {
- ovalSpeedY = (ovalSpeedY-ovalSpeedY-ovalSpeedY);
- }
- if ((ovalY-250)/100 < 0) {
- ovalSpeedY = (ovalSpeedY-ovalSpeedY-ovalSpeedY);
- }
- if ((ovalX+250)/100 > getWidth()){
- score += 1;
- scoreString1 = Integer.toString(score);
- ovalY = (getHeight()/2)*100;
- ovalX = (getWidth()/2)*100;
- hits = 0;
- ovalSpeedX = changer2*((hits+5)/5);
- }
- if ((ovalX+4000)/100<0) {
- score1 += 1;
- scoreString2 = Integer.toString(score1);
- ovalY = (getHeight()/2)*100;
- ovalX = (getWidth()/2)*100;
- hits = 0;
- ovalSpeedX = changer*((hits+5)/5);
- }
- g.setColor(Color.BLACK);
- ovalX = ovalX - ovalSpeedX;
- ovalY = ovalY - ovalSpeedY;
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.WHITE);
- g.fillRect(x, y, 40, 250);
- g.fillRect(getWidth()-40, y2, 40, 250);
- drawPanel.repaint();
- g.fillOval(ovalX/100, ovalY/100, 50, 50);
- g.drawString(scoreString1, 200, 50);
- g.drawString(scoreString2, getWidth()-200, 50);
- }
- public Dimension getPreferredSize() {
- return new Dimension(1000, 500);
- }
- }
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable(){
- public void run(){
- new KeyBidings();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement