Code
PImage board;
pacMan PacMan1=new pacMan(288, 492);
color pink = color (255, 175, 174);
color purple=color (200, 0, 255);
color greeny = color(50,255,100);
ghost ghost1 =new ghost (298, 310, purple);
ghost ghost2 =new ghost (258, 310, pink);
ghost ghost3 =new ghost (338, 310, greeny);
boolean[] whiteDots=new boolean[249];
boolean[] redDots=new boolean[4];
PFont myFont;
float R, G, B, R1, G1, B1;
int score=0;
int[] xDpos={
49,48,47,47,47,48,48,48,48,49,48,49,47,47,68,67,68,69,68,68,68,87,89,89,88,88,88,88,87,88,108,107,109,109,108,107,149,148,149,149,148,148,
148,148,148,149,147,149,148,149,148,149,149,148,149,148,148,148,148,148,148,148,148,127,128,129,129,128,127,168,171,167,169,168,128,128,189,189,189,189,
189,208,209,210,210,209,210,209,210,209,209,209,128,188,229,228,230,230,229,230,229,249,250,249,250,251,249,249,269,269,269,269,270,270,270,270,270,270,270,
270,270,270,289,291,291,311,311,310,331,330,331,331,329,331,330,331,330,330,331,330,330,330,350,351,350,351,351,350,350,370,370,370,370,371,371,371,390,390,
390,391,389,389,391,391,391,391,391,391,411,409,411,413,411,431,431,431,430,430,451,451,451,450,451,451,450,451,451,450,450,450,451,450,451,451,450,451,450,
450,451,450,450,450,451,450,450,451,471,471,472,451,471,472,471,491,492,490,492,490,490,511,530,553,552,552,531,512,512,512,511,531,552,551,552,530,510,552,
531,512,551,551,552,531,511,552,552,552,531,510,552};
int[] yDpos={
53,72,112,132,153,175,194,436,456,476,556,575,595,616,52,132,193,434,494,553,615,615,554,535,514,493,433,194,132,52,52,133,194,434,555,615,
53,73,91,111,132,152,173,193,212,235,253,273,293,313,334,353,375,394,414,434,453,474,493,514,534,555,615,53,132,195,435,553,613,615,495,435,132,52,133,
52,53,134,435,496,615,616,555,536,515,495,435,194,175,153,134,53,52,614,54,132,193,435,496,556,616,617,556,495,436,194,133,54,617,598,576,557,496,475,455,
435,194,133,95,74,54,114,618,495,134,133,496,616,616,596,576,557,496,475,455,435,193,133,113,93,74,53,616,555,496,435,193,133,53,616,555,496,437,193,134,
54,617,555,536,516,496,496,435,194,174,153,134,54,617,497,436,135,53,54,135,435,496,617,617,557,557,537,516,495,476,456,456,436,417,396,377,338,316,296,
276,256,236,215,194,174,154,134,115,95,72,54,53,133,193,357,436,557,616,617,557,436,194,134,53,617,616,617,596,557,557,557,538,516,497,496,475,456,436,435,
436,193,193,193,173,154,134,134,133,114,74,54,53,54,576};
int[] xRpos={
46,550,48,550};
int[] yRpos={
92,92,491,491};
void setup(){
size(600,704);
board = loadImage("pacManBoard_blank.gif");
//print(xDpos.length);
//print(yDpos.length);
myFont = loadFont("Courier-Bold-48.vlw");
textFont(myFont, 48);
for (int i=0; i<whiteDots.length;i++){
whiteDots[i]=true;
}
for (int i=0; i<redDots.length;i++){
redDots[i]=true;
}
}
void draw(){
/* if (mousePressed){
println(mouseX + "\t"+ mouseY);
}*/
framerate(30);
image(board, 0, 0);
//fill(255,0,0);
//ellipse(208,492, 2,2);
//fill(0,0,255);
//ellipse(width/4,height/4,200,200);
for(int i=0; i<xDpos.length;i++){
if (whiteDots[i]){
fill(255);
quad(xDpos[i]-5, yDpos[i]-5, xDpos[i], yDpos[i]-5, xDpos[i], yDpos[i], xDpos[i]-5, yDpos[i]);
}
else{
fill(0);
quad(xDpos[i]-5, yDpos[i]-5, xDpos[i], yDpos[i]-5, xDpos[i], yDpos[i], xDpos[i]-5, yDpos[i]);
}
}
for(int i=0; i<xRpos.length;i++){
if (redDots[i]){
fill(255,0,0);
ellipse(xRpos[i], yRpos[i], 15,15);
}
else{
fill(0);
ellipse(xRpos[i], yRpos[i], 15,15);
}
}
/*if (mousePressed){
println(mouseX + "\t"+mouseY);
}*/
String s="" + score;
fill(255);
text(s, 137, 687);
PacMan1.drawRightPacMan();
ghost1.drawGhost();
ghost2.drawGhost();
ghost3.drawGhost();
if (key == CODED) {
if (keyCode == RIGHT) {
//PacMan1.erasePacMan();
PacMan1.drawRightPacMan();
if(keyPressed){
PacMan1.moveRight();
}
}
if (keyCode == LEFT) {
//PacMan1.erasePacMan();
PacMan1.drawLeftPacMan();
if(keyPressed){
PacMan1.moveLeft();
}
}
if (keyCode == UP) {
//PacMan1.erasePacMan();
PacMan1.drawUpPacMan();
if(keyPressed){
PacMan1.moveUp();
}
}
if (keyCode == DOWN) {
// PacMan1.erasePacMan();
PacMan1.drawDownPacMan();
if(keyPressed){
PacMan1.moveDown();
}
}
}
}
class pacMan{
int xpos;
int ypos;
pacMan (int pcX, int pcY){
xpos=pcX;
ypos=pcY;
}
void drawRightPacMan(){
noStroke();
fill(255, 255, 0);
ellipse (xpos, ypos, 30, 30);
if (millis()%6==0){
fill(255,255,0);
}
else{
fill(0);
}
triangle(xpos+16, ypos+5, xpos+16, ypos-5, xpos, ypos);
}
void drawLeftPacMan(){
noStroke();
fill(255, 255, 0);
ellipse (xpos, ypos,30, 30);
if (millis()%6==0){
fill(255,255,0);
}
else{
fill(0);
}
triangle(xpos-16, ypos-5, xpos-16, ypos+5, xpos, ypos);
}
void drawDownPacMan(){
noStroke();
fill(255, 255, 0);
ellipse (xpos, ypos, 30, 30);
if (millis()%6==0){
fill(255,255,0);
}
else{
fill(0);
}
triangle(xpos+5, ypos+16, xpos-5, ypos+16, xpos, ypos);
}
void drawUpPacMan(){
noStroke();
fill(255, 255, 0);
ellipse (xpos, ypos, 30, 30);
if (millis()%6==0){
fill(255,255,0);
}
else{
fill(0);
}
triangle(xpos+5, ypos-16, xpos-5, ypos-16, xpos, ypos);
}
/* void erasePacMan(){
noStroke();
fill(0);
ellipse (xpos, ypos, 25, 25);
}*/
void moveRight(){
colorGetterRight();
screenWrap();
//println(R +", "+G+", "+B);
if((G>=150.0)||(G<=50.0)){
//erasePacMan();
xpos=xpos+13;
//drawRightPacMan();
int[] tempVarX=new int[249];
int[] tempVarY=new int[249];
int[] tempVarRX=new int[4];
int[] tempVarRY=new int[4];
for (int i=0; i<xDpos.length;i++){
tempVarX[i]=abs(xDpos[i]-xpos);
tempVarY[i]=abs(yDpos[i]-ypos);
if((tempVarX[i]<=18) && (tempVarY[i]<=18)){
whiteDots[i]=false;
score=score+1;
}
}
for (int i=0; i<xRpos.length;i++){
tempVarRX[i]=abs(xRpos[i]-xpos);
tempVarRY[i]=abs(yRpos[i]-ypos);
if((tempVarRX[i]<=18) && (tempVarRY[i]<=18)){
redDots[i]=false;
score=score+2;
}
}
}
}
void moveLeft(){
colorGetterLeft();
screenWrap();
// println(R +", "+G+", "+B);
if((G>=150.0)||(G<=50.0)){
// erasePacMan();
xpos=xpos-13;
// drawLeftPacMan();
int[] tempVarX=new int[249];
int[] tempVarY=new int[249];
int[] tempVarRX=new int[4];
int[] tempVarRY=new int[4];
for (int i=0; i<xDpos.length;i++){
tempVarX[i]=abs(xDpos[i]-xpos);
tempVarY[i]=abs(yDpos[i]-ypos);
if((tempVarX[i]<=18) && (tempVarY[i]<=18)){
whiteDots[i]=false;
score=score+1;
}
}
for (int i=0; i<xRpos.length;i++){
tempVarRX[i]=abs(xRpos[i]-xpos);
tempVarRY[i]=abs(yRpos[i]-ypos);
if((tempVarRX[i]<=18) && (tempVarRY[i]<=18)){
redDots[i]=false;
score=score+2;
}
}
}
}
void moveUp(){
colorGetterUp();
// println(R +", "+G+", "+B);
if((G>=150.0)||(G<=50.0)){
//erasePacMan();
ypos=ypos-13;
//drawUpPacMan();
int[] tempVarX=new int[249];
int[] tempVarY=new int[249];
int[] tempVarRX=new int[4];
int[] tempVarRY=new int[4];
for (int i=0; i<xDpos.length;i++){
tempVarX[i]=abs(xDpos[i]-xpos);
tempVarY[i]=abs(yDpos[i]-ypos);
if((tempVarX[i]<=18) && (tempVarY[i]<=18)){
whiteDots[i]=false;
score=score+1;
}
}
for (int i=0; i<xRpos.length;i++){
tempVarRX[i]=abs(xRpos[i]-xpos);
tempVarRY[i]=abs(yRpos[i]-ypos);
if((tempVarRX[i]<=18) && (tempVarRY[i]<=18)){
redDots[i]=false;
score=score+2;
}
}
}
}
void moveDown(){
colorGetterDown();
//println(R +", "+G+", "+B);
if((G>=150.0)||(G<=50.0)){
//erasePacMan();
ypos=ypos+13;
//drawDownPacMan();
int[] tempVarX=new int[249];
int[] tempVarY=new int[249];
int[] tempVarRX=new int[4];
int[] tempVarRY=new int[4];
for (int i=0; i<xDpos.length;i++){
tempVarX[i]=abs(xDpos[i]-xpos);
tempVarY[i]=abs(yDpos[i]-ypos);
if((tempVarX[i]<=18) && (tempVarY[i]<=18)){
whiteDots[i]=false;
score=score+1;
}
}
for (int i=0; i<xRpos.length;i++){
tempVarRX[i]=abs(xRpos[i]-xpos);
tempVarRY[i]=abs(yRpos[i]-ypos);
if((tempVarRX[i]<=18) && (tempVarRY[i]<=18)){
redDots[i]=false;
score=score+2;
}
}
}
}
void colorGetterRight(){
color colget = get(xpos+16, ypos);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
void colorGetterLeft(){
color colget = get(xpos-17, ypos);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
void colorGetterUp(){
color colget = get(xpos, ypos-17);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
void colorGetterDown(){
color colget = get(xpos, ypos+16);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
void screenWrap(){
if (xpos<=0){
xpos = width;
}
else if(xpos>=width){
xpos = 0;
}
}
}
class ghost{
int gxpos;
int gypos;
color bodyColor;
ghost(int gX, int gY, color gColor){
gxpos=gX;
gypos=gY;
bodyColor=gColor;
}
void drawGhost(){
fill (bodyColor);
ellipse (gxpos, gypos, 32, 40);
triangle(gxpos-16, gypos, gxpos, gypos, gxpos-8, gypos+26);
triangle(gxpos+16, gypos, gxpos, gypos, gxpos+8, gypos+26);
triangle(gxpos-8, gypos, gxpos+8, gypos, gxpos, gypos+26);
triangle(gxpos-16, gypos, gxpos-16, gypos+26, gxpos-8, gypos);
triangle(gxpos+16, gypos, gxpos+17, gypos+26, gxpos+8, gypos);
fill(255);
ellipse (gxpos-7, gypos-5, 10,10);
ellipse (gxpos+7, gypos-5, 10,10);
fill(0);
ellipse(gxpos-7, gypos-3, 3,3);
ellipse(gxpos+7, gypos-3, 3,3);
}
void colorGhostRight(){
color colget = get(gxpos+17, gypos);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
void colorGhostLeft(){
color colget = get(gxpos-17, gypos);
R1 = red(colget);
G1 = green(colget);
B1 = blue(colget);
//println(R +", "+G+", "+B);
}
void colorGhostUp(){
color colget = get(gxpos, gypos-21);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
void colorGhostDown(){
color colget = get(gxpos, gypos+27);
R = red(colget);
G = green(colget);
B = blue(colget);
//println(R +", "+G+", "+B);
}
float constant=1;
float dx = gxpos - gxpos;
float dy = gypos - gypos;
float dh = sqrt(dx*dx + dy*dy);
float fx = constant * dx/(dh*dh);
float fy = constant * dy/(dh*dh);
void screenWrap(){
if (gxpos<=0){
gxpos = width;
}
else if(gxpos>=width){
gxpos = 0;
}
}
void moveGhostRL(){
float constant=.01;
float dx = gxpos - gxpos;
float dy = gypos - gypos;
float dh = sqrt(dx*dx + dy*dy);
float fx = constant * dx/(dh*dh);
float fy = constant * dy/(dh*dh);
colorGhostRight();
colorGhostLeft();
//println(R +", "+G+", "+B);
if(((G>=150.0)||(G<=50.0)) && ((G1>=150.0)||(G1<=50.0))){
gxpos=gxpos*(int)fx;
//drawRightPacMan();
}
}
}
060 -- Personal Arcade: Implementing a classic game (Due W Nov 15)
Statement:Create (or reinvent) a classic arcade game, such as Pong, Breakout, Frogger, Pac-Man, Space Invaders, etc. Give your implementation a personal twist.
Pac-Man