Prev: Can't make exe
Next: VB6 and KBasic
From: FM on 20 Mar 2010 15:08 The visual mechanics of a spinning counter similar to a petrol station slot reel type of hit counter are simple enough but Im having trouble with the logic of clocking up. There are four reels (from left to right: thousands, hundreds, tens, units), hence maximum number display of 9999, and all reels are painted into one picturebox. I actually have this working using an array of counters for each reel and splitting the target score and subtracting the old score from the new score for each reel and using booleans for which reel to spin etc., but Im not happy as its getting over complicated and am wondering if theres a simpler approach. Any suggestions? To give you some idea of what Im trying to do here...copy the following code and add a picture box, command button and a text box. As it is this example spins the Units reel to the target number set in the textbox and shows the mechanics Im using: '########## Private Declare Function GetTickCount Lib "kernel32" () As Long Dim ReelSpeed As Integer Dim ReelFrameCtr As Integer Dim ReelIsSpinning As Boolean Private Sub Form_Load() Me.ScaleMode = vbPixels Me.AutoRedraw = True Me.Font.Size = 14 Me.Font.Bold = True Picture1.ScaleMode = vbPixels Picture1.Move 100, 100, 96, 24 'Tiles are 24*24 Picture1.BorderStyle = vbBSNone Picture1.AutoRedraw = True Command1.Caption = "Spin" Text1.Text = 9 DrawReel Picture1.PaintPicture Me.Image, 0, 0, 24, 24, 0, 0, 24, 24 'Thousands Picture1.PaintPicture Me.Image, 24, 0, 24, 24, 0, 0, 24, 24 'Hundreds Picture1.PaintPicture Me.Image, 48, 0, 24, 24, 0, 0, 24, 24 'Tens Picture1.PaintPicture Me.Image, 72, 0, 24, 24, 0, 0, 24, 24 'Units Picture1.Refresh ReelSpeed = 10 End Sub Private Sub Command1_Click() ReelIsSpinning = Not ReelIsSpinning SpinReel End Sub Private Sub SpinReel() Dim T1 As Long, T2 As Long T2 = GetTickCount Do Until ReelIsSpinning = False DoEvents T1 = GetTickCount If (T1 - T2) >= ReelSpeed Then ReelFrameCtr = ReelFrameCtr + 1 If ReelFrameCtr > 240 Then ReelFrameCtr = 0 Picture1.PaintPicture Form1.Image, 72, 0, 24, 24, 0, ReelFrameCtr, 24, 24 'Units Picture1.Refresh If ReelFrameCtr = ScoreTarget Then ReelIsSpinning = False T2 = GetTickCount End If Loop End Sub Private Function ScoreTarget() As Integer ScoreTarget = CInt(Text1.Text) * 24 End Function Private Sub DrawReel() 'Tiles are 24*24 ' Me.Line (0, 0)-(24, 24), vbRed, BF Me.CurrentX = 6: Me.CurrentY = 0: Me.Print "0" ' Me.Line (0, 24)-(24, 48), vbYellow, BF Me.CurrentX = 6: Me.CurrentY = 24: Me.Print "1" ' Me.Line (0, 48)-(24, 72), vbGreen, BF Me.CurrentX = 6: Me.CurrentY = 48: Me.Print "2" ' Me.Line (0, 72)-(24, 96), vbCyan, BF Me.CurrentX = 6: Me.CurrentY = 72: Me.Print "3" ' Me.Line (0, 96)-(24, 120), vbMagenta, BF Me.CurrentX = 6: Me.CurrentY = 96: Me.Print "4" ' Me.Line (0, 120)-(24, 144), vbRed, BF Me.CurrentX = 6: Me.CurrentY = 120: Me.Print "5" ' Me.Line (0, 144)-(24, 168), vbYellow, BF Me.CurrentX = 6: Me.CurrentY = 144: Me.Print "6" ' Me.Line (0, 168)-(24, 192), vbGreen, BF Me.CurrentX = 6: Me.CurrentY = 168: Me.Print "7" ' Me.Line (0, 192)-(24, 216), vbCyan, BF Me.CurrentX = 6: Me.CurrentY = 192: Me.Print "8" ' Me.Line (0, 216)-(24, 240), vbMagenta, BF Me.CurrentX = 6: Me.CurrentY = 216: Me.Print "9" ' Me.Line (0, 240)-(24, 264), vbRed, BF Me.CurrentX = 6: Me.CurrentY = 240: Me.Print "0" End Sub '########## Regards FM
From: Nobody on 20 Mar 2010 16:39 "FM" <spam(a)uce.gov> wrote in message news:upzGqCGyKHA.404(a)TK2MSFTNGP02.phx.gbl... > The visual mechanics of a spinning counter similar to a petrol station > slot reel type of hit counter are simple enough but Im having trouble with > the logic of clocking up. There are four reels (from left to right: > thousands, hundreds, tens, units), hence maximum number display of 9999, > and all reels are painted into one picturebox. > > I actually have this working using an array of counters for each reel and > splitting the target score and subtracting the old score from the new > score for each reel and using booleans for which reel to spin etc., but Im > not happy as its getting over complicated and am wondering if theres a > simpler approach. Any suggestions? > > To give you some idea of what Im trying to do here...copy the following > code and add a picture box, command button and a text box. As it is this > example spins the Units reel to the target number set in the textbox and > shows the mechanics Im using: I think you can make it simpler by just printing on the PictureBox. For smooth scrolling, you need 2 PictureBoxes, pctOffScreen, and pctOnScreen. You draw on pctOffScreen, which is a hidden PictureBox with AutoRedraw on, then copy it using PaintPicture to pctOnScreen. By specifying coordinates outside the picture box boundary, you can print partial characters. Example: Add a PictureBox to Form1 then use the following code: Private Sub Form_Load() Picture1.FontSize = 24 End Sub Private Sub Picture1_Paint() Dim sCounter As String sCounter = "1235" Picture1.CurrentX = 0 Picture1.CurrentY = 0 Picture1.Print Left(sCounter, 3); Picture1.CurrentY = -200 Picture1.Print Right(sCounter, 1); End Sub
From: Larry Serflaten on 20 Mar 2010 21:59 "FM" <spam(a)uce.gov> wrote in message news:upzGqCGyKHA.404(a)TK2MSFTNGP02.phx.gbl... > The visual mechanics of a spinning counter similar to a petrol station slot > reel type of hit counter are simple enough but Im having trouble with the > logic of clocking up. You have to keep the position of each individual reel. Try the code below in your form (picturebox, textbox, button) Also, avoid using the form for drawing as a form with AutoRedraw set is always as big as the screen. Note I used the picturebox and then saved the image in a variable. LFS '################ Dim ReelData(0 To 4) As Long Dim ReelImage As StdPicture Private Sub Form_Load() Show Picture1.ScaleMode = vbPixels Picture1.AutoRedraw = True Picture1.Font.Size = 14 Picture1.Font.Bold = True Picture1.ScaleMode = vbPixels DrawReel ReelData(0) = 24 ReelData(1) = 24 ReelData(2) = 24 ReelData(3) = 24 ReelData(4) = 24 Me.ScaleMode = vbPixels Picture1.Move 100, 100, 96, 24 'Tiles are 24*24 Picture1.BorderStyle = vbBSNone Command1.Caption = "Spin" Text1.Text = "4" ShowReel End Sub Private Sub Command1_Click() SpinReel Val(Text1) End Sub Private Sub ShowReel() Picture1.PaintPicture ReelImage, 0, 0, 24, 24, 0, ReelData(0), 24, 24 'Thousands Picture1.PaintPicture ReelImage, 24, 0, 24, 24, 0, ReelData(1), 24, 24 'Hundreds Picture1.PaintPicture ReelImage, 48, 0, 24, 24, 0, ReelData(2), 24, 24 'Tens Picture1.PaintPicture ReelImage, 72, 0, 24, 24, 0, ReelData(3), 24, 24 'Units Picture1.Refresh End Sub Private Sub SpinReel(ByVal Amount As Long) Dim A As Long, D As Long, F As Long Dim R(0 To 4) ' Amount is added to value on reels For A = 1 To Amount R(3) = 2 ' Y increment value ' Mark moving reels For D = 2 To 0 Step -1 If ReelData(D + 1) = 0 Then R(D) = R(D + 1) End If Next ' Spin reels one digit For F = 1 To 12 DoEvents For D = 0 To 3 ReelData(D) = ReelData(D) + R(D) Next ShowReel Next ' Catch rollover For D = 0 To 3 R(D) = 0 If ReelData(D) > 238 Then ReelData(D) = 0 Next Next End Sub Private Sub DrawReel() 'Tiles are 24*24 Picture1.Move 0, 0, 420, 4200 Picture1.Line (0, 0)-(24, 24), vbRed, BF Picture1.CurrentX = 6: Picture1.CurrentY = 0: Picture1.Print "9" Picture1.Line (0, 24)-(24, 48), vbYellow, BF Picture1.CurrentX = 6: Picture1.CurrentY = 24: Picture1.Print "0" Picture1.Line (0, 48)-(24, 72), vbGreen, BF Picture1.CurrentX = 6: Picture1.CurrentY = 48: Picture1.Print "1" Picture1.Line (0, 72)-(24, 96), vbCyan, BF Picture1.CurrentX = 6: Picture1.CurrentY = 72: Picture1.Print "2" Picture1.Line (0, 96)-(24, 120), vbMagenta, BF Picture1.CurrentX = 6: Picture1.CurrentY = 96: Picture1.Print "3" Picture1.Line (0, 120)-(24, 144), vbRed, BF Picture1.CurrentX = 6: Picture1.CurrentY = 120: Picture1.Print "4" Picture1.Line (0, 144)-(24, 168), vbYellow, BF Picture1.CurrentX = 6: Picture1.CurrentY = 144: Picture1.Print "5" Picture1.Line (0, 168)-(24, 192), vbGreen, BF Picture1.CurrentX = 6: Picture1.CurrentY = 168: Picture1.Print "6" Picture1.Line (0, 192)-(24, 216), vbCyan, BF Picture1.CurrentX = 6: Picture1.CurrentY = 192: Picture1.Print "7" Picture1.Line (0, 216)-(24, 240), vbMagenta, BF Picture1.CurrentX = 6: Picture1.CurrentY = 216: Picture1.Print "8" Picture1.Line (0, 240)-(24, 264), vbRed, BF Picture1.CurrentX = 6: Picture1.CurrentY = 240: Picture1.Print "9" Set ReelImage = Picture1.Image Picture1 = LoadPicture() End Sub
From: FM on 21 Mar 2010 18:18 Thanks Larry, I knew I could rely on you. This seems to be what Im looking for...not totally clear at first glance whats going on there yet am just playing with it now. Have had a family crisis today so havent had any hobby time. Regards FM
|
Pages: 1 Prev: Can't make exe Next: VB6 and KBasic |