public void Update(GameTime gameTime, Rocket rocket,Vector2 backgroundPosition,Texture2D background) { if ((rocket.rocketPosition.X - view.Width / 2) <= backgroundPosition.X) { if( rocket.rocketPosition.X > backgroundPosition.X) { center.X = backgroundPosition.X; } else { center.X = backgroundPosition.X + background.Width -view.Width; } } else if ((rocket.rocketPosition.X - view.Width / 2) >= backgroundPosition.X + background.Width - view.Width) { if (rocket.rocketPosition.X < backgroundPosition.X + background.Width) { center.X = backgroundPosition.X + background.Width - view.Width; } else { center.X = backgroundPosition.X; } } else { center.X = rocket.rocketPosition.X - view.Width / 2; } if ((rocket.rocketPosition.Y - view.Height / 2) <= backgroundPosition.Y) { if (rocket.rocketPosition.Y > backgroundPosition.Y) { center.Y = backgroundPosition.Y; } else { center.Y = backgroundPosition.Y + background.Height - view.Height; } } else if ((rocket.rocketPosition.Y - view.Height / 2) >= backgroundPosition.Y + background.Height - view.Height) { if (rocket.rocketPosition.Y < backgroundPosition.Y + background.Height) { center.Y = backgroundPosition.Y + background.Height - view.Height; } else { center.Y = backgroundPosition.Y; } } else { center.Y = rocket.rocketPosition.Y - view.Height / 2; } transform = Matrix.CreateScale(new Vector3(1, 1, 0)) * Matrix.CreateTranslation(new Vector3(-center.X, -center.Y, 0)); }
public MyGame(ContentManager content, GraphicsDevice graphics) { this.content = content; this.graphics = graphics; rocket = new Rocket(graphics,content); camera = new Camera(graphics.Viewport); backgroundPosition = new Vector2(-graphics.Viewport.Width/2, -graphics.Viewport.Height/2); pauseBgPosition = new Vector2(-graphics.Viewport.Width / 2, -graphics.Viewport.Height / 2); healthTexture = content.Load<Texture2D>("health/health0"); asteroids = new List<Asteroid>(); background = new List<Texture2D>(); background2 = new List<Texture2D>(); bonus = new List<Bonus>(); r = new Random(); for (int i = 0; i < 5; i++) { Asteroid newasteroid = new Asteroid(content, graphics.Viewport,r.Next(0,3),r.Next(3,4),r.Next(-700,-300),r.Next(-700,-300),r.Next(-5,5),r.Next(-5,5),(float)(r.Next(1,2)/10)); asteroids.Add(newasteroid); } for (int i = 0; i <= 150; i++) { Texture2D backgroundTexture = content.Load<Texture2D>("gamebg/gamebg" + i); background.Add(backgroundTexture); } currentBg = background[0]; pauseBg = content.Load<Texture2D>("pausebg"); pauseTexture = content.Load<Texture2D>("pause"); gameoverTexture = content.Load<Texture2D>("gameover"); font1 = content.Load<SpriteFont>("SpriteFont1"); bgsound1 = content.Load<Song>("audio/bgsound1"); crashsound = content.Load<SoundEffect>("audio/crash"); hitsound = content.Load<SoundEffect>("audio/hit"); MediaPlayer.Play(bgsound1); MediaPlayer.IsRepeating = true; }