Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.breakout.objects;
- import com.breakout.Breakout;
- import com.gej.object.GObject;
- import com.gej.util.GUtil;
- public class Ball extends GObject {
- public Ball(float x, float y){
- super(Breakout.BALL, x, y);
- }
- public void update(long elapsedTime){
- if (getVelocityX()==0 && getVelocityY()==0){
- //if (GKeyBoard.isPressed(KeyEvent.VK_SPACE)){
- setVelocityY(-0.02f);
- int random = GUtil.random(2);
- switch (random){
- case 0:
- setVelocityX(-0.02f);
- break;
- case 1:
- setVelocityX(0.02f);
- break;
- }
- // }
- }
- }
- public void bounce(GObject other){
- if (getVelocityX()>0){
- setX(other.getX()-getWidth());
- } else if (getVelocityX()<0){
- setX(other.getX()+other.getWidth());
- }
- if (getVelocityY()>0){
- setY(other.getY()-getHeight());
- } else if (getVelocityY()<0){
- setY(other.getY()+other.getHeight());
- }
- bounce();
- }
- public void bounce(){
- boolean left = false;
- boolean right = false;
- boolean up = false;
- boolean down = false;
- if (dx < 0) {
- left = true;
- } else if (dx > 0) {
- right = true;
- }
- if (dy < 0) {
- up = true;
- } else if (dy > 0) {
- down = true;
- }
- if (left && up) {
- dx = -dx;
- }
- if (left && down) {
- dy = -dy;
- }
- if (right && up) {
- dx = -dx;
- }
- if (right && down) {
- dy = -dy;
- }
- }
- public void collision(GObject other){
- if (other instanceof Bat || other instanceof Block){
- bounce(other);
- } else if (other instanceof Stone){
- other.destroy();
- bounce(other);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement