This site requires JavaScript, please enable it in your browser!
Applez wrote ...

2018/4/16

## Turn left, Move, Turn right - In a sequence

Applez

2018/4/16

So, I'm making a racing game. I'll try to explain this as detailed as possible. My main object is a car (The player controls the car). The car is facing RIGHT. The controls: Up Arrow = The car moves right, Down Arrow = The car moves left, Left Arrow = The car turns left 20 Degrees left, Right Arrow = The car turns 20 Degrees right. (I've set the rotation so that the car can only move 20 degrees). I'll use the left key as an example What I want it to do is: With 1 press of the Left Arrow Key, I want the car to (Turn left 20 Degrees, Move 5, Turn right 20 degrees). It's like switching lanes. Recap : When I tap the left key ONCE (Not holding it down) I want the car to Turn left 20 Degrees, Move 5, Turn right 20 degrees. So the car would be higher than where it was before and a little bit forward.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

When you post a code, can you please insert your code in mine? I get confused sometimes.

/**
* Write a description of class NSX9 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class NSX9 extends Actor
{
/**
* Act - do whatever the NSX9 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("left") && getRotation() != 340)
{
turn (-5);
}
if (Greenfoot.isKeyDown("right") && getRotation() != 20)
{
turn(5);
}
if (Greenfoot.isKeyDown("up"))
{
move(5);
}
if (Greenfoot.isKeyDown("down"))
{
move(-5);
}
}
}
https://imgur.com/gallery/cO1h2 Thank You
danpost

2018/4/16

Sounds to me like you are going to have to re-think what the other two arrow keys should do (the right and left keys). If turning is what makes the car go up and down, then what of the other keys? "else if" blocks are probably needed with the left and right arrow key checks to straighten the car out.
Applez

2018/4/17

Up Arrow = The car moves right, Down Arrow = The car moves left, Left Arrow = The car turns left 20 Degrees left, Right Arrow = The car turns 20 Degrees right. And what is a block? I'm new to greenfoot.
danpost

2018/4/17

A block is a set of code found between squiggly brackets, For example:
if (boolean_value)
{
// if block code here
}
else if (boolean_value_2)
{
// else if block code here
}
else
{
// else block code here
}
Applez

2018/4/18

Thank you but I'm still having trouble trying to figure out how to do the sequence code..
danpost

2018/4/18

I was thinking something like:
int dt = 0;
if (Greenfoot.isKeyDown("left")) dt--;
if (Greenfoot.isKeyDown("right")) dt++;
if ((dt > 0 && getRotation != 20) || (dt >= 0 && getRotation() > 339)) turn(dt);
if ((dt < 0 && getRotation != 340) || (dt <= 0 && getRotation() > 0 && getRotation() < 21)) turn(dt);
This was not tested, so I do not know how well it will work, if at all. The "up" and "down" keys codes should not need any changes.
Applez

2018/4/18

I typed the code in and i saw absolutely no changes, it moves the same as it was before.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Carrrr here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class NSX4 extends Actor
{
/**
* Act - do whatever the NSX4 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int dt = 0;
if (Greenfoot.isKeyDown("left")) dt--;
if (Greenfoot.isKeyDown("right")) dt++;
if ((dt > 0 && getRotation() != 20) || (dt >= 0 && getRotation() > 339)) turn(dt);
if ((dt < 0 && getRotation() != 340) || (dt <= 0 && getRotation() > 0 && getRotation() < 21)) turn(dt);

if (Greenfoot.isKeyDown("up"))
{
move(3);
}
if (Greenfoot.isKeyDown("down"))
{
move(-2);
}
}
}
danpost

2018/4/18

Applez wrote...
I typed the code in and i saw absolutely no changes, it moves the same as it was before. << Code Omitted >>
It cannot be exactly the same. The should straighten out now, at least (revert back to facing directly right when no keys are pressed).
Applez

2018/4/19

Doesn't seem to work.... Please check if I have used the code properly. Also, Please explain what (dt--) ,(dt++) and getRotation means.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Carrrr here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class NSX4 extends Actor
{
/**
* Act - do whatever the NSX4 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int dt = 0;
if (Greenfoot.isKeyDown("left")) dt--;
if (Greenfoot.isKeyDown("right")) dt++;
if ((dt > 0 && getRotation() != 20) || (dt >= 0 && getRotation() > 339)) turn(dt);
if ((dt < 0 && getRotation() != 340) || (dt <= 0 && getRotation() > 0 && getRotation() < 21)) turn(dt);

if (Greenfoot.isKeyDown("up"))
{
move(3);
}
if (Greenfoot.isKeyDown("down"))
{
move(-2);
}
}
}
danpost

2018/4/19

Applez wrote...
Please explain what (dt--) ,(dt++) and getRotation means.
(dt--;) is the same as (dt = dt-1;). It subtracts one from the value of the variable. Also, it is the same as (dt -= 1;). (dt++;) adds one likewise. getRotation() returns the current rotation of the actor. When you turn an actor, you change its rotation. At zero rotation, the actor is facing right. turn(-1); from there puts the rotation 359. Obviously, the rotation is in degrees and increases (mod 360) clockwise.
Please check if I have used the code properly.
It looks okay. Maybe you only want turning when the car is actually moving?
Applez

2018/4/20

Okay I'll try a different method, please tell me the code to make the actor move upwards. Thanks
danpost

2018/4/20

Applez wrote...
please tell me the code to make the actor move upwards.
setLocation(getX(), getY()-1);
will move the actor upward by one pixel. You could also do something like this:
int rotation = getRotation(); // save current rotation
setRotation(270); // face upward
move(1); // move (upward)
setRotation(rotation); // return to originally facing direction
You need to login to post a reply.