public GamePage()
        {
            this.InitializeComponent();

            //set preffered window size
            ApplicationView.PreferredLaunchViewSize = new Size(700, 500);
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

            //initialize variable for gameplay
            playerLives = 3;
            playerGameScore = 0;
            level = 1;
            
            //initiate classes
            player = new Player(canvas, playerGameScore);
            invaders = new Invaders(canvas, level);
            sounds = new Sounds(grid);

            //start the game dispatch timer
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += Game;
            dispatcherTimer.Interval = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 30);
            dispatcherTimer.Start();
        }
Ejemplo n.º 2
0
        //Draw and update all object for every game frame
        public void Draw(Canvas canvas, Image[,] invaderGrid, Sounds sounds)
        {
            Canvas.SetTop(playerSprite, Window.Current.Bounds.Height - (playerSprite.Height * 2));

            //If player is moving left and is not hitting the left screen, keep moving left.
            if (isMovingLeft && Canvas.GetLeft(playerSprite) >= 0) Canvas.SetLeft(playerSprite, Canvas.GetLeft(playerSprite) - 6);
            //If player is moving right and is not hitting the right screen, keep moving right
            if (isMovingRight && Canvas.GetLeft(playerSprite) <= Window.Current.Bounds.Width - playerSprite.Width) Canvas.SetLeft(playerSprite, Canvas.GetLeft(playerSprite) + 6);
            //if the player is shooting
            if (isShooting)
            {
                //If no bullet is found, create a new one.
                if (!canvas.Children.Contains(playerBullet))
                {
                    playerBullet = new Image();
                    playerBullet.Width = 3 * sizeModifier();
                    playerBullet.Height = 8 * sizeModifier();
                    playerBullet.Source = new BitmapImage(new Uri("ms-appx:///Assets/Sprites/player-bullet.png"));

                    Canvas.SetTop(playerBullet, Canvas.GetTop(playerSprite));
                    Canvas.SetLeft(playerBullet, Canvas.GetLeft(playerSprite) + (playerSprite.Width / 2 - 1));

                    sounds.playPlayerShootSound();
                    canvas.Children.Add(playerBullet);
                }
                //Move bullet upward and check for alienGrid collision.
                for (int r = 0; r < invaderGrid.GetLength(0); r++)
                {
                    for (int c = 0; c < invaderGrid.GetLength(1); c++)
                    {
                        //If the player bullet is in between the x coords of an invader, continue
                        if (Canvas.GetLeft(invaderGrid[r, c]) <= Canvas.GetLeft(playerBullet) && Canvas.GetLeft(invaderGrid[r, c]) + invaderGrid[r, c].Width >= Canvas.GetLeft(playerBullet))
                        {
                            //If the player bullet is in between the y coords of an invader, continue
                            if (Canvas.GetTop(invaderGrid[r, c]) + invaderGrid[r, c].Height >= Canvas.GetTop(playerBullet) && Canvas.GetTop(invaderGrid[r, c]) <= Canvas.GetTop(playerBullet))
                            {
                                //If the invader is not already dead, continue
                                if (invaderGrid[r, c].Tag != null)
                                {
                                    //Kill invader / update player score
                                    playerScore = playerScore + (10 * (invaderGrid.GetLength(1) - c));

                                    if (invaderGrid[r, c].Source == alien1A || invaderGrid[r, c].Source == alien1B) playerScore = playerScore + 100;
                                    else if (invaderGrid[r, c].Source == alien2A || invaderGrid[r, c].Source == alien2B) playerScore = playerScore + 50;
                                    else playerScore = playerScore + 25;

                                    isShooting = false;
                                    invaderGrid[r, c].Tag = null;
                                    sounds.playinvaderKilledSound();
                                    canvas.Children.Remove(playerBullet);
                                    removeKilled(canvas, invaderGrid[r, c]);

                                }
                            }
                        }
                        //If the bullet collides with the top of the screen, remove bullet
                        else if (Canvas.GetTop(playerBullet) <= 0)
                        {
                            isShooting = false;
                            canvas.Children.Remove(playerBullet);
                        }
                    }
                }

                //Move bullet upward
                Canvas.SetTop(playerBullet, Canvas.GetTop(playerBullet) - 15);
            }
        }
Ejemplo n.º 3
0
        //Draw and update all object for every game frame
        public void Draw(Canvas canvas, Image player, Sounds sounds)
        {
            //For every 15 loops (minus the speed) toggle the invader image and move it
            if (count % (15 - speed) == 1) toggleSprite = invadersAreMoving = true;
            //For every 20 loops (minus the speed) make a selected invader shoot if the randomNumber is equal to 2
            if (count % (20 - speed) == 1 && new Random().Next(0, 3) == 2) invaderShoot(canvas, player);
            //Move the invader downward
            if (isMovingDown)
            {
                for (int c = 0; c < columns; c++)
                {
                    for (int r = 0; r < rows; r++)
                    {
                        isMovingDown = false;
                        Canvas.SetTop(invaderGrid[c, r], Canvas.GetTop(invaderGrid[c, r]) + invaderGrid[c, r].Width);
                        //If the invader is not dead, continue
                        if (invaderGrid[c, r].Tag != null)
                        {
                            if (Canvas.GetTop(invaderGrid[c, r]) >= Window.Current.Bounds.Height - (invaderGrid[c, r].Height * 3))
                            {
                                isPlayerAlive = false;
                            }
                        }
                    }
                }
            }

            //change the x coords for all the invaders
            if (invadersAreMoving)
            {
                //Invaders are moving left
                if (isMovingLeft)
                {
                    for (int c = 0; c < columns; c++)
                    {
                        for (int r = 0; r < rows; r++)
                        {
                            if (invaderGrid[c, r].Tag != null)
                            {
                                if (toggleSprite) toggle(invaderGrid[c, r]);
                                if (Canvas.GetLeft(invaderGrid[c, r]) <= 0 + (invaderGrid[c, r].Width * 2))
                                {
                                    isMovingDown = true;
                                    isMovingLeft = false;
                                }

                                Canvas.SetLeft(invaderGrid[c, r], Canvas.GetLeft(invaderGrid[c, r]) - (20 + speed));
                            }
                        }
                    }
                }
                //Invaders are moving right
                else
                {
                    for (int c = 0; c < columns; c++)
                    {
                        for (int r = 0; r < rows; r++)
                        {
                            if (invaderGrid[c, r].Tag != null)
                            {
                                if (toggleSprite) toggle(invaderGrid[c, r]);
                                if (Canvas.GetLeft(invaderGrid[c, r]) >= Window.Current.Bounds.Width - (invaderGrid[c, r].Width * 3))
                                {
                                    isMovingDown = true;
                                    isMovingLeft = true;
                                }

                                Canvas.SetLeft(invaderGrid[c, r], Canvas.GetLeft(invaderGrid[c, r]) + (20 + speed));
                            }
                        }
                    }
                }

                invadersAreMoving = false;
            }

            //Handle bullet collision and motion
            if (isShooting)
            {
                for (int i = 0; i < invaderBullets.Length; i++)
                {
                    if (canvas.Children.Contains(invaderBullets[i]))
                    {
                        //If the invader bullet is in between the x coords of the player, continue
                        if (Canvas.GetLeft(player) <= Canvas.GetLeft(invaderBullets[i]) && Canvas.GetLeft(player) + player.Width >= Canvas.GetLeft(invaderBullets[i]))
                        {
                            //If the invader bullet is in between the y coords of the player, continue
                            if (Canvas.GetTop(player) + player.Height >= Canvas.GetTop(invaderBullets[i]) && Canvas.GetTop(player) <= Canvas.GetTop(invaderBullets[i]))
                            {
                                //Kill the player
                                removeKilled(player);
                                sounds.playPlayerKilledSound();
                                isShooting = false;                                
                            }
                        }
                        //If the invader bullet collides with the bottom screen, continue
                        else if (Canvas.GetTop(invaderBullets[i]) >= Window.Current.Bounds.Height - invaderBullets[i].Height)
                        {
                            //delete bullet
                            canvas.Children.Remove(invaderBullets[i]);
                        }

                        //Move bullet downward
                        Canvas.SetTop(invaderBullets[i], Canvas.GetTop(invaderBullets[i]) + 15);
                    }
                }
            }

            toggleSprite = false;
            count++;
        }