Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PA4
- //variables - Pacman
- float pacmanX = 80;
- float pacmanY = 80;
- float xDelta = 10;
- float yDelta = 10;
- final float PACMAN_SIZE = 70;
- //variables - Ghost
- float ghostX = 600;
- float ghostY = 250;
- float ghostXDelta;
- float ghostYDelta;
- float GHOST_SIZE = 100;
- //variables - Dots
- float [] dotsX;
- float [] dotsY;
- int numOfDots = 50;
- // misc variables
- float dist;
- int playerScore = 0;
- /*
- * setup - prepares environment size and color
- */
- void setup() {
- //set canvas size and Pacman's color
- size(1200, 500);
- //define dot positions
- dotsX = new float[numOfDots];
- dotsY = new float[numOfDots];
- assignDotPositions();
- }
- /*
- * draw - move pacman on diagonals and bounce off top
- * and bottom while rapping around left to right
- */
- void draw() {
- background(0);
- drawDots();
- drawPacman();
- drawGhost();
- //move pacman
- //makePacmanBounce(); // Check bounds and make Pacman bounce off top/bottom
- makePacmanWrap(); // Allows for right/left wrapping
- // move ghost
- moveGhost();
- //Allows Pacman to eat dots
- for(int i = 0; i < numOfDots; i++) {
- dist = sqrt((dotsX[i] - pacmanX) * (dotsX[i] - pacmanX) + (dotsY[i] - pacmanY) * (dotsY[i] - pacmanY));
- if(dist <= 35.00) {
- dotsX[i] = -100;
- dotsY[i] = -100;
- playerScore++;
- break;
- }
- }
- //display score
- displayScore();
- }
- /*
- * drawPacman - draws a packman a the given x, y
- */
- void drawPacman() {
- fill(255, 255, 0);
- arc(pacmanX, pacmanY, PACMAN_SIZE, PACMAN_SIZE, 0.52, 5.76);
- }
- /*
- * makePacmanBounce - redirects pacman to opposite direction
- * when hitting top/bottom
- *
- * PA4 UPDATE: In PA4, this is now replaced by the keyPressed function. This function
- * checks to see which arrow key is pressed, either UP, DOWN, RIGHT, LEFT, and moves
- * Pacman accordingly in that direction
- *
- * The previous makePacmanBounce function has been commented out.
- */
- //void makePacmanBounce() {
- // pacmanX += xDelta; // Always going to be increasing
- // pacmanY += yDelta; // Decreasing or increasing depending on top/bottom boundaries
- // if (((pacmanY > height - (PACMAN_SIZE)/2)) || (pacmanY < 35)) { // He falls between these two points
- // yDelta = -yDelta;
- // }
- //}
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- pacmanY -= yDelta;
- } else if (keyCode == DOWN) {
- pacmanY += yDelta;
- } else if (keyCode == RIGHT) {
- pacmanX += xDelta;
- } else if (keyCode == LEFT) {
- pacmanX -= xDelta;
- }
- }
- }
- /*
- * makePacmanWrap - Wraps Pacman right, left, top, and bottom
- */
- void makePacmanWrap() {
- if (pacmanX > width + (PACMAN_SIZE)/2) { // Right hand side
- pacmanX = -((PACMAN_SIZE)/2);
- } else if (pacmanX < 0 - (PACMAN_SIZE)/2) { // Left hand side
- pacmanX = width + ((PACMAN_SIZE)/2);
- } else if (pacmanY < 0 - (PACMAN_SIZE)/2) { // Top of screen
- pacmanY = height + ((PACMAN_SIZE)/2);
- } else if (pacmanY > height + (PACMAN_SIZE)/2) { // Bottom of screen
- pacmanY = -((PACMAN_SIZE)/2);
- }
- }
- /*
- * drawGhost - Draws the ghost as an upside down half circle with a pink color
- */
- void drawGhost() {
- fill(255,105,180);
- arc(ghostX, ghostY, 100, 100, PI, TWO_PI); // Creats an upside down half ellipse.
- }
- /*
- * moveGhost - Gives the ghost a random movement
- * Also checks to make sure the ghost is within certain bounds
- */
- void moveGhost() {
- //ghostXDelta = random(-20.5, 20.5);
- //ghostYDelta = random(-20.5, 20.5);
- //ghostX += ghostXDelta;
- //ghostY += ghostYDelta;
- //if((ghostX > width - 50) || (ghostX < 50)) {
- // ghostXDelta = -ghostXDelta;
- //} else if((ghostY > height - 50) || (ghostY < 50)) {
- // ghostYDelta = -ghostYDelta;
- // }
- float targetX = pacmanX; // Ghost moves at a "delay". targetX facilitates this delay
- ghostX += (targetX - ghostX) * 0.02;
- float targetY = pacmanY;
- ghostY += (targetY - ghostY) * 0.02;
- }
- /*
- * assignDotPositions - For each number of dots, make random x and y
- * coordinates within the screen
- */
- void assignDotPositions() {
- for(int i = 0; i < numOfDots; i++) {
- dotsX[i] = random(10, 1180);
- dotsY[i] = random(10, 490);
- }
- }
- /*
- * drawDots - Draws the previously assigned dots onto the screen
- */
- void drawDots() {
- fill(255, 255, 0);
- for(int i = 0; i < numOfDots; i++) {
- ellipse(dotsX[i], dotsY[i], 5, 5);
- }
- }
- /*
- * displayScore - Displays the score in the upper right hand corner of the screen
- */
- void displayScore() {
- fill(255);
- textSize(40);
- text("Score: " + playerScore, 1000, 40);
- }
- Planning
- //First shape
- float xDelta = 10, yDelta = 10;
- float x = 250, y = 250;
- final float SHAPE_SIZE = 50;
- //Second shape
- float secondX = 400, secondY = 400;
- float secondXDelta = 5, secondYDelta = 5;
- final float SHAPE_SIZE2 = 100;
- float dist;
- int playerScore = 0;
- void setup() {
- size(500, 500);
- }
- void draw() {
- background(0);
- fill(255, 255, 0);
- rect(x, y, SHAPE_SIZE, SHAPE_SIZE);
- makeShapeWrap();
- displayScore();
- secondShape();
- secondShapeMove();
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- y -= yDelta;
- } else if (keyCode == DOWN) {
- y += yDelta;
- } else if (keyCode == RIGHT) {
- x += xDelta;
- } else if (keyCode == LEFT) {
- x -= xDelta;
- }
- }
- }
- void makeShapeWrap() {
- if (x > width + (SHAPE_SIZE)/2) { // Right hand side
- x = -((SHAPE_SIZE)/2);
- } else if (x < 0 - (SHAPE_SIZE)/2) { // Left hand side
- x = width + ((SHAPE_SIZE)/2);
- } else if (y < 0 - (SHAPE_SIZE)/2) { // Top of screen
- y = height + ((SHAPE_SIZE)/2);
- } else if (y > height + (SHAPE_SIZE)/2) { // Bottom of screen
- y = -((SHAPE_SIZE)/2);
- }
- }
- void displayScore() {
- textSize(40);
- text("Score: " + playerScore, 325, 40);
- }
- void secondShape() {
- fill(0, 0, 255);
- rect(secondX, secondY, SHAPE_SIZE2, SHAPE_SIZE2);
- }
- void secondShapeMove() {
- float targetX = x; // Mouse at a "delay". targetX facilitates this delay
- secondX += (targetX - secondX) * 0.02;
- float targetY = y;
- secondY += (targetY - secondY) * 0.02;
- //float targetX = x; // Mouse at a "delay". targetX facilitates this delay
- //secondX += (targetX - secondX) * 0.1;
- //float targetY = y;
- //secondY += (targetY - secondY) * 0.1;
- // // Keep the x and y values within the screen
- // if (((y > height - (SHAPE_SIZE2)/2)) || (mouseY < 50)) {
- // secondY = (SHAPE_SIZE2)/2;
- //}
- // if (((x > width - (SHAPE_SIZE2)/2)) || (x < 50)) {
- // secondX = (SHAPE_SIZE2)/2;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement