ideas (dx-ball)

PT_PT_
edited June 2014 in Questions about Code

heey, i have made a game like dx-ball (search Google if you don't know what is) but i have a great problem. i have some blocks, with the x- and y- coördinate, and of course the x- and y-coördinate of the ball. my problem is: when the ball hit the block at a corner (no matter which) the ball doesn't rebound well. i have set, that when the ball hit the block on the bottom or the up, deltaY = -deltaY, so that the ball goes up or down and the block will disappeared. but when the ball is on the corner, then i get: deltaX = -deltaX AND deltaY = -deltaY. you understand? how can i solve it? this is my code:

float deltax = 0.5;
float deltay = -0.75;
float x = 0;
float y;
float c;
int aantal_blokjes = 20;
float xwaarde = 0;
boolean beginnen = false;
boolean[][] blokjes = new boolean[aantal_blokjes][aantal_blokjes];
int diameter_bal = 10;
int lengte_balk = 60;
int levens = 3;
int level = 1;
int f;
PImage groen;

void setup() {
  size(displayWidth, displayHeight-70);
  frameRate(500);
  y = height - 25;
  for (int i = 0; i < aantal_blokjes; i++) {
    for (int j = 0; j < aantal_blokjes; j++) {
      blokjes[i][j] = true;
    }
  }
  groen = loadImage("groen.jpg");
}

void draw() {

  // Instellen + opmaak

  if (mouseX < lengte_balk + 5) xwaarde = lengte_balk + 5;
  if (mouseX > width - lengte_balk - 5) xwaarde = width - lengte_balk - 5;
  if (mouseX > lengte_balk && mouseX < width - lengte_balk) xwaarde = mouseX;
  noCursor();
  background(0);

  // Levens

  for (int i = 1; i < levens; i++) {
    fill(#00CCFF);
    ellipse(width - i * 30 - 15, 12.5, 5, 5);
    ellipse(width - i * 30, 12.5, 5, 5);
    fill(150);
    rect(width - i * 30 - 15, 10, 15, 5);
  }

  // Schuifbalk

  fill(#00CCFF);
  strokeWeight(1);
  ellipse(xwaarde - lengte_balk, height-15, 10, 8);
  ellipse(xwaarde + lengte_balk, height-15, 10, 8);
  fill(255);
  for (int i = 10; i <= 20; i++) {
    float r = random(255);
    stroke(r);
    line(xwaarde - lengte_balk, height-i, xwaarde+lengte_balk, height-i);
  }

  // Blokjes

  fill(255);
  strokeWeight(0);
  for (int i = 0; i < aantal_blokjes; i ++) {
    for (int j = 0; j < aantal_blokjes; j ++) {
      if (blokjes[i][j] == true) image(groen, i * 45 + 100, j * 20 + 100);
    }
  }

  // Bal

  fill(255);
  if (beginnen == false) ellipse(xwaarde, y, diameter_bal, diameter_bal);
  if (beginnen == true) {
    ellipse(x, y, diameter_bal, diameter_bal);
    x += deltax;
    y += deltay;
    if (x < diameter_bal / 2) deltax = -deltax;
    if (x > width - diameter_bal / 2) deltax = -deltax;
    if (y < diameter_bal / 2) deltay = -deltay;
    if (y > height - 25) deltay = -deltay;
  }
  blokjegeraakt();
}

void mousePressed() {
  if (beginnen == false) {
    beginnen = true;
    x = xwaarde;
  }
}

void blokjegeraakt() {
  int goed_x = round(x / 5) * 5;
  int goed_y = round(y / 5) * 5;
  for (int i = 0; i < aantal_blokjes; i ++) {
    for (int j = 0; j < aantal_blokjes; j ++) {
      if (blokjes[i][j] == true) {
        if (goed_x >= i * 45 + 100 && goed_x <= i * 45 + 145 && goed_y >= j * 20 + 100 && goed_y <= j * 20 + 120) {
          if (goed_x == i * 45 + 100 || goed_x == i * 45 + 145) deltax = -deltax;
          if (goed_y == j * 20 + 100 || goed_y == j * 20 + 120) deltay = -deltay;
          blokjes[i][j] = false;
          return;
        }
      }
    }
  }
}

void overnieuw() {
  beginnen = false;
  levens -= 1;
  if (levens == 0) noLoop();
  y = height - 25;
  deltax = 2;
  deltay = -3;
}

sorry for my bad english, but i'm netherlands ;)

Answers

  • edited June 2014

    Nice!

    Arkanoid / Breakout / Arkanoid / Megaball

    ok, here:

                  if (goed_x == i * 45 + 100 || goed_x == i * 45 + 145) deltax = -deltax;
                  if (goed_y == j * 20 + 100 || goed_y == j * 20 + 120) deltay = -deltay;
    

    first I recommend not to use deltax = -deltax; because it can lead to stutter when it stays in the zone where condition is true

    better (Edited: no, this is not working!!!):

    if (goed_x == i * 45 + 100) deltax = abs(deltax); // always pos
    
    if (goed_x == i * 45 + 145) deltax = - abs (deltax); // always neg
    

    also

    you can say

    if ..................
    else if......................
    

    thus only x OR y can change direction, not both

    and maybe use english names for your vars, it's hard to read ... ;-)

  • much thanks! but how can i know if i must x OR y change ;)

  • how is this handled in the original game ?

    I feel it's ok when at a corner both directions are changed.

  • i've never seen it that both directions changed

  • just take a ball and shoot it against a corner in your flat

    you'll see, I'm right

  • PT_PT_
    edited June 2014

    haha ;) i mean in the game

  • well the physics in the game should be the same in your flat to be a realistic game

    just try it in your game

Sign In or Register to comment.