Greenfoot:使用不同的钥匙移动两辆车
Greenfoot: Move two cars using different keys
我是 java 和 Greenfoot 的新手。我想以不同的方式移动两辆车,其中一辆使用 "up"、"down"、"left"、"right" 用于 car1 和 "w"、"s"、"a", "d" 分别。
我想为 moveForward()
、Car.car1.isKeyDown("down")
和 Car.car2.isKeyDown("s")
为 moveBack()
等制作类似 Car.car1.isKeyDown("up")
和 Car.car2.isKeyDown("w")
的东西。如何我应该这样做吗?
这是我得到的:
/**
* Class that models objects of type "car"
*
*/
public class Car extends Actor
{
// Attributes of the class Car
private static int numberWheels = 4;
private boolean areHeadlightsOn = false;
private String image1;
private String image2;
private boolean isKeyDown = false;
/**
* Constructor
*/
public Car(String file1, String file2){
setImage1(file1); // Sets the attribute image1 to file1
setImage2(file2); // Sets the attribute image2 to file2
setImage(file1);
// Sets the image that appears in the screen at the beginning to the one in file1
}
/**
* Method that is being always invoked when the program is running
*/
public void act(){
moveForward();
moveBack();
turnLeft();
turnRight();
turnHeadlightsOn();
turnHeadlightsOff();
}
/**
* Move the car forward every time the key "up" is pressed
*/
public void moveForward(){
if (Greenfoot.isKeyDown("up")){
move(5);
}
}
/**
* Move the car back every time the key "down" is pressed
*/
public void moveBack(){
if (Greenfoot.isKeyDown("down")){
move(-1);
}
}
/**
* Turn the car to the left every time the key "left" is pressed
*/
public void turnLeft(){
if (Greenfoot.isKeyDown("left")){
turn(-4);
}
}
/**
* Turn the car to the right every time the key "right" is pressed
*/
public void turnRight(){
if (Greenfoot.isKeyDown("right")){
turn(4);
}
}
/**
* Turn the headlights on when the key "space" is pressed.
* The attribute isKeyDown is used to prevent setting the headlights on and off when the key "space" is pressed (and remains pressed for some time)
*/
public void turnHeadlightsOn(){
if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
if (getAreHeadlightsOn() == false){
setAreHeadlightsOn(true);
setImage(getImage2());
setIsKeyDown(true);
}
}
if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
setIsKeyDown(false);
}
}
/**
* Turn the headlights off when the key "space" is pressed.
* The attribute isKeyDown is used to prevent setting the headlights off and on when the key "space" is pressed (and remains pressed for some time)
*/
public void turnHeadlightsOff(){
if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
if (getAreHeadlightsOn() == true){
setAreHeadlightsOn(false);
setImage(getImage1());
setIsKeyDown(true);
}
}
if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
setIsKeyDown(false);
}
}
public void setAreHeadlightsOn(boolean areHeadlightsOn){
this.areHeadlightsOn = areHeadlightsOn;
}
public boolean getAreHeadlightsOn(){
return this.areHeadlightsOn;
}
public void setImage1(String image1){
this.image1 = image1;
}
public String getImage1(){
return this.image1;
}
public void setImage2(String image2){
this.image2 = image2;
}
public String getImage2(){
return this.image2;
}
public void setIsKeyDown(boolean isKeyDown){
this.isKeyDown = isKeyDown;
}
public boolean getIsKeyDown(){
return this.isKeyDown;
}
}
/*MyWorld class
*/
import greenfoot.*;
public class MyWorld extends World
{
public MyWorld()
{
super(800, 600, 1);
setBackground("road.jpg");
populateWorld();
}
public void populateWorld(){
Car car1 = new Car(new String("car1.png"),
new String("car2.png");
Car car2 = new Car(new String("car3.png"),
new String("car4.png");
addObject(car1, 120, 70);
addObject(car2, 580, 30);
}
}
您只需将要用作参数的钥匙添加到汽车 class。例如
public class Mario {
private upKey;
public Mario(String upKey) {
this.upKey = upKey;
}
public void jump(){
if (Greenfoot.isKeyDown(upKey)){
move(5);
}
}
}
然后就可以为每个对象设置跳转的key了!
Mario player1 = new Mario("up");
Mario player2 = new Mario("w");
您可以仅使用单个字母本身来跟踪任何字母键。 "s"、"a"、"d"。您可以添加更多键作为更多参数。希望这是有道理的。
如果您不想在构造函数中添加所有键,您还可以添加一个 'setKeys(String key)' 方法或其他方法。
我是 java 和 Greenfoot 的新手。我想以不同的方式移动两辆车,其中一辆使用 "up"、"down"、"left"、"right" 用于 car1 和 "w"、"s"、"a", "d" 分别。
我想为 moveForward()
、Car.car1.isKeyDown("down")
和 Car.car2.isKeyDown("s")
为 moveBack()
等制作类似 Car.car1.isKeyDown("up")
和 Car.car2.isKeyDown("w")
的东西。如何我应该这样做吗?
这是我得到的:
/**
* Class that models objects of type "car"
*
*/
public class Car extends Actor
{
// Attributes of the class Car
private static int numberWheels = 4;
private boolean areHeadlightsOn = false;
private String image1;
private String image2;
private boolean isKeyDown = false;
/**
* Constructor
*/
public Car(String file1, String file2){
setImage1(file1); // Sets the attribute image1 to file1
setImage2(file2); // Sets the attribute image2 to file2
setImage(file1);
// Sets the image that appears in the screen at the beginning to the one in file1
}
/**
* Method that is being always invoked when the program is running
*/
public void act(){
moveForward();
moveBack();
turnLeft();
turnRight();
turnHeadlightsOn();
turnHeadlightsOff();
}
/**
* Move the car forward every time the key "up" is pressed
*/
public void moveForward(){
if (Greenfoot.isKeyDown("up")){
move(5);
}
}
/**
* Move the car back every time the key "down" is pressed
*/
public void moveBack(){
if (Greenfoot.isKeyDown("down")){
move(-1);
}
}
/**
* Turn the car to the left every time the key "left" is pressed
*/
public void turnLeft(){
if (Greenfoot.isKeyDown("left")){
turn(-4);
}
}
/**
* Turn the car to the right every time the key "right" is pressed
*/
public void turnRight(){
if (Greenfoot.isKeyDown("right")){
turn(4);
}
}
/**
* Turn the headlights on when the key "space" is pressed.
* The attribute isKeyDown is used to prevent setting the headlights on and off when the key "space" is pressed (and remains pressed for some time)
*/
public void turnHeadlightsOn(){
if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
if (getAreHeadlightsOn() == false){
setAreHeadlightsOn(true);
setImage(getImage2());
setIsKeyDown(true);
}
}
if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
setIsKeyDown(false);
}
}
/**
* Turn the headlights off when the key "space" is pressed.
* The attribute isKeyDown is used to prevent setting the headlights off and on when the key "space" is pressed (and remains pressed for some time)
*/
public void turnHeadlightsOff(){
if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
if (getAreHeadlightsOn() == true){
setAreHeadlightsOn(false);
setImage(getImage1());
setIsKeyDown(true);
}
}
if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
setIsKeyDown(false);
}
}
public void setAreHeadlightsOn(boolean areHeadlightsOn){
this.areHeadlightsOn = areHeadlightsOn;
}
public boolean getAreHeadlightsOn(){
return this.areHeadlightsOn;
}
public void setImage1(String image1){
this.image1 = image1;
}
public String getImage1(){
return this.image1;
}
public void setImage2(String image2){
this.image2 = image2;
}
public String getImage2(){
return this.image2;
}
public void setIsKeyDown(boolean isKeyDown){
this.isKeyDown = isKeyDown;
}
public boolean getIsKeyDown(){
return this.isKeyDown;
}
}
/*MyWorld class
*/
import greenfoot.*;
public class MyWorld extends World
{
public MyWorld()
{
super(800, 600, 1);
setBackground("road.jpg");
populateWorld();
}
public void populateWorld(){
Car car1 = new Car(new String("car1.png"),
new String("car2.png");
Car car2 = new Car(new String("car3.png"),
new String("car4.png");
addObject(car1, 120, 70);
addObject(car2, 580, 30);
}
}
您只需将要用作参数的钥匙添加到汽车 class。例如
public class Mario {
private upKey;
public Mario(String upKey) {
this.upKey = upKey;
}
public void jump(){
if (Greenfoot.isKeyDown(upKey)){
move(5);
}
}
}
然后就可以为每个对象设置跳转的key了!
Mario player1 = new Mario("up");
Mario player2 = new Mario("w");
您可以仅使用单个字母本身来跟踪任何字母键。 "s"、"a"、"d"。您可以添加更多键作为更多参数。希望这是有道理的。
如果您不想在构造函数中添加所有键,您还可以添加一个 'setKeys(String key)' 方法或其他方法。