Why my function is not working as intended?
in
Programming Questions
•
1 years ago
Hello again Processing Forum,
For the past two days, I've been working on one of the questions in my coursework. The question and the incomplete sketch is as follows:
I have left out some parts of the coursework question which I feel were unnecessary towards addressing the issue. The first part of my task is to complete the function checkBallAgainstBat(). I highlight my attempts to accomplish this in blue."Develop some aspects of a very simple game, in Processing. The attached is an incomplete version of a game called Breakout....You need to develop the code as discussed below, to make the game work appropriately...You may add variables as necessary, and additional functions if needed...First complete the functions checkBallAgainstBat()...to do what is specified by them."
- //Basic Breakout game
- //Code from Matthew Yee-King
- //brick position
- float brickX;
- float brickY;
- //brick width and height
- float brickH;
- float brickW;
- //ball position
- float ballX;
- float ballY;
- //ball diameter
- float ballD;
- //ball direction
- float ballDx;
- float ballDy;
- //bat position
- float batX;
- //bat width
- float batW;
- float batH;
- //bat colour
- float batB;
- void setup() {
- size (500, 500, P2D);
- // set sizes of game items
- brickW = 100;
- brickH = 50;
- batW = 100;
- batH = 25;
- ballD = 25;
- batB = 255;
- //random brick position
- brickX = random(0, width - brickW);
- brickY = random(0, height/2);
- // bat in the centre
- batX = (width/2) - (batW/2);
- //ball atop bat
- ballX = batX + (batW/2);
- ballY = height - batH - (ballD/2);
- //ball movement
- ballDx = random(-5, 5);
- ballDy = -5;
- rectMode(CORNER);
- ellipseMode(CENTER);
- frameRate (12);
- }
- void draw() {
- //check for ball collision
- //with top or sides of bat
- checkBallAgainstBat();
- //check for ball collision with
- //left right and top walls
- //and bounce
- checkBallAgainstWalls();
- //check ball against brick
- checkBallAgainstBrick();
- //move the ball
- ballX += ballDx;
- ballY += ballDy;
- background(0);
- //draw the bat
- fill(0, 255, 0);
- rect(batX, height - batH, batW, batH);
- //draw the brick
- fill(0, 0, batB);
- batB = (batB +10) % 255;
- rect(brickX, brickY, brickW, brickH);
- //draw the ball
- fill(255, 0, 0);
- ellipse(ballX, ballY, ballD, ballD);
- if (keyCode == 37) { // left cursor key
- batX -= 10;
- //keep it on the screen
- if (batX < 0) {
- batX = 0;
- }
- }
- if (keyCode == 39) { //right cursor key
- batX += 10;
- if (batX > (width - batW)) {
- batX = width - batW;
- }
- }
- }
- //when they let go of they key, reset the keyCode
- void keyReleased() {
- keyCode = -1;
- }
- //this function checks if the ball has hit the top or sides of the bat and
- //update its
- //direction as appropriate so the ball bounces off the bat
- void checkBallAgainstBat(){
- if ((ballY + ballD/2 > height - batH) && (ballX >= batX && ballX <= batX + batW)){
- ballDy *= -1;
- }
- if ((ballX + ballD/2 <= batX || ballX - ballD/2 >= batX + batW) && (ballY >= height - batH && ballY <= height)){
- ballDx *= -1;
- }
- }
- //this function checks if the ball has hit the brick. It should bounce off
- //the brick and return true if so
- boolean checkBallAgainstBrick() {
- return false;
- }
- //this function checks if the ball has hit the top, left or right
- //walls and update its
- //direction as appropriate so the ball bounces off the walls
- void checkBallAgainstWalls(){
- if (ballX + ballD/2 > width){// right wall
- ballDx *= -1;
- }
- if (ballX - ballD/2 < 0){// left wall
- ballDx *= -1;
- }
- if (ballY - ballD/2 < 0){// top wall
- ballDy *= -1;
- }
- }
I'm hoping I can get some detail explanations as to why my approach to the function does not work, and will appreciate if someone can rectified my logic (or the lack there-off) and equip me with new insights so I can tackle the question armed with new knowledge.
questions by a noob 'processor',
derrick.
1