// made public for debugging, may make it private later: private void Spawn() { float gateMiddlePoint = Layer.ContentSize.Width / 2; gateMiddlePoint = CCRandom.GetRandomFloat(0 + GameCoefficients.FruitRadius + GameCoefficients.gateRadius * 2, Layer.ContentSize.Width - GameCoefficients.FruitRadius - GameCoefficients.gateRadius * 2); //tao red dau tien ben trai createNewFruitRed(0 + GameCoefficients.FruitRadius); for (float i = GameCoefficients.FruitRadius; i < Layer.ContentSize.Width - GameCoefficients.FruitRadius; i += GameCoefficients.FruitRadius * 2) //for (float i = 0; i < Layer.ContentSize.Width + GameCoefficients.FruitRadius; i += GameCoefficients.FruitRadius * 2) { ////tao canh cua de khi vuot qua dau tien la sac dinh tam bang gateMiddlePoint sau do la tao fruit(vatcan) tru di ban kinh cua gate //gateMiddlePoint = Layer.ContentSize.Width / 2; //Layer.ContentSize.Width / 2; //createNewFruitRed(gateMiddlePoint); if (i < gateMiddlePoint - GameCoefficients.gateRadius * 2 || i > gateMiddlePoint + GameCoefficients.gateRadius * 2) { createNewFruitRed(i); } else { creaNewFruitYellow(i); } //if ((0 < i && i < gateMiddlePoint - GameCoefficients.gateRadius * 2) || (i > gateMiddlePoint + GameCoefficients.gateRadius * 2 && i < Layer.ContentSize.Width)) //{ // createNewFruitRed(i); //} //else if (i != 0 + GameCoefficients.FruitRadius)//khong tao yellow dau tien ben trai vi nhu the se de len mau do da tao len dau tien //{ // creaNewFruitYellow(i); //} } //tao red cuoi cung ben phai createNewFruitRed(Layer.ContentSize.Width - GameCoefficients.FruitRadius); }
//--------------------------------------------------------------------------------------------------------- // GetRandomPosition //--------------------------------------------------------------------------------------------------------- // Get a position based on the index and sprite size //--------------------------------------------------------------------------------------------------------- static CCPoint GetRandomPosition(int xIndex, int yIndex) { double rndX = CCRandom.GetRandomFloat((xIndex * CELL_DIMS_HALF * 2) + (SCREEN_X_MARGIN + CELL_DIMS_HALF - CELL_CENTER_ZONE_HALF), (xIndex * CELL_DIMS_HALF * 2) + (SCREEN_X_MARGIN + CELL_DIMS_HALF + CELL_CENTER_ZONE_HALF)); double rndY = CCRandom.GetRandomFloat((yIndex * CELL_DIMS_HALF * 2) + (SCREEN_Y_MARGIN + CELL_DIMS_HALF - CELL_CENTER_ZONE_HALF), (yIndex * CELL_DIMS_HALF * 2) + (SCREEN_Y_MARGIN + CELL_DIMS_HALF + CELL_CENTER_ZONE_HALF)); return(new CCPoint((float)rndX, (float)rndY)); }
// made public for debugging, may make it private later: private void Spawn() { var fruit = new Fruit(); if (Layer == null) { throw new InvalidOperationException("Need to set Layer before spawning"); } fruit.PositionX = CCRandom.GetRandomFloat(0 + fruit.Radius, Layer.ContentSize.Width - fruit.Radius); fruit.PositionY = Layer.ContentSize.Height + fruit.Radius; if (CCRandom.Float_0_1() > .5f) { fruit.FruitColor = FruitColor.Red; } else { fruit.FruitColor = FruitColor.Yellow; } if (FruitSpawned != null) { FruitSpawned(fruit); } }
private void SpawnGroundUnit() { TimeInbetweenGroundUnitSpawns = CCRandom.GetRandomFloat(5f, 35f); CollisionEntity collisionEntity = null; var randomFloat = CCRandom.GetRandomFloat(0.0f, 3.0f); if (randomFloat > 2.0f) { collisionEntity = new Tree(); } else if (randomFloat > 1.0f) { collisionEntity = new House(); } else { collisionEntity = new Radar(); } collisionEntity.Position = new CCPoint(_layer.VisibleBoundsWorldspace.MaxX + collisionEntity.GraphicWidth * 0.5f, _layer.VisibleBoundsWorldspace.MinY); if (EntitySpawned != null) { EntitySpawned(collisionEntity); } }
private void FruitCreated(object sender, FruitEventArgs args) { args.Fruit.PositionX = CCRandom.GetRandomFloat(0 + args.Fruit.Radius, ContentSize.Width - args.Fruit.Radius); args.Fruit.PositionY = ContentSize.Height + args.Fruit.Radius; args.Fruit.Visible = true; AddChild(args.Fruit); }
void RunGameLogic(float frameTimeInSeconds) { // This is a linear approximation, so not 100% accurate ballYVelocity += frameTimeInSeconds * -gravity; ballSprite.PositionX += ballXVelocity * frameTimeInSeconds; ballSprite.PositionY += ballYVelocity * frameTimeInSeconds; // New Code: // Check if the two CCSprites overlap... bool doesBallOverlapPaddle = ballSprite.BoundingBoxTransformedToParent.IntersectsRect( paddleSprite.BoundingBoxTransformedToParent); // ... and if the ball is moving downward. bool isMovingDownward = ballYVelocity < 0; if (doesBallOverlapPaddle && isMovingDownward) { // First let's invert the velocity: ballYVelocity *= -1; // Then let's assign a random value to the ball's x velocity: const float minXVelocity = -300; const float maxXVelocity = 300; ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); score++; scoreLabel.Text = "Score: " + score; } // First let’s get the ball position: float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; float ballTop = ballSprite.BoundingBoxTransformedToParent.MaxY; float ballBot = ballSprite.BoundingBoxTransformedToParent.MinY; // Then let’s get the screen edges float screenRight = VisibleBoundsWorldspace.MaxX; float screenLeft = VisibleBoundsWorldspace.MinX; float screenTop = VisibleBoundsWorldspace.MaxY; float screenBot = VisibleBoundsWorldspace.MinY; // Check if the ball is either too far to the right or left: bool shouldReflectXVelocity = (ballRight > screenRight && ballXVelocity > 0) || (ballLeft < screenLeft && ballXVelocity < 0); if (shouldReflectXVelocity) { ballXVelocity *= -1; } if (ballTop < screenBot) { ballSprite.PositionX = 320; ballSprite.PositionY = 1200; score = 0; scoreLabel.Text = "Score: " + score; Unschedule(RunGameLogic); AddChild(GameOverLabel); gamestop = true; } }
void AddBall() { screenRight = VisibleBoundsWorldspace.MaxX; screenLeft = VisibleBoundsWorldspace.MinX; var xPos = CCRandom.GetRandomFloat(screenLeft + 100, screenRight - 100); var newBall = new Ball(xPos); Balls.Add(newBall); AddChild(newBall); }
private void SpawnCloud() { TimeInbetweenCloudSpawns = CCRandom.GetRandomFloat(0.8f, 3f); var peripheryEntity = new Cloud(); peripheryEntity.PositionX = _layer.VisibleBoundsWorldspace.MaxX + peripheryEntity.ContentSize.Width * 0.5f; peripheryEntity.PositionY = CCRandom.GetRandomFloat(_layer.VisibleBoundsWorldspace.MaxY * 0.25f, _layer.VisibleBoundsWorldspace.MaxY * 0.8f); if (EntitySpawned != null) { EntitySpawned(peripheryEntity); } }
private void SpawnBird() { TimeInbetweenBirdSpawns = CCRandom.GetRandomFloat(_minimumTimeSinceLastBirdSpawn, _maximumTimeSinceLastBirdSpawn); var collisionEntity = new Bird(CCRandom.GetRandomFloat(_minimumBirdSpeed, _maximumBirdSpeed)); collisionEntity.PositionX = _layer.VisibleBoundsWorldspace.MaxX + collisionEntity.GraphicWidth * 0.5f; collisionEntity.PositionY = CCRandom.GetRandomFloat(_layer.VisibleBoundsWorldspace.MaxY * 0.2f, _layer.VisibleBoundsWorldspace.MaxY * 0.7f); if (EntitySpawned != null) { EntitySpawned(collisionEntity); } }
//handle what occurs when the ball hits the paddle (invert ball yVelocity) void HandlePaddleCollisions(CCRect paddleBox, CCRect ballBox, Ball ballRep) { bool doesBallOverlapPaddle = ballBox.IntersectsRect(paddleBox); bool isMovingDown = ballRep.VelocityY < 0; if (doesBallOverlapPaddle && isMovingDown) { //invert velocity ballRep.VelocityY *= -1; //assign a value to the x velocity. Keeping constant speed const float minXVelocity = -300; const float maxXVelocity = 300; ballRep.VelocityX = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); // randomizes the xVelocity //reset hit counter whenever the ball hits anything that isn't a brick hitCount = 0; } }
void RunGameLogic(float frameTimeInSeconds) { // This is a linear approximation of gravity, but it's good enough for this game ballYVelocity += frameTimeInSeconds * -gravity; ballSprite.PositionX += ballXVelocity * frameTimeInSeconds; ballSprite.PositionY += ballYVelocity * frameTimeInSeconds; //////////////////////////New Code Starts here:///////////////////////// // Check if the two CCSprites overlap... bool doesBallOverlapPaddle = ballSprite.BoundingBoxTransformedToParent.IntersectsRect( paddleSprite.BoundingBoxTransformedToParent); // ... and if the ball is moving downward. bool isMovingDownward = ballYVelocity < 0; if (doesBallOverlapPaddle && isMovingDownward) { // First let's invert the velocity: ballYVelocity *= -1; // Then let's assign a random to the ball's x velocity: const float minXVelocity = -300; const float maxXVelocity = 300; ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); score++; scoreLabel.Text = "Score: " + score; } // Check if the ball is either too far to the right or left: float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; float screenRight = mainLayer.VisibleBoundsWorldspace.MaxX; float screenLeft = mainLayer.VisibleBoundsWorldspace.MinX; bool shouldReflectXVelocity = (ballRight > screenRight && ballXVelocity > 0) || (ballLeft < screenLeft && ballXVelocity < 0); if (shouldReflectXVelocity) { ballXVelocity *= -1; } }
/// <summary> /// Check the collision state between the ball and the paddle and ensure that it bounces off /// </summary> public bool DidBallHitPaddle(CCSprite paddleSprite) { bool doesCollide = this.BoundingBoxTransformedToParent.IntersectsRect(paddleSprite.BoundingBoxTransformedToParent); bool ballFalling = YVelocity < 0; if (doesCollide && ballFalling) { //inverts velocity YVelocity *= -1; const float minXVelocity = -300; const float maxXVelocity = 300; XVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); } return(doesCollide && ballFalling); }
public MenuBubble(int xIndex, int yIndex, int listIndex) : base(xIndex, yIndex, listIndex) { XIndex = xIndex; YIndex = yIndex; ListIndex = listIndex; TimeAppear = CCRandom.GetRandomFloat(MIN_TIME_APPEAR, MAX_TIME_APPEAR); TimeHold = CCRandom.GetRandomFloat(MIN_TIME_HOLD, MAX_TIME_HOLD); TimeFade = CCRandom.GetRandomFloat(MIN_TIME_FADE, MAX_TIME_FADE); BubbleSpriteSheet = new CCSpriteSheet("bubbles.plist"); var spriteFileName = GetRandomBubbleColor(); BubbleSprite = new CCSprite(BubbleSpriteSheet.Frames.Find(x => x.TextureFilename.Equals(spriteFileName))); BubbleSprite.AnchorPoint = CCPoint.AnchorMiddle; BubbleSprite.Opacity = 0; AddChild(BubbleSprite); }
//--------------------------------------------------------------------------------------------------------- // BonusBubble Constructor //--------------------------------------------------------------------------------------------------------- public BonusBubble(int xIndex, int yIndex, int listIndex, int tapsNeeded, Level activeLevel) : base(xIndex, yIndex, listIndex) { TapCount = 0; TapsToPop = tapsNeeded; XIndex = xIndex; YIndex = yIndex; ListIndex = listIndex; currentLevel = activeLevel; TimeDelay = CCRandom.GetRandomFloat(MIN_TIME_DELAY, MAX_TIME_DELAY); TimeAppear = CCRandom.GetRandomFloat(MIN_TIME_APPEAR, MAX_TIME_APPEAR); TimeHold = CCRandom.GetRandomFloat(MIN_TIME_HOLD, MAX_TIME_HOLD); TimeFade = CCRandom.GetRandomFloat(MIN_TIME_FADE, MAX_TIME_FADE); BonusSpriteSheet = new CCSpriteSheet("bonus.plist"); BonusType = GetRandomBonusType(); GetRandomTapsToPop(BonusType); GetBubbleSprite(BonusType); GetBonusSprite(BonusType); }
void RunGameLogic(float frameTimeInSeconds) { ballYVelocity += frameTimeInSeconds * -gravity; ballSprite.PositionX += ballXVelocity * frameTimeInSeconds; ballSprite.PositionY += ballYVelocity * frameTimeInSeconds; // Check if the two CCSprites overlap... bool doesBallOverlapPaddle = ballSprite.BoundingBoxTransformedToParent.IntersectsRect(paddleSprite.BoundingBoxTransformedToParent); // ... and if the ball is moving downward. bool isMovingDownward = ballYVelocity < 0; if (doesBallOverlapPaddle && isMovingDownward) { // First let's invert the velocity: ballYVelocity *= -1; // Then let's assign a random value to the ball's x velocity: const float minXVelocity = -300; const float maxXVelocity = 300; ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); score++; scoreLabel.Text = "Score: " + score; } // First let’s get the ball position: float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; // Then let’s get the screen edges float screenRight = VisibleBoundsWorldspace.MaxX; float screenLeft = VisibleBoundsWorldspace.MinX; bool shouldReflectXVelocity = (ballRight > screenRight && ballXVelocity > 0) || (ballLeft < screenLeft && ballXVelocity < 0); if (shouldReflectXVelocity) { ballXVelocity *= -1; } }
public PointBubble(int xIndex, int yIndex, int listIndex, int pointValue, int tapsNeeded) : base(xIndex, yIndex, listIndex) { TapCount = 0; TapsToPop = tapsNeeded; PointValue = pointValue; XIndex = xIndex; YIndex = yIndex; ListIndex = listIndex; TimeDelay = CCRandom.GetRandomFloat(MIN_TIME_DELAY, MAX_TIME_DELAY); TimeAppear = CCRandom.GetRandomFloat(MIN_TIME_APPEAR, MAX_TIME_APPEAR); TimeHold = CCRandom.GetRandomFloat(MIN_TIME_HOLD, MAX_TIME_HOLD); TimeFade = CCRandom.GetRandomFloat(MIN_TIME_FADE, MAX_TIME_FADE); BubbleSpriteSheet = new CCSpriteSheet("bubbles.plist"); BubbleSprite = new CCSprite(BubbleSpriteSheet.Frames.Find(x => x.TextureFilename.Equals("bubble-std-iceblue.png"))); BubbleSprite.AnchorPoint = CCPoint.AnchorMiddle; BubbleSprite.Opacity = 0; AddChild(BubbleSprite); PointLabel = new CCLabel(pointValue.ToString(), "gothic-44-hd.fnt"); PointLabel.AnchorPoint = CCPoint.AnchorMiddle; PointLabel.Opacity = 0; AddChild(PointLabel); PopSprite = new CCSprite(BubbleSpriteSheet.Frames.Find(x => x.TextureFilename.Equals("bubble-pop-iceblue.png"))); PopSprite.AnchorPoint = CCPoint.AnchorMiddle; PopSprite.Scale = 0.0f; AddChild(PopSprite); Emitter = new CCParticleFlower(Position) { Texture = CCTextureCache.SharedTextureCache.AddImage("stars"), TotalParticles = 0, // start with total particles at zero then have it go to a higher number once the bubble sprite is visible }; }
public Cloud() { this.Speed = CCRandom.GetRandomFloat(5f, 80f); InitGraphic(); }
void RunGameLogic(float frameTimeInSeconds) { if (lives == 0) { return; } // Move ball //ballYVelocity += frameTimeInSeconds * -gravity; ballSprite.PositionX += ballXVelocity * frameTimeInSeconds; ballSprite.PositionY += ballYVelocity * frameTimeInSeconds; // Check for ball falling off the bottom if (ballSprite.PositionY < 0) { lives--; label.Text = string.Format("Lives: {0} Score: {1}", lives, score); if (lives == 0) { // Game over gameOver.Visible = true; return; } else { ballSprite.PositionX = 320; ballSprite.PositionY = 800; } } // Check for paddle hit bool doesBallOverlapPaddle = ballSprite.BoundingBoxTransformedToParent.IntersectsRect(paddle.BoundingBoxTransformedToParent); bool isMovingDownward = ballYVelocity < 0; if (doesBallOverlapPaddle && isMovingDownward) { ballYVelocity *= -1; const float minXVelocity = -300; const float maxXVelocity = 300; ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); score++; label.Text = string.Format("Lives: {0} Score: {1}", lives, score); } // Check left and right bounds float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; float screenRight = VisibleBoundsWorldspace.MaxX; float screenLeft = VisibleBoundsWorldspace.MinX; bool shouldReflectXVelocity = (ballRight > screenRight && ballXVelocity > 0) || (ballLeft < screenLeft && ballXVelocity < 0); if (shouldReflectXVelocity) { ballXVelocity *= -1; } // Check for top bounds (bottom handled above) float ballTop = ballSprite.BoundingBoxTransformedToParent.MaxY; float screenTop = VisibleBoundsWorldspace.MaxY; if (ballTop > screenTop && ballYVelocity > 0) { ballYVelocity *= -1; } }
void BallPhysics(float frameTimeInSeconds) { if (CanPlay) { if (IntentType == "CREATE") { Ball.PositionX += ballVelocityX * frameTimeInSeconds * frameTimeInSeconds; Ball.PositionY += ballVelocityY * directionY * frameTimeInSeconds * frameTimeInSeconds; bool ballOverlapP2 = Ball.BoundingBoxTransformedToParent.IntersectsRect(_player2Board.BoundingBoxTransformedToParent); bool ballOverlapP1 = Ball.BoundingBoxTransformedToParent.IntersectsRect(_player1Board.BoundingBoxTransformedToParent); if (ballOverlapP2 || ballOverlapP1) { directionY = -directionY; ballVelocityY += 4000; ballVelocityX = CCRandom.GetRandomFloat(-ballVelocityY, ballVelocityY); } } float screenRightM = VisibleBoundsWorldspace.MaxX; float screenLeftM = VisibleBoundsWorldspace.MaxY; float ballRight = Ball.BoundingBoxTransformedToParent.MaxX; float ballLeft = Ball.BoundingBoxTransformedToParent.MaxY; bool shouldReflectRight = ((screenLeftM < ballLeft) || (ballLeft < 0)); bool shouldReflectLeft = ((screenRightM < ballRight) || (ballRight < 0)); if (shouldReflectRight) { if (screenLeftM < ballLeft) { CountDownTimer.Visible = true; Time = 3; CanPlay = false; ResetPositions(); ScoreP2++; Score_P2.Text = ScoreP2.ToString(); } else { CountDownTimer.Visible = true; Time = 3; CanPlay = false; ResetPositions(); ScoreP1++; Score_P1.Text = ScoreP1.ToString(); } directionY = -directionY; } if (shouldReflectLeft) { ballVelocityX = -ballVelocityX; } } }
public void VerticalBounce() { velocityY *= -1; velocityX = CCRandom.GetRandomFloat(-getMaxSpeedX(), getMaxSpeedX()); StartRotation(); }
void rungame(float framTimeInSecond) { int i = 0; //var bounds = c.VisibleBoundsWorldspace; ballyvelocity += framTimeInSecond * -gravity; ballsprite.PositionX += ballxvelocity * framTimeInSecond; ballsprite.PositionY += (ballyvelocity * framTimeInSecond); bool balloverlap = ballsprite.BoundingBoxTransformedToParent.IntersectsRect(stick.BoundingBoxTransformedToParent); foreach (var item in st) { bool candyover = (item.BoundingBoxTransformedToParent.IntersectsRect(ballsprite.BoundingBoxTransformedToParent)); if (candyover == true) { try { ballxvelocity *= (float)(-0.99); CCAudioEngine.SharedEngine.PlayEffect(filename: "b1.mp3"); this.RemoveChildByTag(item.Tag); Scorecount += 10; Score.Text = Scorecount.ToString(); RemainingCandy.Text = this.st.Count.ToString(); this.st.Remove(item); break; } catch (Exception ex) { Console.WriteLine(ex.Message + "faisal"); } } bool ismoveing = ballyvelocity < 0; if (balloverlap && ismoveing) { ballyvelocity *= -1f; const float minxvelocity = -300; const float minyvelocity = 300; ballxvelocity = CCRandom.GetRandomFloat(minxvelocity, minyvelocity); // Score += 1; // label.Text = Score.ToString(); // CCAudioEngine.SharedEngine.PlayEffect(filename: "b1.mp3"); // Console.WriteLine(ballsprite.PositionX+"iam locationx"); } float ballleft = ballsprite.BoundingBoxTransformedToParent.MinX; float ballright = ballsprite.BoundingBoxTransformedToParent.MaxX; float balltop = ballsprite.BoundingBoxTransformedToParent.MaxY; float screenleft = VisibleBoundsWorldspace.MinX; float screenright = VisibleBoundsWorldspace.MaxX; float screentop = VisibleBoundsWorldspace.MaxY; bool shouldreflaxvelocity = (ballright > screenright && ballxvelocity > 0) || (ballleft < screenleft && ballxvelocity < 0); bool souldreflaxvelcitytop = (balltop > screentop && ballyvelocity > 0); if (shouldreflaxvelocity) { ballxvelocity *= -1; } if (souldreflaxvelcitytop) { ballyvelocity *= -1.1f; ballyvelocity /= -2.5f; } if (ballsprite.PositionY < VisibleBoundsWorldspace.MinY) { ballsprite.PositionX = 320; ballsprite.PositionY = 100; ballyvelocity *= -1; Scorecount = 0; // label.Text = "0"; } } }
void RunGameLogic(float frameTimeInSeconds) { // This is a linear approximation, so not 100% accurate ballYSpeed += frameTimeInSeconds * -gravity; ball.PositionX += ballXSpeed * frameTimeInSeconds; ball.PositionY += ballYSpeed * frameTimeInSeconds; float screenTop = VisibleBoundsWorldspace.MaxY; float screenBottom = VisibleBoundsWorldspace.MinY; // New Code: // Check if the two CCSprites overlap... bool doesBallOverlapPaddle = ball.BoundingBoxTransformedToParent.IntersectsRect( paddle.BoundingBoxTransformedToParent); // ... and if the ball is moving downward. bool isMovingDownward = ballYSpeed < 0; if (doesBallOverlapPaddle && isMovingDownward) { // First let's invert the velocity: ballYSpeed *= -1; const float minXVelocity = -30; const float maxXVelocity = 900; ballXSpeed = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); // New code: score++; mainScore.Text = "Score: " + score; if (score > highScore) { highScore = score; setHighScore(highScore); } } // First let’s get the ball position: float ballRight = ball.BoundingBoxTransformedToParent.MaxX; float ballLeft = ball.BoundingBoxTransformedToParent.MinX; float ballBottom = ball.BoundingBoxTransformedToParent.MinY; // Then let’s get the screen edges float screenRight = VisibleBoundsWorldspace.MaxX; float screenLeft = VisibleBoundsWorldspace.MinX; // Check if the ball is either too far to the right or left: bool shouldReflectXVelocity = (ballRight > screenRight && ballXSpeed > 0) || (ballLeft < screenLeft && ballXSpeed < 0); bool ballOfBottomScreen = (ballBottom < screenBottom && ballXSpeed > 0); if (shouldReflectXVelocity) { ballXSpeed *= -1; } if (ballOfBottomScreen) { } }
public int Update(float dt, ref List <string> componentEntityList, float screenLeft, float screenRight, float minY) { // do collision logic // if ball and paddle colide reutrn 1 // else var ballSprite = EntityManager.Entities["Ball"].GetComponent <Component.SpriteComponent>().Sprite; var paddleSprite = EntityManager.Entities["Paddle"].GetComponent <Component.SpriteComponent>().Sprite; bool doesBallOverlapPaddle = ballSprite.BoundingBoxTransformedToParent.IntersectsRect( paddleSprite.BoundingBoxTransformedToParent); bool isFalling = EntityManager.Entities["Ball"].GetComponent <MotionComponent>().VelocityY < 0; if (doesBallOverlapPaddle && isFalling) { EntityManager.Entities["Ball"].GetComponent <MotionComponent>().VelocityY *= -1; EntityManager.Entities["Ball"].GetComponent <MotionComponent>().VelocityX = CCRandom.GetRandomFloat(MinXVelocity, MaxXVelocity); return(1); } float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; // Then let’s get the screen edges // Check if the ball is either too far to the right or left: bool shouldReflectXVelocity = (ballRight > screenRight && EntityManager.Entities["Ball"].GetComponent <MotionComponent>().VelocityX > 0) || (ballLeft < screenLeft && EntityManager.Entities["Ball"].GetComponent <MotionComponent>().VelocityX < 0); if (shouldReflectXVelocity) { EntityManager.Entities["Ball"].GetComponent <MotionComponent>().VelocityX *= -1; } if (ballSprite.PositionY < minY) { ballSprite.PositionX = 320; ballSprite.PositionY = 600; return(-1); } return(0); }
void RunGameLogic(float frameTimeInSeconds) { // This is a linear approximation, so not 100% accurate. ballYVelocity += frameTimeInSeconds * -gravity; ballSprite.PositionX += ballXVelocity * frameTimeInSeconds; ballSprite.PositionY += ballYVelocity * frameTimeInSeconds; // Check if the two CCSprites overlap... bool doesBallOverlapPaddle = ballSprite.BoundingBoxTransformedToParent.IntersectsRect( paddleSprite.BoundingBoxTransformedToParent); // ... and if the ball is moving downward. bool isMovingDownward = ballYVelocity < 0; if (doesBallOverlapPaddle && isMovingDownward) { // First let's invert the velocity: ballYVelocity *= -1; // Then let's assign a random value to the ball's x velocity: const float minXVelocity = -600; const float maxXVelocity = 600; ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity); score++; scoreLabel.Text = "Score: " + score; var effect = new CCParticleExplosion(ballSprite.Position) { AutoRemoveOnFinish = true, EmissionRate = 100 }; AddChild(effect); CCSimpleAudioEngine.SharedEngine.PlayEffect("tennis_ball"); } // First let’s get the ball position: float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX; float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX; // Then let’s get the screen edges. float screenRight = VisibleBoundsWorldspace.MaxX; float screenLeft = VisibleBoundsWorldspace.MinX; // Check if the ball is either too far to the right or left: bool shouldReflectXVelocity = (ballRight > screenRight && ballXVelocity > 0) || (ballLeft < screenLeft && ballXVelocity < 0); if (shouldReflectXVelocity) { ballXVelocity *= -1; } if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY) { ballSprite.PositionY = VisibleBoundsWorldspace.MaxY; if (highestScore < score) { highestScore = score; highestScoreLabel.Text = "Highest Score: " + highestScore; } score = 0; scoreLabel.Text = "Score: 0"; ballXVelocity = 0; ballYVelocity = 0; } }