Skip to content

Instantly share code, notes, and snippets.

@lisajamhoury
Created September 22, 2015 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisajamhoury/7e65f41d1a3af19bed1e to your computer and use it in GitHub Desktop.
Save lisajamhoury/7e65f41d1a3af19bed1e to your computer and use it in GitHub Desktop.
Trumpong
var trump = {
x:0,
y:0,
w:100,
h:138,
speedx:8,
speedy:8,
};
var jeb = {
x:50,
y:0,
w:100,
h:124,
getTop: function() {
return jeb.y - jeb.h;
},
getBottom: function() {
return jeb.y + jeb.h;
},
getLeft: function() {
return jeb.x - jeb.w;
},
getRight: function() {
return jeb.x + jeb.w;
},
};
var rubio = {
x:0,
y:0,
w:100,
h:136,
getTop: function() {
return rubio.y - rubio.h;
},
getBottom: function() {
return rubio.y + rubio.h;
},
getLeft: function() {
return rubio.x - rubio.w;
},
getRight: function() {
return rubio.x + rubio.w;
},
};
var paddleSpeed = 6;
function preload() {
trump.img = loadImage("assets/trumphead.png");
jeb.img = loadImage("assets/jeb.png");
rubio.img = loadImage("assets/rubio.png");
}
function setup() {
createCanvas(1000, 800);
smooth();
imageMode(CENTER);
fill(0,0,255);
textStyle(BOLD);
textFont("Georgia");
textSize(48);
trump.x= width/2,
trump.y= height/2,
jeb.y = height/2;
rubio.x = width-50;
rubio.y = height/2;
jeb.wins = "JEB WINS!!!";
rubio.wins = "RUBIO WINS!!!"
}
function draw() {
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
trump.x = trump.x + trump.speedx;
trump.y = trump.y + trump.speedy;
// jeb up and down controls
if (keyIsDown(65)) {
jeb.y -= paddleSpeed;
}
if (keyIsDown(90)) {
jeb.y += paddleSpeed;
}
// rubio up and down controls
if (keyIsDown(UP_ARROW)) {
rubio.y -= paddleSpeed;
}
if (keyIsDown(DOWN_ARROW)) {
rubio.y += paddleSpeed;
}
// keep jeb from moving off border of page
if (jeb.y <= 0) {
jeb.y = 1;
}
if (jeb.y >= height) {
jeb.y = height-1;
}
// keep rubio from moving off border of page
if (rubio.y <= 0) {
rubio.y = 1;
}
if (rubio.y >= height) {
rubio.y = height-1;
}
// bounce trump off jeb paddle
if (trump.x >= jeb.getLeft() && trump.x <= jeb.getRight()) {
if (trump.y >= jeb.getTop() && trump.y <= jeb.getBottom()){
trump.speedx = Math.abs(trump.speedx);
}
}
// bounce trump off rubio paddle
if (trump.x >= rubio.getLeft() && trump.x <= rubio.getRight()) {
if (trump.y >= rubio.getTop() && trump.y <= rubio.getBottom()){
trump.speedx = Math.abs(trump.speedx) * -1;
}
}
if (trump.x > width) {
text(jeb.wins, width*0.35, 200, 500, 50);
noLoop();
}
if (trump.x < 0) {
text(rubio.wins, width*0.35, 200, 500, 50);
noLoop();
}
if (trump.y > height || trump.y < 0) {
trump.speedy = trump.speedy * -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment