Ex - Comunidad de Programadores, Nuevo Leon

when you see if { ... } else { ... } you see code, i see my fucking life ...

No te has registrado

#1 2009-04-16 16:44:41

DeathLikeGhost
Nuevo Usuario
Ubicación: Guadalajara, Jalisco, México.
Registrado: 2009-04-16
Mensajes: 2

AscII Game Engine

Hola, este es mi primer post a la comunidad, así que decidí postear un trabajito hecho por mi.

Está programado en VB .Net 2008 express edition, es un engine para programar juegos con "gráficos" ascii, es decir, sólo caractéres en una consola de ms-dos.

Si a alguien le interesa trabajar conmigo para mejorarlo pueden agregarme a su lista de contactos de su sistema de mensajería o como ustedes gusten, mi E-Mail es: burzum_durbub_bot@hotmail.com

Mi siguiente objetivo es ponerle soporte para un juego en linea, me encantaría llegar a eso... pero el código no se presta para hacerlo fácil...

Nota: El engine no es para gente que recién inicia en el mundo de la programación con VB (A partir de las versiónes para windows) Pienso que alguien que ya tiene experiencia (algo así como básica-moderada) en VB comprenderá de inmediato como usar el engine después de la info que les voy a dar, un programador de medio a avanzado supongo entenderá sin tener que leer la sig. info.

Código fuente:

Código:

Imports System.Text
Namespace AscIIGE
    Public Class Map
        Public XY(,) As Char
        Public XYColor(,) As ConsoleColor
        Public XYBColor(,) As ConsoleColor
        Public Background As Char
        Public Background_Color As ConsoleColor
        Public Background_BckColor As ConsoleColor
        Public X As Integer
        Public Y As Integer
        Public Sub New(ByVal XMax As Integer, ByVal YMax As Integer, ByVal B As Char, ByVal BColor As ConsoleColor, ByVal BckColor As ConsoleColor)
            ReDim XY(XMax, YMax)
            For i = 0 To XMax
                For j = 0 To YMax
                    XY(i, j) = B
                Next
            Next
            ReDim XYColor(XMax, YMax)
            ReDim XYBColor(XMax, YMax)
            For i = 0 To XMax
                For j = 0 To YMax
                    XYColor(i, j) = BColor
                    XYBColor(i, j) = BckColor
                Next
            Next
            Background_Color = BColor
            Background_BckColor = BckColor
            Background = B
            X = XMax
            Y = YMax
        End Sub
        Public Sub New(ByVal File As String, ByVal B As Char, ByVal BColor As ConsoleColor, ByVal BckColor As ConsoleColor, ByVal NonBck_Color As ConsoleColor, ByVal NonBck_BckColor As ConsoleColor)
            Dim F() As String = IO.File.ReadAllLines(File)
            Dim MaxX As Integer = UBound(F)
            Dim MaxY As Integer
            For i = 0 To UBound(F)
                If i = 0 Then MaxY = F(i)
                If Len(F(i)) > MaxY Then MaxY = Len(F(i))
            Next
            ReDim XY(MaxX, MaxY)
            ReDim XYColor(MaxX, MaxY)
            ReDim XYBColor(MaxX, MaxY)
            Background_Color = BColor
            Background_BckColor = BckColor
            Background = B
            X = MaxX
            Y = MaxY
            For i = 0 To UBound(F)
                Dim tmp() As Byte = Encoding.ASCII.GetBytes(F(i))
                For j = 0 To UBound(tmp)
                    If Chr(tmp(j)) = " " Then
                        Me.XY(i, j) = B
                        Me.XYColor(i, j) = BColor
                        Me.XYBColor(i, j) = BckColor
                    Else
                        Me.XY(i, j) = Chr(tmp(j))
                        Me.XYColor(i, j) = NonBck_Color
                        Me.XYBColor(i, j) = NonBck_BckColor
                    End If
                Next
            Next
        End Sub
        Public Function DrawOnMemory() As String()
            Dim XY_Tmp As String
            Dim Ret As String
            For i = 0 To X
                XY_Tmp = Nothing
                For j = 0 To Y
                    XY_Tmp = XY_Tmp & XY(i, j)
                Next
                If Ret = Nothing Then
                    Ret = XY_Tmp
                Else
                    Ret = Ret & vbNewLine & XY_Tmp
                End If
            Next
            Return Split(Ret, vbNewLine)
        End Function
        Public Sub DrawOnScreen()
            Console.CursorVisible = False
            Dim XY_Tmp As String
            Dim Ret As String
            Dim Tmp() As Byte
            Dim Str() As String
            For i = 0 To X
                XY_Tmp = Nothing
                For j = 0 To Y
                    XY_Tmp = XY_Tmp & XY(i, j)
                Next
                If Ret = Nothing Then
                    Ret = XY_Tmp
                Else
                    Ret = Ret & vbNewLine & XY_Tmp
                End If
            Next
            Str = Split(Ret, vbNewLine)
            For i = 0 To UBound(Str)
                Tmp = Encoding.ASCII.GetBytes(Str(i))
                For j = 0 To UBound(Tmp)
                    Console.CursorTop = i
                    Console.CursorLeft = j
                    Console.ForegroundColor = XYColor(i, j)
                    Console.BackgroundColor = XYBColor(i, j)
                    Console.Write(XY(i, j))
                Next
            Next
        End Sub
    End Class

    Public Class Graphic
        Public Enum Directions As Integer
            Up
            Down
            Left
            Right
        End Enum
        Public X As Integer
        Public Y As Integer
        Public Color As ConsoleColor
        Public BColor As ConsoleColor
        Public Character As Char
        Public Map_ As Map
        Private Life_ As Integer
        Private Xn As Integer
        Private Yn As Integer
        Private LP As Integer
        Private Mx As Integer
        Private ColChar As Char
        Private IsOnMove As Boolean
        Public Event Collision(ByVal X As Integer, ByVal Y As Integer, ByVal C As Char)
        Public Event LifeChange(ByVal Value As Integer, ByVal Percent As Integer)
        Public Event Life0()
        Public Event LifeMax()
        Public ReadOnly Property Life() As Integer
            Get
                Return Life_
            End Get
        End Property
        Public Sub SetLife(ByVal Value As Integer)
            Life_ = Value
            Mx = Value
            LP = 100
            RaiseEvent LifeChange(Value, 100)
        End Sub
        Public Sub LessLife(ByVal Value As Integer)
            If Life_ - Value >= 0 Then
                Life_ = Life_ - Value
            Else
                Life_ = 0
                RaiseEvent Life0()
            End If
            LP = (Life_ / Mx) * 100
            RaiseEvent LifeChange(Life_, LP)
        End Sub
        Public Sub MoreLife(ByVal Value As Integer)
            If Life_ + Value <= Mx Then
                Life_ = Life_ + Value
            Else
                Life_ = Mx
                RaiseEvent LifeMax()
            End If
            LP = (Life_ / Mx) * 100
            RaiseEvent LifeChange(Life_, LP)
        End Sub
        Public Sub Restore()
            If Map_.XY(X, Y) <> Me.Character Then
                Map_.XY(X, Y) = Me.Character
            End If
        End Sub
        Public Sub New(ByVal Map As Map, ByVal X As Integer, ByVal Y As Integer, ByVal C As Char, ByVal Life As Integer, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            Map.XY(X, Y) = C
            Map.XYColor(X, Y) = Color
            Map.XYBColor(X, Y) = BColor
            Me.X = X
            Me.Y = Y
            Me.Character = C
            Me.Map_ = Map
            Me.Life_ = Life
            Me.LP = 100
            Me.Mx = Life
            Me.BColor = BColor
        End Sub
        Public Sub Morph(ByVal C As Char, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            Map_.XY(X, Y) = C
            Map_.XYColor(X, Y) = Color
            Map_.XYBColor(X, Y) = BColor
            Me.Character = C
            Me.Color = Color
            Me.BColor = BColor
        End Sub
        Public Sub Morph(ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            Map_.XYColor(X, Y) = Color
            Map_.XYBColor(X, Y) = BColor
            Me.Color = Color
            Me.BColor = BColor
        End Sub
        Public Sub Morph(ByVal BColor As ConsoleColor)
            Map_.XYBColor(X, Y) = BColor
            Me.BColor = BColor
        End Sub
        Public Sub Move(ByVal Xn As Integer, ByVal Yn As Integer)
            Try
                If Map_.XY(Xn, Yn) <> Map_.Background Then
                    Me.Xn = Xn
                    Me.Yn = Yn
                    Me.ColChar = Map_.XY(Xn, Yn)
                    IsOnMove = True
                    RaiseEvent Collision(Xn, Yn, Map_.XY(Xn, Yn))
                    Exit Sub
                End If
                If Me.X = Me.Xn Then
                    If Me.Y = Me.Yn Then
                        Map_.XY(X, Y) = Me.ColChar
                        Map_.XY(Xn, Yn) = Character
                        X = Xn
                        Y = Yn
                        Exit Sub
                    End If
                End If
                Map_.XY(X, Y) = Map_.Background
                Map_.XY(Xn, Yn) = Character
                X = Xn
                Y = Yn
            Catch ex As Exception
                Me.Xn = Xn
                Me.Yn = Yn
                Me.ColChar = Nothing
                IsOnMove = True
                RaiseEvent Collision(Xn, Yn, Map_.XY(Xn, Yn))
            End Try
        End Sub
        Public Sub ContinueMove()
            Map_.XY(X, Y) = Map_.Background
            Map_.XY(Xn, Yn) = Character
            X = Xn
            Y = Yn
            IsOnMove = False
        End Sub
        Public Sub CancelMove()
            IsOnMove = False
        End Sub
    End Class

    Public Class BackgroundGraphic
        Public Event Collision(ByVal Map As Map, ByVal X As Integer, ByVal Y As Integer, ByVal CollisionChar As Char)
        Public Enum Directions As Integer
            Up
            Down
            Left
            Right
        End Enum
        Public Sub DrawPoint(ByVal Map As Map, ByVal X As Integer, ByVal Y As Integer, ByVal C As Char, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            If Map.XY(X, Y) <> Map.Background Then
                RaiseEvent Collision(Map, X, Y, Map.XY(X, Y))
            Else
                Map.XY(X, Y) = C
                Map.XYColor(X, Y) = Color
                Map.XYBColor(X, Y) = BColor
            End If
        End Sub
        Public Sub DrawLine(ByVal Map As Map, ByVal X As Integer, ByVal Y As Integer, ByVal Len As Integer, ByVal C As Char, ByVal Direction As Directions, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            Try
                Select Case Direction
                    Case Directions.Up
                        For i = X To X - (Len - 1)
                            If Map.XY(i, Y) <> Map.Background Then
                                Continue For
                            Else
                                Map.XY(i, Y) = C
                                Map.XYColor(i, Y) = Color
                                Map.XYBColor(i, Y) = BColor
                            End If
                        Next
                    Case Directions.Down
                        For i = X To X + (Len - 1)
                            If Map.XY(i, Y) <> Map.Background Then
                                Continue For
                            Else
                                Map.XY(i, Y) = C
                                Map.XYColor(i, Y) = Color
                                Map.XYBColor(i, Y) = BColor
                            End If
                        Next
                    Case Directions.Left
                        For i = Y To Y - (Len - 1)
                            If Map.XY(X, i) <> Map.Background Then
                                Continue For
                            Else
                                Map.XY(X, i) = C
                                Map.XYColor(X, i) = Color
                                Map.XYBColor(X, i) = BColor
                            End If
                        Next
                    Case Directions.Right
                        For i = Y To Y + (Len - 1)
                            If Map.XY(X, i) <> Map.Background Then
                                Continue For
                            Else
                                Map.XY(X, i) = C
                                Map.XYColor(X, i) = Color
                                Map.XYBColor(X, i) = BColor
                            End If
                        Next
                End Select
            Catch ex As Exception
            End Try
        End Sub
        Public Sub DrawSquare(ByVal Map As Map, ByVal X As Integer, ByVal Y As Integer, ByVal SideA As Integer, ByVal SideB As Integer, ByVal C As Char, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            DrawLine(Map, X, Y, SideA, C, Directions.Right, Color, BColor)
            DrawLine(Map, X, Y, SideB, C, Directions.Down, Color, BColor)
            DrawLine(Map, X, Y + (SideA - 1), SideB, C, Directions.Down, Color, BColor)
            DrawLine(Map, X + (SideB - 1), Y, SideA, C, Directions.Right, Color, BColor)
        End Sub
        Public Sub DrawSquare(ByVal Map As Map, ByVal X As Integer, ByVal Y As Integer, ByVal SideA As Integer, ByVal SideB As Integer, ByVal C As Char, ByVal FillC As Char, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            DrawLine(Map, X, Y, SideA, C, Directions.Right, Color, BColor)
            DrawLine(Map, X, Y, SideB, C, Directions.Down, Color, BColor)
            DrawLine(Map, X, Y + (SideA - 1), SideB, C, Directions.Down, Color, BColor)
            DrawLine(Map, X + (SideB - 1), Y, SideA, C, Directions.Right, Color, BColor)
            For i = X To SideB
                For j = Y To SideA
                    Map.XY(i, j) = FillC
                Next
            Next
        End Sub
        Public Sub DrawFile(ByVal Map As Map, ByVal File As String, ByVal Color As ConsoleColor, ByVal BColor As ConsoleColor)
            Dim F() As String = IO.File.ReadAllLines(File)
            For i = 0 To UBound(F)
                Dim tmp() As Byte = Encoding.ASCII.GetBytes(F(i))
                For j = 0 To UBound(tmp)
                    If Chr(tmp(j)) = " " Then
                        Map.XY(i, j) = Map.Background
                        Map.XYColor(i, j) = Map.Background_Color
                        Map.XYBColor(i, j) = Map.Background_BckColor
                    Else
                        Map.XY(i, j) = Chr(tmp(j))
                        Map.XYColor(i, j) = Color
                        Map.XYBColor(i, j) = BColor
                    End If
                Next
            Next
        End Sub
    End Class

    Public Class Menus
        Function CreateMenu(ByVal SelChar1 As Char, ByVal SelChar2 As Char, ByVal Title As String, ByVal Options() As String, ByVal TitleFColor As System.ConsoleColor, ByVal TitleBColor As System.ConsoleColor, ByVal OptionsSelFColor As System.ConsoleColor, ByVal OptionsSelBColor As System.ConsoleColor, ByVal OptionsUnselFColor As System.ConsoleColor, ByVal OptionsUnselBColor As System.ConsoleColor) As Integer
            Dim Rescue() As String = Options
            Options(0) = SelChar1 & " " & LTrim(RTrim(Options(0))) & " " & SelChar2
96:
            Console.ResetColor()
            Console.Clear()
            Console.ForegroundColor = TitleFColor
            Console.BackgroundColor = TitleBColor
            Console.WriteLine(Title)
            For i = 0 To UBound(Options)
                If InStr(Options(i), SelChar1) = 1 Then
                    Console.BackgroundColor = OptionsSelBColor
                    Console.ForegroundColor = OptionsSelFColor
                Else
                    Console.BackgroundColor = OptionsUnselBColor
                    Console.ForegroundColor = OptionsUnselFColor
                End If
                Console.WriteLine(Options(i))
            Next
            Dim Str As ConsoleKey = Console.ReadKey(True).Key
            Select Case Str
                Case ConsoleKey.UpArrow
                    For i = 0 To UBound(Options)
                        If InStr(Options(i), SelChar1 & " ") = 1 Then
                            If i = 0 Then
                                Continue For
                            End If
                            Options(i) = Replace(Options(i), SelChar1 & " ", "  ")
                            Options(i) = Replace(Options(i), " " & SelChar2, "  ")
                            Options(i - 1) = SelChar1 & " " & RTrim(LTrim(Options(i - 1))) & " " & SelChar2
                            GoTo 96
                        End If
                    Next
                Case ConsoleKey.DownArrow
                    For i = 0 To UBound(Options)
                        If InStr(Options(i), SelChar1 & " ") = 1 Then
                            If i = UBound(Options) Then
                                Continue For
                            End If
                            Options(i) = Replace(Options(i), SelChar1 & " ", "  ")
                            Options(i) = Replace(Options(i), " " & SelChar2, "  ")
                            Options(i + 1) = SelChar1 & " " & LTrim(RTrim(Options(i + 1))) & " " & SelChar2
                            GoTo 96
                        End If
                    Next
                Case ConsoleKey.Enter
                    For i = 0 To UBound(Options)
                        If InStr(Options(i), SelChar1 & " ") = 1 Then
                            Options = Rescue
                            Return i
                        End If
                    Next
            End Select
            GoTo 96
        End Function
    End Class
End Namespace

Como ya sabrán, solo es necesario crear una nueva clase a su proyecto (debe ser tipo consola), copiar y pegar el codigo del engine y disfrutar.

Ejemplo del uso del engine: (Solo para probar su funcionalidad)

Código:

Module Module1
    Dim WithEvents Obj As AscIIGE.Graphic
    Sub Main()
        Dim Mapa As New AscIIGE.Map(10, 10, "-", ConsoleColor.White, ConsoleColor.Black)
        Obj = New AscIIGE.Graphic(Mapa, 5, 5, "*", 100, ConsoleColor.Black, ConsoleColor.Blue)
draw:
        Mapa.DrawOnScreen()
        Console.ReadKey(True)
        Obj.LessLife(10)
        GoTo draw
    End Sub

    Private Sub Obj_Life0() Handles Obj.Life0
        Console.Clear()
        Console.WriteLine("You die...")
        Console.ReadKey()
        End
    End Sub

    Private Sub Obj_LifeChange(ByVal Value As Integer, ByVal Percent As Integer) Handles Obj.LifeChange
        Select Case Percent
            Case 100
                Obj.Morph(ConsoleColor.Blue)
            Case 90
                Obj.Morph(ConsoleColor.Blue)
            Case 80
                Obj.Morph(ConsoleColor.Blue)
            Case 70
                Obj.Morph(ConsoleColor.Yellow)
            Case 60
                Obj.Morph(ConsoleColor.Yellow)
            Case 50
                Obj.Morph(ConsoleColor.Yellow)
            Case 40
                Obj.Morph(ConsoleColor.Red)
            Case 30
                Obj.Morph(ConsoleColor.Red)
            Case 20
                Obj.Morph(ConsoleColor.Red)
            Case 10
                Obj.Morph(ConsoleColor.White)
        End Select
    End Sub
End Module

No se cantos caractéres pueda tener mi post, así que sigo a continuación...

Desconectado

 

#2 2009-04-16 17:22:11

DeathLikeGhost
Nuevo Usuario
Ubicación: Guadalajara, Jalisco, México.
Registrado: 2009-04-16
Mensajes: 2

Re: AscII Game Engine

Bueno, como dije antes, aqui sigo.

Clases del engine:

Map:
Como su nombre lo indica, representa un mapa. Esta clase no tiene nada más que dos sobrecargas en la sección de código "new", una para crear el mapa a partir de valores que le indicarás: Valor máximo de X, Valor máximo de Y, Caractér de fondo, Color del caracter de fondo y Color de fondo del caracter de fondo.

La otra sobrecarga es para crear un mapa a partir de un archivo. Sus argumentos son: Archivo, Caracter de fondo, Color del caracter de fondo y el color de fondo del caracter de fondo (:S), tambien el color de los caracteres que sean diferentes al caracter de fondo, y su color de fondo.

El archivo de mapa debería quedar algo así:

(_ es un espacio)

Código:

_____
#####
#___#
#___#
#####

Los espacios son convertidos al caracter de fondo.

Función DrawOnMemory:

Esta función lee el mapa y lo transforma a una matriz de tipo string nada más... sin colores ni nada.

Sección de código DrawOnScreen:

Esto lee el mapa y lo dibuja en la pantalla (consola, como lo quieran ver) con todo y colors.


Clase Graphic:

Esta clase representa un gráfico en el mapa que se desee, no es cualquier gráfico, si no como un objeto que puedes mover por todo el mapa. Este mentado gráfico es de sólo un caracter.

El contructor (sub new) necesita los siguientes argumentos para hacer funcionar el gráfico:

Mapa (un mapa donde estar), X (pocision x del mapa), Y (pocision Y del mapa), su gráfico (un caracter), "Vida", Color, Color de fondo.

Esta clase debe ser declarada con soporte para eventos, ejemplo:

Código:

 Dim WithEvents G as new Graphic(argumentos...)

Propiedad Life:

Esta propiedad representa un integer, y no necesariamente tiene que se la "vida" del objeto, puede ser utilizado como quieras. El valor de esta propiedad no se aplica por defecto, ni se cambia automáticamente al pasar algo, si no que es totalmente manipulable.

Life es privada, así que tal vez no la vean. Para reducir su valor usen la función "LessLife", esta tiene solo un parametro, que es la cantidad a disminuir, para aumentarlo usar "MoreLife" y "SetLife" para cambiarlo a lo que quieran (Tener en cuenta que si cambian este valor, se tomará también como su "vida" máxima).

Esta propiedad desencadena 3 eventos, "LifeChange(Valor, Porcentaje)", que se activa cuando la vida es cambiada, "Life0()", que se activa cuando la vida llega a 0, y "LifeMax()", por si la vida llega a su máximo.

Función restore:

Esta funcion solo tiene un objetivo, restaurarse "físicamente" en el mapa por si desaparece.

Nota: No es automático

Función Morph:

Modifica el grafico a gusto... tiene tres sobrecargas:

Morph(Caracter, Color, Color de fondo)
Morph(Caracter, Color)
Morph(Caracter)

Función Move:

Mueve el caracter de su pocición a otra.

Parámetros:
Pocision X, Pocision Y.

Esta funcion desencadena el último evento, "Collision", este se activa cuando el grafico choca contra algo diferente al fondo.

Collision(X, Y, Caracter)

Cuando se activa "pausa" la acción de moverse, si quieres continuar usa la función "ContinueMove", si no usa "Cancel Move".


Clase BackgroundGraphic:

Esta clase es para dibujar en el mapa, solo usa un evento y no tiene un "sub new" o contructor.

Función DrawPoint:

Dibuja un simple caracter en la pocicion X y Y con el color y color de fondo especificado en el mapa que quieras.

DrawPoint(Mapa, X, Y, Caracter, Color, Color de fondo)

Si la pocisión X, Y está ya ocupada activa el evento Collision y aborta.

Función DrawLine:

Dibuja una linea.

DrawLine(Mapa, X, Y, Tamaño, Caracter, Dirección, Color, Color de fondo)

Funcion DrawSquare:

Dibuja un cuadro, tiene una sobrecarga.

DrawSquare(Mapa, X, Y, Tamaño linea "A", Tamaño linea "B", Caracter, Color, color de fondo)

Su sobrecarga solo agrega la opcion "Fill", un boolean, que te pregunta si llenar o no el cuadro.

Función DrawFile:

Dibuja un archivo de mapa en el mapa. No redimensionará el mapa para nada.

Parámetros:

Archivo, Color, Color de fondo.

Clase Menus:

Esta clase te ayudará enormemente para crear menús interactivos, tengamos en cuenta que tu juego será en consola.

Parámetros:

Caracter de selección 1, Caracter de selección 2, Titulo, Opciones (Arreglo, Matriz, tipo string), Color del título, Color de fondo del título, Color de selección, Color de fondo de selección, Color de no seleccionado, Color de fondo de no seleccionado.

Desconectado

 

Pie del foro

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson