Guest User

Untitled

a guest
May 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.29 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class Racing extends JFrame {
  9.     //makes the screen size
  10.     final int WIDTH = 900, HEIGHT = 650;
  11.    
  12.     //keeps track of player speed
  13.     double plSpeed = .5, p2Speed = .5;
  14.    
  15.     //numbers that represent direction
  16.     final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, STOP = 5;
  17.    
  18.     //keeps track of player direction
  19.     int p1Direction = STOP;
  20.     int p2Direction = STOP;
  21.    
  22.     //create rectangles
  23.     Rectangle left = new Rectangle( 0, 0, WIDTH/9, HEIGHT );
  24.     Rectangle right = new Rectangle( ( WIDTH/9 )*8, 0, WIDTH/9, HEIGHT );
  25.     Rectangle top = new Rectangle ( 0, 0, WIDTH, HEIGHT/9 );
  26.     Rectangle bottom = new Rectangle( 0, ( HEIGHT/9 ) * 8, WIDTH, HEIGHT/9 );
  27.     Rectangle center = new Rectangle( (int) ((WIDTH/9) * 2.5), (int) ((HEIGHT/9) * 2.5), (int) ((WIDTH/9) * 5), (int) ((HEIGHT/9) * 4));
  28.    
  29.     //makes the obstacles
  30.     Rectangle obstacle = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 7), WIDTH/10, HEIGHT/9 );
  31.     Rectangle obstacle2 = new Rectangle ( WIDTH/3, (int) ((HEIGHT/9 ) * 5), WIDTH/10, HEIGHT/4 );
  32.     Rectangle obstacle3 = new Rectangle ( 2 * (WIDTH/3), (int) ((HEIGHT/9) * 5),WIDTH/10, HEIGHT/4 );
  33.     Rectangle obstacle4 = new Rectangle ( WIDTH/3, HEIGHT/9, WIDTH/30, HEIGHT/9 );
  34.     Rectangle obstacle5 = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 1.5), WIDTH/30, HEIGHT/4 );
  35.    
  36.     //makes the finish line
  37.     Rectangle finish = new Rectangle ( WIDTH/9, (HEIGHT/2)-HEIGHT/9, (int) ((WIDTH/9) * 1.5), HEIGHT/70 );
  38.    
  39.     //makes player 1's car
  40.     Rectangle p1 = new Rectangle ( WIDTH/9, HEIGHT/2, WIDTH/30, WIDTH/30 );
  41.    
  42.     //makes player 2's car
  43.     Rectangle p2 = new Rectangle ((( WIDTH/9 ) + ((int) ((WIDTH/9) * 1.5) /2)),(HEIGHT/2) + (HEIGHT/10), WIDTH/30, WIDTH/30);
  44.    
  45.     //constructor
  46.     public Racing() {
  47.         //define defaults for the JFrame
  48.         super ("Racing");
  49.         setSize( WIDTH, HEIGHT );
  50.         setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  51.         setBackground(Color.BLACK);
  52.        
  53.         //start the inner class
  54.         Move1 m1 = new Move1();
  55.         m1.start();
  56.         Move2 m2 = new Move2();
  57.         m2.start();
  58.     }
  59.    
  60.     //draws the cars and race track
  61.     public void paint(Graphics g) {
  62.        
  63.         //make borders green
  64.         g.setColor(Color.GREEN);
  65.        
  66.         //rectangles for start lines (lineO = outer player, lineI = inner player)
  67.         Rectangle lineO = new Rectangle( WIDTH/9, HEIGHT/2, (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
  68.         Rectangle lineI = new Rectangle( ((WIDTH/9)+((int) ((WIDTH/9) * 1.5)/2)), (HEIGHT/2) + (HEIGHT/10), (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
  69.        
  70.         //fill the rectangles
  71.         g.fillRect(left.x,left.y,left.width,left.height);
  72.         g.fillRect(right.x,right.y,right.width,right.height);
  73.         g.fillRect(top.x,top.y,top.width,top.height);
  74.         g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
  75.         g.fillRect(center.x,center.y,center.width,center.height);
  76.         g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
  77.         g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
  78.         g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
  79.         g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
  80.         g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
  81.        
  82.         //make the starting line color white
  83.         g.setColor(Color.WHITE);
  84.        
  85.         //draw starting line
  86.         g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
  87.         g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
  88.        
  89.         //make the finish line yellow
  90.         g.setColor(Color.YELLOW);
  91.        
  92.         //draw the finish line
  93.         g.fillRect(finish.x,finish.y,finish.width,finish.height);
  94.        
  95.         //make player one red
  96.         g.setColor(Color.RED);
  97.         //draw player 1
  98.         g.fill3DRect(p1.x,p1.y,p1.width,p2.height,true);
  99.        
  100.        
  101.         //make player two blue
  102.         g.setColor(Color.BLUE);
  103.         //now draw player two
  104.         g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
  105.     }
  106.     private class Move1 extends Thread implements KeyListener {
  107.         public void run() {
  108.             //makes the key listener "wake up"
  109.             addKeyListener(this);
  110.            
  111.             //should be done in an infinite loop, so it repeats
  112.             while (true) {
  113.                 //make try block, so it can exit if it errors
  114.                 try {
  115.                     //refresh screen
  116.                     repaint();
  117.                    
  118.                     //check to see if car hits outside wall
  119.                     //if so, slow down
  120.                     if (p1.intersects(left) || p1.intersects(right) ||
  121.                         p1.intersects(top) || p1.intersects(bottom) ||
  122.                         p1.intersects(obstacle) || p1.intersects(obstacle2) ||
  123.                         p1.intersects(obstacle3) || p1.intersects(obstacle4) ||
  124.                         p1.intersects(obstacle5) || p1.intersects (center) ||
  125.                         p1.intersects(p2)) {
  126.                             plSpeed = -6;
  127.                             Thread.sleep(128);
  128.                             p1Direction = STOP;
  129.                     }
  130.                    
  131.                     //lets the car stop
  132.                     if (plSpeed==0) {
  133.                         p1Direction = STOP;
  134.                     }
  135.                    
  136.                     //moves player based on direction
  137.                     if (p1Direction==UP) {
  138.                         p1.y -= (int) plSpeed;
  139.                     }
  140.                     if (p1Direction==DOWN) {
  141.                         p1.y += (int) plSpeed;
  142.                     }
  143.                     if (p1Direction==LEFT) {
  144.                         p1.x -= (int) plSpeed;
  145.                     }
  146.                     if (p1Direction==RIGHT) {
  147.                         p1.x += (int) plSpeed;
  148.                     }
  149.                     if (p1Direction==STOP) {
  150.                         plSpeed = 0;
  151.                     }
  152.                    
  153.                     //delays refresh rate
  154.                     Thread.sleep(75);
  155.                 }
  156.                 catch(Exception e) {
  157.                     //if an error, exit
  158.                     break;
  159.                 }
  160.             }
  161.         }
  162.        
  163.         //have to input these (so it will compile)
  164.         public void keyPressed(KeyEvent event) {
  165.             try {
  166.                 //makes car increase speed a bit
  167.                 if (event.getKeyChar()=='w' ||
  168.                     event.getKeyChar()=='a' ||
  169.                     event.getKeyChar()=='s' ||
  170.                     event.getKeyChar()=='d') {
  171.                         plSpeed += .2;
  172.                 }
  173.             } catch (Exception I) {
  174.                 plSpeed = plSpeed;
  175.             }
  176.         }
  177.         public void keyReleased(KeyEvent event) {}
  178.        
  179.         //now, to be able to set the direction
  180.         public void keyTyped(KeyEvent event) {
  181.             if (event.getKeyChar()=='a') {
  182.                 if (p1Direction==RIGHT) {
  183.                     p1Brake();
  184.                 } else {
  185.                     if (plSpeed < .3) {
  186.                         plSpeed = .1;
  187.                     } if (plSpeed > .2) {
  188.                         plSpeed = .3;
  189.                     }
  190.                     p1Direction = LEFT;
  191.                 }
  192.             }
  193.             if (event.getKeyChar()=='s') {
  194.                 if (p1Direction==UP) {
  195.                     p1Brake();
  196.                 } else {
  197.                     if (plSpeed < .3) {
  198.                         plSpeed = .1;
  199.                     } if (plSpeed > .2) {
  200.                         plSpeed = .3;
  201.                     }
  202.                     p1Direction = DOWN;
  203.                 }
  204.             }
  205.             if (event.getKeyChar()=='d') {
  206.                 if (p1Direction==LEFT) {
  207.                     p1Brake();
  208.                 } else {
  209.                     if (plSpeed < .3) {
  210.                         plSpeed = .5;
  211.                     } if (plSpeed > .2) {
  212.                         plSpeed = .5;
  213.                     }
  214.                     p1Direction = RIGHT;
  215.                 }
  216.             }
  217.             if (event.getKeyChar()=='w') {
  218.                 if (p1Direction==DOWN) {
  219.                     p1Brake();
  220.                 } else {
  221.                     if (plSpeed < .3) {
  222.                         plSpeed = .1;
  223.                     } if (plSpeed > .2) {
  224.                         plSpeed = .3;
  225.                     }
  226.                     p1Direction = UP;
  227.                 }
  228.             }
  229.             if (event.getKeyChar()=='z') {
  230.                 p1Brake();
  231.             }
  232.         }
  233.        
  234.         public void p1Brake () {
  235.             try {
  236.                 while (plSpeed != 0) {
  237.                     plSpeed -= .5;
  238.                     Thread.sleep(75);
  239.                 }
  240.             } catch (Exception e) {
  241.                 plSpeed = 0;
  242.             }
  243.         }
  244.     }
  245.     private class Move2 extends Thread implements KeyListener {
  246.         public void run() {
  247.             //makes the key listener "wake up"
  248.             addKeyListener(this);
  249.            
  250.             //should be done in an infinite loop, so it repeats
  251.             while (true) {
  252.                 //make try block, so it can exit if it errors
  253.                 try {
  254.                     //refresh screen
  255.                     repaint();
  256.                    
  257.                     //check to see if car hits outside wall
  258.                     //if so, slow down
  259.                     if (p2.intersects(left) || p2.intersects(right) ||
  260.                         p2.intersects(top) || p2.intersects(bottom) ||
  261.                         p2.intersects(obstacle) || p2.intersects(obstacle2) ||
  262.                         p2.intersects(obstacle3) || p2.intersects(obstacle4) ||
  263.                         p2.intersects(obstacle5) || p2.intersects(p1) ||
  264.                         p2.intersects(center)) {
  265.                             plSpeed = -5;
  266.                             Thread.sleep(128);
  267.                             p2Direction = STOP;
  268.                     }
  269.                    
  270.                     //makes car increase speed a bit
  271.                     if (p2Speed <= 5) {
  272.                         plSpeed += .2;
  273.                     }
  274.                    
  275.                     //lets the car stop
  276.                     if (p2Speed == 0) {
  277.                         p2Direction = STOP;
  278.                     }
  279.                    
  280.                     //moves player based on direction
  281.                     if (p2Direction==UP) {
  282.                         p2.y -= (int) p2Speed;
  283.                     }
  284.                     if (p2Direction==DOWN) {
  285.                         p2.y += (int) p2Speed;
  286.                     }
  287.                     if (p2Direction==LEFT) {
  288.                         p2.x -= (int) p2Speed;
  289.                     }
  290.                     if (p2Direction==RIGHT) {
  291.                         p2.x += (int) p2Speed;
  292.                     }
  293.                     if (p2Direction==STOP) {
  294.                         p2Speed = 0;
  295.                     }
  296.                    
  297.                     //delays refresh rate
  298.                     Thread.sleep(75);
  299.                 }
  300.                 catch(Exception e) {
  301.                     //if an error, exit
  302.                     break;
  303.                 }
  304.             }
  305.         }
  306.  
  307.         public void keyPressed(KeyEvent event) {
  308.             try {
  309.                 //makes car increase speed a bit
  310.                 if (event.getKeyChar()=='j' ||
  311.                     event.getKeyChar()=='k' ||
  312.                     event.getKeyChar()=='l' ||
  313.                     event.getKeyChar()=='i') {
  314.                         plSpeed += .1;
  315.                 }
  316.             } catch (Exception I) {
  317.                 p2Speed = p2Speed;
  318.             }
  319.         }
  320.         public void keyReleased(KeyEvent event) {}
  321.        
  322.         //now, to be able to set the direction
  323.         public void keyTyped(KeyEvent event) {
  324.             if (event.getKeyChar()=='j') {
  325.                 if (p2Direction==RIGHT) {
  326.                     p2Brake();
  327.                 } else {
  328.                     p2Direction = LEFT;
  329.                     if (p2Speed < .3) {
  330.                         p2Speed = .1;
  331.                     } if (p2Speed > .2){
  332.                         p2Speed = .3;
  333.                     }
  334.                 }
  335.             }
  336.             if (event.getKeyChar()=='k') {
  337.                 if (p2Direction==UP) {
  338.                     p2Brake();
  339.                 } else {
  340.                     p2Direction = DOWN;
  341.                     if (p2Speed < .3) {
  342.                         p2Speed = .1;
  343.                     } if (p2Speed > .2) {
  344.                         p2Speed = .1;
  345.                     }
  346.                 }
  347.             }
  348.             if (event.getKeyChar()=='l') {
  349.                 if (p2Direction==LEFT) {
  350.                     p2Brake();
  351.                 } else {
  352.                     p2Direction = RIGHT;
  353.                     if (p2Speed < .3) {
  354.                         p2Speed = .1;
  355.                     } if (p2Speed > .2) {
  356.                         p2Speed = .3;
  357.                     }
  358.                 }
  359.             }
  360.             if (event.getKeyChar()=='i') {
  361.                 if (p2Direction==DOWN) {
  362.                     p2Brake();
  363.                 } else {
  364.                     p2Direction = UP;
  365.                     if (p2Speed < .3) {
  366.                         p2Speed = .1;
  367.                     } if (p2Speed > .2) {
  368.                         p2Speed = .3;
  369.                     }
  370.                 }
  371.             }
  372.             if (event.getKeyChar()=='m') {
  373.                 p2Brake();
  374.             }
  375.         }
  376.        
  377.         public void p2Brake () {
  378.             try {
  379.                 while (p2Speed != 0) {
  380.                     p2Speed -= .5;
  381.                     Thread.sleep(75);
  382.                 }
  383.             } catch (Exception i) {
  384.                 p2Speed = 0;
  385.             }
  386.         }
  387.     }
  388.    
  389.     //finally, to start the program
  390.     public static void main(String[] args) {
  391.         Racing frame = new Racing();
  392.         frame.setVisible( true );
  393.         frame.setLocationRelativeTo( null );
  394.         frame.setResizable( false );
  395.     }
  396. }
Add Comment
Please, Sign In to add comment