Click here to Skip to main content
15,124,861 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have been messing around with c# and I’m trying to make a slot machine game. I have everything figured out but right now I have 3 picture boxes and the program chooses what picture to put in each one randomly. I would really like the picture boxes to cycle through each of the possible symbols like a real slot machine but I don’t know the code to do this. Is it a timer? Or something else?

What I have tried:

All I can get is when you hit the spin button it put a picture in each PictureBox but I can’t get it to cycle
Posted
Updated 11-Dec-17 23:41pm
Comments
rmksiva 11-Dec-17 2:16am
   
Could you show us existing code block ?
Ralf Meier 11-Dec-17 2:24am
   
I think, using a PictureBox for this is the wrong approach - but of course : if you need a board you can derive from a brick ...
I suggest that you create your own custom control. This control should be able to hold all needed pictures/images. In this control you override the OnPaint-method to create the Behaviour you want to have.
________________ 11-Dec-17 3:17am
   
Winform is not suitable for graphical presentations. Use WPF. Yes, it takes time to understand the logic of StoryBoard and Behavior, but than you will create usable modern application.
BillWoodruff 11-Dec-17 12:07pm
   
"Winform is not suitable for graphical presentations. Use WPF."

For what is described here, WinForms is certainy usable.
________________ 12-Dec-17 4:01am
   
Topicaster want to make a game. Games on winforms - was looking terrible, and always has pure performance.
When you use even standard WPF controls - with simple adding shadows and round corners - you will have modern look and smooth motion. Override "OnPaint" of Winform control, and draw something adequate on screen - much more difficult.
Ralf Meier 12-Dec-17 5:34am
   
I'm sorry - I agree with Bill. Until now for me I found nothing which exists in WPF and couldn't be made in Forms also.
The same belongs to this question - the difficulty is not to create this control - the real difficulty is to create a Property which handles and manages the pictures which should be scrolled ...
________________ 12-Dec-17 7:52am
   
Believe me, I had same opinion - because my experience in Winform and obsolete demands to gui (press button - see table). You will change you mind when your knowledge level of Winform and WPF became comparable.

Here's a sketch, which I have deliberately left incomplete, with the goal of giving you some ideas. Consider what you need to do in the 'while loop in the Timer OnClick EventHandler.
C#
private readonly Dictionary<PictureBox, List<Image>> PBToImage = new Dictionary<PictureBox, List<Image>>();

private readonly Random rand = new Random();

private List<int> outCome = new List<int>();

private const int SlotCount = 3;
private const int PicturesPerSlotCount = 5;
private const int SpinCount = 4;
private const int SpinSpeed = 400;

private int currentSpinCount = 0;

private void Form1_Load(object sender, EventArgs e)
{
    timer1.Interval = SpinSpeed;
    timer1.Tick += Timer1OnTick;
}

private void Timer1OnTick(object sender, EventArgs eventArgs)
{
    while (currentSpinCount < SpinCount)
    {
        // left for you to write
    }

    timer1.Stop();
    timer1.Enabled = false;

    SetResult();
}

private void MakeNewOutCome()
{
    outCome.Clear();

    for (int i = 0; i < SlotCount; i++)
    {
        outCome.Add(rand.Next(PicturesPerSlotCount));
    }
}



private void Spin()
{
    MakeNewOutCome();

    currentSpinCount = 0;

    timer1.Enabled = true;
    timer1.Start();
}

private void SetResult()   // set the result
{
    int ndx = 0;

    foreach (var pbtoimagelist in PBToImage)
    {
        pbtoimagelist.Key.Image = pbtoimagelist.Value[outCome[ndx++]];
    }
}
   
WPF - 20 minutes of work:

Xaml:

<Window x:Class="WpfApplicationRollerImage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel x:Name="Roller" HorizontalAlignment="Left" Height="281" Margin="175,19,0,0" VerticalAlignment="Top" Width="125">
            <StackPanel.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </StackPanel.Background>
        </StackPanel>
        <Button Content="Roll!" HorizontalAlignment="Left" Margin="377,278,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    </Grid>
</Window>


Code behind:

C#
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;

namespace WpfApplicationRollerImage
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}
		int indexToPaint = 0;
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			if(Roller.Children.Count > 2)//no nore than 3 images!
			{
				Roller.Children.RemoveAt(0);
			}
			Image nextImage = new Image();

			
			switch (indexToPaint)
			{
				case 0:
				 
				nextImage.Source = new BitmapImage( new Uri(@"D:\Xlam\WpfApplicationRollerImage\NumOnePicture.bmp"));
				break;
				case 1:
				
				nextImage.Source = new BitmapImage( new Uri(@"D:\Xlam\WpfApplicationRollerImage\NumTwoPicture.bmp"));
				break;
				case 2:				 
				nextImage.Source = new BitmapImage( new Uri(@"D:\Xlam\WpfApplicationRollerImage\NumThreePicture.bmp"));
				break;
				case 3:				 
				nextImage.Source = new BitmapImage( new Uri(@"D:\Xlam\WpfApplicationRollerImage\NumOnePicture.bmp"));
				indexToPaint = 0;
				break;
			}
			indexToPaint++;
			this.Roller.Children.Add(nextImage);
			

		}
	}
}

Add your pictures, and any logic of pictures roll, think about adding behavior (slow mouton before disappearing)... :-)
   
Comments
BillWoodruff 13-Dec-17 14:20pm
   
There's nothing in this code that controls the speed at which the pictures are shown.

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900