Created
September 29, 2015 02:34
-
-
Save lisajamhoury/b74328876f2be9ae5df0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var trump = { | |
x:0, | |
y:0, | |
w:100, | |
h:138, | |
speedx:0, | |
speedy:0, | |
}; | |
var jeb = { | |
x:50, | |
y:0, | |
w:100, | |
h:124, | |
wins:"JEB WINS!!!", | |
}; | |
var rubio = { | |
x:0, | |
y:0, | |
w:100, | |
h:136, | |
wins:"RUBIO WINS!!!", | |
}; | |
var trumpSpeed = 8; | |
var paddleSpeed = 6; | |
var startHeight; | |
var instructions = "Player 1 \nA = move up\nZ = move down\n\n\nPress any key to begin." | |
var instructions2 = "Player 2 \nUp Arrow = move up \nDown Arrow = move down" | |
var replay = "Press ENTER to play again." | |
var welcome = "Trumpong!" | |
var star; | |
var stage = 0; | |
function preload() { | |
trump.img = loadImage("assets/trumphead.png"); | |
jeb.img = loadImage("assets/jeb.png"); | |
rubio.img = loadImage("assets/rubio.png"); | |
star = loadImage("assets/star.png"); | |
} | |
function setup() { | |
createCanvas(1280, 600); | |
smooth(); | |
imageMode(CENTER); | |
rectMode(CENTER); | |
fill(0,0,255); | |
textStyle(BOLD); | |
textFont("Georgia"); | |
textSize(48); | |
} | |
function draw() { | |
if (stage === 0) { | |
homeScreen(); | |
} | |
else if (stage == 1) { | |
playGame(); | |
} | |
else if (stage == 2) { | |
playAgain(); | |
} | |
} | |
////////////////////////// Stage 0 //////////////////// | |
function homeScreen() { | |
background(255,0,0); | |
image(star, width/7, height/3, 500, 500); | |
image(trump.img, width/7, height/3, trump.w, trump.h); | |
textSize(100); | |
text(welcome, width/2-50, height/2+50, 200, 500); | |
textSize(25); | |
text(instructions, width/2+100, height/2, 500, 50); | |
text(instructions2, width/2+450, height/2, 500, 50); | |
if (keyIsPressed === true) { | |
stage += 1; | |
setBoard(); | |
} | |
} | |
////////////////////////// Stage 1 //////////////////// | |
function playGame() { | |
background(255,0,0); | |
// define candidates starting positions | |
image(jeb.img, jeb.x, jeb.y, jeb.w, jeb.h); | |
image(rubio.img, rubio.x, rubio.y, rubio.w, rubio.h); | |
image(trump.img, trump.x, trump.y, trump.w, trump.h); | |
// keep trump moving | |
moveTrump(); | |
// keep trump on board | |
if (trump.y > height || trump.y < 0) { | |
bounceTrump(); | |
} | |
// jeb up and down controls | |
if (keyIsDown(65)) { | |
paddleUp(jeb); | |
} | |
if (keyIsDown(90)) { | |
paddleDown(jeb); | |
} | |
// rubio up and down controls | |
if (keyIsDown(UP_ARROW)) { | |
paddleUp(rubio); | |
} | |
if (keyIsDown(DOWN_ARROW)) { | |
paddleDown(rubio); | |
} | |
// keep jeb from moving off border of page | |
if (jeb.y <= 0) { | |
paddleResetTop(jeb); | |
} | |
if (jeb.y >= height) { | |
paddleResetBottom(jeb); | |
} | |
// keep rubio from moving off border of page | |
if (rubio.y <= 0) { | |
paddleResetTop(rubio); | |
} | |
if (rubio.y >= height) { | |
paddleResetBottom(rubio); | |
} | |
// bounce trump off jeb paddle | |
if (trump.x >= getLeft(jeb) && trump.x <= getRight(jeb)) { | |
if (trump.y >= getTop(jeb) && trump.y <= getBottom(jeb)){ | |
trump.speedx = Math.abs(trump.speedx) * 1.1; | |
} | |
} | |
// bounce trump off rubio paddle | |
if (trump.x >= getLeft(rubio) && trump.x <= getRight(rubio)) { | |
if (trump.y >= getTop(rubio) && trump.y <= getBottom(rubio)){ | |
trump.speedx = Math.abs(trump.speedx) * -1.1; | |
} | |
} | |
// declare winners | |
if (trump.x > width) { | |
declareWinner(jeb); | |
} | |
if (trump.x < 0) { | |
declareWinner(rubio); | |
} | |
} | |
////////////////////////// Stage 2 //////////////////// | |
function playAgain() { | |
textSize(30); | |
textAlign(CENTER); | |
text(replay, width/2, height/2+100, 500, 200); | |
if (keyIsDown(ENTER)) { | |
stage = 1; | |
setBoard(); | |
} | |
} | |
////////////////////// Declare functions ////////////// | |
// board setup | |
function setBoard() { | |
trump.x= width/2, | |
rubio.x = width-50; | |
startHeight = height/2; | |
trump.y= startHeight; | |
rubio.y = startHeight; | |
jeb.y = startHeight; | |
trump.speedx = trumpSpeed; | |
trump.speedy = trumpSpeed; | |
} | |
// Trump movements | |
function moveTrump() { | |
trump.x = trump.x + trump.speedx; | |
trump.y = trump.y + trump.speedy; | |
} | |
function bounceTrump() { | |
trump.speedy = trump.speedy * -1; | |
} | |
// Paddle movements | |
function paddleUp(player) { | |
player.y -= paddleSpeed; | |
} | |
function paddleDown(player) { | |
player.y += paddleSpeed; | |
} | |
function paddleResetTop(player) { | |
player.y = 1; | |
} | |
function paddleResetBottom(player) { | |
player.y = height-1; | |
} | |
function getTop(paddle) { | |
return paddle.y - paddle.h; | |
} | |
function getBottom(paddle) { | |
return paddle.y + paddle.h; | |
} | |
function getLeft(paddle) { | |
return paddle.x - paddle.w; | |
} | |
function getRight(paddle) { | |
return paddle.x + paddle.w; | |
} | |
// Winner declaration | |
function declareWinner(winner) { | |
trump.speedx = 0; | |
trump.speedy = 0; | |
textAlign(CENTER); | |
text(winner.wins, width/2, height/2, 500, 200); | |
stage +=1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment