Advertisement
Guest User

ballgame

a guest
Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.event.*;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6.  
  7. public class Main extends JFrame implements KeyListener, Runnable, WindowListener{
  8.  
  9. JPanel panel;
  10. int bright = 1200;
  11. int bleft = 0;
  12. int btop = 40;
  13. int bbottom = 1025;
  14. double vx = 1.0;
  15. double vy = 1.0;
  16. int cx = 0;
  17. int cy = 0;
  18. int speed = 10;
  19. double angle = 15;
  20. boolean yeet = false;
  21.  
  22.  
  23. Main(){
  24. panel = new JPanel();
  25. this.setLayout(null);
  26. this.getContentPane().add(panel);
  27. this.setBounds(0, 0, 1250, 1250);
  28. this.setVisible(true);
  29. this.setResizable(false);
  30.  
  31. new Thread(this).start();
  32.  
  33. this.addKeyListener(this);
  34. this.addWindowListener(this);
  35. }
  36.  
  37. public void paint(Graphics g){
  38.  
  39. super.paint(g);
  40. g.setColor(Color.BLACK);
  41. g.drawOval(this.cx, this.cy, 50, 50);
  42. }
  43.  
  44. public static void main(String[] args) {
  45.  
  46. new Main();
  47.  
  48. }
  49.  
  50. @Override
  51. public void keyPressed(KeyEvent arg0) {
  52. // TODO Auto-generated method stub
  53. System.out.println(arg0.getKeyCode());
  54. if (arg0.getKeyCode() == 107){
  55. this.vx = this.vx * 1.05;
  56. this.vy = this.vy * 1.05;
  57. }
  58. else if(arg0.getKeyCode() == 109){
  59. this.vx = this.vx * 0.95;
  60. this.vy = this.vy * 0.95;
  61. }
  62. else if(arg0.getKeyCode() == 38){
  63. double tmpvx = this.vx;
  64. double tmpvy = this.vy;
  65. this.vx = Math.cos(Math.toRadians(angle)) * tmpvx - Math.sin(Math.toRadians(angle)) * tmpvy;
  66. this.vy = Math.sin(Math.toRadians(angle)) * tmpvx + Math.cos(Math.toRadians(angle)) * tmpvy;
  67. }
  68. else if(arg0.getKeyCode() == 40){
  69. double tmpvx = this.vx;
  70. double tmpvy = this.vy;
  71. this.vx = Math.cos(Math.toRadians(angle * (-1))) * tmpvx - Math.sin(Math.toRadians(angle * (-1))) * tmpvy;
  72. this.vy = Math.sin(Math.toRadians(angle * (-1))) * tmpvx + Math.cos(Math.toRadians(angle * (-1))) * tmpvy;
  73. }
  74.  
  75. }
  76.  
  77. public void moveX(){
  78. int distance = (int)(this.vx * this.speed);
  79. this.cx = this.cx + distance;
  80.  
  81. if(this.cx >= this.bright){
  82. this.vx = this.vx * (-1);
  83. yeet = true;
  84. }
  85.  
  86. if(this.cx >= 51){
  87. yeet = true;
  88. }
  89.  
  90. if((this.cx <= this.bleft) && yeet){
  91. this.vx = this.vx * (-1);
  92. System.out.println("echt");
  93. }
  94. }
  95.  
  96. public void moveY(){
  97. int distance = (int)(this.vy * this.speed);
  98. this.cy = this.cy + distance;
  99.  
  100. if(this.cy >= this.bbottom){
  101. this.vy = this.vy * (-1);
  102. yeet = true;
  103. }
  104.  
  105. if(this.cy >= 50){
  106. yeet = true;
  107. }
  108.  
  109. if((this.cy <= this.btop) && yeet){
  110. this.vy = this.vy * (-1);
  111. }
  112. }
  113.  
  114. @Override
  115. public void keyReleased(KeyEvent arg0) {
  116. // TODO Auto-generated method stub
  117.  
  118. }
  119.  
  120. @Override
  121. public void keyTyped(KeyEvent arg0) {
  122. // TODO Auto-generated method stub
  123.  
  124. }
  125.  
  126. @Override
  127. public void run() {
  128. // TODO Auto-generated method stub
  129. while(true){
  130. moveX();
  131. moveY();
  132.  
  133. try {
  134. Thread.sleep(50);
  135. } catch (InterruptedException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139.  
  140. this.repaint();
  141. }
  142. }
  143.  
  144. @Override
  145. public void windowActivated(WindowEvent arg0) {
  146. // TODO Auto-generated method stub
  147.  
  148. }
  149.  
  150. @Override
  151. public void windowClosed(WindowEvent arg0) {
  152. // TODO Auto-generated method stub
  153.  
  154. }
  155.  
  156. @Override
  157. public void windowClosing(WindowEvent arg0) {
  158. // TODO Auto-generated method stub
  159. System.exit(0);
  160. }
  161.  
  162. @Override
  163. public void windowDeactivated(WindowEvent arg0) {
  164. // TODO Auto-generated method stub
  165.  
  166. }
  167.  
  168. @Override
  169. public void windowDeiconified(WindowEvent arg0) {
  170. // TODO Auto-generated method stub
  171.  
  172. }
  173.  
  174. @Override
  175. public void windowIconified(WindowEvent arg0) {
  176. // TODO Auto-generated method stub
  177.  
  178. }
  179.  
  180. @Override
  181. public void windowOpened(WindowEvent arg0) {
  182. // TODO Auto-generated method stub
  183.  
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement