Results 1 to 2 of 2

Thread: [VB.Net]Plinko

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    [VB.Net]Plinko

    This is the source code and the .exe

    Features:
    • Allows you to play the game Plinko


    Drawbacks:
    • The collision against irregular objects is a little off.


    Notes:
    I plan on tweaking the drawback listed above, but I'm not going to lie... Detecting collision between two irregular objects is tough. You can't just use the objects bounds because bounds are of type 'rectangle' which just doesn't work.

    Project:
    plinko.zip

    Source Code:
    In order for the source to compile you'll need to add:
    1. Three(3) panels. Named Panel1, pnl_Wall1, and pnl_Wall2
    2. One(1) timer. Named Timer1


    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Drawing.Drawing2D
    5. Public Class Form1
    6.     Private bottom_container As New List(Of Panel)
    7.     Private peg_conatiner As New List(Of Circle_Panel)
    8.     Private coin As New Circle_Panel
    9.     Private moving As Boolean = False
    10.     Private move_x, move_y As Integer
    11.     Private r As New Random
    12.  
    13.     Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    14.         If moving = False Then
    15.             'I check for the coin.left/right -/+ 10 to make sure that the coin is in sight
    16.             If e.KeyCode = Keys.Left AndAlso coin.Left - 10 > 0 Then
    17.                 coin.Left -= 10
    18.             ElseIf e.KeyCode = Keys.Right AndAlso coin.Right + 10 < Panel1.Width Then
    19.                 coin.Left += 10
    20.             ElseIf e.KeyCode = Keys.Space Then
    21.                 move_y = 5
    22.                 'Get if the current millisecond is even/odd to set if the coin falls left/right
    23.                 If CBool(DateTime.Now.Millisecond Mod 2) Then
    24.                     move_x = 5
    25.                 Else
    26.                     move_x = -5
    27.                 End If
    28.                 Timer1.Start()
    29.                 moving = True
    30.             End If
    31.         End If
    32.     End Sub
    33.  
    34.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    35.         'Set up the containers at the bottom
    36.         'There's a couple of things I've done differently:
    37.         '1)Loop from 1 - 5, reason is for the pnl.Left property. I didn't want to do 0 * 66 because that'd be 0
    38.         '2)Set the pnl.Left = x * 66. The reason I did 66 is because I took the panel1.width(400) and divided it by 6. The reason for 6
    39.         '             Is because if I did it by 5, the fifth pnl.Left would = Panel1.Width so it wouldn't show.
    40.         For x As Integer = 1 To 5
    41.             Dim pnl As New Panel
    42.             With pnl
    43.                 .Size = New Size(10, 50)
    44.                 .BackColor = Color.SaddleBrown
    45.                 .Left = x * 66
    46.                 .Top = Panel1.Bottom - 50
    47.             End With
    48.  
    49.             bottom_container.Add(pnl)
    50.             Panel1.Controls.Add(pnl)
    51.         Next
    52.  
    53.         'Set up the pegs
    54.         'I loop from 1 -5 & 2 - 9 for the same reason as reason #1 for the containers at the bottom
    55.         For x As Integer = 1 To 5
    56.             For y As Integer = 2 To 9
    57.                 Dim peg As New Circle_Panel
    58.                 With peg
    59.                     .Size = New Size(15, 15)
    60.                     .BackColor = Color.Gainsboro
    61.  
    62.                     'I check if y is divisible by 2 to create a staggering effect
    63.                     If CBool(y Mod 2) Then
    64.                         .Left = CInt((x - 0.5) * 66)
    65.  
    66.                         If x = 5 Then
    67.                             'Extra peg
    68.                             'The reason we use an extra peg is so that the far right side isn't just a straight shot down
    69.                             Dim newpeg As New Circle_Panel
    70.                             With newpeg
    71.                                 .Size = New Size(15, 15)
    72.                                 .BackColor = Color.Gainsboro
    73.                                 .Left = CInt((6 - 0.5) * 66)
    74.                                 .Top = y * 38
    75.                             End With
    76.                             peg_conatiner.Add(newpeg)
    77.                             Panel1.Controls.Add(newpeg)
    78.                         End If
    79.                     Else
    80.                         .Left = x * 66
    81.                     End If
    82.  
    83.                     .Top = y * 38
    84.                 End With
    85.  
    86.                 'Add to the list and to the panel's controls
    87.                 peg_conatiner.Add(peg)
    88.                 Panel1.Controls.Add(peg)
    89.             Next
    90.         Next
    91.  
    92.         'Set up the coin
    93.         With coin
    94.             .BackColor = Color.Gold
    95.             .Size = New Size(25, 25)
    96.             .Location = New Point(10, 5)
    97.         End With
    98.  
    99.         Panel1.Controls.Add(coin)
    100.  
    101.     End Sub
    102.  
    103.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    104.  
    105.         'Loop through each peg and check if the coin hits the peg
    106.         For Each peg As Circle_Panel In peg_conatiner
    107.             Dim gp As New GraphicsPath
    108.             gp.AddEllipse(coin.Bounds)
    109.             Dim gp2 As New GraphicsPath
    110.             gp2.AddEllipse(peg.Bounds)
    111.             If gp2.PathPoints.Any(Function(pt) gp.IsVisible(pt)) Then
    112.  
    113.                 move_x = -move_x
    114.  
    115.             End If
    116.         Next
    117.  
    118.         'Loop through each 'border' at the bottom and check if the coin hits one of those
    119.         For Each border As Panel In bottom_container
    120.             If coin.Bounds.IntersectsWith(border.Bounds) Then
    121.                 move_x = -move_x
    122.             End If
    123.         Next
    124.  
    125.         'Check if the coin hits one of the walls docked to the left/right of Panel1
    126.         If coin.Bounds.IntersectsWith(pnl_Wall1.Bounds) OrElse coin.Bounds.IntersectsWith(pnl_Wall2.Bounds) Then
    127.             move_x = -move_x
    128.         End If
    129.  
    130.         'If the coin is still above ground
    131.         If coin.Bottom < Panel1.Bottom Then
    132.  
    133.             'Keep moving
    134.             coin.Left += move_x
    135.             coin.Top += move_y
    136.         Else
    137.  
    138.             'Stop!
    139.             Timer1.Stop()
    140.             moving = False
    141.             coin.Location = New Point(10, 5)
    142.             'Random score from 0 - 5
    143.             MessageBox.Show(r.Next(0, 6).ToString)
    144.         End If
    145.     End Sub
    146. End Class
    147.  
    148. Public Class Circle_Panel
    149.     Inherits Panel
    150.  
    151.     Private Sub Circle_Panel_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    152.         'Declare graphics path and set a rectangle
    153.         Dim gPath As New GraphicsPath
    154.         Dim rect As Rectangle = Me.ClientRectangle
    155.  
    156.         'Draw the border
    157.         e.Graphics.DrawEllipse(New Pen(Brushes.Black, 3), rect)
    158.  
    159.         'Add the circle to the graphics path & set the region
    160.         gPath.AddEllipse(rect)
    161.         Me.Region = New System.Drawing.Region(gPath)
    162.  
    163.     End Sub
    164.  
    165. End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: [VB.Net]Plinko

    When it comes to game programming, you should never use a Timer, especially in collision. Timers are bad. Instead use a managed game loop locked at 60 frames per second. The problem with timers is that the entire timer must fire every bit of code in them before another timer or another execution could be executed. Thats probably one reason why your collision is off.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width