//////////////////////////////
// Maze Game by Bradley LeDonne
// Interactive Toy - Assignment 2
//This program can be controlled using WASD to move, you must make it to the yellow platform to win!
//The code for rect rect intersect was taken off open processing: http://www.openprocessing.org/sketch/8005 all credit for it goes to the creator
//////////////////////////////
//
//define variable for player position
float playerPositionX;
float playerPositionY;
//first obstacle
float obstacleX1 = 50;
float obstacleY1 = 340;
float obstacleX2 = 400;
float obstacleY2 = 10;
//second obstacle
float obstacleX3 = 10;
float obstacleY3 = 305;
float obstacleX4 = 325;
float obstacleY4 = 10;
//third obstacle
float obstacleX5 = 50;
float obstacleY5 = 270;
float obstacleX6 = 400;
float obstacleY6 = 10;
//fourth obstacle - veritcal
float obstacleX7 = 50;
float obstacleY7 = 100;
float obstacleX8 = 10;
float obstacleY8 = 175;
//fifth obstacle
float obstacleX9 = 50;
float obstacleY9 = 100;
float obstacleX10 = 300;
float obstacleY10 = 10;
//sixth obstacle - vertical
float obstacleX11 = 340;
float obstacleY11 = 0;
float obstacleX12 = 10;
float obstacleY12 = 100;
//seventh obstacle
float obstacleX13 = 0;
float obstacleY13 = 65;
float obstacleX14 = 300;
float obstacleY14 = 10;
//Win Platform
float winX1 = 0;
float winY1 = 10;
float winX2 = 50;
float winY2 = 55;
//define player size
float playerSize=20;
//set default movement state
boolean goLeft=false;
boolean goRight=false;
boolean goUp=false;
boolean goDown=false;
//set canvas size and player position
void setup() {
size(400, 400);
playerPositionX=width/1.07;
playerPositionY=height/1.07;
}
//call custom function to start at program start
void draw() {
background(0);
drawWin();
drawPlayer();
drawObstacles();
movePlayerPosition();
DrawBorder();
collisionDetect();
}
//draw the player square
void drawPlayer() {
rectMode(CORNER);
fill(random(0, 255), random(0, 255), random(0, 255));
rect(playerPositionX, playerPositionY, playerSize, playerSize);
}
//make movement possible with function
void movePlayerPosition() {
//moves up and down
if (goUp && playerPositionY > 20)
{
playerPositionY = playerPositionY - 4;
} else if (goDown && playerPositionY < 400-playerSize)
{
playerPositionY = playerPositionY + 4;
}
//moves right and left
if (goLeft && playerPositionX > 20)
{
playerPositionX = playerPositionX - 4;
} else if (goRight && playerPositionX <400-playerSize)
{
playerPositionX = playerPositionX + 4;
}
}
//draw the red border on the edges of screen
void DrawBorder() {
noStroke();
rectMode(CENTER);
fill(255, 0, 0);
rect(0, 200, 40, 400);
rect(200, 0, 400, 40);
rect(0, 400, 800, 15);
rect(400, 0, 10, 800);
}
//set WASD to activate booleans to true
void keyPressed() {
if (key == 'w') {
goUp=true;
} else if (key == 'a') {
goLeft=true;
} else if (key == 's') {
goDown=true;
} else if (key == 'd') {
goRight=true;
}
}
//when WASD released reset booleans to false
void keyReleased() {
if (key == 'w') {
goUp=false;
} else if (key == 'a') {
goLeft=false;
} else if (key == 's') {
goDown=false;
} else if (key == 'd') {
goRight=false;
}
}
//detect for collision with border and walls
void collisionDetect() {
//border collision detection
if (playerPositionX <= 20)
{
death();
} else if (playerPositionY<=20) {
death();
} else if (playerPositionX>=380) {
death();
} else if (playerPositionY>=380) {
death();
}
//detect for the walls/obstacles around the map using the rectRectIntersect function
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX1, obstacleY1, obstacleX1+obstacleX2, obstacleY1+obstacleY2) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX3, obstacleY3, obstacleX3+obstacleX4, obstacleY3+obstacleY4) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX5, obstacleY5, obstacleX5+obstacleX6, obstacleY5+obstacleY6) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX7, obstacleY7, obstacleX7+obstacleX8, obstacleY7+obstacleY8) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX9, obstacleY9, obstacleX9+obstacleX10, obstacleY9+obstacleY10) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX11, obstacleY11, obstacleX11+obstacleX12, obstacleY11+obstacleY12) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, obstacleX13, obstacleY13, obstacleX13+obstacleX14, obstacleY13+obstacleY14) == true) {
death();
}
if (rectRectIntersect(playerPositionX, playerPositionY, playerPositionX, playerPositionY, winX1, winY1, winX1+winX2, winY1+winY2) == true) {
win();
}
}
//function for collision detection
boolean rectRectIntersect(float left, float top, float right, float bottom,
float otherLeft, float otherTop, float otherRight, float otherBottom) {
return !(left > otherRight || right < otherLeft || top > otherBottom || bottom < otherTop);
}
//reset player position on death
void death() {
playerPositionX=width/1.07;
playerPositionY=height/1.07;
}
//on win display rainbow lines on screen
void win() {
for (int i=0; i<=400; i=i+15) {
stroke((random(0, 255)), random(0, 255), random(0, 255));
strokeWeight(5);
line(i, 400, i, 0);
}
}
//draw the obstacles around the map
void drawObstacles() {
rectMode(CORNER);
fill(255);
//first obstacle
rect(obstacleX1, obstacleY1, obstacleX2, obstacleY2);
//second obstacle
rect(obstacleX3, obstacleY3, obstacleX4, obstacleY4);
//third obstacle
rect(obstacleX5, obstacleY5, obstacleX6, obstacleY6);
//fourth obstacle - vertical
rect(obstacleX7, obstacleY7, obstacleX8, obstacleY8);
//fifth obstacle
rect(obstacleX9, obstacleY9, obstacleX10, obstacleY10);
//sixth obstacle
rect(obstacleX11, obstacleY11, obstacleX12, obstacleY12);
//seventh obstacle
rect(obstacleX13, obstacleY13, obstacleX14, obstacleY14);
}
//draw the win platform at end of map
void drawWin() {
//Win Platform
rectMode(CORNER);
fill(255, 255, 0);
rect(winX1, winY1, winX2, winY2);
}