Advertisement
Guest User

Untitled

a guest
Oct 18th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import javax.swing.JPanel;
  2. import javax.swing.Timer;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.geom.*;
  6. import java.awt.geom.Ellipse2D.Double;
  7.  
  8. public class tipka extends JPanel implements ActionListener, KeyListener {
  9.  
  10. Timer t = new Timer (5,this);
  11. double x=500,y=500,velX=3,velY=3, x1=0,x2=0, y2=0, velX1=3,velY1=3, velX2=3, velY2=3;
  12. boolean gameover = false;
  13. boolean gameover1=false;
  14. int counter = 100;
  15. int counter1=100;
  16.  
  17.  
  18. public tipka(){
  19.  
  20.  
  21. addKeyListener(this);
  22. setFocusable(true);
  23.  
  24. }
  25.  
  26. public void paintComponent (Graphics g){
  27.  
  28.  
  29. super.paintComponent(g);
  30. Graphics2D g2 = (Graphics2D) g;
  31. Ellipse2D circle = new Ellipse2D.Double(x,y,40,40);
  32. Rectangle2D pravokot = new Rectangle2D.Double(x1,0,100,50);
  33. Rectangle2D pravokot1= new Rectangle2D.Double (x2,920,100,50);
  34. g2.fill(pravokot1);
  35. g2.fill(pravokot);
  36. g2.fill(circle);
  37. g2.drawLine(0, 500, 1000, 500);
  38.  
  39. if(gameover) {
  40.  
  41. g2.setFont(new Font("Arial", Font.BOLD, 25));
  42. g2.drawString("Game over", 500, 500);
  43.  
  44. x=500;
  45. y=500;
  46.  
  47. velY=-velY;
  48.  
  49. }
  50.  
  51. if(gameover1) {
  52.  
  53.  
  54. g2.setFont(new Font("Arial", Font.BOLD, 25));
  55. g2.drawString("Game over", 500, 500);
  56.  
  57. x=500;
  58. y=500;
  59.  
  60. velY=+velY;
  61.  
  62. }
  63.  
  64. t.start();
  65.  
  66. }
  67.  
  68.  
  69. public void actionPerformed (ActionEvent e){
  70.  
  71.  
  72. if (gameover){
  73.  
  74. counter--;
  75.  
  76. if (counter ==0){
  77. gameover=false;
  78. velX=-velX;
  79. velY=-velY;
  80.  
  81. }
  82. }else{
  83.  
  84. counter=100;
  85. }
  86.  
  87.  
  88. if (gameover1){
  89.  
  90. counter1--;
  91.  
  92. if (counter1 ==0){
  93. gameover1=false;
  94. velX=-velX;
  95. velY=-velY;
  96.  
  97. }else{
  98. counter1=100;
  99. }
  100. }
  101.  
  102.  
  103. if (x1>900 || x1<0){
  104.  
  105. velX1=-velX1;
  106.  
  107. }
  108.  
  109. x1+=velX1;
  110. repaint();
  111. x+=velX;
  112. y+=velY;
  113. x2+=velX2;
  114.  
  115.  
  116.  
  117. if (x<0 || x>1000){
  118.  
  119. velX=-velX;
  120. }
  121.  
  122. if (y<0 || y>900){
  123.  
  124. velY=-velY;
  125. }
  126.  
  127. if (y<=50){
  128.  
  129. if(x<x1 || x>(x1+100))
  130.  
  131. gameover=true;
  132.  
  133.  
  134.  
  135. if(x>x1 && x<(x1+100))
  136. velY=-velY;
  137. }
  138.  
  139. if (y>=950){
  140.  
  141. if (x>x2 || x<(x2+100))
  142.  
  143.  
  144. gameover1=true;
  145.  
  146.  
  147. if (x>x2 && x>(x2+100))
  148. velY=-velY;
  149. }
  150.  
  151.  
  152. if (x2 <0 || x2>900){
  153.  
  154. velX2=-velX2;
  155. }
  156. }
  157.  
  158. public void left (){
  159.  
  160. velX1=-3;
  161.  
  162. }
  163.  
  164.  
  165. public void right (){
  166.  
  167. velX1=3;
  168.  
  169. }
  170.  
  171.  
  172.  
  173. public void keyPressed(KeyEvent e) {
  174.  
  175. int code = e.getKeyCode();
  176.  
  177. if (code == KeyEvent.VK_LEFT){
  178.  
  179. left();
  180. }
  181.  
  182. if (code == KeyEvent.VK_RIGHT){
  183.  
  184. right ();
  185. }
  186.  
  187. }
  188.  
  189. public void keyReleased(KeyEvent e) {}
  190. public void keyTyped(KeyEvent e) {}
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement