Another code problem

BlitzPlus Forums/BlitzPlus Beginners Area/Another code problem

Siopses(Posted 2007) [#1]
I don't know what the problem with this is now it is I guess
with types but why would a 'variable type mismatch' I have no idea I'm new with types.
;Types
Type Player
   Field x,y,health,speed
End Type 
;Lets define the Player type
player.player\x=400
player.player\y=300
player.player\health=3
player.player\speed=4
Type Projectile
    Field x,y
End Type 
;Lets define the Projectile type
shot.Projectile\x=player\x
shot.Projectile\y=player\y+15
;Functions
Function ControlPlayer()
  While Not KeyDown(1)
      If KeyDown(LEFTKEY) Then
   player\x=player\x-player\speed
      If KeyDown(RIGHTKEY) Then
   player\x=player\x+player\speed 
      If KeyDown(SPACE) Then 
   projectile\y=projectile\y+100
      EndIf
      EndIf
      EndIf 
  Wend
End Function     
ControlPlayer()
End 



Regular K(Posted 2007) [#2]
Graphics 640,480

;Types
Type Player
   Field x,y,health,speed
End Type 
Type Projectile
    Field x,y
End Type 

; make a new player instance and set it to the global variable "realplayer"
Global realplayer.player=New player
realplayer\x=400
realplayer\y=300
realplayer\health=3
realplayer\speed=4

While Not KeyHit(1)
Cls
UpdatePlayer()
UpdateProjectiles()
Flip
Delay 1
Wend
End

Function UpdateProjectiles()
	; loop through all projectiles
	Local shot.projectile
	For shot=Each projectile
		; draw it
		Plot shot\x,shot\y
		; move it down 10 pixels
		shot\y=shot\y+10
	Next
End Function

Function UpdatePlayer()
	Rect realplayer\x-4,realplayer\y-4,8,8
      If KeyDown(203)
  		realplayer\x=realplayer\x-realplayer\speed
      Else If KeyDown(205)
		realplayer\x=realplayer\x+realplayer\speed
	EndIf  
	If KeyDown(57)
		; make a new projectile set to the local variable "shot"
		Local shot.projectile=New projectile
		shot\x=realplayer\x
		shot\y=realplayer\y
      EndIf 
End Function     



Siopses(Posted 2007) [#3]
Thanks, I'll work the updating things out myself.


DjBigWorm(Posted 2007) [#4]
hmm, I am not a big type user too , cuz i am old school and use dim command instead.

I looked at your code and I changed it to this then it didn't generate an error

If KeyDown(LEFTKEY)
For player.player = Each player
player\x=player\x-player\speed
Next
EndIf
If KeyDown(RIGHTKEY) Then
For player.player = Each player
player\x=player\x+player\speed
Next
If KeyDown(SPACE) Then
For shot.Projectile = Each Projectile
shot\y=shot\y+100
Next
EndIf
EndIf

hope this helps c ya:)


CS_TBL(Posted 2007) [#5]
siopses: what do you use to indent? Press Spaces, or press Tab?


Siopses(Posted 2007) [#6]
Spaces, why?


CS_TBL(Posted 2007) [#7]
use Tab instead! Saves you years of typing/deleting and you're sure that columns are straight under eachother. (which is also more comfy for other ppl here to read, let alone make adjustments to your code)

Tab is the universal indenting key for code.


Siopses(Posted 2007) [#8]
Ok fine, but one thing I need to ask: How did you improve
your coding when you were a begginer besides the tests?
I'm trying to make a space shooter but it is way too hard.


CS_TBL(Posted 2007) [#9]
I wasn't aware of things improving, it just happened over time. The biggest problem of about any language is the potential to make a mess of your code. The more mess, the less chance you could ever read it back after half a year. So, before you start thinking about something big, first think of how you are going to program it, prior to actually program it. It's easy to say 'oh, I need another global variable for this and that', and before you know it your whole application becomes a mess because you'll end up with hundreds of badly named variables that are all messing up.

The key to organization is data-hiding: no globals, everything is local.
Another aspect of data-hiding might be called code-hiding: make functions that are so generic and well described that you never need to look into the function code again. You could put all these generic functions into some include file and use it all the way. You truly use a lot of stuff then, but you won't need to know how things work, they just work.

If a space shooter is too hard, then don't do it. I see this again and again: people get into Blitz assuming that the first thing they will ever make is a shooter, RPG, whatever. Although understandable from a BRL-sales point of view, the "make your own games" slogan is way out of bounds. Blitz is just a language as any other language. It comes with handy gfx functions, it hides techstuff, that's all nice -and certainly suitable for beginners-, but it's still a normal language, not a RAD gamebuilder. So, if you had put your hopes on making a serious looking game within a few months.. well, sorry to pop your balloon.. but that ain't gonna happen.. :P

It'll take you years to become a procedural master, then when you finally feel you know the drill, then procedural Blitz won't cut it anymore and you'd wish for an OO approach and you'll end up spending years on BlitzMax again.

How to improve yourself? First think/design, then code. Don't start coding major chunks before you know what you're going to make. Split problems into smaller problems, identify whether multiple situations could use the same code-solution, if so: make sure that this one single code-solution can handle multiple situations. Do this for all your functions.


tonyg(Posted 2007) [#10]
Tutorials (mainly from here , Krylar's book , loads of little test programs and lots of time.


Siopses(Posted 2007) [#11]
Thanks for bolth of your suggestions.
CS_TBL, I know that I cannot make a game within my first
few months of coding. I recently saw a commercial for a
partially useless "computer sciences" degree and to "make your own games" yes I've realized that even making a simple
graphic based game is pretty hard for begginers like me, its
just I wanted to start somewhere.
tonyg, the first tutorial I used on blitzcoders.com was invaluably helpful, thank you! And I might even consider
buying Krylars book.
Thank you all,
Siopses


Jester(Posted 2007) [#12]
Hey Siopses,

Check out the book "Game Programming for Teens,second edition". You can get it on Amazon or just about any big bookstore. It teaches you some pretty good stuff in Blitz Plus. I wish it offered a little more info on using functions and a few other things but for beginners like you and I, it's pretty good. Plus it comes with some art and sound files on a cd you can use in your games.

Take care


Siopses(Posted 2007) [#13]
I have that book and I've already read it, this problem is
COMPLETELY SOLVED I will work any future problems on
my own.