SubGenius

Acid Bounce v1.0.1

Feb 24th, 2015
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 12.94 KB | None | 0 0
  1. Imports WMPLib
  2.  
  3. ' Hey there, SubGenius here.
  4. ' So, Acid Bounce is my first try into GDI gaming and I have to admit, shit is interesting.
  5. ' Here are some informations about the game:
  6. '
  7. ' The game is seperate into 3 major parts.
  8. ' 1st is the input. We read what keyboard keys the user types into the keydown sub.
  9. ' Then it's the GameTime.  A timer which makes the whole processing work. Update, check, move, etc.
  10. ' And last it's the GDI part where we take all the informations and give them shape.
  11. '
  12. ' The code is fully commented.
  13. ' Have fun playing :)
  14. '
  15. '
  16. '      Only3 is the best. <333 i luv u bby :* :*
  17. '
  18.  
  19.  
  20.  
  21. Public Class frmMain
  22.  
  23. #Region "Variables"
  24.  
  25.     'System Variables
  26.     Private WithEvents GameTimer As New Timer With {.Interval = 1}
  27.  
  28.     Private CenterScreenPoint As Point = New Point(Me.Width / 2, Me.Height / 2)
  29.     Private RandomSeed As New Random
  30.     Private MusicPlayer As New WindowsMediaPlayer ' I use media player so we can pause/change volume.
  31.     Private TotalGameTicks As Integer = 0 'We use this as a time interval to spawn the walls
  32.  
  33.     'Game Variables
  34.     Private IsGameRunning As Boolean = False
  35.     Private IsGameFinished As Boolean = False
  36.     Private IsGamePaused As Boolean = False
  37.  
  38.     'Game Score / Level
  39.     Private Score As Long = 0
  40.     Private LastGameScore As Long = 0
  41.     Private BestScore As Long = 0
  42.     Private Level As Integer = 1
  43.     Private Gravity As Integer = 0 ' 0 is for top and 1 is for bottom
  44.  
  45.     'CheatEngine Protection
  46.     Private ScoreOffset As Integer = 0
  47.  
  48.     'Game Objects
  49.     Private PlayerBox As Rectangle
  50.     Class Wall
  51.         Friend TopWall As Rectangle
  52.         Friend PassGap As Rectangle
  53.         Friend BottomWall As Rectangle
  54.         Friend PlayerIn As Boolean
  55.     End Class
  56.     Private SpawnedWalls As New List(Of Wall)
  57.  
  58.     'Game Colors                                'Caribbean green             'Electric Lime              'Jazzberry Jam                'Deep Sky Blue              'Piggy Pink                      'Crimson
  59.     Private GameColorsTable As Color() = {Color.FromArgb(0, 204, 153), Color.FromArgb(206, 255, 29), Color.FromArgb(202, 55, 103), Color.FromArgb(0, 191, 255), Color.FromArgb(253, 221, 230), Color.FromArgb(220, 20, 60)}
  60.     Private PlayerBoxColor As Color
  61.  
  62.     'Paint Objects
  63.     Private NormalBrush As Brush = New SolidBrush(Color.FromArgb(32, 32, 39))
  64.     Private NormalBorderBrush As Brush = New SolidBrush(Color.FromArgb(50, 50, 50))
  65.     Private NormalTextFont As Font = New Font("Segoe UI Light", 15)
  66.     Private SmallTextFont As Font = New Font("Segoe UI Light", 10, FontStyle.Bold)
  67.     Private NormalTextColor As Color = Color.FromArgb(125, 125, 125)
  68.     Private HighLightTextColor As Color = Color.FromArgb(0, 192, 192)
  69.  
  70. #End Region
  71.  
  72.  
  73.     Sub WindowLoad() Handles Me.Load
  74.         Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True) ' Stops the flickering when we update the graphics.
  75.  
  76.         'Load Some music :)
  77.         MusicPlayer.settings.autoStart = False
  78.         MusicPlayer.URL = My.Application.Info.DirectoryPath + "\Data\Dj Rolando - Jaguar.mp3" 'Load the file
  79.         MusicPlayer.settings.playCount = -1 'Loop forever
  80.         MusicPlayer.settings.volume = 10 'Headphone users wont get a heart attack.
  81.  
  82.         'Generate Cheat Engine offset.
  83.         ScoreOffset = RandomSeed.Next(10, 100)
  84.  
  85.         'Load the best score from the settings
  86.         BestScore = My.Settings.BestScore * ScoreOffset
  87.     End Sub
  88.  
  89.     Private Sub GameRun() Handles GameTimer.Tick
  90.         TotalGameTicks += 1
  91.  
  92.         If IsGameRunning = True And IsGameFinished = False Then
  93.             'Move the box towards the gravity point.
  94.             If Gravity = 0 Then PlayerBox.Y -= 3 Else PlayerBox.Y += 3
  95.  
  96.             'Check if the box touches the top or bottom floors.
  97.             If (PlayerBox.Y + PlayerBox.Height) >= ClientSize.Height Or PlayerBox.Y <= 0 Then
  98.                 IsGameRunning = False
  99.                 IsGameFinished = True
  100.                 MusicPlayer.controls.stop()
  101.                 GameTimer.Stop()
  102.                 SpawnedWalls.Clear()
  103.                 LastGameScore = Score
  104.                 If Score >= BestScore Then BestScore = Score
  105.                 My.Settings.BestScore = BestScore / ScoreOffset
  106.             End If
  107.  
  108.             'Spawn Walls
  109.             If TotalGameTicks Mod 150 = 0 Then
  110.                 Dim TempWall As New Wall
  111.                 Dim PassGapHeight As Integer = RandomSeed.Next(60, Height - 180)
  112.                 TempWall.PassGap = New Rectangle(Width - 60, PassGapHeight, 60, 100)
  113.                 TempWall.TopWall = New Rectangle(Width - 60, 0, 60, PassGapHeight)
  114.                 TempWall.BottomWall = New Rectangle(Width - 60, PassGapHeight + 120, 60, Height - PassGapHeight)
  115.                 TempWall.PlayerIn = False
  116.                 SpawnedWalls.Add(TempWall)
  117.             End If
  118.  
  119.             'Move the walls
  120.             For Each Wall In SpawnedWalls.ToArray
  121.                 If Wall.PassGap.X <= -60 Then
  122.                     SpawnedWalls.Remove(Wall)
  123.                 End If
  124.                 Wall.PassGap.X -= (2 + Level)
  125.                 Wall.TopWall.X -= (2 + Level)
  126.                 Wall.BottomWall.X -= (2 + Level)
  127.             Next
  128.  
  129.             'Check If player touches the walls.
  130.             For Each Wall In SpawnedWalls.ToArray
  131.                 'If the box touches the top or bottom wall, end the game.
  132.                 If PlayerBox.IntersectsWith(Wall.TopWall) Or PlayerBox.IntersectsWith(Wall.BottomWall) Then
  133.                     IsGameRunning = False
  134.                     IsGameFinished = True
  135.                     MusicPlayer.controls.stop()
  136.                     GameTimer.Stop()
  137.                     SpawnedWalls.Clear()
  138.                     LastGameScore = Score
  139.                     If Score >= BestScore Then BestScore = Score
  140.                     My.Settings.BestScore = CLng(BestScore / ScoreOffset)
  141.                 End If
  142.  
  143.                 'If the player goes into the gap.
  144.                 If PlayerBox.IntersectsWith(Wall.PassGap) And Wall.PlayerIn = False Then
  145.                     Wall.PlayerIn = True
  146.                 End If
  147.  
  148.                 'If the player was in the gap and now he left, give him some points.
  149.                 If Wall.PlayerIn = True Then
  150.                     If PlayerBox.IntersectsWith(Wall.PassGap) = False Then
  151.                         Score += ScoreOffset
  152.                         Wall.PlayerIn = False
  153.                     End If
  154.                 End If
  155.             Next
  156.  
  157.             'For every 30 points, make the game harder
  158.             If Score / ScoreOffset <> 0 Then If ((Score / ScoreOffset) Mod 30 = 0) Then Level += 1
  159.         End If
  160.  
  161.         If TotalGameTicks = 1000 Then TotalGameTicks = 0
  162.         Invalidate() 'Paint our stuff.
  163.     End Sub
  164.  
  165.     Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
  166.         ' If the game state is not running.
  167.         If IsGameRunning = False And IsGameFinished = False Then
  168.             If e.KeyCode = Keys.Space Then 'If space is pressed start the game.
  169.                 GameTimer.Start()
  170.                 IsGameRunning = True
  171.                 IsGameFinished = False
  172.                 Score = 0
  173.                 Gravity = RandomSeed.Next(0, 2) 'Make the start Gravity random to bottom or top
  174.                 Level = 1
  175.  
  176.                 'Create the PlayerBox, and give it a random color.
  177.                 PlayerBox = New Rectangle(200, CenterScreenPoint.Y, 50, 50)
  178.                 PlayerBoxColor = GameColorsTable(RandomSeed.Next(0, 4))
  179.  
  180.                 'Play the BG music
  181.                 MusicPlayer.controls.play()
  182.             End If
  183.  
  184.             'If escape is pressed, exit game
  185.             If e.KeyCode = Keys.Escape Then
  186.                 Application.Exit()
  187.             End If
  188.             Exit Sub
  189.         End If
  190.  
  191.         'If The game is running.
  192.         If IsGameRunning = True And IsGamePaused = False Then
  193.             If e.KeyCode = Keys.Space Then 'If space is pressed change the gravity.
  194.                 'Toogle gravity
  195.                 If Gravity = 0 Then
  196.                     Gravity = 1
  197.                     PlayerBox.Y += 3
  198.                 Else
  199.                     Gravity = 0
  200.                     PlayerBox.Y -= 3
  201.                 End If
  202.             End If
  203.  
  204.             'If escape is pressed while game is running, pause the game.
  205.             If e.KeyCode = Keys.Escape Then
  206.                 GameTimer.Stop()
  207.                 IsGamePaused = True
  208.                 MusicPlayer.controls.pause()
  209.                 Invalidate() 'Because we stoped the game timer we must update the game manualy
  210.             End If
  211.             Exit Sub
  212.         End If
  213.  
  214.         'If game is paused
  215.         If IsGameRunning = True And IsGamePaused = True Then
  216.             If e.KeyCode = Keys.Space Then 'If space is pressed, unpause the game
  217.                 IsGamePaused = False
  218.                 GameTimer.Start()
  219.                 MusicPlayer.controls.play()
  220.             End If
  221.             Exit Sub
  222.         End If
  223.  
  224.         'If the game is finished
  225.         If IsGameRunning = False And IsGameFinished = True Then
  226.             If e.KeyCode = Keys.Escape Then 'If space is pressed show the main menu.
  227.                 IsGameFinished = False
  228.                 Invalidate()
  229.             End If
  230.         End If
  231.     End Sub
  232.  
  233.     Private Sub frmMain_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
  234.         With e.Graphics
  235.             .CompositingQuality = Drawing2D.CompositingQuality.HighSpeed ' i have no idea
  236.             .Clear(Color.FromArgb(22, 22, 29)) ' paint the background.
  237.  
  238.             'Paint The Starting Screen Menu
  239.             If IsGameRunning = False And IsGameFinished = False Then
  240.                 .FillRectangle(NormalBrush, 242, 180, 200, 100)
  241.                 .DrawString("Acid Bounce", NormalTextFont, New SolidBrush(HighLightTextColor), 288, 184)
  242.                 .DrawString("Press Space To Start", NormalTextFont, New SolidBrush(NormalTextColor), 254, 216)
  243.                 .DrawString("Press Escape to exit and save.", SmallTextFont, New SolidBrush(NormalTextColor), 253, 260)
  244.                 .DrawRectangle(New Pen(NormalBorderBrush), 241, 179, 201, 101)
  245.             End If
  246.  
  247.             'Paint The Paused Screen Menu
  248.             If IsGameRunning = True And IsGamePaused = True Then
  249.                 .FillRectangle(NormalBrush, 242, 180, 200, 100)
  250.                 .DrawString("Acid Bounce", NormalTextFont, New SolidBrush(HighLightTextColor), 288, 184)
  251.                 .DrawString("Press Space To Resume", NormalTextFont, New SolidBrush(NormalTextColor), 239, 226)
  252.                 .DrawRectangle(New Pen(NormalBorderBrush), 241, 179, 201, 101)
  253.             End If
  254.  
  255.             'Paint The End Game Screen Menu
  256.             If IsGameRunning = False And IsGameFinished = True Then
  257.                 .FillRectangle(NormalBrush, 242, 180, 200, 100)
  258.                 .DrawString("Acid Bounce", NormalTextFont, New SolidBrush(HighLightTextColor), 288, 184)
  259.                 .DrawString("Your Score: " & (Score / ScoreOffset).ToString, NormalTextFont, New SolidBrush(NormalTextColor), 265, 220)
  260.                 .DrawString("Press Escape to play again", SmallTextFont, New SolidBrush(NormalTextColor), 263, 260)
  261.                 .DrawRectangle(New Pen(NormalBorderBrush), 241, 179, 201, 101)
  262.             End If
  263.  
  264.             'Paint The Game
  265.             If IsGameRunning = True And IsGamePaused = False Then
  266.  
  267.                 'Paint the PlayerBox
  268.                 .FillRectangle(New SolidBrush(PlayerBoxColor), PlayerBox)
  269.  
  270.                 'Draw The Gravity Icon
  271.                 If Gravity = 0 Then
  272.                     .DrawString("▲", NormalTextFont, New SolidBrush(Color.White), PlayerBox.X + ((PlayerBox.Width / 2)), PlayerBox.Y + ((PlayerBox.Height / 2)))
  273.                 Else
  274.                     .DrawString("▼", NormalTextFont, New SolidBrush(Color.White), PlayerBox.X + ((PlayerBox.Width / 2)), PlayerBox.Y + ((PlayerBox.Height / 2)))
  275.                 End If
  276.  
  277.                 'Draw Walls
  278.                 For Each Wall As Wall In SpawnedWalls
  279.                     Dim RandomWallColor As Color = GameColorsTable(RandomSeed.Next(0, 4))
  280.                     .FillRectangle(New SolidBrush(RandomWallColor), Wall.TopWall)
  281.                     .FillRectangle(New SolidBrush(RandomWallColor), Wall.BottomWall)
  282.                 Next
  283.  
  284.                 'Show the score on screen.
  285.                 If Score > BestScore Then 'If the current score is biggerst than the best, show current score as best.
  286.                     .DrawString("Score: " & (Score / ScoreOffset).ToString & " | Last Game: " & (LastGameScore / ScoreOffset).ToString & " | Best: " & (Score / ScoreOffset).ToString, SmallTextFont, New SolidBrush(NormalTextColor), 2, 2)
  287.                 Else
  288.                     .DrawString("Score: " & (Score / ScoreOffset).ToString & " | Last Game: " & (LastGameScore / ScoreOffset).ToString & " | Best: " & (BestScore / ScoreOffset).ToString, SmallTextFont, New SolidBrush(NormalTextColor), 2, 2)
  289.                 End If
  290.             End If
  291.         End With
  292.  
  293.     End Sub
  294.  
  295. End Class
Advertisement
Add Comment
Please, Sign In to add comment