Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <canvas id="gc" width="640" height="480"></canvas>
- <script>
- var ymover= 0;
- var score = 0;
- var enemyscore = 0;
- var velocityx = 1;
- var velocityy = 1;
- var ballx = 320;
- var speedy = 0;
- var speedx= -5;
- var center = ymover -50;
- var hitboxtop = ymover+ 50;
- var hitboxbottom = ymover- 50;
- var bally = 240
- window.onload= function() {
- c=document.getElementById('gc');
- cc=c.getContext('2d');
- document.addEventListener("keydown",keyPush)
- setInterval(update,1000/30);
- };
- function keyPush (evt) {
- switch(evt.keyCode) {
- case 83:
- ymover += 10;
- break;
- case 87:
- ymover -= 10;
- break;
- }
- }
- function update() {
- cc.fillStyle='black';
- cc.fillRect(0,0,c.width,c.height);
- cc.fillStyle='red';
- cc.fillRect(20,ymover,10,100);
- cc.fillStyle ='white';
- cc.fillText(score, 320, 240, 500);
- cc.fillRect(ballx, bally, 20, 20,);
- center = ymover-50;
- // Starting the paddle Code
- if (ymover>480) {
- ymover= 0;
- }
- else if (ymover<0) {
- ymover = 480;
- }
- else {
- }
- // Starting the Ball Code
- if (ballx >640) {
- score++;
- ballx = 320;
- speedy = 0;
- bally = 240;
- speedx = -speedx;
- }
- else if (ballx<0) {
- enemyscore++;
- ballx = 320;
- speedy = 0;
- bally = 240;
- speedx = -speedx;
- }
- if (ballx> 0 && ballx <40 && bally>hitboxbottom && bally<hitboxtop) {
- speedx = -speedx;
- console.log('collision');
- if (bally> hitboxbottom && bally< ymover) {
- speedy -= Math.floor(Math.random() * 5) + 2;
- }
- else if (bally<hitboxtop && bally> ymover) {
- speedy -= Math.floor(Math.random() * 5) + 2;
- }
- }
- if (bally>470) {
- bally = 469;
- speedy = -speedy;
- }
- else if (bally<10) {
- bally = 11;
- speedy = -speedy;
- }
- ballx += speedx*velocityx;
- bally += speedy;
- hitboxbottom = center-50;
- hitboxtop = center+50;
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement