-
Jul 11th, 2022, 02:03 PM
#1
"Scratch off" approaches
I saw another site where some VB5 and VB6 programmers were arguing over the best way to produce an educational game "lottery ticket scratch off" effect for answers to questions.
All of them look like they would work, but they seemed very convoluted involving multiple regions and blitting operations (at best) or weird gymnastics setting pixel-by-pixel reveals calculating curves and on and on.
Seemed odd to me, but here Is one way I cobbled together. As far as I can tell it works from Win NT 4.0 forward, though not on Win9x due to GDI limitations there.
Basically I create a broad (24 pixels here) geometric brush from the "printed" message to be revealed. Then clear the PictureBox to all "silver." Finally let the user click and drag to "scratch" the ticket.
Make a new Project with a Form, add two controls and this code:
Code:
Option Explicit
'This Form has 1 PictureBox: Picture1
' 1 CommandButton: Command1
Private Const WIN32_NULL As Long = 0
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Enum PenStyles 'Abbreviated list.
PS_GEOMETRIC = &H10000
End Enum
Private Enum BrushStyles 'Abbreviated list.
BS_PATTERN = 3&
End Enum
Private Type LOGBRUSH
lbStyle As BrushStyles
lbColor As Long
lbHatch As Long
End Type
Private Declare Function ExtCreatePen Lib "gdi32" ( _
ByVal dwPenStyle As PenStyles, _
ByVal dwWidth As Long, _
ByRef LOGBRUSH As LOGBRUSH, _
Optional ByVal dwStyleCount As Long = 0, _
Optional ByVal lpStyle As Long = WIN32_NULL) As Long
Private Declare Function SelectObject Lib "gdi32" ( _
ByVal hDC As Long, _
ByVal hObject As Long) As Long
Private hPen As Long
Private hPenOrig As Long
Private Sub NewScratcher()
Const PLAY_AGAIN As String = "SORRY, PLAY AGAIN"
Dim I As Long
Dim Text As String
Dim LOGBRUSH As LOGBRUSH
With Picture1
.Visible = False
.BackColor = vbWhite
.ForeColor = &HC08040
'Print generated message centered:
I = Int(Rnd() * 13)
If I = 0 Then
.CurrentY = (.ScaleHeight - .TextHeight(PLAY_AGAIN)) / 2
.CurrentX = (.ScaleWidth - .TextWidth(PLAY_AGAIN)) / 2
Picture1.Print PLAY_AGAIN;
Else
Text = MonthName(I)
.CurrentY = (.ScaleHeight - .TextHeight(Text) * 3) / 2
.CurrentX = (.ScaleWidth - .TextWidth(Text)) / 2
Picture1.Print Text
Text = "will be"
.CurrentX = (.ScaleWidth - .TextWidth(Text)) / 2
Picture1.Print Text
Text = WeekdayName(Int(Rnd() * 7) + 1, , vbSunday)
.CurrentX = (.ScaleWidth - .TextWidth(Text)) / 2
Picture1.Print Text;
End If
'Convert the message to a brush:
LOGBRUSH.lbStyle = BS_PATTERN
LOGBRUSH.lbHatch = .Image.Handle
If hPen <> WIN32_NULL Then DeleteObject hPen
hPen = ExtCreatePen(PS_GEOMETRIC, 24, LOGBRUSH)
'Add "scratch-off layer" by clearing to a silvery color and select our brush:
.BackColor = &HD8D8D8
.Visible = True
hPenOrig = SelectObject(.hDC, hPen)
End With
End Sub
Private Sub Command1_Click()
NewScratcher
End Sub
Private Sub Form_Load()
Caption = "Click and drag to scratch off"
With Picture1
'Position and size as desired, such as at design-time:
.Move (ScaleWidth - 3600) / 2, _
120, _
3600, _
1200
.FontName = "Comic Sans MS"
.FontSize = 12
.AutoRedraw = True
'More positioning:
Command1.Caption = "New scratcher"
Command1.Move (ScaleWidth - 1800) / 2, _
.Top + .Height + 120, _
1800
End With
NewScratcher
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Picture1.Line Step(0, 0)-(X, Y)
Else
Picture1.CurrentX = X
Picture1.CurrentY = Y
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
SelectObject hDC, hPenOrig
DeleteObject hPen
End Sub
Anyone have anything simpler to suggest?
-
Jul 11th, 2022, 07:39 PM
#2
Re: "Scratch off" approaches
If I were doing this, I'd start with a surface painted with whatever image represents the "cover". I'd have a separate image in memory that holds the image of whatever is to be revealed. As the user moves the mouse to "scratch", I'd draw parts of the image in memory over the painted surface based on the where they were scratching. Seems like the most straightforward way to do this.
I wouldn't mind doing a sample but the thought of having to wade through all of my old VB6 project to copy paste GDI function imports makes me nauseous. I'm just too lazy to do all that right now lol... Maybe later I'd knock together something in a few minutes in .Net though. Seems like an interesting distraction.
-
Jul 11th, 2022, 07:46 PM
#3
Re: "Scratch off" approaches
Working code isn't as important as concepts.
I suppose you could blit a series of small rectangles from the "message" to the "cover" following the mouse. I'm not sure about the quality of the effect though.
In mine the line-drawing endcaps default to round and this gives more of a "rubbing" effect. I'd rather have some kind of ragged endcaps but I'm not sure how to do that.
-
Jul 11th, 2022, 08:22 PM
#4
Re: "Scratch off" approaches
Just to be fun, one might consider a way to make the "brush" have less density or hardness. This is all just aesthetics, but it might be fun, and give a bit more of the feel of something actually being scratched off.
Less dense:
Less hard:
And, you could do both.
When I first read your OP, I was thinking it'd have a fuzz on the edges (less hard). IDK, just something to make them work a bit more at it to do the scratch-off. I mean, otherwise, just have a button that says "Reveal".
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jul 11th, 2022, 08:51 PM
#5
Re: "Scratch off" approaches
-
Jul 11th, 2022, 10:25 PM
#6
Re: "Scratch off" approaches
So... many ways to scratch a cat.
-
Jul 12th, 2022, 05:35 AM
#7
Re: "Scratch off" approaches
Ha that would be great, top layer cat, under layer cat skeleton.
Or top layer cute girl under layer scary witch or lingerie or witch in lingerie sure why not,
-
Jul 12th, 2022, 06:13 AM
#8
Re: "Scratch off" approaches
I think anyone who wants this is probably making a game for kids. I doubt photorealism is very important since it just another funky way to "reveal" answers or hints or something.
-
Jul 12th, 2022, 08:05 AM
#9
Fanatic Member
Re: "Scratch off" approaches
So... many ways to scratch a cat.
I love the simplicity in your solution.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|