-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature fun #2
base: development
Are you sure you want to change the base?
Feature fun #2
Changes from 6 commits
1b13fdb
cac9e33
10e8eb5
1167937
6856319
54d6158
246baef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,30 @@ app.height = window.innerHeight | |
document.body.scrollTop = 0; // <-- pull the page back up to the top | ||
document.body.style.overflow = 'hidden'; // <-- relevant addition | ||
|
||
var net = new Net('net',app,'orange') | ||
|
||
var PlayerOne = new Player('player-one',app,'blue',1) | ||
var PlayerTwo = new Player('player-two',app,'red',2) | ||
|
||
var ball = new Ball('ball',app,'black') | ||
var roundBall = new RoundBall('roundball',app,'white') | ||
|
||
var player1Score = new Text('score-two',(app.width/4) * 3, app.height / 4,50,"0",app) | ||
var player2Score = new Text('score-one',app.width / 4, app.height / 4,50,"0",app) | ||
var startText = new Text('start',app.width/2, app.height - 50, 50,"Press 'Enter' to Start",app) | ||
var pauseText = new Text('pause',app.width/2, app.height - 50,50,"",app) | ||
var pauseInst = new Text('pauseInst',app.width/2, app.height - 25,25,"",app) | ||
var mainText = 50, secondText = 25; | ||
var player1Score = new Text('score-two',(app.width/4) * 3, app.height / 4, mainText*1.5,"0",app) | ||
var player2Score = new Text('score-one',app.width / 4, app.height / 4, mainText*1.5,"0",app) | ||
var startText = new Text('start',app.width/2 + (mainText/2), app.height - 50, mainText,"Press 'Enter' to Start",app) | ||
var pauseText = new Text('pause',app.width/2 + (mainText/2), app.height - 50, mainText,"",app) | ||
var pauseInst = new Text('pauseInst',app.width/2 + (secondText/2), app.height - 25, secondText,"",app) | ||
|
||
var state = 'START' | ||
|
||
/** | ||
* Add Game Music Level1.wav | ||
* Created by https://juhanijunkala.com/ | ||
*/ | ||
var music = new Sound('sounds/level1.wav', true) | ||
|
||
|
||
// Init | ||
app.onInit = function(){ | ||
|
||
document.addEventListener('keydown', (event) => { | ||
|
@@ -51,13 +62,16 @@ app.onInit = function(){ | |
PlayerTwo.moveDown(false) | ||
|
||
if(keyName == 'Enter'){ | ||
if(state == 'START') | ||
if(state == 'START'){ | ||
music.play() // Start Game Music | ||
state = 'GAME' | ||
} | ||
} | ||
|
||
if(keyName == ' '){ | ||
if(state == 'GAME' || state == 'PAUSE') | ||
if(state == 'GAME' || state == 'PAUSE'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a typo and the game breaks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know how to use enum? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see, I haven´t use enum before but I can change it and not use strings |
||
this.pause() | ||
} | ||
} | ||
|
||
if(keyName == 'r'){ | ||
|
@@ -87,11 +101,13 @@ app.onUpdate = function(time){ | |
|
||
app.pause = function(){ | ||
if(state == 'GAME'){ | ||
music.pause() // Pause Game Music | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, yes I define the "music" object and added some comments when I invoke the methods |
||
state = 'PAUSE' | ||
pauseText.setText("Pause") | ||
pauseInst.setText("press 'R' for reset") | ||
} | ||
else{ | ||
music.play() // Resume Game Music | ||
state = 'GAME' | ||
pauseText.setText("") | ||
pauseInst.setText("") | ||
|
@@ -103,7 +119,7 @@ app.reset = function(){ | |
PlayerTwo.reset() | ||
player1Score.setText("0") | ||
player2Score.setText("0") | ||
ball.reset(true) | ||
roundBall.reset(true) | ||
state = 'START' | ||
pauseText.setText("") | ||
pauseInst.setText("") | ||
|
@@ -114,15 +130,15 @@ function gameRuning(deltatime) { | |
PlayerOne.update(deltatime) | ||
PlayerTwo.update(deltatime) | ||
|
||
ball.update(deltatime) | ||
player1Score.setText(ball.score.one) | ||
player2Score.setText(ball.score.two) | ||
roundBall.update(deltatime) | ||
player1Score.setText(roundBall.score.one) | ||
player2Score.setText(roundBall.score.two) | ||
|
||
if(collision(ball.getNode(),PlayerOne.getNode())){ | ||
changeDirection(ball,PlayerOne,app) | ||
if(collision(roundBall.getNode(),PlayerOne.getNode())){ | ||
changeDirection(roundBall,PlayerOne,app) | ||
} | ||
if(collision(ball.getNode(),PlayerTwo.getNode())){ | ||
changeDirection(ball,PlayerTwo,app) | ||
if(collision(roundBall.getNode(),PlayerTwo.getNode())){ | ||
changeDirection(roundBall,PlayerTwo,app) | ||
} | ||
} | ||
|
||
|
@@ -133,17 +149,18 @@ function collision(ball,player) { | |
let pLeft = player.x | ||
let pRight = (player.x) + player.width | ||
|
||
let bTop = ball.y | ||
let bBottom = (ball.y) + ball.height | ||
let bLeft = ball.x | ||
let bRight = (ball.x) + ball.width | ||
let bTop = ball.y - ball.r | ||
let bBottom = (ball.y) + ball.r | ||
let bLeft = ball.x - ball.r | ||
let bRight = (ball.x) + ball.r | ||
|
||
// boolean for collision | ||
return bRight > pLeft && bTop < pBottom && bLeft < pRight && bBottom > pTop | ||
} | ||
|
||
// Change ball direction once it collides with player | ||
function changeDirection(ball,player,app) { | ||
ball.playBounce() // Play Ball bounce | ||
let collideHit = ball.getNode().y - (player.getNode().y + player.getNode().height / 2) | ||
collideHit = collideHit / (player.getNode().height / 2) | ||
let angle = (Math.PI/4) * collideHit | ||
|
@@ -155,5 +172,5 @@ function changeDirection(ball,player,app) { | |
ball.velocityX = direction * ball.speed * Math.cos(angle) | ||
ball.velocityY = direction * ball.speed * Math.sin(angle) | ||
|
||
ball.speed += 0.5 | ||
ball.speed += 1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ever heard of magic numbers?
You're a true magician putting magic all over the code :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You´re right, I should create some constants for those values to be easy to track.