# Maze Game

For this week’s assignment, I made a simple maze game. The game has two modes: one player and two players. For the one player mode, the player uses keys to move the dot around the maze. The player wins once he reaches the end of the maze. For the two players mode, two players start out on different sides of the maze. Then they move the dot to reach the opposite side of the maze. The one who reaches the other side first wins the game. In both modes, players need to move the dot carefully so as not to touch any edge of the maze. If the dot is in contact with the edge, the player loses the game. (p.s. Don’t try to crash the dots. You will die.)

Here are some basic instructions for my game.

‘a’ – move dot 1 to the left

‘d’ – move dot 1 to the right

‘w’ – move dot 1 up

’s’ – move dot 1 down

‘j’ – move dot 2 to the left

‘l’ – move dot 2 to the right

‘i’ – move dot 2 up

’k’ – move dot 2 down

‘+’ – increase speed

“-” – decrease speed

space – start the game

’n’ – restart

My code is attached below.

```boolean start = false;
boolean moveU1 = false;
boolean moveL1 = false;
boolean moveR1 = false;
boolean moveD1 = false;
boolean moveU2 = false;
boolean moveL2 = false;
boolean moveR2 = false;
boolean moveD2 = false;
boolean move = true;
float gap1 = 10; //distance from the edge
float direction; //direction that the dot moves
float xPos1;
float yPos1;
float xPos2;
float yPos2;
float diam = 15;
float xSpeed = 1;
float ySpeed = 1;
int dead1 = 0;
int dead2 = 0;
int num_player = 0;

void setup() {
size(720, 720);
}

void draw() {
int time = second();
float unitL = (width-gap1*2)/10;

basic_setup();

if (move == true) {
move1();
move2();
}

//one player
if (num_player == 1) {
background(255);
maze(); //draw background maze

if  (start == false && dead1 == 0) {
changingDot(gap1+unitL*0.5, gap1+unitL*3.5);
xPos1 = gap1+unitL*0.5;
yPos1 = gap1+unitL*3.5;
}
if (start == true) {
fill(255, 255, 0); //yellow
noStroke();
ellipse(xPos1, yPos1, diam, diam);

checkDeath1();
checkWin1();
}
}

//two players
if (num_player == 2) {
background(255);
maze(); //draw background maze

if  (start == false && dead1 == 0 && dead2 == 0) {
changingDot(gap1+unitL*0.5, gap1+unitL*3.5);
changingDot(gap1+unitL*9.5, gap1+unitL*1.5);

xPos2 = gap1+unitL*9.5;
yPos2 = gap1+unitL*1.5;

xPos1 = gap1+unitL*0.5;
yPos1 = gap1+unitL*3.5;
}
if (start == true) {
fill(255, 255, 0); //yellow
noStroke();
ellipse(xPos1, yPos1, diam, diam); //player 1 dot
ellipse(xPos2, yPos2, diam, diam); //player 2 dot

checkDeath1();
checkDeath2();
checkWin1();
checkWin2();

//COLLISION
if (dist(xPos1, yPos1, xPos2, yPos2) <= diam) {
background(0);
println("THEY ARE DESTROYED");
destroyed();
ani_end();
}
}
}
}

void keyPressed() {
if (key == ' ' ) { //press space to start the game
start = true;
}

if (key == 'n') { //restart the game
start = false;
num_player = 0;
move = true;
xSpeed = 1;
ySpeed = 1;
}

//change speed
if (key == '=') {
xSpeed += 1;
ySpeed += 1;
}
if (key == '-') {
xSpeed -= 1;
ySpeed -= 1;
}

//player 1 direction
if (key == 'a') {
moveL1 = true;
}
if (key == 'd') {
moveR1 = true;
}
if (key == 'w') {
moveU1 = true;
}
if (key == 's') {
moveD1 = true;
}

//player 2 direction
if (key == 'j') {
moveL2 = true;
}
if (key == 'l') {
moveR2 = true;
}
if (key == 'i') {
moveU2 = true;
}
if (key == 'k') {
moveD2 = true;
}
}

void keyReleased() {
if (key == 'a') {
moveL1 = false;
}
if (key == 'd') {
moveR1 = false;
}
if (key == 'w') {
moveU1 = false;
}
if (key == 's') {
moveD1 = false;
}

if (key == 'j') {
moveL2 = false;
}
if (key == 'l') {
moveR2 = false;
}
if (key == 'i') {
moveU2 = false;
}
if (key == 'k') {
moveD2 = false;
}
}

void mousePressed() {
float unitL = (width-gap1*2)/10;

if (mouseX >= gap1+unitL*2 && mouseX <= gap1+unitL*8) {
if (mouseY >= gap1+unitL*3 && mouseY <= gap1+unitL*4.5) {
num_player = 1;
}
}

if (mouseX >= gap1+unitL*2 && mouseX <= gap1+unitL*8) {
if (mouseY >= gap1+unitL*5 && mouseY <= gap1+unitL*6.5) {
num_player = 2;
}
}
}

void basic_setup() {
float unitL = (width-gap1*2)/10;

background(255);
fill(255);
noStroke();
rect(gap1+unitL*2, gap1+unitL*3, unitL*6, unitL*1.5); //rect for one player
rect(gap1+unitL*2, gap1+unitL*5, unitL*6, unitL*1.5); //rect for two player
textSize(50);
fill(255, 0, 0);//red
text("ONE PLAYER", gap1+unitL*2.5, gap1+unitL*4);
text("TWO PLAYERS", gap1+unitL*2.5, gap1+unitL*6);
}

void maze() {
stroke(0);
strokeWeight(3);
float unitL = (width-gap1*2)/10;

//draw horizontal lines
line(gap1, gap1, width-gap1, gap1); //end of line 0
line(gap1+unitL, gap1+unitL, gap1+unitL*2, gap1+unitL);
line(gap1+unitL*4, gap1+unitL, gap1+unitL*5, gap1+unitL);
line(gap1+unitL*6, gap1+unitL, gap1+unitL*8, gap1+unitL); //end of line 1
line(gap1+unitL*2, gap1+unitL*2, gap1+unitL*7, gap1+unitL*2);//end of line 2
line(gap1+unitL*3, gap1+unitL*3, gap1+unitL*8, gap1+unitL*3);//end of line 3
line(gap1+unitL*0, gap1+unitL*4, gap1+unitL*1, gap1+unitL*4);
line(gap1+unitL*5, gap1+unitL*4, gap1+unitL*7, gap1+unitL*4);//end of line 4
line(gap1+unitL*3, gap1+unitL*5, gap1+unitL*4, gap1+unitL*5);//end of line 5
line(gap1+unitL*4, gap1+unitL*6, gap1+unitL*5, gap1+unitL*6);//end of line 6
line(gap1+unitL*2, gap1+unitL*7, gap1+unitL*6, gap1+unitL*7);//end of line 7
line(gap1+unitL*3, gap1+unitL*8, gap1+unitL*7, gap1+unitL*8);
line(gap1+unitL*8, gap1+unitL*8, gap1+unitL*9, gap1+unitL*8);//end of line 8
line(gap1+unitL*0, gap1+unitL*9, gap1+unitL*9, gap1+unitL*9);//end of line 9
line(gap1, height-gap1, width-gap1, height-gap1); //end of line 10

//draw vertical lines
line(gap1, gap1, gap1, gap1+unitL*3);
line(gap1, gap1+unitL*4, gap1, height-gap1); // end of column 0
line(gap1+unitL, gap1+unitL, gap1+unitL, gap1+unitL*8); //end of column 1
line(gap1+unitL*2, gap1+unitL*2, gap1+unitL*2, gap1+unitL*6);
line(gap1+unitL*2, gap1+unitL*7, gap1+unitL*2, gap1+unitL*8); // end of column 2
line(gap1+unitL*3, gap1, gap1+unitL*3, gap1+unitL*2);
line(gap1+unitL*3, gap1+unitL*3, gap1+unitL*3, gap1+unitL*4);
line(gap1+unitL*3, gap1+unitL*5, gap1+unitL*3, gap1+unitL*6); //end of column 3
line(gap1+unitL*4, gap1+unitL*4, gap1+unitL*4, gap1+unitL*5);//end of column 4
line(gap1+unitL*5, gap1, gap1+unitL*5, gap1+unitL);
line(gap1+unitL*5, gap1+unitL*4, gap1+unitL*5, gap1+unitL*6); //end of column 5
line(gap1+unitL*6, gap1+unitL*5, gap1+unitL*6, gap1+unitL*7); //end of column 6
line(gap1+unitL*7, gap1+unitL*1, gap1+unitL*7, gap1+unitL*2);
line(gap1+unitL*7, gap1+unitL*4, gap1+unitL*7, gap1+unitL*8); //end of column 7
line(gap1+unitL*8, gap1+unitL*2, gap1+unitL*8, gap1+unitL*8);//end of column 8
line(gap1+unitL*9, gap1+unitL*1, gap1+unitL*9, gap1+unitL*7);
line(gap1+unitL*9, gap1+unitL*8, gap1+unitL*9, gap1+unitL*9);//end of column 9
line(gap1+unitL*10, gap1, gap1+unitL*10, gap1+unitL*1);
line(gap1+unitL*10, gap1+unitL*2, gap1+unitL*10, gap1+unitL*10);//end of column 10
}

void changingDot(float x_, float y_) {
float unitL = (width-gap1*2)/10;
int time = second();

//xPos1 = gap1+unitL*0.5;
//yPos1 = gap1+unitL*3.5;

if (time % 2 == 0) {
fill(255, 0, 0); //red
noStroke();
ellipse(x_, y_, diam, diam);
} else {
fill(255);
noStroke();
ellipse(x_, y_, diam, diam);
}
}

void checkDeath1() {
float unitL = (width-gap1*2)/10;

if (xPos1 <= gap1 || yPos1 <= 0 || yPos1 >= height) { //exceed the boundary
}

//check the columns
if (xPos1 >= gap1+unitL-diam/2 && xPos1 <= gap1+unitL+diam/2) { //column 1
if (yPos1 >=  gap1+unitL && yPos1 <= gap1+unitL*8) {
}
}

if (xPos1 >= gap1+unitL*2-diam/2 && xPos1 <= gap1+unitL*2+diam/2) { //column 2
if (yPos1 >=  gap1+unitL*2 && yPos1 <= gap1+unitL*6) {
}
if (yPos1 >=  gap1+unitL*7 && yPos1 <= gap1+unitL*8) {
}
}

if (xPos1 >= gap1+unitL*3-diam/2 && xPos1 <= gap1+unitL*3+diam/2 ) { //column 3
if (yPos1 >=  gap1+unitL*0 && yPos1 <= gap1+unitL*2) {
}
if (yPos1 >=  gap1+unitL*3 && yPos1 <= gap1+unitL*4) {
}
if (yPos1 >=  gap1+unitL*5 && yPos1 <= gap1+unitL*6) {
}
}

if (xPos1 >= gap1+unitL*4-diam/2 && xPos1 <= gap1+unitL*4+diam/2) { //column 4
if (yPos1 >=  gap1+unitL*4 && yPos1 <= gap1+unitL*5) {
}
}

if (xPos1 >= gap1+unitL*5-diam/2 && xPos1 <= gap1+unitL*5+diam/2) { //column 5
if (yPos1 >=  gap1+unitL*0 && yPos1 <= gap1+unitL*1) {
}
if (yPos1 >=  gap1+unitL*4 && yPos1 <= gap1+unitL*6) {
}
}

if (xPos1 >= gap1+unitL*6-diam/2 && xPos1 <= gap1+unitL*6+diam/2) { //column 6
if (yPos1 >=  gap1+unitL*5 && yPos1 <= gap1+unitL*7) {
}
}

if (xPos1 >= gap1+unitL*7-diam/2 && xPos1 <= gap1+unitL*7+diam/2) { //column 7
if (yPos1 >=  gap1+unitL*1 && yPos1 <= gap1+unitL*2) {
}
if (yPos1 >=  gap1+unitL*4 && yPos1 <= gap1+unitL*8) {
}
}

if (xPos1 >= gap1+unitL*8-diam/2 && xPos1 <= gap1+unitL*8+diam/2) { //column 8
if (yPos1 >=  gap1+unitL*2 && yPos1 <= gap1+unitL*8) {
}
}

if (xPos1 >= gap1+unitL*9-diam/2 && xPos1 <= gap1+unitL*9+diam/2) { //column 9
if (yPos1 >=  gap1+unitL*1 && yPos1 <= gap1+unitL*7) {
}
if (yPos1 >=  gap1+unitL*8 && yPos1 <= gap1+unitL*9) {
}
}

if (xPos1 >= gap1+unitL*10-diam/2 && xPos1 <= gap1+unitL*10+diam/2) { //column 10
if (yPos1 >=  gap1+unitL*0 && yPos1 <= gap1+unitL*1) {
}
if (yPos1 >=  gap1+unitL*2 && yPos1 <= gap1+unitL*10) {
}
}

//check the lines
if (yPos1 >= gap1 + unitL*1-diam/2 && yPos1 <= gap1 + unitL*1+diam/2) { //line 1
if (xPos1 >= gap1+unitL*1 && xPos1 <= gap1+unitL*2) {
}
if (xPos1 >= gap1+unitL*4 && xPos1 <= gap1+unitL*5) {
}
if (xPos1 >= gap1+unitL*6 && xPos1 <= gap1+unitL*8) {
}
}

if (yPos1 >= gap1 + unitL*2-diam/2 && yPos1 <= gap1 + unitL*2+diam/2) { //line 2
if (xPos1 >= gap1+unitL*2 && xPos1 <= gap1+unitL*7) {
}
}

if (yPos1 >= gap1 + unitL*3-diam/2 && yPos1 <= gap1 + unitL*3+diam/2) { //line 3
if (xPos1 >= gap1+unitL*3 && xPos1 <= gap1+unitL*8) {
}
}

if (yPos1 >= gap1 + unitL*4-diam/2 && yPos1 <= gap1 + unitL*4+diam/2) { //line 4
if (xPos1 >= gap1+unitL*0 && xPos1 <= gap1+unitL*1) {
}
if (xPos1 >= gap1+unitL*5 && xPos1 <= gap1+unitL*7) {
}
}

if (yPos1 >= gap1 + unitL*5-diam/2 && yPos1 <= gap1 + unitL*5+diam/2) { //line 5
if (xPos1 >= gap1+unitL*3 && xPos1 <= gap1+unitL*4) {
}
}

if (yPos1 >= gap1 + unitL*6-diam/2 && yPos1 <= gap1 + unitL*6+diam/2) { //line 6
if (xPos1 >= gap1+unitL*4 && xPos1 <= gap1+unitL*5) {
}
}

if (yPos1 >= gap1 + unitL*7-diam/2 && yPos1 <= gap1 + unitL*7+diam/2) { //line 7
if (xPos1 >= gap1+unitL*2 && xPos1 <= gap1+unitL*6) {
}
}

if (yPos1 >= gap1 + unitL*8-diam/2 && yPos1 <= gap1 + unitL*8+diam/2) { //line 8
if (xPos1 >= gap1+unitL*3 && xPos1 <= gap1+unitL*7) {
}
if (xPos1 >= gap1+unitL*8 && xPos1 <= gap1+unitL*9) {
}
}

if (yPos1 >= gap1 + unitL*9-diam/2 && yPos1 <= gap1 + unitL*9+diam/2) { //line 9
if (xPos1 >= gap1+unitL*0 && xPos1 <= gap1+unitL*9) {
}
}

//PLAER 1 LOSE
if (dead1 == 1 && dead2 != 1) {
background(0);
textSize(90);
fill(255, 0, 0);//red
text("PLAYER 1 LOST", 30, width/2);
move = false;
}
}

void checkDeath2() {
float unitL = (width-gap1*2)/10;
if (xPos2 >= gap1+unitL*10 || yPos2 <= 0 || yPos2 >= height) { //exceed the boundary
}

//check the columns
if (xPos2 >= gap1+unitL-diam/2 && xPos2 <= gap1+unitL+diam/2) { //column 1
if (yPos2 >=  gap1+unitL && yPos2 <= gap1+unitL*8) {
}
}

if (xPos2 >= gap1+unitL*2-diam/2 && xPos2 <= gap1+unitL*2+diam/2) { //column 2
if (yPos2 >=  gap1+unitL*2 && yPos2 <= gap1+unitL*6) {
}
if (yPos2 >=  gap1+unitL*7 && yPos2 <= gap1+unitL*8) {
}
}

if (xPos2 >= gap1+unitL*3-diam/2 && xPos2 <= gap1+unitL*3+diam/2 ) { //column 3
if (yPos2 >=  gap1+unitL*0 && yPos2 <= gap1+unitL*2) {
}
if (yPos2 >=  gap1+unitL*3 && yPos2 <= gap1+unitL*4) {
}
if (yPos2 >=  gap1+unitL*5 && yPos2 <= gap1+unitL*6) {
}
}

if (xPos2 >= gap1+unitL*4-diam/2 && xPos2 <= gap1+unitL*4+diam/2) { //column 4
if (yPos2 >=  gap1+unitL*4 && yPos2 <= gap1+unitL*5) {
}
}

if (xPos2 >= gap1+unitL*5-diam/2 && xPos2 <= gap1+unitL*5+diam/2) { //column 5
if (yPos2 >=  gap1+unitL*0 && yPos2 <= gap1+unitL*1) {
}
if (yPos2 >=  gap1+unitL*4 && yPos2 <= gap1+unitL*6) {
}
}

if (xPos2 >= gap1+unitL*6-diam/2 && xPos2 <= gap1+unitL*6+diam/2) { //column 6
if (yPos2 >=  gap1+unitL*5 && yPos2 <= gap1+unitL*7) {
}
}

if (xPos2 >= gap1+unitL*7-diam/2 && xPos2 <= gap1+unitL*7+diam/2) { //column 7
if (yPos2 >=  gap1+unitL*1 && yPos2 <= gap1+unitL*2) {
}
if (yPos2 >=  gap1+unitL*4 && yPos2 <= gap1+unitL*8) {
}
}

if (xPos2 >= gap1+unitL*8-diam/2 && xPos2 <= gap1+unitL*8+diam/2) { //column 8
if (yPos2 >=  gap1+unitL*2 && yPos2 <= gap1+unitL*8) {
}
}

if (xPos2 >= gap1+unitL*9-diam/2 && xPos2 <= gap1+unitL*9+diam/2) { //column 9
if (yPos2 >=  gap1+unitL*1 && yPos2 <= gap1+unitL*7) {
}
if (yPos1 >=  gap1+unitL*8 && yPos1 <= gap1+unitL*9) {
}
}

if (xPos2 >= gap1+unitL*10-diam/2 && xPos2 <= gap1+unitL*10+diam/2) { //column 10
if (yPos2 >=  gap1+unitL*0 && yPos2 <= gap1+unitL*1) {
}
if (yPos2 >=  gap1+unitL*2 && yPos2 <= gap1+unitL*10) {
}
}

//check the lines
if (yPos2 >= gap1 + unitL*1-diam/2 && yPos2 <= gap1 + unitL*1+diam/2) { //line 1
if (xPos2 >= gap1+unitL*1 && xPos2 <= gap1+unitL*2) {
}
if (xPos2 >= gap1+unitL*4 && xPos2 <= gap1+unitL*5) {
}
if (xPos2 >= gap1+unitL*6 && xPos2 <= gap1+unitL*8) {
}
}

if (yPos2 >= gap1 + unitL*2-diam/2 && yPos2 <= gap1 + unitL*2+diam/2) { //line 2
if (xPos2 >= gap1+unitL*2 && xPos2 <= gap1+unitL*7) {
}
}

if (yPos2 >= gap1 + unitL*3-diam/2 && yPos2 <= gap1 + unitL*3+diam/2) { //line 3
if (xPos2 >= gap1+unitL*3 && xPos2 <= gap1+unitL*8) {
}
}

if (yPos2 >= gap1 + unitL*4-diam/2 && yPos2 <= gap1 + unitL*4+diam/2) { //line 4
if (xPos2 >= gap1+unitL*0 && xPos2 <= gap1+unitL*1) {
}
if (xPos2 >= gap1+unitL*5 && xPos2 <= gap1+unitL*7) {
}
}

if (yPos2 >= gap1 + unitL*5-diam/2 && yPos2 <= gap1 + unitL*5+diam/2) { //line 5
if (xPos2 >= gap1+unitL*3 && xPos2 <= gap1+unitL*4) {
}
}

if (yPos2 >= gap1 + unitL*6-diam/2 && yPos2 <= gap1 + unitL*6+diam/2) { //line 6
if (xPos2 >= gap1+unitL*4 && xPos2 <= gap1+unitL*5) {
}
}

if (yPos2 >= gap1 + unitL*7-diam/2 && yPos2 <= gap1 + unitL*7+diam/2) { //line 7
if (xPos2 >= gap1+unitL*2 && xPos2 <= gap1+unitL*6) {
}
}

if (yPos2 >= gap1 + unitL*8-diam/2 && yPos2 <= gap1 + unitL*8+diam/2) { //line 8
if (xPos2 >= gap1+unitL*3 && xPos2 <= gap1+unitL*7) {
}
if (xPos2 >= gap1+unitL*8 && xPos2 <= gap1+unitL*9) {
}
}

if (yPos2 >= gap1 + unitL*9-diam/2 && yPos2 <= gap1 + unitL*9+diam/2) { //line 9
if (xPos2 >= gap1+unitL*0 && xPos2 <= gap1+unitL*9) {
}
}

//PLAYER 2 LOSE
if (dead2 == 1 && dead1 != 1) {
background(0);
textSize(90);
fill(255, 0, 0);//red
text("PLAYER 2 LOST", 30, width/2);
move = false;
}
}

void checkWin1() {
float unitL = (width-gap1*2)/10;

if (dead1 == 0) {
if (xPos1 >= gap1+unitL*10-diam/2) {
if (yPos1 > gap1+unitL*1 && yPos1 < gap1+unitL*2) {
background(0);
println("P1 WIN");
textSize(90);
fill(255, 0, 0);//red
text("PLAYER 1 WON!!", gap1, width/2);
move = false;
}
}
}
}

void checkWin2() {
float unitL = (width-gap1*2)/10;

if (dead2 == 0) {
if (xPos2 <= gap1) {
if (yPos2 > gap1+unitL*3 && yPos1 < gap1+unitL*4) {
background(0);
println("P2 WIN");
textSize(90);
fill(255, 0, 0);//red
text("PLAYER 2 WON!!", gap1, width/2);
move = false;
}
}
}
}

void destroyed() {
background(0);
textSize(80);
fill(255, 0, 0);//red
text("GAME OVER", 125, height/2);
}

void ani_end() {
float xPosH = width/2;
float yPosH1 = height;
float yPosH2 = 0;

float xSpeedH = random (-30, 30);
float ySpeedH1 = random (-30, -10);
float ySpeedH2 = random (0, 30);

fill(255, 0, 0); //red
ellipse(xPosH, yPosH1, 5, 5);
ellipse(xPosH, yPosH2, 5, 5);

for (int j = 0; j < 30; j += 1) {
xPosH += xSpeedH;
yPosH1 += ySpeedH1;
ellipse(xPosH, yPosH1, 5, 5);
}

for (int i = 0; i < 30; i += 1) {
xPosH += xSpeedH;
yPosH2 += ySpeedH2;
ellipse(xPosH, yPosH2, 5, 5);
}
}

void move1() {
if (moveL1 == true) {
xPos1 -= xSpeed;
}
if (moveR1 == true) {
xPos1 += xSpeed;
}
if (moveU1 == true) {
yPos1 -= xSpeed;
}
if (moveD1 == true) {
yPos1 += xSpeed;
}
}

void move2() {
if (moveL2 == true) {
xPos2 -= xSpeed;
}
if (moveR2 == true) {
xPos2 += xSpeed;
}
if (moveU2 == true) {
yPos2 -= xSpeed;
}
if (moveD2 == true) {
yPos2 += xSpeed;
}
}```