Skip to content

Instantly share code, notes, and snippets.

@NizzeB
Created September 22, 2015 12:35
Show Gist options
  • Save NizzeB/d8a1d2584d7409ef72ce to your computer and use it in GitHub Desktop.
Save NizzeB/d8a1d2584d7409ef72ce to your computer and use it in GitHub Desktop.
<script src="http://spelprogrammering.nu/simple.js">
fill("white");
rectangle(5, 50, totalWidth-10, totalHeight-55, "black");
var p1 = {x: random(5, totalWidth-5), y: random(50, totalHeight-5), size: 5, xSpeed: 5, ySpeed: 0};
var p2 = {x: random(5, totalWidth-5), y: random(50, totalHeight-5), size: 5, xSpeed: 5, ySpeed: 0};
if(p1.x > totalWidth/2) { p1.xSpeed = -5; } //Om dessa står i en update function så dör man så fort man kommer in i den
if(p2.x > totalWidth/2) { p2.xSpeed = -5; } //högra delen av planen för att man då få negativ xSpeed fastän spelet är igång.
var r1w = 0; //Dessa värden får inte nollställas genom att köra dessa rader igen,
var r2w = 0; //för då går liven tillbaka till max i början av varje runda.
function drawLifePlayer1()
{
rectangle(5, 15, 30, 20, "blue");
rectangle(40, 15, 30, 20, "blue");
rectangle(75, 15, 30, 20, "blue");
rectangle(110, 15, 30, 20, "blue");
rectangle(145, 15, 30, 20, "blue");
}
function drawLifePlayer2()
{
rectangle(totalWidth-5, 15, -30, 20, "red");
rectangle(totalWidth-40, 15, -30, 20, "red");
rectangle(totalWidth-75, 15, -30, 20, "red");
rectangle(totalWidth-110, 15, -30, 20, "red");
rectangle(totalWidth-145, 15, -30, 20, "red");
}
function drawLife()
{
drawLifePlayer1();
drawLifePlayer2();
}
drawLife();
function drawGame()
{
rectangle(p1.x, p1.y, p1.size, p1.size, "blue");
rectangle(p2.x, p2.y, p2.size, p2.size, "red");
}
function propertiesPlayers()
{
p1.x += p1.xSpeed;
p1.y += p1.ySpeed;
p2.x += p2.xSpeed;
p2.y += p2.ySpeed;
}
function goingUpPlayer1()
{
if(keyboard.a) { p1.ySpeed = 0; p1.xSpeed = -5; }
if(keyboard.d) { p1.ySpeed = 0; p1.xSpeed = 5; }
}
function goingDownPlayer1()
{
if(keyboard.a) { p1.ySpeed = 0; p1.xSpeed = -5; }
if(keyboard.d) { p1.ySpeed = 0; p1.xSpeed = 5; }
}
function goingLeftPlayer1()
{
if(keyboard.w) { p1.ySpeed = -5; p1.xSpeed = 0; }
if(keyboard.s) { p1.ySpeed = 5; p1.xSpeed = 0; }
}
function goingRightPlayer1()
{
if(keyboard.w) { p1.ySpeed = -5; p1.xSpeed = 0; }
if(keyboard.s) { p1.ySpeed = 5; p1.xSpeed = 0; }
}
function controlsPlayer1()
{
if(p1.ySpeed <= -5 && p1.xSpeed == 0) { goingUpPlayer1(); }
if(p1.ySpeed >= 5 && p1.xSpeed == 0) { goingDownPlayer1(); }
if(p1.ySpeed == 0 && p1.xSpeed <= -5) { goingLeftPlayer1(); }
if(p1.ySpeed == 0 && p1.xSpeed >= 5) { goingRightPlayer1(); }
}
function goingUpPlayer2()
{
if(keyboard.left) { p2.ySpeed = 0; p2.xSpeed = -5; }
if(keyboard.right) { p2.ySpeed = 0; p2.xSpeed = 5; }
}
function goingDownPlayer2()
{
if(keyboard.left) { p2.ySpeed = 0; p2.xSpeed = -5; }
if(keyboard.right) { p2.ySpeed = 0; p2.xSpeed = 5; }
}
function goingLeftPlayer2()
{
if(keyboard.up) { p2.ySpeed = -5; p2.xSpeed = 0; }
if(keyboard.down) { p2.ySpeed = 5; p2.xSpeed = 0; }
}
function goingRightPlayer2()
{
if(keyboard.up) { p2.ySpeed = -5; p2.xSpeed = 0; }
if(keyboard.down) { p2.ySpeed = 5; p2.xSpeed = 0; }
}
function controlsPlayer2()
{
if(p2.ySpeed <= -5 && p2.xSpeed == 0) { goingUpPlayer2(); }
if(p2.ySpeed >= 5 && p2.xSpeed == 0) { goingDownPlayer2(); }
if(p2.ySpeed == 0 && p2.xSpeed <= -5) { goingLeftPlayer2(); }
if(p2.ySpeed == 0 && p2.xSpeed >= 5) { goingRightPlayer2(); }
}
function startAgain()
{
rectangle(180, 10, r1w, 30, "white");
rectangle(totalWidth-180, 10, r2w, 30, "white");
if(r1w == -175)
{
stopUpdate();
clearScreen();
var r = confirm("RED WINS!\n\nPLAY AGAIN?");
if(r == true) { location.reload(); }
else { history.back(); }
}
if(r2w == 175)
{
stopUpdate();
clearScreen();
var r = confirm("BLU WINS!\n\nPLAY AGAIN?");
if(r == true) { location.reload(); }
else { history.back(); }
}
rectangle(5, 50, totalWidth-10, totalHeight-55, "black");
p1 = {x: random(5, totalWidth-5), y: random(50, totalHeight-5), size: 5, xSpeed: 5, ySpeed: 0};
p2 = {x: random(5, totalWidth-5), y: random(50, totalHeight-5), size: 5, xSpeed: 5, ySpeed: 0};
if(p1.x > totalWidth/2) { p1.xSpeed = -5; }
if(p2.x > totalWidth/2) { p2.xSpeed = -5; }
update();
}
function winnerPlayer1()
{
if(getPixel(p2.x, p2.y).blue != 0 || getPixel(p2.x, p2.y).red != 0) //Fixa så att hela gubben kan dö, inte bara översta pixeln
{
r2w += 35;
setTimeout(startAgain(), 3000);
}
}
function winnerPlayer2()
{
if(getPixel(p1.x, p1.y).red != 0 || getPixel(p1.x, p1.y).blue != 0) //Fixa så att hela gubben kan dö, inte bara översta pixeln
{
r1w -= 35;
setTimeout(startAgain(), 3000);
}
}
function gameOver()
{
winnerPlayer1();
winnerPlayer2();
}
function update()
{
drawGame();
propertiesPlayers();
controlsPlayer1();
controlsPlayer2();
gameOver();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment