更改游戏背景的问题

ssgvzors  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(314)

我创建了一个名为score的视频游戏,它保存了用户杀死的每一只鸟的得分。
因此,基本上在10个分数之后,我希望背景自动替换为另一个。我尝试将分数初始化为一个静态变量,并且在我的gameview activity getscore中创建了一个唯一的方法来返回它的值。
之后,我用if语句将值传递给background类,以自动替换背景,但它不起作用背景保持原样,并且在10分后不会替换。如果有人能帮助我,我将非常高兴
这是我的密码:

  1. package com.example.gobirdgo;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.graphics.Rect;
  12. import android.graphics.drawable.Drawable;
  13. import android.media.AudioAttributes;
  14. import android.media.AudioManager;
  15. import android.media.SoundPool;
  16. import android.os.Build;
  17. import android.view.MotionEvent;
  18. import android.view.SurfaceView;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Random;
  22. public class GameView extends SurfaceView implements Runnable {
  23. private int screenX,screenY;
  24. public static int score=0;
  25. public int candyhouseheight,candyhousewidth;
  26. private FlightActivity flight;
  27. private BirdActivity[]birds;
  28. private Boolean isGameOver=false;
  29. private SoundPool soundPool;
  30. private int sound;
  31. Context context;
  32. private Random random;
  33. private SharedPreferences prefs;
  34. Bitmap candyhouse1;
  35. int candyX,candyY;
  36. private Paint paint;
  37. private Thread thread;
  38. private GameActivity activity;
  39. public static float screenRatioX,screenRatioY;//suit the screen to every app
  40. private boolean isPlaying;
  41. private List<BulletActivity>bullets;
  42. private Background background1,background2;
  43. public GameView(GameActivity activity, int screenX, int screenY) {
  44. super(activity);
  45. prefs=activity.getSharedPreferences("game",Context.MODE_PRIVATE);
  46. if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
  47. AudioAttributes audioAttributes=new AudioAttributes.Builder().
  48. setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
  49. .setUsage(AudioAttributes.USAGE_GAME)
  50. .build();
  51. soundPool=new SoundPool.Builder().setAudioAttributes(audioAttributes).build();
  52. }
  53. else
  54. soundPool=new SoundPool(1, AudioManager.STREAM_MUSIC,0);
  55. sound=soundPool.load(activity,R.raw.shoot,1);
  56. candyX=1100;
  57. candyY=400;
  58. this.activity=activity;
  59. screenRatioX=1920f/screenX;
  60. screenRatioY=1080/screenY;
  61. this.screenX=screenX;
  62. this.screenY=screenY;
  63. background1=new Background(screenX,screenY,getResources());
  64. background2=new Background(screenX,screenY,getResources());
  65. flight=new FlightActivity(this,screenY,getResources());
  66. bullets=new ArrayList<>();
  67. background2.x=screenX;
  68. paint=new Paint();
  69. paint.setTextSize(50);
  70. paint.setColor(Color.WHITE);
  71. birds=new BirdActivity[4];
  72. for(int i=0;i<4;i++){
  73. BirdActivity bird=new BirdActivity(getResources());
  74. birds[i]=bird;
  75. }
  76. random=new Random();
  77. }
  78. @Override
  79. public void run() {
  80. while(isPlaying){
  81. update();
  82. draw();
  83. sleep();
  84. }
  85. }
  86. private void update(){//change screen position
  87. background1.x-=50*screenRatioX;
  88. background2.x-=50*screenRatioX;
  89. if(background1.x+background1.background.getWidth()<0){
  90. background1.x=screenX;
  91. }
  92. if(background2.x+background2.background.getWidth()<0){
  93. background2.x=screenX;
  94. }
  95. if(flight.isGoingUp)
  96. flight.y-=40*screenRatioY;//put the player on the top of the screen
  97. else
  98. flight.y+=40*screenRatioY;//put the player on buttom
  99. if(flight.y<0)
  100. flight.y=0;//ensure the player wont fall from the screen
  101. if(flight.y>screenY-flight.height)//if player is already off the screen from the buttom
  102. flight.y=screenY-flight.height;//
  103. List<BulletActivity>trash=new ArrayList<>();//bullets which are off the screen are here
  104. for(BulletActivity bullet:bullets){
  105. if(bullet.x>screenX)
  106. trash.add(bullet);
  107. bullet.x+=50*screenRatioX;//falling off the screen
  108. for(BirdActivity bird:birds){
  109. if(Rect.intersects(bird.getCollisionShape(),bullet.getCollisionShape())){
  110. score++;
  111. bird.x=-500;
  112. bullet.x=screenX+500;
  113. bird.wasShot=true;
  114. }
  115. }
  116. }
  117. for(BulletActivity bullet:trash)
  118. bullets.remove(bullet);//remove bullets hence they are not in need any more
  119. for(BirdActivity bird:birds){
  120. bird.x-=bird.speed;
  121. if(bird.x+bird.width<0){
  122. int bound=(int)(30*screenRatioX);
  123. bird.speed=random.nextInt(bound);
  124. if(bird.speed<10*screenRatioX)
  125. if(bird.speed<10*screenRatioX)//in case the speed is zero and bird wont move
  126. bird.speed=(int)(10*screenRatioX);
  127. bird.x=screenX;//replace the bird
  128. bird.y=random.nextInt(screenY-bird.height);
  129. bird.wasShot=false;
  130. }
  131. if(Rect.intersects(bird.getCollisionShape(),flight.getCollisionShape())){
  132. isGameOver=true;
  133. return;
  134. //rectangle calculate the collision between player and biird
  135. }
  136. }
  137. }
  138. private void draw(){
  139. if(getHolder().getSurface().isValid()){//in case we can allocate canva
  140. Canvas canvas=getHolder().lockCanvas();//lock current canvas in the same position
  141. canvas.drawBitmap(background1.background,background1.x
  142. ,background1.y,paint);
  143. canvas.drawBitmap(background2.background,background2.x,background2.y,paint);
  144. candyhouse1=BitmapFactory.decodeResource(getResources(),R.drawable.candi);
  145. canvas.drawBitmap(candyhouse1,candyX,candyY,null);
  146. canvas.drawBitmap(flight.getFlight(),flight.x,flight.y,paint);
  147. for(BirdActivity bird:birds)
  148. canvas.drawBitmap(bird.getBird(),bird.x,bird.y,paint);
  149. canvas.drawText(getContext().getString(R.string.your_score_txt)+score+"",screenX/2f,164,paint);
  150. if(isGameOver){
  151. isPlaying=false;
  152. canvas.drawBitmap(flight.getDied(),flight.x,flight.y,paint);
  153. saveIfHighScore();
  154. waitBeforeExiting();
  155. getHolder().unlockCanvasAndPost(canvas);//draw out backgrounds on screen
  156. }
  157. canvas.drawBitmap(flight.getFlight(),flight.x,flight.y,paint);
  158. for(BulletActivity bullet:bullets)
  159. canvas.drawBitmap(bullet.bullet,bullet.x,bullet.y,paint);
  160. getHolder().unlockCanvasAndPost(canvas);//draw out backgrounds on screen
  161. }
  162. }
  163. private void waitBeforeExiting() {
  164. try {
  165. Thread.sleep(3000);
  166. activity.startActivity(new Intent(activity,MainActivity.class));
  167. activity.finish();
  168. } catch (InterruptedException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. private void saveIfHighScore() {
  173. if(prefs.getInt("highscore",0)<score){
  174. SharedPreferences.Editor editor=prefs.edit();
  175. editor.putInt("highscore",score);
  176. editor.apply();
  177. }
  178. }
  179. private void sleep(){
  180. try {
  181. Thread.sleep(17);
  182. } catch (InterruptedException e) {
  183. e.printStackTrace();
  184. }
  185. }
  186. public void resume(){
  187. isPlaying=true;
  188. thread=new Thread(this);
  189. thread.start();
  190. }
  191. public void pause(){
  192. try {
  193. isPlaying=false;
  194. thread.join();
  195. } catch (InterruptedException e) {
  196. e.printStackTrace();
  197. }
  198. }
  199. @Override
  200. public boolean onTouchEvent(MotionEvent event) {
  201. switch(event.getAction()){
  202. case MotionEvent.ACTION_DOWN://if user press button of screen
  203. if(event.getX()<screenX/2){//if user type in the left side of the screen
  204. flight.isGoingUp=true;
  205. }
  206. break;
  207. case MotionEvent.ACTION_UP://if user leave the screen
  208. flight.isGoingUp=false;
  209. if(event.getX()>screenX/2)
  210. flight.toShoot++;
  211. break;
  212. }
  213. return true;
  214. }
  215. public void newBullet() {
  216. if(prefs.getBoolean("isMute",false))
  217. soundPool.play(sound,1,1,0,0,1);
  218. BulletActivity bullet=new BulletActivity(getResources());
  219. bullet.x=flight.x+flight.width;
  220. bullet.y=flight.y+(flight.height/2);
  221. bullets.add(bullet);
  222. }
  223. public static int getScore(){
  224. return score;
  225. }
  226. }
  1. package com.example.gobirdgo;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.content.res.Resources;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.os.Bundle;
  8. import android.text.NoCopySpan;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. public class Background extends AppCompatActivity {
  11. int x=0,y=0;
  12. private GameView gameview;
  13. static int score;
  14. Bitmap background;
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. score = GameView.getScore();
  18. }
  19. Background(int screenX, int screenY,Resources res){
  20. if(score>=0&&score<=10)
  21. background= BitmapFactory.decodeResource(res,R.drawable.forest_stage);
  22. else
  23. background= BitmapFactory.decodeResource(res,R.drawable.stage2);
  24. background=Bitmap.createScaledBitmap(background,screenX,screenY,false);
  25. }
  26. }

还有后台总是在做一个问题和中途插播游戏

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题