player goes through walls while jumping (JAVA) - java

I am trying to make a donkey kong game.
Currently, I have a boolean called jump which tells that game if the player is jumping or not, when the space bar is pressed.
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE){
StartJump();
}
}
StartJump: Makes the sprite start jumping by changing velocity. It also changes the jump boolean to true.
public void StartJump() {
if (onground) {
velocityY = -4;
onground = false;
jump = true;
}
}
KeyReleased:
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE){
EndJump();
}
}
EndJump: Sets jump to false
public void EndJump() {
if(velocityY < -6.0)
velocityY = -6;
jump = false;
}
The problem occurs when you hold down the spacebar, jump is set to true.
Which then causes the forloop which sets the Y position of Mario to not work and he falls through the floor.
Loop: bricks just contains rectangles for Mario to stand on
for(int i = 0; i < bricks.size(); i++) {
if(marHitBox.intersects(bricks.get(i)) && !mario.getClimb() && !mario.getJump() && marHitBox.intersects(jump.get(i))) {
mario.setMarY((int)bricks.get(i).getY()-40);
mario.setVeloY(0f);
mario.setOnGround(true);
}
}
It is the !mario.getJump() that is causing the problem. Is there any way to see if the spacebar is held for more than x milliseconds to make jump = false?

Try measuring the passing time between the spacebar is pressed and released, it will fit your need.

I think just a check would make it go away
public void StartJump(){
if (!jump && onground){
velocityY = -4;
onground = false;
jump = true;
}
}

Related

How do I register a full key press in Java?

I am working on a game that relies on the player's keyboard input.
The current implementation registers whether a key is currently pressed or not.
However, I would like to implement a system in which the player has to do a full key press in order to increment the game state. For example, they'd have to press and release 'A' in order to move to the next position. Then press and release the button to move on again, and so forth. Holding the button down should not result in moving forward.
Here is a very simplified version of what I've got right now:
int velocityX = 0;
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {ActiveA = true;}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {ActiveA = false;}
}
public void tick() {
x += velocityX;
if (ActiveA){velocityX = 5; }
else if (!ActiveA){velocityX = 0; }
}
The game class calls tick() at about 60 times a second, so X constantly increments while the key is pressed but not while the key is released.
By adding another variable that is set TRUE only when the player releases the key, we can set velocity to 5 for one tick, then set that variable false so it doesn't keep repeating.
int velocityX = 0;
boolean ActiveA = false;
boolean didFullPress = false;
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {
ActiveA = true;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A && ActiveA) {
ActiveA = false;
didFullPress = true;
}
}
public void tick() {
x += velocityX;
if (didFullPress){
velocityX = 5
didFullPress = false;
}
else {velocityX = 0 }
}

Java pong ball not moving in the other direction when hitting the paddle

i am trying to make the game Pong in Java. Now i have a problem with the collision on the paddle. It is not moving the other direction. It does work when it hits the sides of the panel.
For now i just needs to go the other way when it hits the X value of the paddle.
I made a function to check the collision.
What am i doing wrong here. Can someone please explain me this?
Thanks in advance
public void checkCollision()
{
if (ball.getY() == 0)
{
moveDown = true;
moveUp = false;
}
if ((ball.getY() + ball.getHeight()) == height)
{
moveDown = false;
moveUp = true;
}
if (ball.getX() == computer.getX())
{
moveRight = false;
moveLeft = true;
}
}
class TimerHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (counter == 0)
{
Random rand = new Random();
int random = rand.nextInt(2) + 1 ;
if (random == 1)
{
moveUp = true;
moveRight = true;
counter++;
}
else if (random == 2)
{
moveDown = true;
moveLeft = true;
counter++;
}
}
controlMovement();
checkCollision();
repaint();
}
}
I believe that your checkCollision function is incomplete... somehow, the function must have access to both paddle current positions to check if the ball hits one of the paddles...
Another suggestion is to use only one flag to indicante the movement in one axis, for example, boolean monvingUp = true indicantes that the ball is moving up at the Y-axis, when false, such flag indicates that is moving down at the Y-axis...
I changed the = to > and it fixed the problem

Collision detection not working with my brick breaker program?

My program has been experiencing an odd bug i just cannot seem to fix. In summary, whenever I switch two if statements, only the one coming afte tthe first one will work.
Example: (The ball will successfully collide with the top, but won't with the bottom.)
public void update()
{
if(enemies != null)
{
for(Enemy e : enemies)
if(c.getBall().getHitbox().intersects(e.getHitbox()))
{
if(e.collidedBottom())
c.getBall().setSpeedY((c.getBall().ballSpeed));
if(e.collidedTop())
c.getBall().setSpeedY(-(c.getBall().ballSpeed));
deadEnemies.add(e);
}
enemies.removeAll(deadEnemies);
}
Now if i switched the positions of the two if statements like so;
if(e.collidedTop())
c.getBall().setSpeedY(-(c.getBall().ballSpeed));
if(e.collidedBottom())
c.getBall().setSpeedY((c.getBall().ballSpeed));
The ball will successfully react the way it should when colliding with the bottom, but not with the top. Here is the two collision methods:
public boolean collidedBottom()
{
if(c.getBall().getBallY() <= enemy.y + enemy.height &&
c.getBall().getBallX() + c.getBall().getHitbox().width >= enemy.x
&& c.getBall().getBallX() <= enemy.x + enemy.width)
return true;
return false;
}
public boolean collidedTop()
{
if(c.getBall().getBallY() + c.getBall().getHitbox().height >= enemy.y &&
c.getBall().getBallX() + c.getBall().getHitbox().width >= enemy.x
&& c.getBall().getBallX() <= enemy.x + enemy.width)
return true;
return false;
}
Enemy refers to the current focused block in the loop.

Make Finch object follow an object

Class file hereIn my Java code, the Finch robot is named myF.
I need to make myF do the following:
Tap once to move toward an object placed in front of it, with it's beak colour (myF.setLED..) red if the object is stationary and green if that object is moving.
The Finch robot should follow the object around with it's beak lighting red if the object stops and green if that object moves.
Whilst in motion, the Finch should be buzzing, but not too loud. ( myF.buzz(Buzz, BuzzDuration)
However my code isn't working as should, I know there are some mistakes but I do not know how to carry on having tried many times. Please see my code below. Any help will be greatly appreciated.
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class Main {
//Variable declaration.
static int Buzz = 300;
static int BuzzDuration = 1200;
static int R = 250;
static int G = 250;
static int velocityLeft = 150;
static int velocityRight = 150;
static int turnLeft = -50;
static int turnRight = -50;
static int time = 0;
static int tappedCount = 0;
//Initialization of Finch
static Finch myF = new Finch();
//Main method
public static void main(String[] args)
//TAP FINCH ONCE FOR IT TO DO EITHER OF THE FOLLOWING:
//Turn red and follow stationary object
//Turn green and follow an object in motion
//Do nothing if no object is in front of Finch
{
//while (myF.isFinchUpsideDown() == false)
{
if
(myF.isTapped() == true && myF.isObstacle() == true)
{
{
//Set Beak LED to Green if object in front is moving.
// *******need to add a conditional statement for whether obstacle in front of finch is moving.
myF.setLED(R,0,0);
//myF.setWheelVelocities(velocityLeft,velocityRight);
myF.buzz(Buzz, BuzzDuration);
if (myF.isObstacleRightSide() == false)
{
myF.setWheelVelocities(0, 255);
}
else if (myF.isObstacleLeftSide() == false)
{
myF.setWheelVelocities(255, 0);
}
else if (myF.isObstacle() == true)
{
myF.stopWheels();
}
}
}
//Beak supposed to be RED if object in front is stationary, Finch should move towards object.
else {
// if
//***beak should stay off unless object is placed ahead of it and/or is moving, respectively
// (myF.isTapped() == false)
{
myF.setLED(0,0,0);
myF.setWheelVelocities(0,0);
//myF.quit();
//myF.stopWheels();
tappedCount = 0;
}
}
}
myF.quit();
// FollowObjectAround();
//RUN AGAIN i.e. "the Move method" whilst running the first algorithms above.
// boolean RunAgain = true;
//
// while(RunAgain) {
//
// Move();
//
// }
} //main(String[] args) ends here.
//FIRST PART SUCCESSFUL
//Next part: follow object incorporating corresponding beak colour
//public Main() {
public static void FollowObjectAround() {
while (true){
//This begins the movement of the finch
//if (myF.isObstacleLeftSide() == false && myF.isObstacleRightSide() == false || myF.isObstacle() == false)
//LED colours are red, green and blue
//myF.setLED(R,0,0);
//myF.setWheelVelocities(velocityLeft, velocityRight);
//^^
//here, the robot runs straight into the object and keeps going.
//might be best to have the finch stop when it reaches the object, proceeding movement if that object moves also.
//Triggers the RunAgain function to true so that the program does not stop in one run so that the Finch will continue to move
boolean RunAgain = true;
while(RunAgain) {
//Calling of the Move method for the Finch movements.
Move();
while
(myF.isTapped()==true && myF.isTapped()==true)
//if (myF.isTapped()==true && myF.isTapped()==true)
{
myF.setLED(0,0,0);
myF.stopWheels();
//break;
}
} // I just added this brace, wasn't there before.
}
//Inside the while, RunAgain loop , there is a conditional statement that makes the program terminate if the Finch has been tapped twice
//if (myF.isTapped()==true && myF.isTapped()==true)
// {
// break;
// }
}
//}
//}
// Method for Finch movements
public static void Move() {
if (myF.isObstacleRightSide() == false && myF.isObstacleLeftSide() == false && myF.isObstacle() == true)
{
MoveStraight();
}
else if (myF.isObstacleRightSide() == false && myF.isObstacleLeftSide() == true)
{
MoveLeft();
}
else if ( myF.isObstacleRightSide() == true && myF.isObstacleLeftSide() == false)
{
MoveRight();
}
else if (myF.isObstacleRightSide()==true && myF.isObstacleLeftSide()==true)
{
StopMoving();
}
}
//=====================================================================================================
//If the finch is moving straight, the light will be green and both of the wheels will move at 150
public static void MoveStraight()
{
myF.setLED(0, G, 0);
myF.setWheelVelocities(velocityLeft, velocityRight);
myF.buzz(Buzz, BuzzDuration);
}
public static void MoveLeft()
{
//If the finch is moving left, the light will be green, the left wheel will move at -50 and the right wheel will move at 150
myF.setLED(0, G, 0);
myF.setWheelVelocities(turnLeft, velocityRight);
myF.buzz(Buzz, BuzzDuration);
}
public static void MoveRight()
//If the finch is moving right, the light will be green the left wheel will move at 150 and the right wheel will move at -50
{
myF.setLED(0, G, 0);
myF.setWheelVelocities(velocityLeft, turnRight);
myF.buzz(Buzz, BuzzDuration);
}
public static void StopMoving()
//if the Finch is not moving, the colour of the light will be red and the buzzing will stop
{
myF.setLED(R, 0 , 0);
myF.stopWheels();
myF.buzz(Buzz, BuzzDuration);
}
}
//while (myF.isFinchUpsideDown() == false)
{
the "{ " here is wrong and here
{
//Set Beak LED to Green if object in front is moving.
// *******need to add a conditional statement for whether obstacle in front of finch is moving.

Release on TouchEvent not working

Here is my method code I use in onTouchEvent:
public void touch(MotionEvent event) {
// starta = "klicka för att börja";
if (event.getAction() == MotionEvent.ACTION_DOWN) {
starta = "Click to start";
fN.aktivitetNer.start();
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
if (y == -300) {
touch(event);
}
else if (y > -300) {
fN.aktivitetNer.interrupt();
fU.aktivitetUpp.start();
p++;
}
}
else if (y2 <= y + xMax) {
fN.aktivitetNer.interrupt();
fU.aktivitetUpp.start();
p = 0;
}
}
}
What should happening is a knife coming down very fast with help of a Thread(fN.aktivitetNer) and if the knife slice the finger(y2) you loose, if you're fast enough you score one point(p), when you loose or win the knife will go up again with another Thread(fU.aktivitetUpp)
I know it's not perfect but it's now working at all!
ps. if you click two times on the screen the programm will crash.
Well it certainly won't work in it's current form, because the code will never enter your second if statement.
Your first if statement asserts that event.getAction() == MotionEvent.ACTION_DOWN. If this is true, then atevent.getAction() cannot possibly be equal to MotionEvent.ACTION_CANCEL and you will never enter that block of code.
Since I don't know what your thread does I can't be certain, but you probably want something more like this:
public void touch(MotionEvent event)
{
// starta = "klicka för att börja";
int actionCode = event.getAction();
if (actionCode == MotionEvent.ACTION_DOWN)
{
//Start to drop knife
}
else if (actionCode == MotionEvent.ACTION_CANCEL || actionCode == MotionEvent.ACTION_UP)
{
if (Knife is falling, and touch point is below knife)
{
//Add one to score
}
else if (Knife is falling, and touch point is above knife)
{
//Reset score to 0
}
}
else if (Knife has already fallen and still touching screen)
{
//Reset score to 0
}
}

Categories

Resources