Full Code for save the presents wpf c# game –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; // add this for the timer
namespace Save_the_Presents_Game
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int maxItem = 5; // max item integer set to 5
int currentItems = 0; // current item integer set to 0
Random r = new Random(); // new random class to generate random numbers
int score = 0; // default score integer is 0
int missed = 0; // default missed integer is 0
DispatcherTimer GameTimer = new DispatcherTimer(); // make a new timer for this gam
List<Rectangle> itemstoremove = new List<Rectangle>(); // game new rectangle list called items to remove
ImageBrush playerImage = new ImageBrush(); // new player image brush - will be used to show player image
ImageBrush backgroundImage = new ImageBrush(); // new background image brush - will be used to show background
public MainWindow()
{
InitializeComponent();
// create the timer, add a drop item event and give it a 20 millisecond interval
GameTimer.Tick += dropItems;
GameTimer.Interval = TimeSpan.FromMilliseconds(20);
GameTimer.Start();
// load the images for background and player to the image brush class
playerImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/netLeft.png"));
backgroundImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/background.jpg"));
// assign the pictures for the player object and canvas object
player1.Fill = playerImage;
myCanvas.Background = backgroundImage;
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
// Get the x and y coordinates of the mouse pointer.
System.Windows.Point position = e.GetPosition(this);
double pX = position.X;
double pY = position.Y;
// Sets the Height/Width of the circle to the mouse coordinates.
Canvas.SetLeft(player1, pX - 10);
// if the player is positioned less than 260 pixels
// we will set the net to the left image
if (Canvas.GetLeft(player1) < 260)
{
playerImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/netLeft.png"));
}
// if the olayer positioned more than 260 pixels
// we will set the net to the right image
if (Canvas.GetLeft(player1) > 260)
{
playerImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/netRight.png"));
}
}
private void dropItems(object sender, EventArgs e)
{
ScoreText.Content = "Caught: " + score; // link score label to score integer
missedText.Content = "Missed: " + missed; // link missed label to missed integer
// if the current image is less than max item which is 5
// if there is less than 5 items in the screen
if (currentItems < maxItem)
{
makePresents(); // run the make presents function
currentItems++; // add 1 to the current items
itemstoremove.Clear(); // clear the items to remove list
}
// now we need to tell the game what to do with the items thats on the screen e.g. Rectangles
// run a loop to check how many rectangles there are on the screen and organise them in the x variable
foreach (var x in myCanvas.Children.OfType<Rectangle>())
{
// check if x has a tag of drops
if ((string)x.Tag == "drops")
{
// if it does then do the following
int dropThis = r.Next(8, 20); // new integer called dropThis generates a random number between 8 and 20
// set the x's speed to drop this
Canvas.SetTop(x, Canvas.GetTop(x) + dropThis);
// make two new Rect objects link one to the player and one to the items
// this will be used to check for collision between the player and the presents
Rect items = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height);
Rect playerObject = new Rect(Canvas.GetLeft(player1), Canvas.GetTop(player1), player1.Width, player1.Height);
//if player object collides with items
if (playerObject.IntersectsWith(items))
{
// add the item to the items to be removed list
itemstoremove.Add(x);
// deduct 1 from the current items list
currentItems--;
// add 1 to the score
score++;
}
else if (Canvas.GetTop(x) > 700)
{
// if the player missed the item and its gone passed 700 pixels from the main window
// add the item to the removed list
itemstoremove.Add(x);
// deduct 1 from the current items
currentItems--;
// add 1 to the missed integer
missed++;
}
}
// if the player missed more than 6 items
if (missed > 6)
{
// stop the main game timer
GameTimer.Stop();
// show a message box that says you lost click OK to try again
MessageBox.Show("You Lost" + Environment.NewLine + " Click OK to try again !");
// then run the reset game function
resetGame();
}
}
// removing its from the scene
// check how many rectangles are in the items move list
// remove them from the list using the for each loop
foreach (Rectangle y in itemstoremove)
{
myCanvas.Children.Remove(y);
}
}
private void resetGame()
{
//restart the game with the current settings.
System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
// This will go in the event handler for your button click.
Application.Current.Shutdown();
Environment.Exit(0);
}
private void makePresents()
{
ImageBrush presents = new ImageBrush(); // make a new image brush
int i = r.Next(1, 6); // get a random number between 1 and 6
// this switch statement will check what number comes back and assign a present according
// for example if the number is 1 then it will give present 1 image 2 give present 2 and so on
switch (i)
{
case 1:
presents.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/present_01.png"));
break;
case 2:
presents.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/present_02.png"));
break;
case 3:
presents.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/present_03.png"));
break;
case 4:
presents.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/present_04.png"));
break;
case 5:
presents.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/present_05.png"));
break;
case 6:
presents.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/present_06.png"));
break;
}
// once the present image is set we now can make a new rectangle for the presents
// inside of this rectangle we set the height, width, tag and fill link to the presents image brush we created above
Rectangle newRec = new Rectangle
{
Tag = "drops",
Width = 50,
Height = 50,
Fill = presents,
};
// once the presents rectangle is created now, we can set it randomly on the canvas
// the reason we have a * -1 end of the top location is because whatever number comes back we want to move the
// presents off the screen so it can continue to drop down
Canvas.SetTop(newRec, r.Next(60, 150) * -1);
Canvas.SetLeft(newRec, r.Next(10, 450));
// once the location is set now we can add it to the canvas
myCanvas.Children.Add(newRec);
}
}
}
Related