Results 1 to 1 of 1

Thread: [VB.Net] Tron - Lightcycles

  1. #1

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

    [VB.Net] Tron - Lightcycles

    This is the source code and exe

    Features
    Lets you play the famous game from Tron, lightcycles

    Drawbacks
    • Only player v. player


    Plans
    1. Add player v. cpu


    Notes
    I'm disappointed that there is no player v. cpu, but I will work up an AI to allow for 1p games.

    File:
    Tron - LightCycles.zip

    Source Code:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
    #Region "Globals"
        Private p1 As PictureBox
        Private p2 As PictureBox
        Private tmr1 As New Timer
        Private playing As Boolean
        Private pvp As Boolean
        Private p1trail As New List(Of Point)
        Private p2trail As New List(Of Point)
        Private bothtrail As New List(Of Point)
        Private p1Direct As PlayerDirection
        Private p2Direct As PlayerDirection
    
        Public Enum PlayerDirection
            Up
            Down
            Left
            Right
        End Enum
    #End Region
    
    #Region "Form1 Events"
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If playing = False AndAlso e.KeyCode = Keys.Space Then
                playing = True
                tmr1.Start()
            End If
    
            If playing Then
                Select Case e.KeyCode
                    Case Keys.A
                        p2Direct = PlayerDirection.Left
                    Case Keys.S
                        p2Direct = PlayerDirection.Down
                    Case Keys.D
                        p2Direct = PlayerDirection.Right
                    Case Keys.W
                        p2Direct = PlayerDirection.Up
                    Case Keys.Left
                        p1Direct = PlayerDirection.Left
                    Case Keys.Down
                        p1Direct = PlayerDirection.Down
                    Case Keys.Right
                        p1Direct = PlayerDirection.Right
                    Case Keys.Up
                        p1Direct = PlayerDirection.Up
                End Select
            End If
    
            
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With Me
                .BackColor = Color.Black
                .DoubleBuffered = True
                .KeyPreview = True
                .MaximizeBox = False
                .Text = "Tron - Lightcycles"
                .WindowState = FormWindowState.Maximized
            End With
    
            tmr1.Interval = 100
    
    
            'For future player v cpu
            'Dim result As DialogResult = MessageBox.Show("Are you playing against the computer?", Me.Text, MessageBoxButtons.YesNo)
            'If result = DialogResult.Yes Then
            '    pvp = False
            'ElseIf result = DialogResult.No Then
            '    pvp = True
            'End If
    
            pvp = True
    
            AddHandler tmr1.Tick, AddressOf tmr1_Tick
    
            Call newGame()
        End Sub
    
    #End Region
    
    #Region "New/End Game"
    
        Private Sub newGame()
            Me.Controls.Remove(p1)
            Me.Controls.Remove(p2)
            p1 = New PictureBox
            p2 = New PictureBox
            playing = False
            p1trail.Clear()
            p2trail.Clear()
            bothtrail.Clear()
            p1Direct = PlayerDirection.Right
            p2Direct = PlayerDirection.Left
    
    
            Me.Refresh()
    
            With p1
                .BackColor = Color.Blue
                .Location = New Point(CInt(Me.Width / 2 - 10 - Me.Width / 5), CInt(Me.Height / 2))
                .Size = New Size(20, 20)
            End With
    
            With p2
                .BackColor = Color.Red
                .Location = New Point(CInt(Me.Width / 2 + 10 + Me.Width / 5), CInt(Me.Height / 2))
                .Size = New Size(20, 20)
            End With
    
            p1trail.Add(p1.Location)
            p2trail.Add(p2.Location)
            bothtrail.AddRange({p1.Location, p2.Location})
    
            Me.Controls.AddRange({p1, p2})
    
            Me.Refresh()
        End Sub
    
        Private Sub checkEndGame()
            Dim endGame As Boolean = False
    
            For Each trail As Point In bothtrail
                Dim rect As New Rectangle(trail, New Size(15, 15))
                If p1.Bounds.IntersectsWith(rect) AndAlso p1trail.Count > 1 Then
                    tmr1.Stop()
                    endGame = True
                    MessageBox.Show("Player1 Lost")
                    Exit For
                ElseIf p2.Bounds.IntersectsWith(rect) AndAlso p2trail.Count > 1 Then
                    tmr1.Stop()
                    endGame = True
                    MessageBox.Show("Player2 Lost")
                    Exit For
                End If
            Next
    
            If p1.Left < 0 OrElse p1.Top < 0 OrElse p1.Left > Me.Width OrElse p1.Top > Me.Height Then
                tmr1.Stop()
                endGame = True
                MessageBox.Show("Player1 Lost")
            ElseIf p2.Left < 0 OrElse p2.Top < 0 OrElse p2.Left > Me.Width OrElse p2.Top > Me.Height Then
                tmr1.Stop()
                endGame = True
                MessageBox.Show("Player2 Lost")
            End If
    
            If endGame Then
                Call newGame()
            End If
        End Sub
    
    #End Region
    
    #Region "Painting"
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim g As Graphics = e.Graphics
    
            For Each trail As Point In p1trail
                Dim rect As New Rectangle(trail, New Size(20, 20))
                g.FillRectangle(New SolidBrush(p1.BackColor), rect)
            Next
    
            For Each trail As Point In p2trail
                Dim rect As New Rectangle(trail, New Size(20, 20))
                g.FillRectangle(New SolidBrush(p2.BackColor), rect)
            Next
    
        End Sub
    
    #End Region
    
    #Region "Timers"
    
        Private Sub tmr1_Tick(ByVal sender As Object, ByVal e As EventArgs)
            Select Case p1Direct
                Case PlayerDirection.Left
                    p1.Left -= 20
                Case PlayerDirection.Down
                    p1.Top += 20
                Case PlayerDirection.Right
                    p1.Left += 20
                Case PlayerDirection.Up
                    p1.Top -= 20
            End Select
    
            Select Case p2Direct
                Case PlayerDirection.Left
                    p2.Left -= 20
                Case PlayerDirection.Down
                    p2.Top += 20
                Case PlayerDirection.Right
                    p2.Left += 20
                Case PlayerDirection.Up
                    p2.Top -= 20
            End Select
    
            Call checkEndGame()
    
            p1trail.Add(p1.Location)
            p2trail.Add(p2.Location)
            bothtrail.AddRange({p1.Location, p2.Location})
    
        End Sub
    
    #End Region
    
    End Class
    Controls

    Player1
    Up - Up key
    Down - Down Key
    Left - Left Key
    Right - Right Key

    Player2
    Up - W key
    Down - S key
    Left - A key
    Right - D key

    Start game - Spacebar
    Last edited by dday9; Mar 21st, 2013 at 06:43 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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