Pressing "Okay" button after making selection returns to main program

3 views (last 30 days)
I am trying to design a Blackjack game which allows you to select a bet amount. So I am trying to make a GUI which has a start screen that lets you select your bet amount, using a slider. Once the user has selected the bet amount, this bet value should be stored for later use, and once they press an "Okay" button I am trying to just have it return to the main program to then begin the game of Blackjack.
Here is what I've got:
function basebet = test1
f = figure('Visible','on','name','Bet Selection','color',[0 0 0],'pos',[100 100 300 400]);
txt = uicontrol('style','text','string','Welcome to Blackjack!',...
'pos',[70 255 160 130],'fontsize',20,'fontweight','bold',...
'foreground','green','background','black');
txt = uicontrol('style','text','string','Created by S. Murray and J. Avila',...
'fontsize',8,'foreground','white','background','black',...
'pos',[1 1 200 30]);
txt = uicontrol('style','text','string','Please choose bet amout:',...
'fontsize',8,'foreground','red','background','black',...
'pos',[37 210 160 30]);
basebet = 0;
okay = uicontrol('Style','pushbutton','String','Lets Play!','Position',[100 100 150 30]);
set(okay,'vis','on','enable','on','foreground','red','fontsize',18,...
'fontweight','bold','background','black','Callback',@start);
% Create slider
sld = uicontrol('Style', 'slider',...
'Min', 100,'Max', 1000,'Value', 100,...
'Position', [40 180 250 40],...
'Callback', @storebet);
% Display the initial bet amount in the slider.
txt = uicontrol('Style','text',...
'Position',[150 140 60 20],...
'String','$ 100','fontsize',12,'fontweight','bold');
% setup current bet
%basebet = 100;
function storebet(source, callbackdata)
% access the value in the slider using the get function
basebet = get(sld,'Value');
% This is a hacked way to change the bet amount
txt = uicontrol('Style','text',...
'Position',[150 140 60 20],...
'String',['$ ', num2str(basebet)],...
'fontsize',12,'fontweight','bold');
end
end
function start(basebet)
basebet
end;
%end

Answers (1)

Geoff Hayes
Geoff Hayes on 9 Nov 2014
Scott - when I run the above code, I observe the following error message when I press the Let's Play (or okay) button
Error using test1>start
Too many input arguments.
Error while evaluating uicontrol Callback
This is because the function signature for start is
function start(basebet)
which is expecting a single input parameter only, and there are probably at least two parameters being passed in (by default) - an object handle, and some event data. Since you want this function to have access to basebet, then I would do the same thing for this function as you did for storebet: create start as a nested function within test1 so that it has access to the local variables declared within this "parent" function, and change the function signature and body to
function start(source, callbackdata)
% do something with basebet
basebet
end
Note that in the above, source will be the handle to the push button (so will be identical to the handle okay), and that callbackdata will be empty.

Categories

Find more on Card games in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!