Results 1 to 20 of 20

Thread: VB6 Tron Game

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Arrow VB6 Tron Game

    I am having difficulty with a school assignment. We have to create a Tron game and I am having trouble with the part where if a player hits their or another player's trail, it is game over. The teacher told us that we should use intColour =
    CStr(PictureBox.Point(intX, intY)) and checking whether the intColour matches a trail colour. The problem is though that I am constantly hitting my own trail. The code I have written so far is down below. I added a file to make it easier.
    Code:
    Private Sub cmdClear_Click()
    picField.Cls
    lstClicks.Clear
    End Sub
    
    
    Private Sub Form_Activate()
    'Initialize
    strUserDirection = "Stationary"
    intUserX = 200
    intUserY = 200
    picField.PSet (intUserX, intUserY), vbGreen
    picField.Circle (intUserX, intUserY), 4, vbBlue
    End Sub
    
    Private Sub picField_KeyPress(KeyAscii As Integer)
    lstClicks.AddItem KeyAscii & "  " & Chr(KeyAscii)
    'PLAYER ONE - using the keyboard on the right hand side
    If KeyAscii = 56 Then 'which was the key 8
        strUserDirection = "North" ' or up
    ElseIf KeyAscii = 50 Then 'which was the key 2
        strUserDirection = "South" ' or down
    ElseIf KeyAscii = 52 Then 'which was the key 4
        strUserDirection = "West" ' or left
    ElseIf KeyAscii = 54 Then
        strUserDirection = "East" ' or right
    ElseIf KeyAscii = 32 Then 'which is the space key
        strUserDirection = "Stationary"
    End If
    
    End Sub
    
    Private Sub picField_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    lstClicks.AddItem CStr(picField.Point(X, Y))
    picField.PSet (X, Y), vbBlue
    picField.Circle (X, Y), 3, vbGreen
    
    End Sub
    
    Private Sub tmrGamePlay_Timer()
    
    If strUserDirection = "North" Then
        intUserY = intUserY - 1
    ElseIf strUserDirection = "South" Then
        intUserY = intUserY + 1
    ElseIf strUserDirection = "West" Then
        intUserX = intUserX - 1
    ElseIf strUserDirection = "East" Then
        intUserX = intUserX + 1
    End If
    picField.PSet (intUserX, intUserY), RGB(66, 135, 245)
    
    If intUserX = picField.Width Or intUserY = picField.Height Then
        lstClicks.AddItem "Hello"
        MsgBox ("Game Over")
    ElseIf intUserX = picField Or intUserY = picField Then
        lstClicks.AddItem "Hello"
        MsgBox ("Game Over")
    End If
    
    'Declaration
    Dim lngColour As Long
    'Initialization
    lngColour = 0
    'Input
    'does not apply
    'Processing/Output
    
    
    lngColour = CStr(picField.Point(intUserX, intUserY))
    
    End Sub
    Attached Files Attached Files
    Last edited by Shaggy Hiker; May 24th, 2022 at 07:33 PM. Reason: Added CODE tags.

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,974

    Re: VB6 Tron Game

    Welcome.

    One of the things a programmer should be able to do is explain what, exactly, the purpose and expected outcome is of any line(s) of code they have written. Basically, "what does this line of code do"?

    That being said, presuming that picField is a PictureBox control on your form, what is this line of code doing?

    Code:
    ElseIf intUserX = picField Or intUserY = picField Then

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    The purpose of that line is if the player's hit the border of the picturebox, it is game over.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    This is what I am having trouble doing. The purpose of that line of code is if the player's trail hit the border of the picturebox, it is game over.

    Hitting a racerís trail would be determined when you check the colour of the pixel of the playerís next location. This
    can be determined using intColour = CStr(PictureBox.Point(intX, intY)) and checking whether the intColour matches a trail colour.

  5. #5
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,895

    Re: VB6 Tron Game

    u dont need to check any color.
    just check the x,y coordination of the player.
    when x is equal or above the x of the border its a hit. the same u can do for all the 4 borders, x+, x-, y+, y-
    join the pandemic discussion here: https://discord.gg/Zhx7aJZRMh (no longer available in vbforums)

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    The border thing is not my main concern right now. It is the player hitting their own trail.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    My teacher says I have to check the colour of the pixel of the player's next location and use CStr to do it.

  8. #8
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,895

    Re: VB6 Tron Game

    u can create an array for it.
    each time u move, u store that to an array,
    and each time u move u check the array if the coordination is already used.
    join the pandemic discussion here: https://discord.gg/Zhx7aJZRMh (no longer available in vbforums)

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    We have not been taught arrays yet so can't use them. All my teacher said is that I have to develop logic and use CStr to detect if the player has hit their own trail by checking the colour of the pixel.

  10. #10
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,895

    Re: VB6 Tron Game

    this is working example. its "similar" to what you ask but I did not follow your source since I want you to do it yourself.
    this should make you understand the principle.

    Code:
    Dim UserDirection As Integer
    Dim intUserX As Long
    Dim intUserY As Long
    Dim Color As Long
    Dim Body As Long
    
    Private Sub picField_KeyPress(KeyAscii As Integer)
        UserDirection = KeyAscii
    End Sub
    
    Private Sub cmdClear_Click()
        UserDirection = 0
        Color = RGB(66, 135, 245)
        Body = 4
        Me.ScaleMode = 3
        With picField
            .Cls
            .AutoRedraw = True
            .ScaleMode = 3
            intUserX = .Width \ 2 - Body \ 2
            intUserY = .Height \ 2 - Body \ 2
            .SetFocus
        End With
        tmrGamePlay.Enabled = True
        UserDirection = CInt(Rnd * 3) * 2 + 50
    End Sub
    
    Private Sub Form_Activate()
        Randomize Timer
        Call cmdClear_Click
    End Sub
    
    Private Sub tmrGamePlay_Timer()
        With picField
        Select Case UserDirection
            Case 56: intUserY = intUserY - 1: If intUserY < Body Or .Point(intUserX, intUserY - Body) = Color Then GoTo GameOver
            Case 50: intUserY = intUserY + 1: If intUserY > .Height - Body Or .Point(intUserX, intUserY + Body) = Color Then GoTo GameOver
            Case 52: intUserX = intUserX - 1: If intUserX < Body Or .Point(intUserX - Body, intUserY) = Color Then GoTo GameOver
            Case 54: intUserX = intUserX + 1: If intUserX > .Width - Body Or .Point(intUserX + Body, intUserY) = Color Then GoTo GameOver
        End Select
        End With
        picField.Circle (intUserX, intUserY), Body, Color
        picField.PSet (intUserX, intUserY), vbWhite
        Exit Sub
    GameOver:
        tmrGamePlay.Enabled = False
        MsgBox "Game Over"
    End Sub
    join the pandemic discussion here: https://discord.gg/Zhx7aJZRMh (no longer available in vbforums)

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,529

    Re: VB6 Tron Game

    You're hitting your own tail all the time because you're checking the color after you've drawn the color in that position.
    You need to check the color of the position before you draw in it.
    If it is already set to a color then you are colliding with that color.

    Usually this game can be quite simple to write if you set the background color to some known color, and then draw a border around the gaming area. Then you can simple do a single test for the color of the position you are moving to.
    If the new position's color is not the background color, then you've collided with something, i.e. the border, your tail, or the player's tail.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  12. #12
    Lively Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    87

    Re: VB6 Tron Game

    I just use this code if I want to interogate a Picturebox's pixel colour (x,y)
    This goes in the Module section:
    Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long

    This is how you call it in your code. It returns the Long Value of the Colour you want to check.
    Chosen_Pixel_Colour = GetPixel(My_Picture_Box.hDC, X, Y)

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    Thank you everyone for your help. I manage to fix the problem.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Question Re: VB6 Tron Game

    I have encountered another problem. I need to count the number of pixels the player has "painted" and I manage to do so by creating a global variable and adding 1 each time I increment my X or Y coordinate for the player. The problem with the code I have right now is that the listbox is displaying repeatedly and I know this is because of the timer but I do not know where else to put it. What I want is the listbox to display the score and the score updating as the player covers more of the picturebox and not repeating so only taking up one line. The code I have so far is:

    Note: lngPlayerScore is the global variable

    Private Sub tmrGamePlay_Timer()

    If strUserDirection = "North" Then
    intUserY = intUserY - 1
    lngPlayerScore = lngPlayerScore + 1
    ElseIf strUserDirection = "South" Then
    intUserY = intUserY + 1
    lngPlayerScore = lngPlayerScore + 1
    ElseIf strUserDirection = "West" Then
    intUserX = intUserX - 1
    lngPlayerScore = lngPlayerScore + 1
    ElseIf strUserDirection = "East" Then
    intUserX = intUserX + 1
    lngPlayerScore = lngPlayerScore + 1
    End If

    lstClicks.AddItem lngPlayerScore

    'Declaration
    Dim lngColour As Long
    Dim intPoint As Integer

    'Initialization
    lngColour = 0
    'Input
    'does not apply
    'Processing/Output

    If strUserDirection = "North" Or strUserDirection = "South" Or strUserDirection = "West" Or strUserDirection = "East" Then

    lngColour = CStr(picField.Point(intUserX, intUserY))

    If lngColour = RGB(66, 135, 245) Then

    lstClicks.AddItem "hello"
    tmrGamePlay.Enabled = False

    End If

    End If

    picField.PSet (intUserX, intUserY), RGB(66, 135, 245)

    If intUserX = picField.Width Or intUserY = picField.Height Then
    lstClicks.AddItem "Hello"
    MsgBox ("Game Over")
    ElseIf intUserX = picField Or intUserY = picField Then
    lstClicks.AddItem "Hello"
    MsgBox ("Game Over")
    End If

    End Sub
    Last edited by rya56; May 26th, 2022 at 01:30 PM.

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    Also, sorry for how badly formatted the code is. The code was formatted before it was posted and after, all of the spaces disappeared.

  16. #16
    Lively Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    87

    Re: VB6 Tron Game

    Listbox? Wut. Shouldn't you be using a Textbox or Label for displaying scores?

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    I will change it later but right now I am using a listbox just to see if my code works or will using a textbox or label fix my problem?

  18. #18
    Lively Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    87

    Re: VB6 Tron Game

    Quote Originally Posted by rya56 View Post
    I will change it later but right now I am using a listbox just to see if my code works or will using a textbox or label fix my problem?
    Well, most likely. Listboxes add, indefinitely. Textboxes and Labels only grab the one instance of what you send to it. Ie: They only change when you update them. ... If I understand what you're problem is.

  19. #19

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    12

    Re: VB6 Tron Game

    Ok, I will try doing that.

  20. #20
    PowerPoster
    Join Date
    Jun 2013
    Posts
    6,049

    Re: VB6 Tron Game

    Here's my take at it...
    - already prepared for two Players (Arrow-Keys Player1 ... WASD-Keys for Player2 ... <Space> for Pause )
    - works without APIs and no Extra-Controls, on a plain VB6-Form

    Code:
    Option Explicit
    
    Private Canvas As VB.PictureBox, WithEvents tmrGamePlay As VB.Timer
    Private Player1 As VB.Shape, Player2 As VB.Shape
    
    Private Sub Form_Load()
      Caption = "Move via ArrowKeys (DblClick for Reset)"
      KeyPreview = True
    
      Set Canvas = Controls.Add("VB.PictureBox", "Canvas") '<- could also be a PictureBox (here we use the Form itself as the Game-Canvas)
          Canvas.ScaleMode = vbPixels: Canvas.Enabled = False
          Canvas.BackColor = vbBlack
          Canvas.AutoRedraw = True: Canvas.Move 0, 0, ScaleWidth, ScaleHeight: Canvas.Visible = True
    
      Set Player1 = Controls.Add("VB.Shape", "Player1", Canvas)
          Player1.Shape = vbShapeCircle: Player1.Move 0, 0, 4, 4: Player1.Visible = True
          Player1.BorderColor = vbRed
          Player1.FillColor = vbGreen 'repurpose the Shapes FillColor-Prop for the "Trail-Color"
    
      Set Player2 = Controls.Add("VB.Shape", "Player2", Canvas)
          Player2.Shape = vbShapeCircle: Player2.Move 0, 0, 4, 4: Player2.Visible = True
          Player2.BorderColor = vbCyan
          Player2.FillColor = vbYellow 'repurpose the Shapes FillColor-Prop for the "Trail-Color"
    
      Set tmrGamePlay = Controls.Add("VB.Timer", "tmrGamePlay")
          tmrGamePlay.Interval = 50
    
      ResetGame
    End Sub
    Private Sub Form_DblClick()
      ResetGame
    End Sub
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      Select Case KeyCode 'Apply Movement-Vectors for the two Players
        Case vbKeyLeft:  Player1.Tag = "-1,0"
        Case vbKeyUp:    Player1.Tag = "0,-1"
        Case vbKeyRight: Player1.Tag = "+1,0"
        Case vbKeyDown:  Player1.Tag = "0,+1"
    
        Case vbKeyA:     Player2.Tag = "-1,0"
        Case vbKeyW:     Player2.Tag = "0,-1"
        Case vbKeyD:     Player2.Tag = "+1,0"
        Case vbKeyS:     Player2.Tag = "0,+1"
        
        Case vbKeySpace: Player1.Tag = "": Player2.Tag = "" '<- no movement
      End Select
    End Sub
    Private Sub Form_Resize()
      Canvas.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub
    
    Private Sub tmrGamePlay_Timer()
      If ApplyMovementFor(Player1) Then 'only when Movement was applied...
         If CheckGameOverFor(Player1) Then MsgBox "Game Over for Player1!": ResetGame '...we check the GameOver-Condition
      End If
      If ApplyMovementFor(Player2) Then 'only when Movement was applied...
         If CheckGameOverFor(Player2) Then MsgBox "Game Over for Player2!": ResetGame '...we check the GameOver-Condition
      End If
      ExtendTrailFor Player1
      ExtendTrailFor Player2
    End Sub
    
    Function ApplyMovementFor(P As VB.Shape) As Boolean
      If Len(P.Tag) Then ApplyMovementFor = True Else Exit Function
      P.Move P.Left + Split(P.Tag, ",")(0), P.Top + Split(P.Tag, ",")(1)
    End Function
    
    Function CheckGameOverFor(P As VB.Shape) As Boolean 'if a Player is outside the Canvas, the Point()-Method will return -1
      CheckGameOverFor = CBool(Canvas.Point(P.Left + 2, P.Top + 2) <> Canvas.BackColor) '<- so we can simply check it this way
    End Function
    
    Sub ExtendTrailFor(P As VB.Shape)
      Canvas.PSet (P.Left + 2, P.Top + 2), P.FillColor 'the state of a Players trail is held by the Canvas-Pixels
    End Sub
    
    Sub ResetGame() 'delete the movement-state by re-setting the Tag-Prop (and move the two players slightly off-center
      Player1.Tag = "": Player1.Move Canvas.ScaleWidth \ 2 + 15, Canvas.ScaleHeight \ 2 + 15
      Player2.Tag = "": Player2.Move Canvas.ScaleWidth \ 2 - 15, Canvas.ScaleHeight \ 2 - 15
      Canvas.Cls 'clear the AutoRedraw-Bitmap of the Canvas
    End Sub
    @rya56
    Games are all about state-management (and then reflecting this state in the render-output).

    In the above example, the Player-State (Position and Colors) is held in two VB.Shape-Objects -
    and the "State of a Players Trail" is held in the Canvas' AutoRedraw-Bitmap.

    And properly named Functions and SubRoutines will finally make the stuff which happens in your tmrGamePlay-Loop
    more understandable and readable.

    HTH

    Olaf
    Last edited by Schmidt; May 26th, 2022 at 10:00 PM. Reason: Code-Change from Form-Canvas to PicBox-Canvas

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