0

I am trying to get a character to move automatically at random and bounce of walls. Theoretically the code that I have should work however the character just isn't moving. I get no errors and have tried to find a problem but come short.

I fixed a couple of problems with declarations and some of the comparisons however I have not been able to find another issue that is stopping this code from running. I am running this on a 48x84 LCD screen.

The main function isn't exact but the order of how things are run currently is the same

int t_xy[27][2] = { {0,0}, {4,0}, {0,1}, {1,1}, {3,1}, {4,1}, {0,2}, {1,2}, {2,2}, {3,2}, {4,2}, {0,3}, {2,3}, {4,3}, {0,4}, {1,4}, {3,4}, {4,4}, {0,5}, {1,5}, {2,5}, {3,5}, {4,5}, {1,6}, {2,6}, {3,6} }; //27 pixels in bitmap

//Tom's positon
double t_x = LCD_X - 5, t_y = LCD_Y - 9,  t_dx, t_dy;

void t_setup() {
    double gait = 0.3;
    double t_dir = rand() * 3.14 * 2 / RAND_MAX; // random direction
    t_dx = gait * cos(t_dir);
    t_dy = gait * sin(t_dir);
}

void t_move() {
    int new_x = round(t_x + t_dx);
    int new_y = round(t_y + t_dy);
    int bounced = 0;
        if (new_x == 0 || new_x == LCD_X - 5) {
            t_dx = -t_dx;
            bounced = 1;

        }
        if (new_y == 8 || new_y == LCD_Y - 7) {
            t_dy = -t_dy;
            bounced = 1;
        }
        if (!(bounced == 1)) {
            t_x += t_dx;
            t_y += t_dy;
        }

void main()
    t_setup();
    while (!(game_over == 1))
    {
        t_move();
    }
}

I expect tom to be able to move automatically and bounce off the borders of the screen and move in random directions but currently tom does not move at all but there are no errors appearing and every other game element works fine

Leptoflux
  • 43
  • 1
  • 8
  • The bounce detection should take the direction of movement into account, which it doesn't, currently. – Ian Abbott Oct 21 '19 at 10:39
  • I added a call to each of the bounce statements to send it back to t_setup for a new direction. I'm not too concerned with him bouncing off the wall at the same direction as he hits it. – Leptoflux Oct 21 '19 at 11:02
  • `void main();` -> you've put semicolon, that is why `main` is not working at all. – Miradil Zeynalli Oct 21 '19 at 12:02
  • @MiradilZeynalli that isn't my actual main and was just an error I made when writing the code so people could get an understanding of what I was doing. My actual main works fine as the timer and playercharacter movement work fine along with some other stuff – Leptoflux Oct 21 '19 at 12:10
  • Okay then. Are you working on Arduino IDE? If yes, you can try debugging with `Serial.print`s. I have feeling that, your `game_over` is always 1 from beginning. – Miradil Zeynalli Oct 21 '19 at 12:12
  • @MiradilZeynalli I'm coding mainly with a custom library but game_over is initialised to 0 and the code that changes that value isn't fully implemented yet. I am using cygwin to compile and avr as well for part of it – Leptoflux Oct 21 '19 at 12:40
  • Could you print something in `t_move` function, just to check if that function is called or not? Moreover, after calculating `t_dx` and `t_dy` do you update that on the screen? – Miradil Zeynalli Oct 21 '19 at 12:43
  • I moved the draw_tom(); call into the t_move function and it still printed at the initial values declared for t_x and t_y but didn't move. Also the t_move functions updates every loop so t_dx and t_dy should update and draw_tom draws it at the updated point. I'm going to print t_dx and t_dy to see if they are actually calculating properly though – Leptoflux Oct 21 '19 at 12:57

0 Answers0