Paceman
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 ghost1; | |
var pacman1; | |
function setup() { | |
createCanvas(800, 800); | |
angleMode(DEGREES); | |
frameRate(30); | |
ghost2 = new ghost(255, 150, 0); | |
ghost1 = new ghost(255, 30, 0); | |
ghost3 = new ghost(50, 200, 255); | |
ghost4 = new ghost(255, 50, 150); | |
pacman1 = new pacman(120, 120); | |
} | |
function draw() { | |
background(0); | |
for (var x = 100; x < 700; x = x += 30) { | |
for (var y = 100; y < 700; y += 30) { | |
ellipse(x, y, 8, 8); | |
} | |
} | |
//background | |
fill(255); | |
noStroke(); | |
//top blocks | |
block(150, 150, 50, 40); | |
block(240, 150, 110, 40); | |
block(580, 150, 50, 40); | |
block(420, 150, 110, 40); | |
//center | |
block(330, 350, 125, 90); | |
//border | |
noFill(); | |
strokeWeight(2); | |
rect(90, 90, 590, 600); | |
rect(80, 80, 610, 620); | |
//center cross top | |
fill(0); | |
cross(350, 245); | |
//center cross middle | |
cross(350, 475); | |
//center cross bottom | |
cross(350, 595); | |
//cross right | |
push(); | |
fill(0); | |
translate(530, 260); | |
rotate(90); | |
cross(0, 0); | |
pop(); | |
//cross left | |
push(); | |
fill(0); | |
translate(270, 360); | |
rotate(270); | |
cross(0, 0); | |
pop(); | |
//sidebars left | |
sidebox(79, 275); | |
sidebox(79, 410); | |
//sidebars right1 | |
push(); | |
translate(692, 345); | |
rotate(180); | |
sidebox(0, 0); | |
pop(); | |
push(); | |
translate(692, 480); | |
rotate(180); | |
sidebox(0, 0); | |
pop(); | |
//bottom-L barrier L | |
bottomBarrier(240, 575); | |
//bottom-L barrier R | |
push(); | |
translate(360, 0); | |
bottomBarrier(240, 575); | |
pop(); | |
//ghosts | |
// ghost(255, 30, 0); | |
ghost1.move(); | |
ghost1.display(); | |
ghost2.display(); | |
ghost2.move(); | |
ghost3.display(); | |
ghost3.move(); | |
ghost4.display(); | |
ghost4.move(); | |
pacman1.display(); | |
pacman1.move(); | |
pacman1.bounce(); | |
//print(pacman1); | |
g = random(0, 255); | |
e = random(0, 255); | |
stroke(0, 100, 255); | |
strokeWeight(5); | |
fill(g, e, g); | |
textSize(28); | |
text("Click to move Paceman!", 0, 0, 600, 300); | |
} | |
// //create rectangles and then make into a function | |
function block(x, y, a, b) { | |
stroke(0, 100, 255); | |
strokeWeight(2); | |
fill(0); | |
rect(x, y, a + 10, b + 10); | |
rect(x + 5, y + 5, a, b); | |
} | |
function cross(x, y) { | |
beginShape(); | |
vertex(x, y); | |
vertex(x + 100, y); | |
vertex(x + 100, y + 20); | |
vertex(x + 60, y + 20); | |
vertex(x + 60, y + 60); | |
vertex(x + 40, y + 60); | |
vertex(x + 40, y + 20); | |
vertex(x, y + 20); | |
endShape(CLOSE); | |
beginShape(); | |
vertex(x + 5, y + 5); | |
vertex(x + 95, y + 5); | |
vertex(x + 95, y + 15); | |
vertex(x + 55, y + 15); | |
vertex(x + 55, y + 55); | |
vertex(x + 45, y + 55); | |
vertex(x + 45, y + 15); | |
vertex(x + 5, y + 15); | |
endShape(CLOSE); | |
} | |
function sidebox(x, y) { | |
fill(0); | |
beginShape(); | |
vertex(x, y); | |
vertex(x + 120, y); | |
vertex(x + 120, y + 70); | |
vertex(x, y + 70) | |
endShape(); | |
beginShape(); | |
vertex(x, y + 5); | |
vertex(x + 115, y + 5); | |
vertex(x + 115, y + 65); | |
vertex(x, y + 65) | |
endShape(); | |
} | |
function bottomBarrier(x, y) { | |
beginShape(); | |
vertex(x, y); | |
vertex(x + 20, y); | |
vertex(x + 20, y + 50); | |
vertex(x + 50, y + 50); | |
vertex(x + 50, y + 70); | |
vertex(x - 100, y + 70); | |
vertex(x - 100, y + 50); | |
vertex(x, y + 50); | |
endShape(CLOSE); | |
beginShape(); | |
vertex(x + 5, y + 5); | |
vertex(x + 15, y + 5); | |
vertex(x + 15, y + 55); | |
vertex(x + 45, y + 55); | |
vertex(x + 45, y + 65); | |
vertex(x - 95, y + 65); | |
vertex(x - 95, y + 55); | |
vertex(x + 5, y + 55); | |
endShape(CLOSE); | |
push(); | |
fill(0); | |
noStroke(); | |
rect(600, 680, 70, 50); | |
pop(); | |
} | |
function ghost(r, g, b) { | |
// x and y for all ghosts | |
this.x = random(330, 450); | |
this.y = random(360, 440); | |
this.speed = 1; | |
this.move = function() { | |
this.x += random(-this.speed, this.speed); | |
this.y += random(-this.speed, this.speed); | |
} | |
//body | |
this.display = function() { | |
stroke(r, g, b); | |
fill(r, g, b); | |
arc(this.x, this.y, 40, 60, 180, 360, CHORD); | |
rect(this.x + 15, this.y, 5, 5); | |
rect(this.x + 4, this.y, 5, 5); | |
rect(this.x - 8, this.y, 5, 5); | |
rect(this.x - 20, this.y, 5, 5); | |
//eyes | |
fill(255); | |
stroke(255); | |
ellipse(this.x - 5, this.y - 15, 5, 5); | |
ellipse(this.x + 5, this.y - 15, 5, 5); | |
stroke(0); | |
ellipse(this.x - 5, this.y - 15, 1, 1); | |
ellipse(this.x + 5, this.y - 15, 1, 1); | |
} | |
} | |
function pacman(x, y) { | |
this.x = x; | |
this.y = y; | |
this.speed = 0 | |
this.speedy = 0 | |
this.counter = 0 | |
this.display = function() { | |
println(this.counter); | |
this.counter = this.counter + 2.5; | |
if (this.counter > 20) { | |
this.counter = 0; | |
} | |
fill(255, 255, 0); | |
if (this.counter > 10) { | |
ellipse(this.x, this.y, 50, 50); | |
} else { | |
//ellipse(this.x, this.y, 50, 50); | |
arc(this.x, this.y, 50, 50, 0, 320, PIE); | |
} | |
} | |
this.move = function() { | |
this.x = this.x + this.speed; | |
this.y = this.y + this.speedy; | |
} | |
this.bounce = function() { | |
if (mouseIsPressed) { | |
this.x = mouseX; | |
this.y = mouseY; | |
} else { | |
this.x = this.x + this.speed; | |
this.y = this.y + this.speedy; | |
} | |
if (this.x < 670 && y < 251) { | |
this.speed = 2; | |
} | |
if (this.x > 670) { | |
this.speed = 0; | |
this.speedy = 2; | |
} | |
if (this.y > 250) { | |
this.speedy = 0; | |
this.speed = -2; | |
this.x = this.x + this.speed; | |
} | |
if (this.x < 550 && this.y > 251) { | |
this.speed = 0; | |
this.speedy = 2; | |
this.y = this.y + this.speedy; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment