How does this code look to you? & collition woes..
BlitzMax Forums/BlitzMax Beginners Area/How does this code look to you? & collition woes..
| ||
| Ok, really just started using this in fact any programming language so forgive me if I sound a little green! First of all, I was really looking for feedback on how my code looks or how it could be better organised or optimised for later addition of lists (still can't get my head round them, need them for the Alien grid I think). Second, I can't get the ImagesCollide thing to work. Could someone point me in the right direction? :) Here's my code:
'------------INIT-------------
Graphics 224,240,0
AutoMidHandle(True)
'-Loading Images-
Global iplayer:TImage = LoadImage ("C:\Space Invaders\Sprites\player.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
If iplayer=Null Then Print "Image not found"
Global iinvader1a:TImage = LoadImage ("C:\Space Invaders\Sprites\invader1a.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
If iinvader1a=Null Then Print "Image not found"
Global iinvader1b:TImage = LoadImage ("C:\Space Invaders\Sprites\invader1b.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
If iinvader1b=Null Then Print "Image not found"
Global ishot:TImage = LoadImage ("C:\Space Invaders\Sprites\invadershot.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
If ishot=Null Then Print "Image not found"
Global invaderdeath:TImage = LoadImage ("C:\Space Invaders\Sprites\invaderdeath.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
If invaderdeath=Null Then Print "Image not found"
'-Setting start points-
Global playerx=112
Global playery=207
Global invaderx=25
Global invadery=55
Global invadertimerx=0
Global invadertimery=0
Global counter=0
Global shotlist:TList = CreateList()
Global shoty=200
Global invaderd=0
'-----------------TYPES------------
Type tplayer
Field playerx,playery
EndType
Type tinvader
Field invaderx,invadery,invaderspeed
EndType
Type tshot
Field shotx,shoty,shotspeed
Method Destroy()
shotlist.Remove(Self)
End Method
End Type
'--------------MAIN LOOP------------
Repeat
drawplayer()
playermove()
playerupdatestate()
drawinvader()
shot()
invadermove()
invaderdie()
Flip;Cls
Until KeyDown(key_escape)
End
'--------------FUNCTIONS--------------
'-PLAYER FUNCTIONS-
Function drawplayer()
DrawImage (iplayer,playerx,playery)
EndFunction
Function playermove()
If KeyDown(key_left) Then playerx:-2
If KeyDown(key_right) Then playerx:+2
EndFunction
Function playerupdatestate()
If playerx<20 Then playerx=20
If playerx>200 Then playerx=200
EndFunction
Function shot()
If KeyDown(key_space) Then shotx=playerx Else shotx=shotx
If Not KeyDown(key_space) Then shoty=playery
If KeyDown(key_space) Then DrawImage (ishot,shotx,shoty)
If KeyDown(key_space) Then shoty:-3
If shoty>208 Then shoty:-1
If shoty<0 Then shoty=207
EndFunction
'-INVADER FUNCTIONS-
Function drawinvader()
invadertimerx:+1
If invadertimerx<50 Then DrawImage (iinvader1a,invaderx,invadery)
If invadertimerx>50 Then DrawImage (iinvader1b,invaderx,invadery)
If invadertimerx>50 Then counter:+2
If invadertimerx>100 Then invadertimerx=0
If counter>30 Then counter=0
EndFunction
Function invadermove()
If invaderx =25 Then
invaderd = 0
invadery:+0.2
Else If invaderx = 190 Then
invaderd = 1
invadery:+0.2
EndIf
If invadertimerx=100 And invaderx<200 And invaderd = 0 Then invaderx:+5
If invadertimerx=50 And invaderx<200 And invaderd = 0 Then invaderx:+5
If invadertimerx=100 And invaderx<200 And invaderd = 1 Then invaderx:-5
If invadertimerx=50 And invaderx<200 And invaderd = 1 Then invaderx:-5
EndFunction
Function invaderdie()
If ImagesCollide(iinvader1a,invaderx,invadery,0,ishot,playerx,shoty,0)
SetClsColor 255,0,0
Else
SetClsColor 0,0,0
EndIf
If ImagesCollide(iinvader1b,invaderx,invadery,0,ishot,shotx,shoty,0)
SetClsColor 255,0,0
Else
SetClsColor 0,0,0
EndIf
EndFunctionThanks! |
| ||
Global iplayer:TImage = LoadImageSafe("Sprites\player.png")
Function LoadImageSafe:TImage(File:String,Flags:Int=MIPMAPPEDIMAGE|FILTEREDIMAGE)
Local image:TImage=LoadImage(File,Flags)
If image=Null Then Print "Failed to load " +File+ "!"
Return image
End Function
Don't use absolute file paths (C:\Space Invaders\Sprites\player.png), use relative paths (Sprites\player.png). They're relative of where the main BMX file is that you compile. To keep your code organized and to prevent stupid little errors, look up Strict or SuperStrict. They'll help. And for reptitive things like your loading images that also checks for errors, its best to use a function for that (as illustrated above) |