跳过FOR循环中的特定迭代

时间:2018-09-16 02:47:07

标签: windows batch-file

在我使用Batch进行编程的游戏中,特定功能用于绘制带有预设图块的地图。这相当复杂,需要花费几秒钟的时间来绘制整个全彩图。

我正在尝试为播放器添加一个图标。我不想更改地图数据,而是希望完全在graphics子例程中实现它。

现在,将显示图标,但是它将整个图形行向后推一位。我想让它跳过播放器位于其上的图块,但是执行此操作的唯一方法似乎是执行与BASIC中的ye olde NEXT命令类似的操作。我不知道与此等效的批处理,因此我来这里寻求帮助。绘制播放器磁贴后,我需要跳过FOR循环的特定迭代。

如果没有大量上下文,这将毫无意义,但这是它的子例程:

:D
REM Draw tiles
IF /I %OSV% LSS 3 (
FOR /L %%G IN (0,1,31) DO (
    REM Old tileset
    IF "%2" == "1" IF "%%G" == "%X%" CALL :C 0C "i" & REM skip the rest
    IF "!M%MAP%%1:~%%G,1!" == "0" CALL :C 00 " "
    IF "!M%MAP%%1:~%%G,1!" == "1" CALL :C 2A "w"
    IF "!M%MAP%%1:~%%G,1!" == "2" CALL :C 87 "S"
    IF "!M%MAP%%1:~%%G,1!" == "3" CALL :C 6E "W"
    IF "!M%MAP%%1:~%%G,1!" == "4" CALL :C 0A "T"
    IF "!M%MAP%%1:~%%G,1!" == "5" CALL :C 0A "n"
    IF "!M%MAP%%1:~%%G,1!" == "6" CALL :C 6E "H"
    IF "!M%MAP%%1:~%%G,1!" == "7" CALL :C 91 "m"
    IF "!M%MAP%%1:~%%G,1!" == "8" CALL :C 19 "m"
    IF "!M%MAP%%1:~%%G,1!" == "9" CALL :C B3 "M"
    IF "!M%MAP%%1:~%%G,1!" == "A" CALL :C 4C "m"
    IF "!M%MAP%%1:~%%G,1!" == "B" CALL :C C4 "M"
    IF "!M%MAP%%1:~%%G,1!" == "C" CALL :C 6E "O"
    IF "!M%MAP%%1:~%%G,1!" == "D" CALL :C 6E "W"
    IF "!M%MAP%%1:~%%G,1!" == "E" CALL :C 78 "W"
    IF "!M%MAP%%1:~%%G,1!" == "F" CALL :C 0D "X"
    IF "!M%MAP%%1:~%%G,1!" == "G" CALL :C 6E "E"
    IF "!M%MAP%%1:~%%G,1!" == "H" CALL :C 48 "E"
    IF "!M%MAP%%1:~%%G,1!" == "I" CALL :C 68 "n"
    IF "!M%MAP%%1:~%%G,1!" == "J" CALL :C 6E "m"
    IF "!M%MAP%%1:~%%G,1!" == "K" CALL :C E6 "W"
    IF "!M%MAP%%1:~%%G,1!" == "L" CALL :C 6A "T"
    IF "!M%MAP%%1:~%%G,1!" == "M" CALL :C 2A "w"
    IF "!M%MAP%%1:~%%G,1!" == "N" CALL :C 87 "S"
    IF "!M%MAP%%1:~%%G,1!" == "O" CALL :C 0D "i"
    IF "!M%MAP%%1:~%%G,1!" == "P" CALL :C 6A "Y"
    IF "!M%MAP%%1:~%%G,1!" == "Q" CALL :C BF "C"
)
)
GOTO :EOF

请注意,我删除了高级(“花式”)图块集,因为它增加了更多的复杂性,并且在此处无法正确显示。

绘制角色(第六行)之后,我需要跳过序列的其余部分,以防止其绘制玩家站在其上方的图块。

传递给函数的变量为:

  • %1-要绘制的Y值
  • %2-玩家是否使用此Y值

除了通过C函数向终端输出以外,没有任何输出,C函数仅绘制彩色字符。

任何想法都很棒。

编辑:这是一些没有播放器图标的示例输出。红色的琥珀色指示玩家的位置。我想用播放器的图标替换该位置的图块。

Overworld屏幕截图

Overworld screenshot

沙漠截图

Desert screenshot

1 个答案:

答案 0 :(得分:0)

您没有创建MCVE,并且您的代码没有意义,因为如果它在第6 行之后停止,那么其余代码就没有用了,也不需要循环32次。因此,这只是一个例子

要在条件为真时阻止块运行,只需反转条件

package com.example.chinmayi.androiduberclone;

    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.animation.Interpolator;
    import android.view.animation.LinearInterpolator;
    import android.widget.Toast;

    import com.firebase.geofire.GeoFire;
    import com.firebase.geofire.GeoLocation;
    import com.github.kmenager.materialanimatedswitch.MaterialAnimatedSwitch;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.location.FusedLocationProviderClient;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;
    import com.google.android.gms.maps.CameraUpdate;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptor;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;

    public class Welcome extends FragmentActivity implements OnMapReadyCallback,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener

    {

        private GoogleMap mMap;

        private static final int MY_PERMISSION_REQUEST_CODE =7000;
        private static final int PLAYY_SERVICE_ES_REQUEST = 7001;

        private LocationRequest mLocationRequest;
        private GoogleApiClient mGoogleApiClient;
        private Location mLastLocation;

        private static int UPDATE_INTERVAL = 5000;
        private static int FASTEST_INTERVAL =3000;
        private static int DISPLACEMENT = 10;

        DatabaseReference drivers;


        GeoFire geoFire;

        Marker mCurrent;

        MaterialAnimatedSwitch location_switch;

        SupportMapFragment mapFragment;

        FusedLocationProviderClient fusedLocationProviderClient;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_welcome);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

            location_switch = (MaterialAnimatedSwitch)findViewById(R.id.location_switch);
            location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(boolean isOnline) {
                    if(isOnline)
                    {
                        startLocationUpdates();
                        displayLocation();
                        Snackbar.make(mapFragment.getView(),"You are online",Snackbar.LENGTH_SHORT)
                            .show();

                    }
                    else
                    {
                        stopLocationUpdates();
                        mCurrent.remove();
                        Snackbar.make(mapFragment.getView(),"You are offline",Snackbar.LENGTH_SHORT)
                                .show();
                    }
                }
            });
            //Geo Fire
            drivers = FirebaseDatabase.getInstance().getReference("Drivers");
            geoFire = new GeoFire(drivers);

            setUpLocation();

        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode)
            {
                case MY_PERMISSION_REQUEST_CODE:
                    if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                    {
                        if(checkPlayServices())
                        {
                            buildGoogleApiClient();
                            createLocationRequest();
                            if(location_switch.isChecked())
                                displayLocation();
                        }
                    }
            }
        }

        private void setUpLocation() {
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED  )
            {
                ActivityCompat.requestPermissions(this, new String []{
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION
                },MY_PERMISSION_REQUEST_CODE);
            }
            else
            {
                if(checkPlayServices())
                {
                    buildGoogleApiClient();
                    createLocationRequest();
                    //fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
                    if(location_switch.isChecked())
                        displayLocation();
                }
            }
        }

        private void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(UPDATE_INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
        }

        private void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
            mGoogleApiClient.connect();
        }

        private boolean checkPlayServices() {
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.SUCCESS) {
                if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
                    GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAYY_SERVICE_ES_REQUEST).show();
                 else
                    {
                    Toast.makeText(this, "This device is not supported", Toast.LENGTH_SHORT).show();
                    finish();
                    }
                return false;
            }
            return true;
        }
        private void stopLocationUpdates() {
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED  )
            {
                return;
            }
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
        }

        private void displayLocation() {
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED  )
            {
                return;
            }
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if(mLastLocation!=null)
            {
                if(location_switch.isChecked())
                {
                    final double latitude = mLastLocation.getLatitude();
                    final double longitude = mLastLocation.getLongitude();

                    //Upgrade
                    geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
                        @Override
                        public void onComplete(String key, DatabaseError error) {
                            if(mCurrent!=null)
                            {
                                mCurrent.remove();
                                mCurrent=mMap.addMarker(new MarkerOptions()
                                                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.car))
                                                            .position(new LatLng(latitude,longitude))
                                                            .title("You"));

                                //move camera to this position
                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),15.0f));

                                //draw animation rotation marker
                                rotateMarker(mCurrent,-360,mMap);
                            }
                        }
                    });
                }

            }
            else
            {
                Log.d("ERROR","Cannot get your location");
            }

        }

        private void rotateMarker(final Marker mCurrent, final float i, GoogleMap mMap) {
            final Handler handler=new Handler();
            final long start = SystemClock.uptimeMillis();
            final float startRotation = mCurrent.getRotation();
            final long duration = 1500;

            final Interpolator interpolator = new LinearInterpolator();

            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float t = interpolator.getInterpolation((float)elapsed/duration);
                    float rot = t*i+(1-t)*startRotation;
                    mCurrent.setRotation(-rot > 180?rot/2:rot);
                    if(t<1.0)
                    {
                        handler.postDelayed(this,16);
                    }
                }
            });


        }

        private void startLocationUpdates() {
            if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED  )
            {
                return;
            }
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (com.google.android.gms.location.LocationListener) this);
           // fusedLocationProviderClient.requestLocationUpdates(mGoogleApiClient,mLocationRequest,(com.google.android.gms.location.LocationListener)this);
        }


        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;


        }

        @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            displayLocation();

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onConnected(@Nullable Bundle bundle) {
            displayLocation();
            startLocationUpdates();

        }

        @Override
        public void onConnectionSuspended(int i) {
            mGoogleApiClient.connect();

        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        }
    }

在这种情况下,如果您想在第一行的条件为true时跳过其余命令,那么

FOR /L %%G IN (0,1,31) DO (
    rem Things that will done every time
    IF NOT "%%G" == "6" (do something) else (
        rem Things that will be done when %%G != 6
        rem i.e. if %%G != 6 next
    )
)

再次使用for循环不是必需的,因为每个循环只跳到一种情况。第一行似乎正在检查是否是播放器,因此只需将其拆分为单独的功能

FOR /L %%G IN (0,1,31) DO (
    IF "%2" == "1" IF "%%G" == "%X%" (CALL :C 0C "i") ELSE (
        IF "!M%MAP%%1:~%%G,1!" == "0" CALL :C 00 " "
        IF "!M%MAP%%1:~%%G,1!" == "1" CALL :C 2A "w"
        IF "!M%MAP%%1:~%%G,1!" == "2" CALL :C 87 "S"
        IF ...
    )
)
相关问题