2017-03-27 1 views
0

Quand je reçois mes points (poang) à highscore quand je frappe les blocs avec 5 points. Ils ne seront pas mis à jour les mêmes, les points parfois (de Poäng) seront 18 et highscore seront 20.Mes meilleurs scores ne correspondent pas parfois à mes points

(Désolé mon mauvais anglais)

  • Poäng = points
  • linje = ligne
  • liv = vit
  • boll = balle
  • Poäng = points
  • blockröd = blockred
  • blockgrön = blockgreen

    public class Game1 : Game 
    { 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    SpriteFont spritefont; 
    Texture2D linje_texture; 
    Texture2D linjeliten_texture; 
    Texture2D boll_texture; 
    Texture2D blockröd_texture; 
    Texture2D blockgrön_texture; 
    Texture2D gameover_texture; 
    Rectangle linje_rect; 
    Rectangle linjeliten_rect; 
    Rectangle boll_rect; 
    Rectangle blockröd_rect; 
    Rectangle blockgrön_rect; 
    Rectangle gameover_rect; 
    
    Vector2 linje_speed; 
    Vector2 linjeliten_speed; 
    Vector2 boll_speed; 
    
    Random random; 
    
    StreamReader sr; 
    StreamWriter sw; 
    
    int liv = 3; 
    int poang = 0; 
    int highscore; 
    
    List<Rectangle> block = new List<Rectangle>(); 
    List<Rectangle> block2 = new List<Rectangle>(); 
    
    bool Start = false; 
    bool holdingleft = false; 
    bool holdingright = false; 
    bool resetballspeed = false; 
    
    public Game1() 
    { 
        graphics = new GraphicsDeviceManager(this); 
        Content.RootDirectory = "Content"; 
    
        graphics.PreferredBackBufferWidth = 760; 
        graphics.PreferredBackBufferHeight = 620; 
    } 
    
    protected override void Initialize() 
    { 
        random = new Random(); 
        linje_speed.X = 6f; 
        linjeliten_speed.X = 6f; 
        boll_speed.X = random.Next(-1, 1); 
        boll_speed.Y = 7f; 
    
        sr = new StreamReader("highscore.txt"); 
        highscore = int.Parse(sr.ReadLine()); 
        sr.Close(); 
    
        base.Initialize(); 
    } 
    
    protected override void LoadContent() 
    { 
        spriteBatch = new SpriteBatch(GraphicsDevice); 
        spritefont = Content.Load<SpriteFont>("Fonts/Myfont"); 
        linje_texture = Content.Load<Texture2D>("Pics/linje-lång"); 
        linjeliten_texture = Content.Load<Texture2D>("Pics/linje"); 
        boll_texture = Content.Load<Texture2D>("Pics/boll"); 
        blockgrön_texture = Content.Load<Texture2D>("Pics/block-grön"); 
        blockröd_texture = Content.Load<Texture2D>("Pics/block-röd"); 
        gameover_texture = Content.Load<Texture2D>("Pics/GameOver"); 
        linje_rect = new Rectangle((Window.ClientBounds.Width - linje_texture.Width)/2, 580, linje_texture.Width, linje_texture.Height); 
        linjeliten_rect = new Rectangle((Window.ClientBounds.Width - linjeliten_texture.Width)/2, 580, linjeliten_texture.Width, linjeliten_texture.Height); 
        boll_rect = new Rectangle((Window.ClientBounds.Width - boll_texture.Width)/2, 556, boll_texture.Width, boll_texture.Height); 
        gameover_rect = new Rectangle((Window.ClientBounds.Width/2) - (gameover_texture.Width/2), (Window.ClientBounds.Height/2) - gameover_texture.Height/2, gameover_texture.Width, gameover_texture.Height); 
    
        block.Add(blockgrön_rect); 
        block2.Add(blockröd_rect); 
        for (int i = 1; i < 2; i++) 
        { 
         for (int g = 1; g < 13; g++) 
         { 
          block2.Add(new Rectangle((g * 63) - 60, (i * 20), blockröd_texture.Width, blockröd_texture.Height)); 
         } 
        } 
        for (int i = 1; i < 4; i++) 
        { 
         for (int g = 1; g < 13; g++) 
         { 
          block.Add(new Rectangle((g * 63) - 60, (i * 20) + 20, blockgrön_texture.Width, blockgrön_texture.Height)); 
         } 
        } 
    } 
    
    protected override void UnloadContent() 
    { 
        // TODO: Unload any non ContentManager content here 
    } 
    
    protected override void Update(GameTime gameTime) 
    { 
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
        { 
         if (poang == highscore) 
         { 
          sw = new StreamWriter("highscore.txt"); 
          sw.WriteLine(poang); 
          sw.Close(); 
         } 
         Exit(); 
        } 
        if (Start == true) 
        { 
         boll_rect.X += (int)boll_speed.X; 
         boll_rect.Y += (int)boll_speed.Y; 
        } 
    
        if (Start == false) 
        { 
         boll_rect.X = linje_rect.X + ((linje_texture.Width/2) - (boll_texture.Width/2)); //how the ball condition is to the long line 
         boll_rect.X = linjeliten_rect.X + ((linjeliten_texture.Width/2) - (boll_texture.Width/2)); //the same but ball condition to small line 
        } 
        if (boll_rect.X > Window.ClientBounds.Width - boll_texture.Width || boll_rect.X < 0) 
         boll_speed.X *= -1; 
    
        if (boll_rect.Y > Window.ClientBounds.Height - boll_texture.Height || boll_rect.Y < 0) 
         boll_speed.Y *= -1; 
    
        if (boll_rect.Y > Window.ClientBounds.Height - boll_texture.Height) 
        { 
         liv -= 1; 
         Start = false; 
         boll_rect.X = (Window.ClientBounds.Width - boll_texture.Width)/2; 
         boll_rect.Y = 556; 
         linje_rect.X = (Window.ClientBounds.Width - linje_texture.Width)/2; 
         linje_rect.Y = 580; 
         linjeliten_rect.X = (Window.ClientBounds.Width - linjeliten_texture.Width)/2; 
         linjeliten_rect.Y = 580; 
        } 
    
        KeyboardState ks = Keyboard.GetState(); 
        if (ks.IsKeyDown(Keys.Left)) 
        { 
         linje_rect.X -= (int)linje_speed.X; 
         linjeliten_rect.X -= (int)linjeliten_speed.X; 
         holdingleft = true; 
        } 
        else if (ks.IsKeyDown(Keys.Right)) 
        { 
         linje_rect.X += (int)linje_speed.X; 
         linjeliten_rect.X += (int)linjeliten_speed.X; 
         holdingright = true; 
        } 
        else if (ks.IsKeyDown(Keys.Space)) 
        { 
         Start = true; 
        } 
    
        if (ks.Equals(new KeyboardState())) 
        { 
         resetballspeed = true; 
        } 
    
        if (linje_rect.X > Window.ClientBounds.Width - linje_texture.Width) 
         linje_rect.X = (Window.ClientBounds.Width - linje_texture.Width); 
    
        if (linjeliten_rect.X > Window.ClientBounds.Width - linjeliten_texture.Width) 
         linjeliten_rect.X = (Window.ClientBounds.Width - linjeliten_texture.Width); 
    
        if (linje_rect.X < 0) 
         linje_rect.X = 0; 
    
        if (linjeliten_rect.X < 0) 
         linjeliten_rect.X = 0; 
    
        if (linje_rect.Intersects(boll_rect)) 
        { 
         boll_speed.Y *= -1; 
         boll_rect.Y += (int)boll_speed.Y; 
         if (holdingleft == true) 
         { 
          boll_speed.X -= 2; 
         } 
         else if (holdingright == true) 
         { 
          boll_speed.X += 2; 
         } 
         else if (resetballspeed == true) 
         { 
          boll_speed.X = 1; 
         } 
        } 
    
        if (linjeliten_rect.Intersects(boll_rect)) 
        { 
         boll_speed.Y *= -1; 
         boll_rect.Y += (int)boll_speed.Y; 
         if (holdingleft == true) 
         { 
          boll_speed.X -= 1; 
         } 
         else if (holdingright == true) 
         { 
          boll_speed.X += 1; 
         } 
         else if (resetballspeed == true) 
         { 
          boll_speed.X = 1; 
         } 
        } 
    
        for (int j = 1; j < block.Count; j++) 
        { 
         if (boll_rect.Intersects(block[j])) 
         { 
          boll_speed.Y *= -1; 
          poang += 1; 
          block.RemoveAt(j); 
          if (poang > highscore) 
          { 
           highscore++; 
          } 
         } 
        } 
        for (int k = 1; k < block2.Count; k++) 
        { 
         if (boll_rect.Intersects(block2[k])) 
         { 
          boll_speed.Y *= -1; 
          poang += 5; 
          block2.RemoveAt(k); 
          if (poang > highscore) 
          { 
           highscore += 5; 
          } 
         } 
        } 
    
        holdingleft = false; 
        holdingright = false; 
    
    
        base.Update(gameTime); 
    } 
    
    protected override void Draw(GameTime gameTime) 
    { 
        GraphicsDevice.Clear(Color.Black); 
    
        spriteBatch.Begin(); 
        if (liv > 0) 
        { 
         if (poang < 10) 
         { 
          spriteBatch.Draw(linje_texture, linje_rect, Color.White); 
         } 
         else if (poang > 9) 
         { 
          spriteBatch.Draw(linjeliten_texture, linjeliten_rect, Color.White); 
         } 
         spriteBatch.Draw(boll_texture, boll_rect, Color.White); 
         spriteBatch.DrawString(spritefont, "Lives left: " + liv, Vector2.Zero, Color.White); 
         spriteBatch.DrawString(spritefont, "Points: " + poang, new Vector2(350, 0), Color.White); 
         spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(660, 0), Color.White); 
         foreach (Rectangle g in block) 
         { 
          spriteBatch.Draw(blockgrön_texture, g, Color.White); 
         } 
         foreach (Rectangle t in block2) 
         { 
          spriteBatch.Draw(blockröd_texture, t, Color.White); 
         } 
        } 
        else if (liv == 0) 
        { 
         spriteBatch.Draw(gameover_texture, gameover_rect, Color.White); 
         if (poang == highscore) 
         { 
          sw = new StreamWriter("highscore.txt"); 
          sw.WriteLine(poang); 
          sw.Close(); 
         } 
        } 
    
        spriteBatch.End(); 
    
        base.Draw(gameTime); 
    } 
    } 
    

difficulté à ces pièces:

 for (int j = 1; j < block.Count; j++) //loopar igenom alla block 
     { 
      if (boll_rect.Intersects(block[j])) //om bollen träffar rutorna 
      { 
       boll_speed.Y *= -1; 
       poang += 1; 
       block.RemoveAt(j); //tar bort gröna blocket man träffar 
       if (poang > 9) 
       { 
        linje_rect.Width = 60; 
       } 
       if (poang > highscore) 
       { 
        highscore++; 
       } 
      } 
     } 

 for (int k = 1; k < block2.Count; k++) 
     { 
      if (boll_rect.Intersects(block2[k])) 
      { 
       boll_speed.Y *= -1; 
       poang += 5; 
       block2.RemoveAt(k); 
       if (poang > highscore) 
       { 
        highscore += 5; 
       } 
       block2.RemoveAt(k); 
      } 
     } 

Répondre

1

Remplacer highscore++; avec highscore = poang;

et highscore += 5; avec highscore = poang;

pour éviter discordances entre highscore et score.

+0

Merci beaucoup, ça marche! Haha n'a pas pensé à cette simple correction :) –