Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Breakout - GameObjects.vb

GameObjects.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports Microsoft.Xna.Framework
  2. Imports Microsoft.Xna.Framework.Graphics
  3.  
  4. Public Class GameObject
  5.     Protected _Position As Vector2
  6.     Protected _Velocity As Vector2
  7.     Protected _Rotation As Single
  8.     Protected _Center As Vector2
  9.     Protected _Sprite As Texture2D
  10.     Protected _Color As Color
  11.     Protected _Scale As Single
  12.     Protected _Enabled As Boolean = True
  13.  
  14.     Public Overridable Property Position() As Vector2
  15.         Get
  16.             Return _Position
  17.         End Get
  18.         Set(ByVal Value As Vector2)
  19.             _Position = Value
  20.         End Set
  21.     End Property
  22.  
  23.     Public Overridable Property Velocity() As Vector2
  24.         Get
  25.             Return _Velocity
  26.         End Get
  27.         Set(ByVal Value As Vector2)
  28.             _Velocity = Value
  29.         End Set
  30.     End Property
  31.  
  32.     Public Overridable Property Rotation() As Single
  33.         Get
  34.             Return _Rotation
  35.         End Get
  36.         Set(ByVal Value As Single)
  37.             _Rotation = Value
  38.         End Set
  39.     End Property
  40.  
  41.     Public Overridable Property Center() As Vector2
  42.         Get
  43.             Return _Center
  44.         End Get
  45.         Set(ByVal Value As Vector2)
  46.             _Center = Value
  47.         End Set
  48.     End Property
  49.  
  50.     Public Overridable Property Sprite() As Texture2D
  51.         Get
  52.             Return _Sprite
  53.         End Get
  54.         Set(ByVal value As Texture2D)
  55.             _Sprite = value
  56.         End Set
  57.     End Property
  58.  
  59.     Public Overridable Property Color() As Color
  60.         Get
  61.             Return _Color
  62.         End Get
  63.         Set(ByVal value As Color)
  64.             _Color = value
  65.         End Set
  66.     End Property
  67.  
  68.     Public Overridable Property Scale() As Single
  69.         Get
  70.             Return _Scale
  71.         End Get
  72.         Set(ByVal value As Single)
  73.             _Scale = value
  74.         End Set
  75.     End Property
  76.  
  77.     Public Overridable Property Enabled() As Boolean
  78.         Get
  79.             Return _Enabled
  80.         End Get
  81.         Set(ByVal value As Boolean)
  82.             _Enabled = value
  83.         End Set
  84.     End Property
  85.  
  86.     Public Overridable Sub Draw(ByVal Batch As SpriteBatch)
  87.         If Me.Enabled Then
  88.             Batch.Draw(Me.Sprite, Me.Position, Nothing, Me.Color, Me.Rotation, Me.Center, Me.Scale, SpriteEffects.None, 0)
  89.         End If
  90.     End Sub
  91.  
  92.     Public Overridable Sub UpdatePosition()
  93.         Me.Position += Me.Velocity
  94.     End Sub
  95.  
  96.     Sub New()
  97.         Me.Color = Graphics.Color.White
  98.         Me.Position = Vector2.Zero
  99.         Me.Rotation = 0
  100.         Me.Scale = 1.0
  101.         Me.Velocity = Vector2.Zero
  102.     End Sub
  103.  
  104.     Sub New(ByVal Sprite As Texture2D)
  105.         Me.New()
  106.         Me.Sprite = Sprite
  107.         Me.Center = Me.Position + New Vector2(Me.Sprite.Width * Me.Scale / 2, Me.Sprite.Height * Me.Scale / 2)
  108.     End Sub
  109. End Class
  110.  
  111. Public Class GameText
  112.     Inherits GameObject
  113.  
  114.     Private _Font As Drawing.Font
  115.     Private _Text As String
  116.  
  117.     Public Property Font() As Drawing.Font
  118.         Get
  119.             Return _Font
  120.         End Get
  121.         Set(ByVal value As Drawing.Font)
  122.             _Font = value
  123.         End Set
  124.     End Property
  125.  
  126.     Public Property Text() As String
  127.         Get
  128.             Return _Text
  129.         End Get
  130.         Set(ByVal value As String)
  131.             _Text = value
  132.         End Set
  133.     End Property
  134.  
  135.     Sub New()
  136.         MyBase.New()
  137.     End Sub
  138.  
  139.     Public Sub CreateSprite(ByVal Device As GraphicsDevice)
  140.         Dim B As New Drawing.Bitmap(100, 100)
  141.         Dim G As Drawing.Graphics = Drawing.Graphics.FromImage(B)
  142.         Dim StringSize As Drawing.SizeF = G.MeasureString(Me.Text, Me.Font)
  143.         Dim DrawingColor As Drawing.Color = Drawing.Color.FromArgb(Me.Color.A, Me.Color.R, Me.Color.G, Me.Color.B)
  144.         Dim Stream As IO.MemoryStream
  145.  
  146.         B = New Drawing.Bitmap(CInt(StringSize.Width), CInt(StringSize.Height))
  147.         G = Drawing.Graphics.FromImage(B)
  148.         G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
  149.         G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
  150.         G.DrawString(Me.Text, Me.Font, New Drawing.SolidBrush(DrawingColor), 0, 0)
  151.  
  152.         Stream = New IO.MemoryStream
  153.         B.Save(Stream, Drawing.Imaging.ImageFormat.Png)
  154.         Stream.Position = 0
  155.  
  156.         Me.Sprite = Texture2D.FromFile(Device, Stream)
  157.         Stream.Close()
  158.     End Sub
  159. End Class
  160.  
  161. Public Class Block
  162.     Inherits GameObject
  163.  
  164.     Private _Life As Int16 = 1
  165.     Private _Name As String
  166.  
  167.     Public Property Life() As Int16
  168.         Get
  169.             Return _Life
  170.         End Get
  171.         Set(ByVal value As Int16)
  172.             _Life = value
  173.         End Set
  174.     End Property
  175.  
  176.     Public Property Name() As String
  177.         Get
  178.             Return _Name
  179.         End Get
  180.         Set(ByVal value As String)
  181.             _Name = value
  182.         End Set
  183.     End Property
  184.  
  185.     Sub New()
  186.         MyBase.new()
  187.     End Sub
  188.  
  189.     Sub New(ByVal Sprite As Texture2D)
  190.         Me.New()
  191.         Me.Sprite = Sprite
  192.         Me.Center = Me.Position + New Vector2(Me.Sprite.Width * Me.Scale / 2, Me.Sprite.Height * Me.Scale / 2)
  193.     End Sub
  194. End Class