Simulating dog racing.

BlitzMax Forums/BlitzMax Beginners Area/Simulating dog racing.

Xerra(Posted 2008) [#1]
I've been following Wave's Beginners guide to BlitzMax - mainly on using types, functions and methods as I've always had problems getting my head round those and I've not done any programming for a long while now.

Anyway, I put this together as I fancied creating a little gambling game with dog racing, betting, odds and so on and wanted to see something moving so I could get an idea on simulating the actual races. It works ok but I'm after some of you experts to have a look over it and see if I'm doing anything sloppy specifically with the way the random number is used to increase a dogs speed of movement.

I don't think I've really got my head around types totally yet although doing this has helped massively so I'd appreciate any suggestions or advice.

Thanks.

' ** Dog racing by Tony Black - 29/6/08
' ** Created as an example to learn the use of functions, types and methods.
'
Strict
Global YPos:Int
Global N:Int
Global List:TList ' Keeps the dogs inside Dogtype
Global Finished:Byte = False
Global Winner:String

Type DogType
	Field X:Int
	Field Y:Int
	Field Speed:Int
	Field Name:String
	Field Colour:Int

	Method Draw() ' Display the dog on screen.
		DrawOval (X , Y ,50 , 25) 
	End Method

	Method Move() ' Move dog according to Speed along its X axis
		Speed=Rand(1,5)
		X:+ Speed
		'DrawText Name+" "+Colour+" "+X+" "+Y+" "+Speed,0,30 
		If X > 760
			Finished = True
			Winner=Name
		EndIf
	End Method
	
	Method Reset() ' Reset the dogs starting positions for new race
		Speed = 1
		X = 100
	End Method
	
	Function Create() 
		Local NewDog:DogType ' declares a variable to store our Dogs type.
		NewDog = New DogType ' puts the address of the new Dog into the variable NewDog
		NewDog.X = 100 ; NewDog.Y = YPos ' Sets the start position of the Dog on screen.
		' YPos is global so it can have the dogs on different start positions down the screen
		' by incrementing 20 each time a dog is created or they'd all be in the same spot on screen.

		' Let's name the puppies. I couldn't work out how to run through a the type and ammend the
		' type.name field so had to name them on creation of each dog using the following method which
		' is a bit scrappy. N is the counter for/next loop calling the creation function for each dog.
		
		If N = 1
			NewDog.Name = "Patch"
			NewDog.Colour = 20
		EndIf
		If N = 2
			NewDog.Name = "Fido"
			NewDog.Colour = 40
		EndIf
		If N = 3
			NewDog.Name = "Sparky"
			NewDog.Colour = 60
		EndIf
		If N = 4
			NewDog.Name = "Rex"
			NewDog.Colour = 80
		EndIf
		If N = 5
			NewDog.Name = "Blue"
			NewDog.Colour = 100
		EndIf
		If N = 6
			NewDog.Name = "Buddy"
			NewDog.Colour=150
		EndIf
		If N = 7
			NewDog.Name = "Lassie"
			NewDog.Colour = 200
		EndIf
		If N = 8
			NewDog.Name = "Mutley"
			NewDog.Colour = 250
		EndIf
		
		If Not List Then List = CreateList() 
		List.AddLast (NewDog) ' Puts the new dog into our dogs list.
	End Function
	
End Type

Graphics 800 , 600
Global DogList:TList = CreateList() ' Create a list to store all Dogs

' Create the dogs.
For N = 1 To 8 ' Fixed number of dogs as they are all named.
	YPos=100+N*40
	DogType.Create()
Next

While Not KeyHit(Key_Escape)
	Flip ; Cls
	DrawText "Press Esc to Exit...." , 0 , 0
	YPos=140
	For Local Dog:DogType = EachIn List
		Dog.Draw()
		Dog.Move()
		SetColor 255,100,Dog.Colour
		DrawText Dog.Name , 0 , YPos
		YPos:+ 40
	Next
	If Finished=True
		SetColor 255,255,200
		DrawText "The Winner: "+Winner+" ... Press Space to Restart.",0,500
		Flip
		Repeat
		Until KeyHit(Key_Space) 
		Finished = False
		For Local Dog:DogType = EachIn List
			Dog.Reset() 
		Next
	EndIf
Wend



Jesse(Posted 2008) [#2]
firt of I am not an expert sence I have about two years of self teaching myself OOP. But I can tell you how I might have done it. and who knows I might be learning something in the process.
First thing use the least amount of globals as possible.
globals hinders program speed with the exception of glabals in types. It may not be abvious in this sample but it is a nice habit to develop.
Second when you create a type, try having everything related to that type included in it. YPos is the same as the y for each dog So yust eliminate it and assign the value directly to the dog's y instead.

third when you create a type try making it as usable as possible, you don't want to mess with it everytime you want to use new names for the dogs.

finally I would moved the flip to the bottom of the code. This is a minor thing sence you are flipping the page at the beggining of the program/game with out having drawn anything on the screen which makes it useless.

I re-did your code to how I would have done it.
some of the stuff I did not mention is just my way of doing things. It is not necessarily better than what you had done.


[edited]
modified the restart loop
[/edited]


christian223(Posted 2008) [#3]
You know, i think you got it allright, after all, objects exist to help us, they exist to simplify things and to make larger projects easier to work with. The key is to divide everything into manageable pieces and to make a big things from little ones.

I recommend you to instead of doing something simple as dogs, try doing cars for different types of people or maybe different types of robots, for example, do a game about different kinds of people and theyr cars. The important thing for you is to decompose all the parts that make a car that makes it able to do what it does. An engine would be a type, the fuel of the engine another type, the tires another type, and each different type would be used by the "Car" type with its methods.

What type of car would need a millionaire/veterinarian/war veteran/president/etc ?, what kind of tires?, what kind of seats?, what kind of interior decoration?, what kind of electronics will the car have?, think on all the parts a car would have. You dont even need to code it, just writte it on paper.

When i was in school, my teacher told us to do as if we where working on a store, and make us writte everything we would need to do from the time we entered the store to the time we closed the store, detail by detail, and thats what you need to think on, on what needs to be done, and its details and all its parts and how they fit in the whole.

I hope that helps :)


Xerra(Posted 2008) [#4]
@Jesse

Thanks for your comments - they're much appreciated. I see where you're going with the redundant bits. I can pick up a few pointers there. The extra flip at the start of the loop you mentioned was there because I'd put the code in to pause everything while displaying the winning dog as I recall. Definetly needed some optimising there, then :-)

I wasn't aware that the use of globals was that demmanding so I can watch out for that too. It's kind of force of habbit that I've been using them willy-nilly until recently as I thought my programming style was getting a bit messy. I only recently got in the habbit of using strict to combat that as I hate looking at code that isn't as readable as possible to someone else looking at it.

@Christian223

Dogs was just an example I was thinking about to test out messing with types as I've recently been to Yarmouth dogs and I also used to love the arcades on a pier at the seaside when I was a kid that had those daft machines you could use to bet on the different colours. I kind of ran with the idea of simulating something similar myself as a programming exersize but now I fancy making a bit of a game out of it.

I suspect it's very possible to adapt it to the other ideas you mention with not too much work if following what's been said. Using a self contained type to do as much work as it can without having to change other bits of code around it is definetly the way to make the whole job easier.

I worked out a lot of ideas on paper yesterday to expand on the original concept to see how it works as a game so I'll run with it a bit and see how it comes together as I'm bound to learn a few more new tricks.

Thanks a lot for the comments, guys. More welcome by all means - i'll take all the advice I can get that'll help me get better at this.