Click here to Skip to main content
15,880,392 members
Articles / Desktop Programming / XAML

The Bricks Game for Silverlight

Rate me:
Please Sign up or sign in to vote.
4.92/5 (43 votes)
8 Sep 2010CPOL13 min read 80.1K   1.3K   81  
An article exploring the use of MVVM, styles and templates in game programming in Silverlight 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Bricks.Silverlight.ViewModels;
using System.Windows.Threading;

namespace Bricks.Silverlight
{
    public partial class BricksView : UserControl
    {
        DispatcherTimer timer;
        BricksViewModel viewModel = new BricksViewModel();
        int amplitude = 5;

        public BricksView()//BricksViewModel viewModel)
        {
            InitializeComponent();
            //this.viewModel = viewModel;
            this.DataContext = viewModel;

            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 0, 3, 0);

            this.Loaded += new RoutedEventHandler(BricksView_Loaded);
        }

        void BricksView_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Start();
        }

        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            medClick.Stop();
            medClick.Play();

            switch (e.Key)
            {
                case Key.Left:
                    viewModel.MoveLeft();
                    break;
                case Key.Right:
                    viewModel.MoveRight();
                    break;
                case Key.Down:
                    viewModel.MoveDown();
                    break;
                case Key.Up:
                    viewModel.Rotate270();
                    break;
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            medIntro.Stop();
            medIntro.Play();

            
        }

        public void GenerateRandomPoints()
        {
            Random r = new Random();

            int pt00x = r.Next(-amplitude, amplitude);
            int pt10x = 25 + r.Next(-amplitude, amplitude);
            int pt20x = 75 + r.Next(-amplitude, amplitude);
            int pt30x = 100 + r.Next(-amplitude, amplitude);
            int pt31x = 100 + r.Next(-amplitude, amplitude);
            int pt32x = 100 + r.Next(-amplitude, amplitude);
            int pt33x = 100 + r.Next(-amplitude, amplitude);
            int pt01x = r.Next(-amplitude, amplitude);
            int pt02x = r.Next(-amplitude, amplitude);
            int pt03x = r.Next(-amplitude, amplitude);
            int pt13x = 25 + r.Next(-amplitude, amplitude);
            int pt23x = 75 + r.Next(-amplitude, amplitude);
            int pt00y = r.Next(-amplitude, amplitude);
            int pt10y = r.Next(-amplitude, amplitude);
            int pt20y = r.Next(-amplitude, amplitude);
            int pt30y = r.Next(-amplitude, amplitude);
            int pt31y = 25 + r.Next(-amplitude, amplitude);
            int pt32y = 75 + r.Next(-amplitude, amplitude);
            int pt33y = 100 + r.Next(-amplitude, amplitude);
            int pt01y = 25 + r.Next(-amplitude, amplitude);
            int pt02y = 75 + r.Next(-amplitude, amplitude);
            int pt03y = 100 + r.Next(-amplitude, amplitude);
            int pt13y = 100 + r.Next(-amplitude, amplitude);
            int pt23y = 100 + r.Next(-amplitude, amplitude);

            viewModel.P1Data = string.Format("M {0},{1} C {2},{3} {4},{5} {6},{7}", pt00x, pt00y, pt01x, pt01y, pt02x, pt02y, pt03x, pt03y);
            viewModel.P2Data = string.Format("M {0},{1} C {2},{3} {4},{5} {6},{7}", pt30x, pt30y, pt31x, pt31y, pt32x, pt32y, pt33x, pt33y);
            viewModel.P3Data = string.Format("M {0},{1} C {2},{3} {4},{5} {6},{7}", pt00x, pt00y, pt10x, pt10y, pt20x, pt20y, pt30x, pt30y);
            viewModel.P4Data = string.Format("M {0},{1} C {2},{3} {4},{5} {6},{7}", pt03x, pt03y, pt13x, pt13y, pt23x, pt23y, pt33x, pt33y);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            medIntro.Stop();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions