This site requires JavaScript, please enable it in your browser!
Greenfoot back
Horizon
Horizon wrote ...

2022/1/18

Help Turning My Race Car

Horizon Horizon

2022/1/18

#
Hi, I'm really new to green foot and have been working on coding a 2d racing game for the past week. In order to get the barriers around the race track to work I implemented the following code I found made by danpost. The code works and stops the cars from going out of the track, but they won't turn left or right anymore, instead the car goes up and down vertically. Here is the code I implemented for the barriers:

 public void act() 
    {
       int dx = 0, dy = 0; 
       if (Greenfoot.isKeyDown("W")) dx = 6;
       if (Greenfoot.isKeyDown("S")) dx = -6;
       if (Greenfoot.isKeyDown("A")) dy = -4;
       if (Greenfoot.isKeyDown("D")) dy = 4;
       setLocation(getX()+dx, getY()+dy);
      
       if (isTouching(Tire.class))
       {
           setLocation(getX()-dx, getY()-dy);
       }

    }

This is the code prior to me creating the barriers

 public void act() 
    {
      if (Greenfoot.isKeyDown("Up"))
      {
          move(6);
      }
      if (Greenfoot.isKeyDown("Down"))
      {
          move(-6);
      }
      if (Greenfoot.isKeyDown("Right"))
      {
          turn(4);
      }
      if (Greenfoot.isKeyDown("Left"))
      {
          turn(-4);
      }
      
any assistance you guys could give would be greatly appreciated
danpost danpost

2022/1/18

#
Horizon wrote...
I implemented the following code I found made by danpost. << Code Omitted >>
That does not quite look like any car movement code I have made. First off, "a" and "d" should be for turning -- not moving. Here is a simplified version of my Car class code used in my Park It scenario:
import greenfoot.*;

public class Car extends Actor
{
    public void act()
    {
        // moving input
        int ds = 0;
        if (Greenfoot.isKeyDown("w")) ds++;
        if (Greenfoot.isKeyDown("s")) ds--;
        // if moving
        if (ds != 0)
        {
            // move
            move(1+5*ds);
            // turning input
            int dr = 0;
            if (Greenfoot.isKeyDown("d")) dr++;
            if (Greenfoot.isKeyDown("a")) dr--;
            // turn
            turn(dr*(1+5*ds)/2);
            // if collision
            if (isTouching(Tire.class))
            {
                // cancel moving and turning
                move(-1-5*ds);
                turn(-dr*(1+5*ds)/2);
            }
        }
    }
}
Horizon Horizon

2022/1/18

#
sorry let me clarify, I found the code from a discussion on how to make barriers it wasn't directly related to cars. Also thank you a lot for helping me, and this code definitely does block the cars. But, I have just found out from playing that when a car gets stuck, and I attempt to turn left or right, my car will end up slowly going through the wall. Do you know why this is happening and how I could fix it?
danpost danpost

2022/1/19

#
Horizon wrote...
when a car gets stuck, and I attempt to turn left or right, my car will end up slowly going through the wall. Do you know why this is happening and how I could fix it?
My bad. Swap lines 26 and 27 (need to cancel in reverse order).
Horizon Horizon

2022/1/19

#
thanks so much man your literally a god on this site
Horizon Horizon

2022/1/20

#
one more thing and this might be a really stupid question but what would I change in the code to increase how sharp the cars turns are. I tried changing the 1+5 to a bigger number but it seemed to have messed everything up. Thanks again for helping me out
danpost danpost

2022/1/20

#
Horizon wrote...
one more thing and this might be a really stupid question but what would I change in the code to increase how sharp the cars turns are. I tried changing the 1+5 to a bigger number but it seemed to have messed everything up. Thanks again for helping me out
Removing "/2" from lines 21 and 27 will double the amount of turn.
Horizon Horizon

2022/1/20

#
thanks again so much dude
You need to login to post a reply.