Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Wichtig der Code hat keine HTML einbindung, weil ich ihn derzeit über khanacademy.org laufen lasse.
- var keys = [];
- keyPressed = function() {
- keys[keyCode] = true;
- };
- keyReleased = function() {
- keys[keyCode] = false;
- };
- var Ball = function(x,y,size,speed){
- this.x=x;
- this.y=y;
- this.xVel = Math.floor((Math.random() * 1) + 2 );
- this.yVel = Math.floor((Math.random() * 2) + 4 );
- this.size = size;
- this.speed = speed;
- this.update = function(){
- this.x = this.x + this.xVel;
- this.y = this.y - this.yVel;
- if(this.y < 10 || this.y > 390){
- this.yVel = -this.yVel;
- }
- fill(225,0,0);
- arc(this.x, this.y, this.size, this.speed, Math.PI*2, 365);
- };
- };
- var Player = function(x,y,size,speed){
- this.x=x;
- this.y=y;
- this.size = size;
- this.speed = speed;
- this.update = function(){
- fill(225,0,0);
- rect(this.x,this.y,this.size,this.speed);
- if(keyIsPressed && keys[UP]){
- this.y = this.y-5;
- if(this.y < 1){
- this.y = 1;
- }
- }else if(keyIsPressed && keys[DOWN]){
- this.y = this.y+5;
- if(this.y > 300){
- this.y = 300;
- }
- }
- };
- };
- var Player2 = function(x,y,size,speed){
- this.x=x;
- this.y=y;
- this.size = size;
- this.speed = speed;
- this.update = function(){
- fill(225,0,0);
- rect(this.x,this.y,this.size,this.speed);
- if(keyIsPressed && keys[87]){
- this.y = this.y-5;
- if(this.y < 1){
- this.y = 1;
- }
- }else if(keyIsPressed && keys[83]){
- this.y = this.y+5;
- if(this.y > 300){
- this.y = 300;
- }
- }
- };
- };
- var player = new Player(10,139,10,100);
- var player2 = new Player2(380,139,10,100);
- var ball = new Ball(200,Math.floor((Math.random() * 300) + 1),20,20);
- var cfc = function (player, ball){
- var distance = dist(this.player.x, this.player.y, this.ball.x, this.ball.y);
- if(distance <= this.player.size + this.ball.size/2){
- this.ball = -this.ball;
- }else{
- this.xVel = +this.xVel;
- }
- };
- draw = function(){
- background(46, 40, 46);
- player.update();
- player2.update();
- ball.update();
- cfc(player2, ball);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement