Help with making code way WAY smaller

this isnt so much an issue, but it does require just some guidance, help or just a finger that points me a good direction.

so ive been lovingly busy with a new version of my game FRONG and have really enjoyed working with HAXE/OPENFL. but the one thing ive only really worked on for ages in single player stuff. so my code right now is just STUPID big and over bloated to have the selection of 1 to 4 players playing. it works, but is just horribly large :wink: [thats what she said ;)]

so i have this in my main script, which is handling not so much everything since im splitting it all up into different classes. but this player choice is just large, too large for my liking, and i do know that it can be better, smaller and just downright looking more sexy than it is. there are too many doubles for my liking, but they arent crossing with each other luckily.

so this is my current player selection, along with the controls for the players. i think the keyUp/keyDown stuff can also be way better.

but if anyone could lend a helping word, id be more than happy to listen. if there is something that i could read up on, even better, if a video then i love you :wink:

the selection first is using the numbered keys to select players that are playing. which are tehn controlled by a QWERTY layout of keys

thanks to anyone who has a nice solution or guidance :wink:

private function keyDown(event:KeyboardEvent):Void {
		//1 player
		if (currentGameState == Paused && event.keyCode == 49) {
			setGameState(Playing01);
		} else if (event.keyCode == 87){
			player01KeyUp = true;
		} else if (event.keyCode == 83){
			player01KeyDown = true;
		}

		//2 player
		if (currentGameState == Paused && event.keyCode == 50) {
			setGameState(Playing02);
		} else if (event.keyCode == 87){
			player01KeyUp = true;
		} else if (event.keyCode == 83){
			player01KeyDown = true;
		} else if (event.keyCode == 82) {
			player02KeyUp = true;
		} else if (event.keyCode == 70) {
			player02KeyDown = true;
		}

		//3 player
		if (currentGameState == Paused && event.keyCode == 51) {
			setGameState(Playing03);
		} else if (event.keyCode == 87){
			player01KeyUp = true;
		} else if (event.keyCode == 83){
			player01KeyDown = true;
		} else if (event.keyCode == 82) {
			player02KeyUp = true;
		} else if (event.keyCode == 70) {
			player02KeyDown = true;
		} else if (event.keyCode == 89) {
			player03KeyUp = true;
		} else if (event.keyCode == 72) {
			player03KeyDown = true;
		}

		//4 player
		if (currentGameState == Paused && event.keyCode == 52) {
			setGameState(Playing04);
		} else if (event.keyCode == 87){
			player01KeyUp = true;
		} else if (event.keyCode == 83){
			player01KeyDown = true;
		} else if (event.keyCode == 82) {
			player02KeyUp = true;
		} else if (event.keyCode == 70) {
			player02KeyDown = true;
		} else if (event.keyCode == 89) {
			player03KeyUp = true;
		} else if (event.keyCode == 72) {
			player03KeyDown = true;
		} else if (event.keyCode == 73) {
			player04KeyUp = true;
		} else if (event.keyCode == 75) {
			player04KeyDown = true;
		}
	}

	private function keyUp(event:KeyboardEvent):Void {
		//1 player
		if (currentGameState == Paused && event.keyCode == 49) {
			setGameState(Playing01);
		} else if (event.keyCode == 87){
			player01KeyUp = false;
		} else if (event.keyCode == 83){
			player01KeyDown = false;
		}

		//2 player
		if (currentGameState == Paused && event.keyCode == 50) {
			setGameState(Playing02);
		} else if (event.keyCode == 87){
			player01KeyUp = false;
		} else if (event.keyCode == 83){
			player01KeyDown = false;
		} else if (event.keyCode == 82) {
			player02KeyUp = false;
		} else if (event.keyCode == 70) {
			player02KeyDown = false;
		}

		//3 player
		if (currentGameState == Paused && event.keyCode == 51) {
			setGameState(Playing03);
		} else if (event.keyCode == 87){
			player01KeyUp = false;
		} else if (event.keyCode == 83){
			player01KeyDown = false;
		} else if (event.keyCode == 82) {
			player02KeyUp = false;
		} else if (event.keyCode == 70) {
			player02KeyDown = false;
		} else if (event.keyCode == 89) {
			player03KeyUp = false;
		} else if (event.keyCode == 72) {
			player03KeyDown = false;
		}

		//4 player
		if (currentGameState == Paused && event.keyCode == 52) {
			setGameState(Playing04);
		} else if (event.keyCode == 87){
			player01KeyUp = false;
		} else if (event.keyCode == 83){
			player01KeyDown = false;
		} else if (event.keyCode == 82) {
			player02KeyUp = false;
		} else if (event.keyCode == 70) {
			player02KeyDown = false;
		} else if (event.keyCode == 89) {
			player03KeyUp = false;
		} else if (event.keyCode == 72) {
			player03KeyDown = false;
		} else if (event.keyCode == 73) {
			player04KeyUp = false;
		} else if (event.keyCode == 75) {
			player04KeyDown = false;
		}
	}

	private function everyFrame(event:Event):Void {
		if (currentGameState == Playing01) {
			// player 01
			if (player01KeyUp) {
				player01.y -= playerSpeed;
			}
			if (player01KeyDown) {
				player01.y += playerSpeed;
			}
			if (player01.y < 5) player01.y = 5;
			if (player01.y > 695) player01.y = 695;

			// player 02
			if (ball.x > 400 && ball.y > player02.y + 70) {
				player02.y += playerSpeed;
			}
			if (ball.x > 400 && ball.y < player02.y + 30) {
				player02.y -= playerSpeed;
			}
			if (player02.y < 5) player02.y = 5;
			if (player02.y > 695) player02.y = 695;

			// player03
			if (ball.y < 300 && ball.x > player03.x + 70) {
				player03.x += playerSpeed;
			}
			if (ball.y < 300 && ball.x < player03.x + 30) {
				player03.x -= playerSpeed;
			}
			if (player03.x < 5) player03.x = 5;
			if (player03.x > 695) player03.x = 695;

			// player04
			if (ball.y > 300 && ball.x > player04.x + 70) {
				player04.x += playerSpeed;
			}
			if (ball.y > 300 && ball.x < player04.x + 30) {
				player04.x -= playerSpeed;
			}
			if (player04.x < 5) player04.x = 5;
			if (player04.x > 695) player04.x = 695;

			ball.x += ballMovement.x;
			ball.y += ballMovement.y;

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

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

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

			if (ball.y < 5 || ball.y > 795) ballMovement.y *= -1;
			if (ball.x > 795) {
				winGame(Human01);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore02.play();
			}
			if (ball.x < 5) {
				winGame(Human02);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore01.play();
			}
			if (ball.y > 795) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human03);
				playerSoundScore04.play();
			}
			if (ball.y < 5) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human04);
				playerSoundScore03.play();
			}
		}

		if (currentGameState == Playing02) {
			// player 01
			if (player01KeyUp) {
				player01.y -= playerSpeed;
			}
			if (player01KeyDown) {
				player01.y += playerSpeed;
			}
			if (player01.y < 5) player01.y = 5;
			if (player01.y > 695) player01.y = 695;

			//newPlayer02 movement
			if (player02KeyUp) {
				player02.y -= playerSpeed;
			}
			if (player02KeyDown) {
				player02.y += playerSpeed;
			}
			if (player02.y < 5) player02.y = 5;
			if (player02.y > 695) player02.y = 695;

			// player03
			if (ball.y < 300 && ball.x > player03.x + 70) {
				player03.x += playerSpeed;
			}
			if (ball.y < 300 && ball.x < player03.x + 30) {
				player03.x -= playerSpeed;
			}
			if (player03.x < 5) player03.x = 5;
			if (player03.x > 695) player03.x = 695;

			// player04
			if (ball.y > 300 && ball.x > player04.x + 70) {
				player04.x += playerSpeed;
			}
			if (ball.y > 300 && ball.x < player04.x + 30) {
				player04.x -= playerSpeed;
			}
			if (player04.x < 5) player04.x = 5;
			if (player04.x > 695) player04.x = 695;

			ball.x += ballMovement.x;
			ball.y += ballMovement.y;

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

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

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

			if (ball.y < 5 || ball.y > 795) ballMovement.y *= -1;
			if (ball.x > 795) {
				winGame(Human01);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore02.play();
			}
			if (ball.x < 5) {
				winGame(Human02);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore01.play();
			}
			if (ball.y > 795) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human03);
				playerSoundScore04.play();
			}
			if (ball.y < 5) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human04);
				playerSoundScore03.play();
			}
		}

		if (currentGameState == Playing03) {
			// player 01
			if (player01KeyUp) {
				player01.y -= playerSpeed;
			}
			if (player01KeyDown) {
				player01.y += playerSpeed;
			}
			if (player01.y < 5) player01.y = 5;
			if (player01.y > 695) player01.y = 695;

			//newPlayer02 movement
			if (player02KeyUp) {
				player02.y -= playerSpeed;
			}
			if (player02KeyDown) {
				player02.y += playerSpeed;
			}
			if (player02.y < 5) player02.y = 5;
			if (player02.y > 695) player02.y = 695;

			//newPlayer03 movement
			if (player03KeyUp) {
				player03.x -= playerSpeed;
			}
			if (player03KeyDown) {
				player03.x += playerSpeed;
			}
			if (player03.x < 5) player03.x = 5;
			if (player03.x > 695) player03.x = 695;

			// player04
			if (ball.y > 300 && ball.x > player04.x + 70) {
				player04.x += playerSpeed;
			}
			if (ball.y > 300 && ball.x < player04.x + 30) {
				player04.x -= playerSpeed;
			}
			if (player04.x < 5) player04.x = 5;
			if (player04.x > 695) player04.x = 695;

			ball.x += ballMovement.x;
			ball.y += ballMovement.y;

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

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

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

			if (ball.y < 5 || ball.y > 795) ballMovement.y *= -1;
			if (ball.x > 795) {
				winGame(Human01);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore02.play();
			}
			if (ball.x < 5) {
				winGame(Human02);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore01.play();
			}
			if (ball.y > 795) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human03);
				playerSoundScore04.play();
			}
			if (ball.y < 5) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human04);
				playerSoundScore03.play();
			}
		}

		if (currentGameState == Playing04) {
			// player 01
			if (player01KeyUp) {
				player01.y -= playerSpeed;
			}
			if (player01KeyDown) {
				player01.y += playerSpeed;
			}
			if (player01.y < 5) player01.y = 5;
			if (player01.y > 695) player01.y = 695;

			//newPlayer02 movement
			if (player02KeyUp) {
				player02.y -= playerSpeed;
			}
			if (player02KeyDown) {
				player02.y += playerSpeed;
			}
			if (player02.y < 5) player02.y = 5;
			if (player02.y > 695) player02.y = 695;

			//newPlayer03 movement
			if (player03KeyUp) {
				player02.x -= playerSpeed;
			}
			if (player03KeyDown) {
				player03.x += playerSpeed;
			}
			if (player03.x < 5) player03.x = 5;
			if (player03.x > 695) player03.x = 695;

			//newPlayer04 movement
			if (player04KeyUp) {
				player04.x -= playerSpeed;
			}
			if (player04KeyDown) {
				player04.x += playerSpeed;
			}
			if (player04.x < 5) player04.x = 5;
			if (player04.x > 695) player04.x = 695;

			ball.x += ballMovement.x;
			ball.y += ballMovement.y;

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

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

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

			if (ball.y < 5 || ball.y > 795) ballMovement.y *= -1;
			if (ball.x > 795) {
				winGame(Human01);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore02.play();
			}
			if (ball.x < 5) {
				winGame(Human02);
				winGame(Human03);
				winGame(Human04);
				playerSoundScore01.play();
			}
			if (ball.y > 795) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human03);
				playerSoundScore04.play();
			}
			if (ball.y < 5) {
				winGame(Human01);
				winGame(Human02);
				winGame(Human04);
				playerSoundScore03.play();
			}
		}

Hi!
In the first round, I think the key events can be simpler:

private function keyDown(event:KeyboardEvent):Void
{
    if (currentGameState != Paused)
    {
        switch (event.keyCode)
        {
            case 87: player01KeyUp = true;
            case 83: player01KeyDown = true;
            case 82: player02KeyUp = true;
            case 70: player02KeyDown = true;
            case 89: player03KeyUp = true;
            case 72: player03KeyDown = true;
            case 73: player04KeyUp = true;
            case 75: player04KeyDown = true;
        }
    }
}

private function keyUp(event:KeyboardEvent):Void
{
    if (currentGameState == Paused) // setGameState() is enough in here
    {
        switch (event.keyCode)
        {
            case 49: setGameState(Playing01);
            case 50: setGameState(Playing02);
            case 51: setGameState(Playing03);
            case 52: setGameState(Playing04);
        }
    }
    else
    {
        switch (event.keyCode)
        {
            case 87: player01KeyUp = true;
            case 83: player01KeyDown = true;
            case 82: player02KeyUp = true;
            case 70: player02KeyDown = true;
            case 89: player03KeyUp = true;
            case 72: player03KeyDown = true;
            case 73: player04KeyUp = true;
            case 75: player04KeyDown = true;
        }
    }
}

And in the everyFrame function try not to repeat lines. Make functions from them, if needed then with parameters.
Eg. for every player control you can use such a function:

/**
 * Contorl a player
 * @param    player Player object: player01, player02, player03, player04
 * @param    keyUp eg.: player01KeyUp
 * @param    keyDown eg.: player01KeyDown
 * @param    ycoord The y-coord is used?
 */
private function contorlPlayer(player, keyUp:Bool, keyDown:Bool, ycoord:Bool)
{
    if (keyUp)
    {
        if (ycoord) player.y -= playeySpeed;
        else player.x -= playeySpeed;
    }
    if (keyDwon)
    {
        if (ycoord) player.y += playeySpeed;
        else player.x += playeySpeed;
    }
    // limit ht position
    if (ycoord)
    {
        if (player.y < 5) player.y = 5;
        else if (player.y > 695) player.y = 695;
    }
    else
    {
        if (player.x < 5) player.x = 5;
        if (player.x > 695) player.x = 695;
    }
}

Best regards!

1 Like

wow, that looks sexy. thank you for the help. its most welcome and thankful for :wink:

as said ive only really done single player, and actually quite simple games. i really need to sit back and look at some things more and more since HAXE is now the language i want to stick with.

again, thanks :wink:

this smaller code has helped a lot. but one thing i have noticed is that when i restart the level, the game seems to remember or leave some data from moving. so when i start the game again, it either moves by itself, or it doesnt move. its weird. its okay if i leave the controls for a minute though. will look into this further

Make sure you set the keyUp/keyDown values to false as well as speed to 0

1 Like

aye, i had done the true/false stuff. but did forget about speed. HA :wink: