Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Code Cleanup - Round 3

 
 

 

As usual, source code, last tutorial, now let's go. This tutorial will be quick and i'm not going to show you all the changes that i made, but I will explain them. There were a few bugs in the game that I have fixed in this tutorial, a few improvements, designed to make the game more efficient and a couple of changes.

The first bug involved the moving ball in the game suddenly moving straight up and stopping sometimes outside the playarea, the reason for this bug eludes me but it was fixed by removing the RedimBallsArray Sub call from the FindBallsToDrop Sub. Next, there was a bug that caused an exception when the ball launcher balls were moving, this again was fixed by removing the RedimBallsArray call as above.

 

Then I tried to improve the render sub by removing any unnecessary looping through the balls array. Nearly every function implements at least one loop though the balls array, if we can jon these functions so that they can be run using only one loop then it greatly improves the game efficiency. First off, the Gamestate = "GameWon" and GameState = "GameLost" checks which both loop through the balls array can be joined together and use only one loop. This same idea can then be implemented to all other functions, therefore decreasing the total number of array loops per render cycle. I managed to join a couple more functions but not them all, any function that looped twice through the array could not be joined to another sub.

Nextl I have renamed Form1 to MainForm and added the following line of code directly under the text Public Class Mainform:

Inherits Form

This says that MainForm inherits al methods and declarations from a windows form. Next I created a new sub in the Form1 code as shown below:

Public Shared Sub Main()

Using frm As New MainForm

If Not frm.IntializeGraphics Then ' Initialize XNA
MessageBox.Show("Could not initialize XNA Device.")
Return
End If

frm.Show()

' While the form is still valid, render and process messages
While (frm.Created)
Application.DoEvents()
End While
End Using

End Sub

This Sub creates a new copy of Mainform called frm and then runs the InitializeGraphics Sub, this shows the game window and then while the game is running calls Application.DoEvents. This tells windows that if you have anything to do to the game window then do it now. This is not so important in our game because it only uses at more 30% CPU, but if your game uses up to 100% CPU then windows will not get a chance to DoEvents, the above sub gives windows a portion of CPU time. Next open the project properties and make the following change:

XNA in VB.NET - Sub Main

Uncheck "Enable application framework" and then change the Startup Object to Sub Main. Note that Sub Main will only appear if it is Declared as Shared in the Sub Definition.

Next I have removed the InitalizeGrpahics Call from the OnPaint function, as it is now called from Sub Main.

I decided that i wanted the highest score to remain after the game has been closed, this was done by adding new code to both Form1_Load and MainForm_Closing as shown below:

In Form1_Load

'Get High Score from High Score File
If My.Computer.FileSystem.FileExists(Application.StartupPath & "\HighScore.txt") = True Then
Try
Using sr As StreamReader = New StreamReader(Application.StartupPath & "\HighScore.txt")
Dim x As String = sr.ReadToEnd
sr.Close()
If IsNumeric(x) = True Then
HighScore = CInt(x)
End If
End Using
Catch ex As Exception

End Try
End If

This code just checks for the existence of a file called HighScore.txt in the same folder as the game executable, if it exists then it opens a reads it. The data it reads is checked to make sure that is is a number and then assigns it's value to the variable HighScore.

In the MainForm_Closing Sub:

Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

'Write High Score to High Score File
If My.Computer.FileSystem.FileExists(Application.StartupPath & "\HighScore.txt") = True Then
Try
Using sr As StreamReader = New StreamReader(Application.StartupPath & "\HighScore.txt")
Dim x As String = sr.ReadToEnd
sr.Close()
If IsNumeric(x) = True Then
x = CInt(x)
If x < HighScore Then
Using sw As StreamWriter = New StreamWriter(Application.StartupPath & "\HighScore.txt", False)
sw.WriteLine(HighScore)
sw.Close()
End Using
End If
End If
End Using
Catch ex As Exception

End Try
Else
Try
Using sw As StreamWriter = New StreamWriter(Application.StartupPath & "\HighScore.txt", False)
sw.WriteLine(HighScore)
sw.Close()
End Using
Catch ex As Exception

End Try
End If

End Sub

This sub checks for the HighScore.txt file, if it is not there, it creates it and if it is there, then it reads the score and checks it against the current high score. If the current high score is bigger then the file is overwritten, if it is not then the sub exits. This Sub requires the Imports System.IO statement.

Ok, that's it for this tutorial, next I'm going to work on either adding sound to the game or Multi-threading it. Whatever I choose, I will upload it to the tutorials page when its finished.

 

CodeCleanup3 Source Code - 312Kb
Next Tutorial - Sound and MultiThreading

 

     
 
 
     

 

Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates
   
 
 

 

__PayPal

PayPal - Any Amount is Welcome
 
Please Donate to the Nvidia Geforce Go 7950 GTX Fund, All donations welcome. Thanks.

 

XNA in C#

 
 

Games at Amazon