package com.example.example;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.Forestadventure.allaboutoddbuds.R;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.startapp.android.publish.ads.banner.Banner;
import com.startapp.android.publish.adsCommon.StartAppAd;
import com.startapp.android.publish.adsCommon.StartAppSDK;
public class MainActivity extends Activity {
private CCGLSurfaceView mGLSurfaceView;
public static boolean lock_level = false;
public static MainActivity app;
//<!-- Admob Ads Using Google Play Services SDK -->
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
private static final String AD_INTERSTITIAL_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
public StartAppAd startAppAd;
public static String STARTAPP_ID = "20346xxxx";
/** The Admob ad. */
private InterstitialAd interstitialAd = null;
public AdView adView = null;
AdRequest interstitialAdRequest;
public void onCreate(Bundle savedInstanceState)
{
app = this;
StartAppSDK.init(this, STARTAPP_ID, true);
StartAppAd.disableSplash();
startAppAd = new StartAppAd(this);
super.onCreate(savedInstanceState);
lock_level = LibConstante.booleanValue(getResources().getString(R.string.lock_level), "on");
LibConstante.valueGameover = Integer.parseInt(getResources().getString(R.string.show_interstitial_every_gameover));
LibConstante.valueGameCompleted = Integer.parseInt(getResources().getString(R.string.show_interstitial_every_gamecompleted));
LibConstante.heroNumber = Integer.parseInt(getResources().getString(R.string.hero_number));
// set view
mGLSurfaceView = new CCGLSurfaceView(this);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("D700C61EFAE5E803737A2EF9D9861E27")
.build();
//Ads ----------------
// Create the adView
final RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
final RelativeLayout.LayoutParams bannerParameters =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(mGLSurfaceView);
//<!-- Ads Using Google Play Services SDK -->
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.loadAd(adRequest);
adView.setAdListener(new AdListener(){
@Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
Banner startAppBanner = new Banner(MainActivity.this);
if (startAppBanner.getParent() != null) {
ViewGroup tempVg = (ViewGroup) startAppBanner.getParent();
tempVg.removeView(startAppBanner);
}
layout.addView(startAppBanner, bannerParameters);
}
@Override
public void onAdLoaded() {
// TODO Auto-generated method stub
if (adView.getParent() != null) {
ViewGroup tempVg = (ViewGroup) adView.getParent();
tempVg.removeView(adView);
}
layout.addView(adView, bannerParameters);
}
});
setContentView(layout);
// Create an Interstitial ad.
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
interstitialAdRequest = new AdRequest.Builder()
.addTestDevice("D700C61EFAE5E803737A2EF9D9861E27")
.build();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
//interstitialAd.show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Interstitial Ads loading failed", Toast.LENGTH_SHORT).show();
}
});
// Load the interstitial ad.
LoadInterstitialAds();
//----------------------
// set director
CCDirector director = CCDirector.sharedDirector();
director.attachInView(mGLSurfaceView);
director.setAnimationInterval(1/60);
// get display info
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
G.display_w = displayMetrics.widthPixels;
G.display_h = displayMetrics.heightPixels;
G.scale = Math.max(G.display_w/1280.0f, G.display_h/800.0f);
G.width = G.display_w / G.scale;
G.height = G.display_h / G.scale;
// get data
SharedPreferences sp = CCDirector.sharedDirector().getActivity().getSharedPreferences("GameInfo", 0);
G.music = sp.getBoolean("music", true);
G.sound = sp.getBoolean("sound", true);
// create sound
G.soundMenu = MediaPlayer.create(this, R.raw.menu);
G.soundMenu.setLooping(true);
G.soundGame = MediaPlayer.create(this, R.raw.game);
G.soundGame.setLooping(true);
G.soundCollide = MediaPlayer.create(this, R.raw.collide);
G.soundJump = MediaPlayer.create(this, R.raw.jump);
G.soundLongJump = MediaPlayer.create(this, R.raw.long_jump);
G.soundSpeedDown = MediaPlayer.create(this, R.raw.speed_down);
G.soundSpeedUp = MediaPlayer.create(this, R.raw.speed_up);
G.soundDirection = MediaPlayer.create(this, R.raw.direction_sign);
G.soundClick = MediaPlayer.create(this, R.raw.menu_click);
G.soundCollect = MediaPlayer.create(this, R.raw.collect);
G.bgSound = G.soundMenu;
// show menu
CCScene scene = CCScene.node();
scene.addChild(new MenuLayer(true));
director.runWithScene(scene);
}
@Override
public void onPause()
{
super.onPause();
G.bgSound.pause();
CCDirector.sharedDirector().onPause();
}
@Override
public void onResume()
{
super.onResume();
if( G.music ) G.bgSound.start();
CCDirector.sharedDirector().onResume();
}
@Override
public void onDestroy()
{
super.onDestroy();
G.bgSound.pause();
CCDirector.sharedDirector().end();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
CCDirector.sharedDirector().onKeyDown(event);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void LoadInterstitialAds()
{
interstitialAd.loadAd(interstitialAdRequest);
}
public void showInterstitialAds()
{
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()){
interstitialAd.show();
LoadInterstitialAds();
}
else{
startAppAd.showAd();
LoadInterstitialAds();
}
}
});
}
}
我不知道为什么我的输出总是重复第3和第4行,无论我输入什么数字。 谢谢
答案 0 :(得分:2)
a != 0 or 1
实际上被解释为(a != 0) or (1)
,其中括在括号中的表达式被评估为布尔值。由于1
始终为True
,因此无论while
的值如何,a
循环都将继续运行。
请参阅近似重复的How do I test one variable against multiple values?,详细了解or
运算符无法满足您在此情况下可能达到的目的的原因。
您应该在条件中使用字符串,因为raw_input
返回一个字符串,并使用a
运算符和元组检查not in
是否是多种可能之一:
a = raw_input("please type 1 or 0")
while a not in ('1', '0'):
print("please retype")
a = raw_input("please type 1 or 0")
print("thanks")
答案 1 :(得分:0)
另一个例子:
a = raw_input("please type 1 or 0\n")
while True:
if a in ("1", "0"):
break
print "please retype"
a = raw_input("please type 1 or 0\n")
print "thanks"
答案 2 :(得分:0)
您可以使用辅助变量来确定是否符合条件。
b = True
while b:
a = input("please type 1 or 0")
if a == "0" or a == "1":
b = False
else:
print("please retype")
print("thanks")
这也可以防止非整数字符抛出错误。