Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.awt.geom.Rectangle2D;
  9.  
  10. import javax.swing.JFrame;
  11. import javax.swing.Timer;
  12.  
  13. public class Main extends JFrame implements ActionListener , KeyListener{
  14.  
  15. private static final long serialVersionUID = 3206847208968227199L;
  16.  
  17. public static int Score = 0;
  18. public static boolean run = true;
  19.  
  20. Timer tm = new Timer(5 , this);
  21.  
  22. double velx = 0, vely = 0 ;
  23.  
  24. int x = 0, y = 0;
  25.  
  26. public static void setScore(){
  27. do{
  28. Score = Score + 1 ;
  29. }while(run);
  30. }
  31.  
  32. public void up(){
  33. vely = -1.5;
  34. velx = 0;
  35. }
  36.  
  37. public void down(){
  38. vely = 1.5;
  39. velx = 0;
  40. }
  41.  
  42. public void left(){
  43. velx = -1.5;
  44. vely = 0;
  45. }
  46.  
  47. public void right(){
  48. velx = 1.5;
  49. vely = 0;
  50. }
  51.  
  52. @Override
  53. public void keyTyped(KeyEvent e) {
  54.  
  55. }
  56.  
  57. @Override
  58. public void keyPressed(KeyEvent e) {
  59. int code = e.getKeyCode();
  60.  
  61. if(code == KeyEvent.VK_UP){
  62.  
  63. if(y <= 20){
  64. y = 20;
  65. }else{
  66. up();
  67. }
  68. }
  69. if(code == KeyEvent.VK_DOWN){
  70.  
  71. if(y >= 400){
  72. y = 460;
  73. }else{
  74. down();
  75. }
  76. }
  77. if(code == KeyEvent.VK_LEFT){
  78.  
  79. if(x <= 0){
  80. x = 0;
  81. }else{
  82. left();
  83. }
  84. }
  85. if(code == KeyEvent.VK_RIGHT){
  86.  
  87. if(x >= 400){
  88. x = 460;
  89. }else{
  90. right();
  91. }
  92. }
  93.  
  94. }
  95.  
  96. @Override
  97. public void keyReleased(KeyEvent e) {
  98.  
  99. int code = e.getKeyCode();
  100.  
  101. if(code == KeyEvent.VK_UP){
  102. velx = 0;
  103. vely = -0.1;
  104. }
  105. if(code == KeyEvent.VK_DOWN){
  106. velx = 0;
  107. vely = 0.1;
  108. }
  109. if(code == KeyEvent.VK_LEFT){
  110. velx = -0.1;
  111. vely = 0;
  112. }
  113. if(code == KeyEvent.VK_RIGHT){
  114. velx = 0.1;
  115. vely = 0;
  116. }
  117. }
  118.  
  119.  
  120. public void paint(Graphics g){
  121.  
  122.  
  123. super.paintComponents(g);
  124. Graphics2D g2 = (Graphics2D) g;
  125. g2.setColor(Color.BLACK);
  126. g2.fill(new Rectangle2D.Double(x , y , 40 , 40));
  127. g2.drawString("Score : " + Score, 5 , 10 );
  128. tm.start();
  129.  
  130. }
  131.  
  132. public Main(){
  133. addKeyListener(this);
  134. pack();
  135. setTitle("Game");
  136. setSize(500, 500);
  137. setResizable(false);
  138. setVisible(true);
  139. setLocationRelativeTo(null);
  140. setDefaultCloseOperation(EXIT_ON_CLOSE);
  141. }
  142.  
  143. public static void main(String [] args) {
  144.  
  145. new Main();
  146.  
  147. }
  148.  
  149. @Override
  150. public void actionPerformed(ActionEvent arg0) {
  151. repaint();
  152. x += velx;
  153. y += vely;
  154.  
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement