Skip to content
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

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ var app = {
this.context.font = node.size+'px serif';
this.context.fillText(node.text, node.x, node.y);
}else {
this.context.fillStyle = node.color;
this.context.fillRect(node.x, node.y, node.width, node.height);
if(node.isball) {
this.context.fillStyle = node.color;
this.context.beginPath();
this.context.arc(node.x, node.y, node.r, 0, Math.PI * 2);
this.context.closePath();
this.context.fill()
}else {
this.context.fillStyle = node.color;
this.context.fillRect(node.x, node.y, node.width, node.height);
}
}
}

Expand Down
65 changes: 53 additions & 12 deletions public/js/modules/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Player {
constructor(id,app,color,playerNum) {
this.initialY = app.height/2
this.speed = 5
this.speed = 8
this.move = {
up : false,
down : false
Expand Down Expand Up @@ -52,21 +52,20 @@ class Player {
getNode() { return this.node }
}


// Node ball class
class Ball {
// Node RoundBall class
class RoundBall {
constructor(id,app,color) {
this.ref = app
this.speed = 6
this.velocityX = 6
this.velocityY = 6
this.speed = 10
this.velocityX = 7
this.velocityY = 7
this.initial = {
id : id,
width : app.width/30,
height : app.height/25,
r : 15, // Ball Radius
x : app.width/2,
y : app.height/2,
color : color
color : color,
isball: true
}
app.nodes.push(this.initial)
this.node = app.getNode(this.initial.id)
Expand All @@ -86,7 +85,9 @@ class Ball {
}
this.node.x = this.ref.width/2
this.node.y = this.ref.height/2
this.speed = 6
this.speed = 10
this.velocityX = 7
this.velocityY = 7
this.velocityX = -this.velocityX
}

Expand All @@ -97,7 +98,7 @@ class Ball {
}

// Change the ball direction when collide with top and botton border
if(this.node.y + (this.node.height/2) > this.ref.height || this.node.y - (this.node.height/2) < 0) {
if(this.node.y + this.node.r > this.ref.height || this.node.y - this.node.r < 0) {
this.velocityY = -this.velocityY
}

Expand All @@ -114,6 +115,7 @@ class Ball {
getNode() { return this.node }
}


// Node text class
class Text {
constructor(id,x,y,size,text,app) {
Expand All @@ -135,4 +137,43 @@ class Text {
}

getText() { return this.node } // Return text node
}


// Node Net class
class Net {
constructor(id,app,color) {
this.initial = {
id : id,
width : 20,
height : app.height,
x : app.width/2 - (10),
y : 0,
color : color
}
app.nodes.push(this.initial)
this.node = app.getNode(this.initial.id)
}
}

// Sound Class
class Sound {
constructor(src,loop) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.setAttribute("loop", loop)
this.sound.style.display = "none";
document.body.appendChild(this.sound);
}

play() {
this.sound.play()
}

pause() {
this.sound.pause()
this.sound.currentTime = 0
}
}
53 changes: 34 additions & 19 deletions public/js/modules/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ 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,'green')

var mainText = 50, secondText = 25;
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 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) => {
Expand Down Expand Up @@ -51,13 +61,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'){
Copy link
Collaborator

@demiculus demiculus Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a typo and the game breaks
maybe you shouldn't use strings huh?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know how to use enum?

Copy link
Owner Author

Choose a reason for hiding this comment

The 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'){
Expand Down Expand Up @@ -87,11 +100,13 @@ app.onUpdate = function(time){

app.pause = function(){
if(state == 'GAME'){
music.pause() // Pause Game Music
Copy link
Collaborator

@demiculus demiculus Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

music.pause() isn't descriptive enough so you added // Pause Game Music to make sure the code reader understands right? :D

Copy link
Owner Author

Choose a reason for hiding this comment

The 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("")
Expand All @@ -103,7 +118,7 @@ app.reset = function(){
PlayerTwo.reset()
player1Score.setText("0")
player2Score.setText("0")
ball.reset(true)
roundBall.reset(true)
state = 'START'
pauseText.setText("")
pauseInst.setText("")
Expand All @@ -114,15 +129,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)
}
}

Expand All @@ -133,10 +148,10 @@ 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
Expand All @@ -155,5 +170,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
}
Binary file added public/sounds/level1.wav
Binary file not shown.