Tuesday, April 17, 2012

Small Basic Dice Roller

If it takes me less than one hour of programming, it is a good project for beginner! That in mind, this is an example of a dice roller. Obviously, it's just a lot of variable assignments. You can change the source code if you want, say D100. An example of using the GUI (Control) elements.

Also, I put in a picture loader. Push the "0" button to check it out.












'Dice Roller - MWH959
'A GUI example
'by Harry Hardjono
' April 2012
'
'Init
GraphicsWindow.Show()
GraphicsWindow.Clear()
backpic=""

screen_x=GraphicsWindow.Width
screen_y=GraphicsWindow.Height
NumDice=1
ValDice=6

Button[0]=Controls.AddButton("0",50,50)
Button[1]=Controls.AddButton("1",80,50)
Button[2]=Controls.AddButton("2",110,50)
Button[3]=Controls.AddButton("3",140,50)
Button[4]=Controls.AddButton("4",170,50)
Button[5]=Controls.AddButton("5",50,90)
Button[6]=Controls.AddButton("6",80,90)
Button[7]=Controls.AddButton("7",110,90)
Button[8]=Controls.AddButton("8",140,90)
Button[9]=Controls.AddButton("9",170,90)

Dice[2]=Controls.AddButton("D2",50,150)
Dice[3]=Controls.AddButton("D3",90,150)
Dice[4]=Controls.AddButton("D4",130,150)
Dice[5]=Controls.AddButton("D5",170,150)
Dice[6]=Controls.AddButton("D6",50,190)
Dice[7]=Controls.AddButton("D7",90,190)
Dice[8]=Controls.AddButton("D8",130,190)
Dice[9]=Controls.AddButton("D9",170,190)
Dice[10]=Controls.AddButton("D10",50,230)
Dice[11]=Controls.AddButton("D11",90,230)
Dice[12]=Controls.AddButton("D12",130,230)
Dice[13]=Controls.AddButton("D13",170,230)
Dice[14]=Controls.AddButton("D14",50,270)
Dice[15]=Controls.AddButton("D15",90,270)
Dice[16]=Controls.AddButton("D16",130,270)
Dice[17]=Controls.AddButton("D17",170,270)
Dice[18]=Controls.AddButton("D18",50,310)
Dice[19]=Controls.AddButton("D19",90,310)
Dice[20]=Controls.AddButton("D20",130,310)
Dice[21]=Controls.AddButton("D21",170,310)

Controls.ButtonClicked=OnButtonClicked

'Event
Sub OnButtonClicked
LastButton=Controls.LastClickedButton
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(240,50,400,400)
If (backpic<>"") Then
GraphicsWindow.DrawResizedImage(backpic,0,0,screen_x,screen_y)
EndIf
GraphicsWindow.BrushColor="black"
GraphicsWindow.FontSize=30
If (LastButton=Button[0]) Then 'Use sparingly. Picture is loaded every screen redraw!
backpic=Flickr.GetRandomPicture("dice")
GraphicsWindow.DrawResizedImage(backpic,0,0,screen_x,screen_y)
NumDice=1
ElseIf (LastButton=Button[1]) then
NumDice=1
ElseIf (LastButton=Button[2]) then
NumDice=2
ElseIf (LastButton=Button[3]) then
NumDice=3
ElseIf (LastButton=Button[4]) then
NumDice=4
ElseIf (LastButton=Button[5]) then
NumDice=5
ElseIf (LastButton=Button[6]) then
NumDice=6
ElseIf (LastButton=Button[7]) then
NumDice=7
ElseIf (LastButton=Button[8]) then
NumDice=8
ElseIf (LastButton=Button[9]) then
NumDice=9
Else
'Do nothing
endif

If (LastButton=Dice[2]) Then
ValDice=2
ElseIf (LastButton=Dice[3]) then
ValDice=3
ElseIf (LastButton=Dice[4]) then
ValDice=4
ElseIf (LastButton=Dice[5]) then
ValDice=5
ElseIf (LastButton=Dice[6]) then
ValDice=6
ElseIf (LastButton=Dice[7]) then
ValDice=7
ElseIf (LastButton=Dice[8]) then
ValDice=8
ElseIf (LastButton=Dice[9]) then
ValDice=9
ElseIf (LastButton=Dice[10]) then
ValDice=10
ElseIf (LastButton=Dice[11]) then
ValDice=11
ElseIf (LastButton=Dice[12]) then
ValDice=12
ElseIf (LastButton=Dice[13]) then
ValDice=13
ElseIf (LastButton=Dice[14]) then
ValDice=14
ElseIf (LastButton=Dice[15]) then
ValDice=15
ElseIf (LastButton=Dice[16]) then
ValDice=16
ElseIf (LastButton=Dice[17]) then
ValDice=17
ElseIf (LastButton=Dice[18]) then
ValDice=18
ElseIf (LastButton=Dice[19]) then
ValDice=19
ElseIf (LastButton=Dice[20]) then
ValDice=20
ElseIf (LastButton=Dice[21]) then
ValDice=21
Else
EndIf

GraphicsWindow.DrawText(260,70,NumDice)
GraphicsWindow.DrawText(290,70,("D"+ValDice))


Result=0
For i=1 To NumDice
Result=Result+Math.Floor(Math.GetRandomNumber(ValDice))
EndFor
GraphicsWindow.FontSize=70
GraphicsWindow.DrawText(290,120,Result)
EndSub

No comments: