Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. // Wichtig der Code hat keine HTML einbindung, weil ich ihn derzeit über khanacademy.org laufen lasse.
  2.  
  3.  
  4. var keys = [];
  5.  
  6.  
  7. keyPressed = function() {
  8. keys[keyCode] = true;
  9. };
  10.  
  11. keyReleased = function() {
  12. keys[keyCode] = false;
  13. };
  14.  
  15. var Ball = function(x,y,size,speed){
  16. this.x=x;
  17. this.y=y;
  18. this.xVel = Math.floor((Math.random() * 1) + 2 );
  19. this.yVel = Math.floor((Math.random() * 2) + 4 );
  20. this.size = size;
  21. this.speed = speed;
  22.  
  23. this.update = function(){
  24. this.x = this.x + this.xVel;
  25. this.y = this.y - this.yVel;
  26. if(this.y < 10 || this.y > 390){
  27. this.yVel = -this.yVel;
  28. }
  29.  
  30.  
  31.  
  32. fill(225,0,0);
  33. arc(this.x, this.y, this.size, this.speed, Math.PI*2, 365);
  34.  
  35.  
  36. };
  37. };
  38.  
  39. var Player = function(x,y,size,speed){
  40. this.x=x;
  41. this.y=y;
  42. this.size = size;
  43. this.speed = speed;
  44.  
  45.  
  46. this.update = function(){
  47.  
  48.  
  49. fill(225,0,0);
  50. rect(this.x,this.y,this.size,this.speed);
  51.  
  52.  
  53. if(keyIsPressed && keys[UP]){
  54. this.y = this.y-5;
  55. if(this.y < 1){
  56. this.y = 1;
  57. }
  58.  
  59. }else if(keyIsPressed && keys[DOWN]){
  60. this.y = this.y+5;
  61. if(this.y > 300){
  62. this.y = 300;
  63. }
  64.  
  65. }
  66. };
  67. };
  68.  
  69. var Player2 = function(x,y,size,speed){
  70. this.x=x;
  71. this.y=y;
  72. this.size = size;
  73. this.speed = speed;
  74.  
  75.  
  76. this.update = function(){
  77.  
  78.  
  79. fill(225,0,0);
  80. rect(this.x,this.y,this.size,this.speed);
  81.  
  82.  
  83. if(keyIsPressed && keys[87]){
  84. this.y = this.y-5;
  85. if(this.y < 1){
  86. this.y = 1;
  87. }
  88.  
  89. }else if(keyIsPressed && keys[83]){
  90. this.y = this.y+5;
  91. if(this.y > 300){
  92. this.y = 300;
  93. }
  94. }
  95. };
  96. };
  97. var player = new Player(10,139,10,100);
  98. var player2 = new Player2(380,139,10,100);
  99. var ball = new Ball(200,Math.floor((Math.random() * 300) + 1),20,20);
  100.  
  101.  
  102. var cfc = function (player, ball){
  103. var distance = dist(this.player.x, this.player.y, this.ball.x, this.ball.y);
  104. if(distance <= this.player.size + this.ball.size/2){
  105. this.ball = -this.ball;
  106. }else{
  107. this.xVel = +this.xVel;
  108. }
  109.  
  110.  
  111. };
  112. draw = function(){
  113. background(46, 40, 46);
  114. player.update();
  115. player2.update();
  116. ball.update();
  117. cfc(player2, ball);
  118.  
  119.  
  120. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement