public override void Update(GameTime gameTime, Rectangle clientBounds) { elapsedTime += gameTime.ElapsedGameTime.Milliseconds; if (elapsedTime >= intervalTime) { elapsedTime -= intervalTime; currentFrame.X++; if (direction == false) { if (currentFrame.X == sheetSize.X && currentFrame.Y == 9) { currentFrame.X = 1; currentFrame.Y = 6; } if (currentFrame.X > sheetSize.X) { currentFrame.X = 1; currentFrame.Y++; } } if (direction == true) { if (currentFrame.X == sheetSize.X && currentFrame.Y == 3) { currentFrame.X = 1; currentFrame.Y = 0; } if (currentFrame.X > sheetSize.X) { currentFrame.X = 1; currentFrame.Y++; } } } MouseState mouseState = Mouse.GetState(); if (mouseState.LeftButton == ButtonState.Pressed) { position.X = mouseState.X; position.Y = mouseState.Y; } /* if (position.X != mouseState.X || position.Y != mouseState.Y) * { * position.X = mouseState.X; * position.Y = mouseState.Y; * }*/ if (position.X < 0) { position.X = 0; } if (position.Y < 0) { position.Y = 0; } if (position.X > (clientBounds.Width - (int)((1 + (level - 1) / 10) * frameSize.X))) { position.X = clientBounds.Width - (int)((1 + (level - 1) / 10) * frameSize.X); } if (position.Y > (clientBounds.Height - (int)((1 + (level - 1) / 10) * frameSize.Y))) { position.Y = clientBounds.Height - (int)((1 + (level - 1) / 10) * frameSize.Y); } //鼠标相对位置 if ((mouseState.X > position.X + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.X / 2))) && mouseState.Y > position.Y + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.X / 2)))) && (mouseState.Y > position.Y + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.Y / 2))) && mouseState.X > position.X + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.X / 2))))) { Now_direction = re_direction.RD; speed.X = speed.Y = 5; direction = true; } if ((mouseState.X > position.X + (int)(1 + (level - 1) / 10) * (5 + (int)(frameSize.X / 2)) && mouseState.Y < position.Y - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.Y / 2)))) && (mouseState.Y < position.Y - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.Y / 2))) && mouseState.X > position.X + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.X / 2))))) { Now_direction = re_direction.RU; speed.X = 5; speed.Y = -5; direction = true; } if ((mouseState.X < position.X - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.X / 2))) && mouseState.Y > position.Y + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.Y / 2)))) && (mouseState.Y > position.Y + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.Y / 2))) && mouseState.X < position.X - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.X / 2))))) { Now_direction = re_direction.LD; speed.X = -5; speed.Y = 5; direction = false; } if ((mouseState.X < position.X - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.X / 2))) && mouseState.Y < position.Y - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.Y / 2)))) && (mouseState.Y < position.Y + (int)((1 + (level - 1) / 10) * ((int)(frameSize.Y / 2) - 5)) && mouseState.X < position.X - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.X / 2))))) { Now_direction = re_direction.LU; speed.X = speed.Y = -5; direction = false; } if (mouseState.X >= position.X - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.X / 2))) && mouseState.X <= position.X + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.X / 2))) && mouseState.Y >= position.Y - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.Y / 2))) && mouseState.Y <= position.Y + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.Y / 2)))) { speed.X = speed.Y = 0; Now_direction = last; } if (mouseState.Y >= position.Y - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.Y / 2))) && mouseState.Y <= position.Y + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.Y / 2))) && mouseState.X > position.X + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.X / 2)))) { speed.X = 5; speed.Y = 0; Now_direction = last; } if (mouseState.Y >= position.Y - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.Y / 2))) && mouseState.Y <= position.Y + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.Y / 2))) && mouseState.X < position.X - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.X / 2)))) { speed.X = -5; speed.Y = 0; Now_direction = last; } if (mouseState.X >= position.X - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.X / 2))) && mouseState.X <= position.X + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.X / 2))) && mouseState.Y > position.Y + (int)((1 + (level - 1) / 10) * (5 + (int)(frameSize.Y / 2)))) { speed.X = 0; speed.Y = 5; Now_direction = last; } if (mouseState.X >= position.X - (int)((1 + (level - 1) / 10) * (3 - (int)(frameSize.X / 2))) && mouseState.X <= position.X + (int)((1 + (level - 1) / 10) * (3 + (int)(frameSize.X / 2))) && mouseState.Y < position.Y - (int)((1 + (level - 1) / 10) * (5 - (int)(frameSize.Y / 2)))) { speed.X = 0; speed.Y = -5; Now_direction = last; } //等待化简!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1 if (last == re_direction.RU && Now_direction == re_direction.LU) { currentFrame.X = 0; currentFrame.Y = 6; } if (last == re_direction.RU && Now_direction == re_direction.LD) { currentFrame.X = 0; currentFrame.Y = 6; } if (last == re_direction.LU && Now_direction == re_direction.RU) { currentFrame.X = 0; currentFrame.Y = 0; } if (last == re_direction.LU && Now_direction == re_direction.RD) { currentFrame.X = 0; currentFrame.Y = 0; } if (last == re_direction.RD && Now_direction == re_direction.LD) { currentFrame.X = 0; currentFrame.Y = 6; } if (last == re_direction.RD && Now_direction == re_direction.LU) { currentFrame.X = 0; currentFrame.Y = 6; } if (last == re_direction.LD && Now_direction == re_direction.RD) { currentFrame.X = 0; currentFrame.Y = 0; } if (last == re_direction.LD && Now_direction == re_direction.RU) { currentFrame.X = 0; currentFrame.Y = 0; } last = Now_direction; position += speed; }
public override void Update(GameTime gameTime, Rectangle clientBounds) { //base.Update(gameTime, clientBounds); elapsedTime += gameTime.ElapsedGameTime.Milliseconds; position += Speed; if (Speed.X < 0) { direction = false; } if (Speed.X > 0) { direction = true; } if (position.X > (clientBounds.Width)) { currentFrame.X = currentFrame.Y = 0; } if (position.X < 0 - frameSize.X * 2.5) { currentFrame.X = 0; currentFrame.Y = 4; } if (position.X < 0 - frameSize.X * 2.5 || position.X > (clientBounds.Width)) { speed.X *= -1; if (last == re_direction.LU) { Now_direction = re_direction.RU; } if (last == re_direction.LD) { Now_direction = re_direction.RD; } if (last == re_direction.RU) { Now_direction = re_direction.LU; } if (last == re_direction.RD) { Now_direction = re_direction.LD; } } if (position.Y < 0 - frameSize.Y * 2.5 || position.Y > (clientBounds.Height)) { speed.Y *= -1; if (last == re_direction.LU) { Now_direction = re_direction.LD; } if (last == re_direction.LD) { Now_direction = re_direction.LU; } if (last == re_direction.RU) { Now_direction = re_direction.RD; } if (last == re_direction.RD) { Now_direction = re_direction.RU; } } if (elapsedTime >= intervalTime) { elapsedTime -= intervalTime; currentFrame.X++; if (speed.X < 0) { if (currentFrame.X == sheetSize.X && currentFrame.Y == 3) { currentFrame.X = 1; currentFrame.Y = 0; } if (currentFrame.X > sheetSize.X) { currentFrame.X = 1; currentFrame.Y++; } } if (speed.X > 0) { if (currentFrame.X == sheetSize.X && currentFrame.Y == 7) { currentFrame.X = 1; currentFrame.Y = 4; } if (currentFrame.X > sheetSize.X) { currentFrame.X = 1; currentFrame.Y++; } } } }