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 - Game.vb

Game.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports Microsoft.Xna.Framework
  2. Imports Microsoft.Xna.Framework.Input
  3. Imports Microsoft.Xna.Framework.Graphics
  4. Imports Microsoft.DirectX.AudioVideoPlayback
  5. Imports DistortionPipeline
  6. Imports DistortionSample
  7.  
  8. Public Class Breakout
  9.     Inherits Microsoft.Xna.Framework.Game
  10.  
  11.     Private Enum BonusType
  12.         LongBat
  13.         BigBall
  14.         QuickBat
  15.         SlowBall
  16.         DoubleBall
  17.         Super
  18.     End Enum
  19.  
  20.     Public Const DefaultWidth As Int32 = 1024
  21.     Public Const DefaultHeight As Int32 = 768
  22.  
  23.     Private BatSpeed As Single = 4
  24.     Private BallSpeed As Single = 4
  25.  
  26.     Private Graphics As GraphicsDeviceManager
  27.     Private Batch As SpriteBatch
  28.     Private SubTitle As GameText
  29.  
  30.     Private Title As GameObject
  31.     Private Background As GameObject
  32.     Private BackgroundMusic As Audio
  33.  
  34.     Private Level As Int16 = 0
  35.     Private Lives As Int16 = 3
  36.     Private LevelCompleted As Boolean = True
  37.     Private GameCompleted As Boolean = False
  38.  
  39.     Private Bat As GameObject
  40.     Private Ball As GameObject
  41.     Private Blocks As New List(Of Block)
  42.     Private Super As Boolean = False
  43.     Private Angle As Single = 0
  44.  
  45.     Private _BonusTime(5) As Int16
  46.  
  47.     Private Property BonusTime(ByVal Type As BonusType) As Int16
  48.         Get
  49.             Return _BonusTime(Type)
  50.         End Get
  51.         Set(ByVal value As Int16)
  52.             _BonusTime(Type) = value
  53.         End Set
  54.     End Property
  55.  
  56.     Sub New()
  57.         Me.Graphics = New GraphicsDeviceManager(Me)
  58.         Me.Content.RootDirectory = "content"
  59.     End Sub
  60.  
  61.     Private Function GetTexture(ByVal Name As String) As Texture2D
  62.         Return Texture2D.FromFile(Me.Graphics.GraphicsDevice, My.Application.Info.DirectoryPath & "\" & Name)
  63.     End Function
  64.  
  65.     Private Function GetBounds(ByVal Obj As GameObject) As Rectangle
  66.         Return New Rectangle(Obj.Position.X - (Obj.Sprite.Width / 2), Obj.Position.Y - (Obj.Sprite.Height / 2), Obj.Sprite.Width, Obj.Sprite.Height)
  67.     End Function
  68.  
  69.     Private Sub PlaySound(ByVal Name As String)
  70.         My.Computer.Audio.Play(My.Application.Info.DirectoryPath & "\" & Name, AudioPlayMode.Background)
  71.     End Sub
  72.  
  73.     Private Sub CalculateBallVelocity(ByVal Obj As GameObject)
  74.         Dim DeltaX As Single = Ball.Position.X - Obj.Position.X
  75.         Dim DevAngle As Single = Math.Abs(DeltaX) * ((Math.PI / 3) / (Obj.Sprite.Width / 2))
  76.         Dim AngleOnX As Single = MathHelper.PiOver2 - Math.Abs(DevAngle)
  77.  
  78.         Dim YComponent As Single = BallSpeed * Math.Sin(AngleOnX)
  79.         Dim XComponent As Single = BallSpeed * Math.Cos(AngleOnX)
  80.  
  81.         If DeltaX < 0 Then
  82.             XComponent = -XComponent
  83.         End If
  84.  
  85.         If Ball.Position.Y <= Obj.Position.Y Then
  86.             Ball.Velocity = New Vector2(XComponent, -YComponent)
  87.         Else
  88.             Ball.Velocity = New Vector2(XComponent, YComponent)
  89.         End If
  90.     End Sub
  91.  
  92.     Private Sub CollisionCheck()
  93.         Dim BallRect As Rectangle = GetBounds(Ball)
  94.         Dim BatRect As Rectangle = GetBounds(Bat)
  95.         Dim Bounced As Boolean = False
  96.  
  97.         If BallRect.Intersects(BatRect) Then
  98.             CalculateBallVelocity(Bat)
  99.             PlaySound("Boink1.wav")
  100.         End If
  101.  
  102.         If ((Ball.Position.X < 5) Or (Ball.Position.X > Me.Graphics.GraphicsDevice.Viewport.Width - 5)) _
  103.             And (Ball.Position.Y < Me.Graphics.GraphicsDevice.Viewport.Height - 5) Then
  104.             Ball.Velocity = New Vector2(-Ball.Velocity.X, Ball.Velocity.Y)
  105.             PlaySound("Boink1.wav")
  106.         End If
  107.         If Ball.Position.Y < 5 Then
  108.             Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
  109.             PlaySound("Boink1.wav")
  110.         End If
  111.  
  112.         If Ball.Position.Y > Me.Graphics.GraphicsDevice.Viewport.Height Then
  113.             If (Not Super) Then
  114.                 PlaySound("Beep.wav")
  115.                 Lives -= 1
  116.                 Ball.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2, Me.Graphics.GraphicsDevice.Viewport.Height / 2)
  117.                 Ball.Velocity = New Vector2(0, 1)
  118.             Else
  119.                 Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
  120.                 PlaySound("Boink1.wav")
  121.             End If
  122.         End If
  123.  
  124.         Dim HitBlocks As New List(Of Block)
  125.         For Each Block As Block In Blocks
  126.             Dim BlockRect As Rectangle = GetBounds(Block)
  127.             If BallRect.Intersects(BlockRect) And (Not Bounced) Then
  128.                 If (Ball.Position.X > Block.Position.X + Block.Sprite.Width / 2) Or _
  129.                    (Ball.Position.X < Block.Position.X - Block.Sprite.Width / 2) Then
  130.                     Ball.Velocity = New Vector2(-Ball.Velocity.X, Ball.Velocity.Y)
  131.                 Else
  132.                     Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
  133.                 End If
  134.                 Bounced = True
  135.                 Block.Life -= 1
  136.                 PlaySound("Boink1.wav")
  137.                 If Block.Life = 0 Then
  138.                     Block.Enabled = False
  139.                     HitBlocks.Add(Block)
  140.                     If Block.Name.Contains("Bonus") Then
  141.                         ActivateBonus(Block.Name)
  142.                     End If
  143.                 Else
  144.                     Block.Sprite = GetTexture("Block" & Block.Life.ToString.PadLeft(2, "0") & ".png")
  145.                 End If
  146.             End If
  147.         Next
  148.  
  149.         For Each Block As Block In HitBlocks
  150.             Blocks.Remove(Block)
  151.         Next
  152.  
  153.         If Lives = 0 Then
  154.             SubTitle.Text = "Peccato! Hai perso!"
  155.             SubTitle.CreateSprite(Me.Graphics.GraphicsDevice)
  156.             SubTitle.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2 - SubTitle.Sprite.Width / 2, 300)
  157.             LevelCompleted = True
  158.             GameCompleted = True
  159.         End If
  160.     End Sub
  161.  
  162.     Private Sub KeyboardCheck()
  163.         Dim KeyState As KeyboardState = Keyboard.GetState
  164.  
  165.         If KeyState.IsKeyDown(Keys.Right) Then
  166.             If Bat.Position.X + Bat.Sprite.Width / 2 < Me.GraphicsDevice.Viewport.Width Then
  167.                 Bat.Position += New Vector2(BatSpeed, 0)
  168.             End If
  169.         End If
  170.         If KeyState.IsKeyDown(Keys.Left) Then
  171.             If Bat.Position.X - Bat.Sprite.Width / 2 > 0 Then
  172.                 Bat.Position += New Vector2(-BatSpeed, 0)
  173.             End If
  174.         End If
  175.         If KeyState.IsKeyDown(Keys.Escape) Then
  176.             Me.Exit()
  177.         End If
  178.         If KeyState.IsKeyDown(Keys.F2) Then
  179.             'Blocks.Clear()
  180.         End If
  181.     End Sub
  182.  
  183.     Private Sub AdvanceLevel()
  184.         Level += 1
  185.         LevelCompleted = False
  186.  
  187.         If Level = 1 Then
  188.             SubTitle.Text = "Premi invio per continuare"
  189.             SubTitle.CreateSprite(Me.Graphics.GraphicsDevice)
  190.             SubTitle.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2 - SubTitle.Sprite.Width / 2, 300)
  191.             'BackgroundMusic = New Audio(My.Application.Info.DirectoryPath & "\Rapsodia.mp3")
  192.             'BackgroundMusic.Volume = 0
  193.             'BackgroundMusic.Play()
  194.             'Togliete i commenti per avviare la musica. Il file NON č incluso nel paccheto
  195.             'perchč pesa troppo. Potete comunque usarne uno voi
  196.         End If
  197.  
  198.         Try
  199.             Dim Reader As New IO.StreamReader(My.Application.Info.DirectoryPath & "\Level" & Level & ".lvl")
  200.             Dim Line As String
  201.  
  202.             Do While Not Reader.EndOfStream
  203.                 Line = Reader.ReadLine
  204.  
  205.                 If String.IsNullOrEmpty(Line) Then
  206.                     Continue Do
  207.                 End If
  208.  
  209.                 Dim Vals() As String = Line.Split("=")
  210.                 Dim Name As String = Vals(0).Trim
  211.                 Dim Arg As String = Vals(1).Trim
  212.  
  213.                 If Arg.Contains(",") Then
  214.                     Dim Coords() As String = Arg.Split(",")
  215.                     Dim CoX As String = Coords(0).Trim
  216.                     Dim CoY As String = Coords(1).Trim
  217.                     Dim Area As New Rectangle
  218.  
  219.                     If CoX.Contains(">") Then
  220.                         Dim Xs() As String = CoX.Split(">")
  221.                         Dim MinX As Int16 = CInt(Xs(0).Trim)
  222.                         Dim MaxX As Int16 = CInt(Xs(1).Trim)
  223.                         Area.X = MinX
  224.                         Area.Width = MaxX - MinX
  225.                     Else
  226.                         Area.X = CInt(CoX)
  227.                         Area.Width = 0
  228.                     End If
  229.  
  230.                     If CoY.Contains(">") Then
  231.                         Dim Ys() As String = CoY.Split(">")
  232.                         Dim MinY As Int16 = CInt(Ys(0).Trim)
  233.                         Dim MaxY As Int16 = CInt(Ys(1).Trim)
  234.                         Area.Y = MinY
  235.                         Area.Height = MaxY - MinY
  236.                     Else
  237.                         Area.Y = CInt(CoY)
  238.                         Area.Height = 0
  239.                     End If
  240.  
  241.                     For X As Int16 = Area.X To (Area.X + Area.Width) Step 35
  242.                         For Y As Int16 = Area.Y To (Area.Y + Area.Height) Step 19
  243.                             Dim B As New Block(GetTexture(Name & ".png"))
  244.                             B.Position = New Vector2(X, Y)
  245.                             B.Name = Name
  246.                             If Name.StartsWith("Block") Then
  247.                                 B.Life = CInt(Name.Remove(0, Name.Length - 2))
  248.                             End If
  249.                             Blocks.Add(B)
  250.                         Next
  251.                     Next
  252.                 End If
  253.             Loop
  254.  
  255.             Reader.Close()
  256.         Catch FNFE As IO.FileNotFoundException
  257.             GameCompleted = True
  258.             SubTitle.Text = "Complimenti! Hai finito il gioco!"
  259.             SubTitle.CreateSprite(Me.Graphics.GraphicsDevice)
  260.             SubTitle.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2 - SubTitle.Sprite.Width / 2, 300)
  261.         End Try
  262.     End Sub
  263.  
  264.     Private Sub UpdateBonus()
  265.         For I As Int16 = 0 To 5
  266.             If BonusTime(I) = 1 Then
  267.                 Select Case I
  268.                     Case BonusType.LongBat
  269.                         Bat.Scale = 1
  270.                     Case BonusType.BigBall
  271.                         Ball.Scale = 1
  272.                     Case BonusType.QuickBat
  273.                         BatSpeed = 4
  274.                     Case BonusType.SlowBall
  275.                         BallSpeed = 4
  276.                     Case BonusType.DoubleBall
  277.  
  278.                     Case BonusType.Super
  279.                         BatSpeed = 4
  280.                         BallSpeed = 4
  281.                         Super = False
  282.                         For Each B As Block In Blocks
  283.                             B.Rotation = 0
  284.                         Next
  285.                         Bat.Rotation = 0
  286.                         Ball.Rotation = 0
  287.                 End Select
  288.                 Ball.Velocity = Vector2.Transform(Ball.Velocity, Matrix.CreateScale(BatSpeed / 4))
  289.             End If
  290.             If BonusTime(I) > 0 Then
  291.                 BonusTime(I) -= 1
  292.             End If
  293.         Next
  294.     End Sub
  295.  
  296.     Private Sub ActivateBonus(ByVal Name As String)
  297.         Select Case Name
  298.             Case "Bonus01"
  299.                 Bat.Scale = 2
  300.                 BonusTime(BonusType.LongBat) = 1000
  301.             Case "Bonus02"
  302.                 Ball.Scale = 2
  303.                 BonusTime(BonusType.BigBall) = 1000
  304.             Case "Bonus03"
  305.                 BatSpeed = 6
  306.                 BonusTime(BonusType.QuickBat) = 1000
  307.             Case "Bonus04"
  308.                 BallSpeed = 2
  309.                 BonusTime(BonusType.SlowBall) = 1000
  310.             Case "Bonus05"
  311.                 BonusTime(BonusType.DoubleBall) = 1000
  312.             Case "Bonus06"
  313.                 BatSpeed = 9
  314.                 BallSpeed = 9
  315.                 Super = True
  316.                 BonusTime(BonusType.Super) = 500
  317.         End Select
  318.         Ball.Velocity = Vector2.Transform(Ball.Velocity, Matrix.CreateScale(BatSpeed / 4))
  319.     End Sub
  320.  
  321.     ''' <summary>
  322.     ''' Inizializza gli oggetti della classe.
  323.     ''' </summary>
  324.     Protected Overrides Sub Initialize()
  325.         Batch = New SpriteBatch(Me.Graphics.GraphicsDevice)
  326.         MyBase.Initialize()
  327.     End Sub
  328.  
  329.     ''' <summary>
  330.     ''' Carica tutte le risorse che occorrono (texture, modelli 3D, suoni, ecc...).
  331.     ''' </summary>
  332.     Protected Overrides Sub LoadContent()
  333.         Background = New GameObject(GetTexture("Background.png"))
  334.         Background.Position = Background.Center
  335.  
  336.         Dim Size As New Viewport
  337.         Size.Width = Background.Sprite.Width
  338.         Size.Height = Background.Sprite.Height
  339.         Size.X = 0
  340.         Size.Y = 0
  341.         Me.Graphics.GraphicsDevice.Viewport = Size
  342.         Me.Graphics.PreferredBackBufferHeight = Size.Height
  343.         Me.Graphics.PreferredBackBufferWidth = Size.Width
  344.         Me.Graphics.ApplyChanges()
  345.  
  346.         Title = New GameObject(GetTexture("Title.png"))
  347.         Title.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2, 100)
  348.  
  349.         SubTitle = New GameText()
  350.         SubTitle.Font = New Drawing.Font("Monotype Corsiva", 30, Drawing.FontStyle.Italic)
  351.         SubTitle.Text = "Premi invio per iniziare"
  352.         SubTitle.CreateSprite(Me.Graphics.GraphicsDevice)
  353.         SubTitle.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2 - SubTitle.Sprite.Width / 2, 300)
  354.  
  355.         Bat = New GameObject(GetTexture("Bat.png"))
  356.         Bat.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2, Me.Graphics.GraphicsDevice.Viewport.Height - 40)
  357.  
  358.         Ball = New GameObject(GetTexture("Ball.png"))
  359.         Ball.Position = New Vector2(Me.Graphics.GraphicsDevice.Viewport.Width / 2, Me.Graphics.GraphicsDevice.Viewport.Height / 2)
  360.         Ball.Velocity = New Vector2(0, 3)
  361.  
  362.         MyBase.LoadContent()
  363.     End Sub
  364.  
  365.     ''' <summary>
  366.     ''' Rilascia tutte le risorse che non servono pių.
  367.     ''' </summary>
  368.     Protected Overrides Sub UnloadContent()
  369.         MyBase.UnloadContent()
  370.     End Sub
  371.  
  372.     ''' <summary>
  373.     ''' Aggiorna la situazione di gioco (tutti gli oggetti vengono aggiornati).
  374.     ''' </summary>
  375.     Protected Overrides Sub Update(ByVal GameTime As GameTime)
  376.         If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
  377.             Me.Exit()
  378.         End If
  379.  
  380.         If (Not LevelCompleted) Then
  381.             KeyboardCheck()
  382.         Else
  383.             If Keyboard.GetState().IsKeyDown(Keys.Enter) Then
  384.                 AdvanceLevel()
  385.             End If
  386.         End If
  387.  
  388.         CollisionCheck()
  389.         UpdateBonus()
  390.  
  391.         If (Not LevelCompleted) Or ((LevelCompleted) And (Level = 0)) Then
  392.             Ball.UpdatePosition()
  393.         End If
  394.  
  395.         If Blocks.Count = 0 Then
  396.             LevelCompleted = True
  397.         End If
  398.  
  399.         If Super Then
  400.             Angle += 0.02
  401.         End If
  402.  
  403.         MyBase.Update(GameTime)
  404.     End Sub
  405.     ''' <summary>
  406.     ''' Disegna tutti gli oggetti visibili sullo schermo.
  407.     ''' </summary>
  408.     Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
  409.         Me.Graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
  410.  
  411.         Batch.Begin()
  412.  
  413.         If Super Then
  414.             For Each B As Block In Blocks
  415.                 B.Rotation = Angle
  416.             Next
  417.             Bat.Rotation = Angle
  418.             Ball.Rotation = Angle
  419.         End If
  420.  
  421.         Background.Draw(Batch)
  422.         Bat.Draw(Batch)
  423.         Ball.Draw(Batch)
  424.  
  425.         If LevelCompleted Then
  426.             If (Level = 0) Or (GameCompleted) Then
  427.                 Title.Draw(Batch)
  428.             End If
  429.             SubTitle.Draw(Batch)
  430.         End If
  431.  
  432.         For Each Block As GameObject In Blocks
  433.             Block.Draw(Batch)
  434.         Next
  435.  
  436.         Batch.End()
  437.  
  438.         MyBase.Draw(gameTime)
  439.     End Sub
  440. End Class