[Breakout game] How can i change my code, to move the paddle with my mouse via clicking.

edited March 2018 in Questions about Code

Hey Guys. I'm in some trouble with my Code.. i am a newbie about Java and now i wrote a Code with my teacher at the university. The problem is, i want to change my paddle movement from KeyPressed to "MouseListener". or i can just add MouseListener to KeyPressed so i can use them both. I just added the MouseListener via implements and then the class says: "The type BreakOut5 must implement the inherited abstract method MouseListener.mouseExited(MouseEvent)"

My second class is to create the ball and the actions.

Thank you in Advance!

`

import processing.core.PApplet; import java.awt.*; import java.awt.event.*;

public class BreakOut5 extends PApplet implements MouseListener {

public static void main(String[] args)
{
    PApplet.main("javaaufgabe.BreakOut5");    }



int[] stoneHits;

public void settings()
{



    size(500, 500);
}



ball ball;

public void setup()
{

    this.paddlePos = width / 2 - paddleWidth / 2;
    ball = new ball();

    ball.ballXPos = width / 2;

    stoneHits = new int[width / 80];
}

int thicc = 15;

public void draw()
{
    background(255);
    fill(0, 0, 0);
    noStroke();

    walls();
    paddle();
    ball.drawBall(this);

    bricks();

    //animate
    ball.ballYPos = ball.ballYPos + ball.ballYSpeed;
}

void bricks()
{
    int xPos = 30;
    int stoneNo = 0;
    while (xPos < width)
    {
        fill(0 + stoneHits[stoneNo] * 100);
        rect(xPos, 50, 40, 15);
        xPos += 80;
        stoneNo++;
    }
}

void hitBrick()
{
    int stoneNumb = 0;

    if (50 < ball.ballYPos && ball.ballYPos < 50 + 15)
    {
        while (stoneNumb < 6)
        {
            int xPos = 30 + 80 * stoneNumb;

            if (xPos < ball.ballXPos && ball.ballXPos < xPos + 40 && stoneHits[stoneNumb] < 3)
            {
                // hit it
                ball.ballYSpeed = -ball.ballYSpeed;

                stoneHits[stoneNumb] += 1;
            }

            stoneNumb++;
        }
    }
}

void hitPaddle()
{
    if (ball.ballYPos > height - thicc - 8)
    {
        if (paddlePos < ball.ballXPos && ball.ballXPos < paddlePos + paddleWidth)
        {
            ball.ballYSpeed = -ball.ballYSpeed;

            //angle
            ball.ballXSpeed = (ball.ballXPos - paddlePos - paddleWidth / 2) / 20;
        }
        else
        {
            //gameover
            ball.ballYPos = 42;
        }
    }
}

void hitWall()
{
    if (ball.ballXPos < thicc)
    {
        ball.ballXSpeed = -ball.ballXSpeed;
    }
    else if (ball.ballXPos > width - thicc)
    {
        ball.ballXSpeed = -ball.ballXSpeed;
    }

    if (ball.ballYPos < thicc + 8)
    {
        ball.ballYSpeed = -ball.ballYSpeed;
    }
}

void walls()
{
    rect(0, 0, width, thicc);
    rect(0, thicc, thicc, height);
    rect(width - thicc, thicc, thicc, height);
}

int paddlePos = width / 2;
int paddleWidth = 80;



void paddle()
{
    fill(0, 0, 0);
    rect(paddlePos, height - thicc - 5, paddleWidth, 2 * thicc);

    if (keyPressed && key == 'k')
    {
        //moveLeft
        paddlePos -= 5;

    }
    else if (keyPressed && key == 'l')
    {
        //moveright
        paddlePos += 5;
    }
}

} `

Tagged:

Answers

Sign In or Register to comment.