# 4 way ball movement and points scoring?

howdy folks,

im just nearing actually the end of doing a complete remake of my game FRONG. originally it was made in UNITY, but have decided now since im sticking to HAXE/OPENFL, to update the games ive made previous. this being the first.

but FRONG is a 4 player breakout/pong mash-up, so has 4 different scores, but im just having a small bit o’ trouble doing the scores for each individual, plus the ball movement is fun, since its actually not predictable, but would like to make it more fluid and life like per-se

the trouble is actually matching the 4 scores first. so say if the 2nd player gets scored past, id like a point to go to the other 3.
i have tried something like this: but does run up with errors. obviously, i kind of knew it would. but just looking for a way for it to apply to others.

``````        if (ball.y < 5 || ball.y > 795) ballMovement.y *= -1;
if (ball.x > 795) winGame(Human01 || Human03 || Human04);
if (ball.x < 5) winGame(Human02 || Human03 || Human04);
if (ball.y > 795) winGame(Human03 || Human01 || Human02);
if (ball.y < 5) winGame(Human03 || Human01 || Human04);
}
private function winGame(player:Player):Void {
if (player == Human01) {
scorePlayer01++;
} else if (player == Human02) {
scorePlayer02++;
} else if (player == Human03) {
scorePlayer03++;
} else if (player == Human04) {
scorePlayer04++;
}
setGameState(Paused);
}
``````

and lastly. the ball movement is a bit erratic when bouncing off of all paddles, it doesnt have as natural bounce. it could go 1 way or the other. could you show me how to fix this. can see the problem, but cant work out the solution

``````	private function bounceBall():Void {
var direction:Int = (ballMovement.x > 0)?(-1):(1);
var randomAngle:Float = (Math.random() * Math.PI / 3) - 45;
ballMovement.x = direction * Math.cos(randomAngle) * ballSpeed;
ballMovement.y = direction * Math.sin(randomAngle) * ballSpeed;
}
``````

i will be sharing the entire code base once finished. plus will be putting it online for everyone to play as well. maybe on itch.io

plus will actually do the breakout part as well, and add proper 4 player mode too it. its currently just single player

this original tutorial was from the haxecoder one which teaches you pong. but i have then taken it further to completely remake my FRONG project

this is the original inception of it [you do need unity player for it to work. though once this is done,you wont ;)]. enjoy

http://smokingbunny.net/downloads/unity/frong/FRONG.html

I would like to help but first, I need to play how do you play?
what triggers the game on unity?

HA. here are the instructions
http://www.smokingbunny.net/frong/

its supposed to be played with 4 people. its all about getting as much points as the others. and if you score past them, there score goes down

this new updated HAXE version will have better things in. already its feeling better. its just computers, but will put in my 4 human player thing in it.

the unity one is just 4 human players

Perhaps you should do score for all “but” a certain player:

``````if (ball.x > 795) scoreAgainst (1);

...

private function scoreAgainst (player:Int):Void {

for (i in players.length) {

if (i != player) {

score[i]++;

}

}

}
``````

If you want, you could even use an abstract enum for `Player`

``````@:enum abstract Player(Int) from Int to Int {

var Player1 = 0;
var Player2 = 1;
var Player3 = 2;
var Player4 = 3;

}
``````

Then you can `switch (player)` but can also use it as an `Int` for a score array, etc. If you would prefer not to use an array, you can also use a map for score:

``````public var scores = new Map<Player> ();
score.set (player, score.get (player) + 1);
``````

oooh. ill give those a look-see. thanks

this one is probably the one ill look at most actually. it looks right in my way of thinking

EDIT
hmmm actually quite torn between them both

and lastly actually. regarding the ball or bouncing.

at this moment in time, it does do quite a lovely and un predictable way of bouncing with i quite like, but really should be natural.
because from the tutorial from haxecoder, it does just mainly deal in X rather than Y so much due too it being just left to right. but now with the intro of 2 extra players, does the bouncing wrong now.

i have done just simple copies of each player or even the bounce of them, then reversed it to suit the needs. but it has this unpredictable or un-natural way of bouncing. ill post the code below.
again, thanks for the help. the more i use HAXE/OPENFL the more im annoyed i didnt use it before. but better late than never

this is in my ‘everyFrame’ function

``````//player01 bounce off
if (ballMovement.x < 0 && ball.x < 30 && ball.y >= player01.y && ball.y <= player01.y + 100) {
bounceBall();
ball.x = 30;
}
//player02 bounce off
if (ballMovement.x > 0 && ball.x > 770 && ball.y >= player02.y && ball.y <= player02.y + 100) {
bounceBall();
ball.x = 770;
}

//player03 bounce off
if (ballMovement.y < 0 && ball.y < 30 && ball.x >= player03.x && ball.x <= player03.x + 100) {
bounceBall();
ball.y = 30;
}

//player04 bounce off
if (ballMovement.y > 0 && ball.y > 770 && ball.x >= player04.x && ball.x <= player04.x + 100) {
bounceBall();
ball.y = 770;
}
``````

and this is the bounceBall function. which ive not edited seen going for dinner. but will continue back on it now

``````private function bounceBall():Void {
var direction:Int = (ballMovement.x > 0)?(-1):(1);
var randomAngle:Float = (Math.random() * Math.PI / 3) - 45;
ballMovement.x = direction * Math.cos(randomAngle) * ballSpeed;
ballMovement.y = direction * Math.sin(randomAngle) * ballSpeed;
}
``````

many thanks again. the help is great

EDIT
this isnt for people to do it for me, no no no. just to help figure out a problem.

If it doesn’t introduce too much complexity, you could look at using Nape or Box2D to add physics, then create an object for each goal, when the ball collides with a goal, that’s when you trigger score and remove the body for your ball

1 Like

very true, box2d is good. did think of that, bu thought to maybe try without it. just for practice sake than anything. i will look though. thanks

its a bit crude, but just done this for now

``````if (ball.x > 795) {
winGame(Human01);
winGame(Human03);
winGame(Human04);
}
if (ball.x < 5) {
winGame(Human02);
winGame(Human03);
winGame(Human04);
}
if (ball.y > 795) {
winGame(Human01);
winGame(Human02);
winGame(Human03);
}
if (ball.y < 5) {
winGame(Human01);
winGame(Human02);
winGame(Human04);
}
``````