/**************************************************************************** By Xinran Ma October 4th, 2017 Welcome to Maze Dash. Get to the blue goal at the end for a point. Avoid walls and white squares. Controls are W/A/S/D (if you can get a score higher than 5, literally you are god.) ****************************************************************************/ //Player variables float playerX = 310; float playerY = 375; float playerXDirect = 0; float playerYDirect = 0; //Projectile square variables float squareX = 0; float squareY = 200; //Score count int score = 0; //I set up the project and print a brief of the game void setup () { size(400, 400); background(0); frameRate(60); ellipseMode(CENTER); noStroke(); println( "Welcome to Maze Dash. Get to the blue goal at the end for a point. Avoid walls and white squares!" ); println("Controls: W/A/S/D to move Player"); } /* Where I execute functions. I execute the collision/update ones first so that the calculations and rule checking can be done in advance. (hard to explain, should be self explanatory) */ void draw () { drawBackground(); wallCollision(); squareCollision(); goalReset(); updateSquare(); updatePlayer(); drawGrid(); drawWalls(); drawGoal(); drawPlayer(); drawSquare(); } //Background void drawBackground() { rectMode(CORNER); fill(232, 245, 247); rect(0, 0, 400, 400); } //Obligatory loop for pattern //Drawing a vertical line every 10 pixels void drawGrid() { stroke(255); int x = 0; while(x<width){ line(x,0,x,height); x = x+10; } } //Walls made out of rectangles void drawWalls() { fill(245, 255, 234); noStroke(); rectMode(CORNERS); rect(0, 0, 60, 400); rect(340, 0, 400, 400); rect(10, 360, 280, 400); rect(120, 240, 370, 300); rect(10, 120, 280, 180); rect(120, 0, 370, 60); } //Blue Goal rectangle void drawGoal() { fill(188, 236, 242); rect(60, 0, 120, 20); } //Drawing player void drawPlayer() { fill(188, 236, 242); ellipse(playerX, playerY, 35, 35); } //Update player, setting speed (5) and direction (positive, negative) void updatePlayer() { playerX = playerX+5 * playerXDirect; playerY = playerY+5 * playerYDirect; } //Drawing square /* (fun fact I was contemplating there to be multiple of these but it didn't make for very encouraging gameplay so I left it out intentionally) */ void drawSquare() { fill(255); rectMode(CENTER); rect(squareX, squareY, 30, 30); } //Updating square, setting speed (8), randomly generating where //along the y-axis they spawn void updateSquare() { squareX += 8; if (squareX >=400) { squareY = random(0, 400); squareX = 0; } } //If any instance of player entering "wall area", they are sent back //to the spawn area and lose one point if they already have accumulated some. void wallCollision(){ if ( playerX>340 | playerX<60 | playerX<280 & playerY>360 | playerY>240 & playerY<300 & playerX>120 | playerX<280 & playerY<180 & playerY>120 | playerX>120 & playerY<60 | playerY>400 ){ playerX = 310; playerY = 375; if (score > 0){ score = score-1; println("Score", score); } } } //Testing if square collides within player position's 30px radius. //If they collide, player loses one point and is respawned void squareCollision(){ if ( squareX >= (playerX-30) & squareX <= (playerX+30) & squareY >= (playerY-30) & squareY <= (playerY+30) ){ playerX = 310; playerY = 375; if (score > 0){ score = score-1; println("Score", score); } } } //If the player gets to the height of the goal, they are "looped back" to the //respawn point. They get a point. void goalReset(){ if (playerY<20){ playerX = 310; playerY = 375; score = score + 1; println("Score", score); } } //WASD controls for direction void keyPressed() { if (key == 'w') { playerYDirect = -1; } if (key == 's') { playerYDirect = 1; } if (key == 'a') { playerXDirect = -1; } if (key == 'd') { playerXDirect = 1; } } //Releasing a key makes that axis immobile. //You only go in one direction, unless you press two buttons at once //Stops if you don't press anything. void keyReleased() { if (key == 'w') { playerYDirect = 0; } else if (key == 's') { playerYDirect = 0; } else if (key == 'a') { playerXDirect = 0; } else if (key == 'd') { playerXDirect = 0; } }