Reflections

BlitzPlus Forums/BlitzPlus Programming/Reflections

En929(Posted 2009) [#1]
It seems like I have been advancing pretty well with learning BlitzBasic within the last few months (from scratch). So far, it’s doing everything that I’ve wanted it to do. But, my thought is it seems like I’m doing many things the long way; for example, if one looks at my code below, I made the borders in my game out of actual images that I made with PAINT, and uploaded them. It seems like making those borders take forever; it took a day to make those. Does it usually take that long? I’m making and organizing the code in such a way that makes sense to me (simply because I’m a beginner). It seems like the code that I put together doesn’t look like everybody else code, but looks like maybe a longer route to go. Thus, I was wondering what are some thoughts on this? Things still seem to be going pretty good even though I’ve been doing it my way.



Graphics 1600,1000



;************************************************************888
							;Load Visual Images

HENRY = LoadAnimImage ("Move2.PNG",400,370,0,13)
BACKGROUND = LoadImage ("RoomWithNoDoorsOpened.png")


;*************************************************************888
							;Load Game Boundries


HENRYUPCLUTCH  = LoadImage ("Henry'sUpClutch.png")
HENRYDOWNCLUTCH = LoadImage ("Henry'sDownClutch.png")
HENRYRIGHTCLUTCH = LoadImage ("Henry's Right Clutch.png")
HENRYLEFTCLUTCH = LoadImage ("Henry's LeftClutch.png") 
UPLIMIT = LoadImage ("Uplimit.png")
UPOPENDOORCLUTCH = LoadImage ("Upopendoorclutch.png")
GOINDOOR = LoadImage ("Leftlimit.png")
LEFTLEFTLIMIT = LoadImage ("LeftLeftLimit.png")
GOINSIDECLUTCH = LoadImage ("Goinsideclutch.png")
UPGOINSIDECLUTCH = LoadImage ("Upgoinsideclutch.png")

;*************************************************************888
							;Miscellaneous Loads
							
MUSIC_FOR_SCENE_1 = LoadSound ("Bells and Whistles.wav")
							



;*************************************************************888
							;Types to Each Character


Type HENRY
	Field x,y
	Field frame
End Type


Type BACKGROUND
	Field x,y
	Field Image
End Type

Type HENRYUPCLUTCH

	Field x,y
End Type 

Type HENRYDOWNCLUTCH 
	Field x,y
End Type

Type HENRYRIGHTCLUTCH
	
	Field x,y
End Type

Type HENRYLEFTCLUTCH

	Field x,y
End Type 

Type UPLIMIT

	Field x,y
End Type 


Type GOINDOOR
	Field x,y
End Type 

Type LEFTLEFTLIMIT
	Field x,y
End Type 

Type UPOPENDOORCLUTCH 
	Field x,y
End Type

Type GOINSIDECLUTCH
	Field x,y
End Type 

Type UPGOINSIDECLUTCH
	Field x,y
End Type 


;*********************************************************************************
									;Set Character Variables
	


H.HENRY = New HENRY
H\X = 585
H\Y = 400
H\frame = 0

;Instead of Henry actually colliding with certain objects, this invisible object remains ABOVE Henry and is used to 
;make it seem like Henry is actually colliding with an object

HCU.HENRYUPCLUTCH = New HENRYUPCLUTCH
HCU\X = 390
HCU\Y = 160

;This invisible object remains BELOW HENRY and is used to 
;make it seem like Henry is actually colliding with an object


HDU.HENRYDOWNCLUTCH = New HENRYDOWNCLUTCH 
HDU\X = 390
HDU\Y = 460

;This invisible object remains to the RIGHT of HENRY and is used to 
;make it seem like Henry is actually colliding with an object

HDR.HENRYRIGHTCLUTCH = New HENRYRIGHTCLUTCH

HDR\X = 300
HDR\Y = 300

;This invisible object remains to the LEFT of HENRY and is used to 
;make it seem like Henry is actually colliding with an object

HDL.HENRYLEFTCLUTCH = New HENRYLEFTCLUTCH

HDL\X = 500
HDL\Y = 290

;This invisible border keeps HENRY on the ground when the invisible HENRYUPCLUTCH character collides with it. Otherwise,
;when the "up" arrow key is pressed, HENRY will go up without limit

UPL.UPLIMIT = New UPLIMIT
UPL\X = 430
UPL\Y = 105

;This invisible border switches the next scene and changes the background (That is, because the HENRYLEFTCLUTCH collides with
;this border, it gives the illusion that HENRY actually went inside the door)

GID.GOINDOOR = New GOINDOOR
GID\X =  -120
GID\Y = 180


;If the HENRYLEFTCLUTCH character (the invisible border that stays at that stays at HENRY's left) collides with LEFTLIMIT, it
;keeps HENRY from going too far off the screen to the left. Otherwise, HENRY will go to the left without limit.


LLL.LEFTLEFTLIMIT = New LEFTLEFTLIMIT
LLL\X = -230
LLL\Y = 200

;The BACKGROUND for the first scene

b.BACKGROUND = New BACKGROUND
b\x = -1100
b\y = 90
b\image = BACKGROUND

;This opens the door when HENRYUPCLUTCH collides with the UPLIMIT border

UPODC.UPOPENDOORCLUTCH = New UPOPENDOORCLUTCH 

UPODC\X = 480
UPODC\Y = 110


;When HENRYUPCLUTCH collides with this, then 

UPGIN.UPGOINSIDECLUTCH = New UPGOINSIDECLUTCH

UPGIN\X = 480
UPGIN\Y = 85


GIN.GOINSIDECLUTCH = New GOINSIDECLUTCH

GIN\X = -190
GIN\Y = 230

;************************************************************************
									;Miscellaneous
									
PlaySound MUSIC_FOR_SCENE_1
LoopSound MUSIC_FOR_SCENE_1									
									
									
									
									
									
;********************************************************************8888									




While Not KeyDown (1)


Cls


;When the right arrow key is pressed, it moves HENRY and his collision borders to the right (collision borders are 
;the invisible borders that collide with other invisible borders to make changes in the environment, and keeps HENRY from 
;going off screen, etc) 


If KeyDown(205)
	
	HCU\X = HCU\X + 3	
	HDU\X = HDU\X + 3
	HDR\X = HDR\X + 3	
	HDL\X = HDL\X + 3
	
	H\X = H\X + 3


		h\frame = h\frame + 1
		
		If h\frame > 2 Then 
		h\frame = 0
		
		EndIf 

EndIf 


;When the left arrow key is pressed, it moves HENRY and his collision borders (described above)
;to the Left

If KeyDown(203)

	HCU\X = HCU\X - 3	
	HDU\X = HDU\X - 3
	HDR\X = HDR\X - 3	
	HDL\X = HDL\X - 3	
    H\X = H\X - 3
	
		 h\frame = h\frame + 1
		

		If h\frame > 12 Then
		
		 h\frame = 6


     	EndIf 

EndIf


;This moves HENRY and his invisible collision borders up


If KeyDown (200)

	
	HCU\Y = HCU\Y - 3	
	HDU\Y = HDU\Y - 3
	HDR\Y = HDR\Y - 3	
	HDL\Y = HDL\Y - 3
	H\Y = H\Y - 3
EndIf 


;This moves HENRY and his invisible collision borders down
   

If KeyDown (208)

	HCU\Y = HCU\Y + 3	
	HDU\Y = HDU\Y + 3
	HDR\Y = HDR\Y + 3	
	HDL\Y = HDL\Y + 3
	H\Y = H\Y + 3
EndIf 



;Tiles the background image

TileImage (b\image,b\x,b\y)


;Draws Henry onto the screen

DrawImage (HENRY,h\x,h\y)



;These DrawImage commands in this section below will actually be deleted when the game is finished. These are put here in
;for reference while I'm making the game so that I can see where the borders are.

DrawImage(HENRYUPCLUTCH,HCU\X,HCU\Y)
DrawImage (HENRYDOWNCLUTCH, HDU\X,HDU\Y) 
DrawImage (HENRYRIGHTCLUTCH,HDR\X,HDR\Y)
DrawImage (HENRYLEFTCLUTCH,HDL\X,HDL\Y)
DrawImage (UPLIMIT,UPL\X,UPL\Y)
DrawImage (GOINDOOR,GID\X,GID\Y)
DrawImage (LEFTLEFTLIMIT,LLL\X,LLL\Y)
DrawImage (UPOPENDOORCLUTCH, UPODC\X,UPODC\Y)
DrawImage (GOINSIDECLUTCH,GIN\X,GIN\Y)
DrawImage (UPGOINSIDECLUTCH,UPGIN\X,UPGIN\Y)


;When HENRY's invisible clutch collides with the invisible UPLIMIT border, 
;it'll open a door that I will eventually put into the game.

If ImagesCollide (HENRYUPCLUTCH,HCU\X,HCU\Y,0,UPLIMIT,UPL\X,UPL\Y,0) Then

				
				h\y = 290
				HCU\Y= 50
				HDR\Y = 176
				HDL\Y = 200
				HDU\Y = 350
EndIf 

If ImagesCollide (HENRYRIGHTCLUTCH,HDR\X,HDR\Y,0, LEFTLEFTLIMIT,LLL\X,LLL\Y,0) Then

				h\x = 50
		        HDR\X = -225
				HDL\X = -20		
				HCU\X = -150
				HDU\X = -150
				
				If KeyDown (205)
				
				HCU\X = HCU\X + 3	
				HDU\X = HDU\X + 3
				HDR\X = HDR\X + 3	
				HDL\X = HDL\X + 3
	
				H\X = H\X + 3
			
				EndIf 
EndIf 





Flip
Wend 




Sauer(Posted 2009) [#2]
You could greatly reduce the size and complexity of your code by simply using less types and type instances.

For example, you have a new type and a instance of that type for each clutch that Henry has. However, you know that these places will always be the same in comparison to the others; every time you move Henry you increment or decrement the values by the same amount.

Another issue you'll run into is that when you remove those draw images, ImagesCollide will never return true because they are not drawn.

A better solution would be to use bounding boxes to determine your collisions. Basically, rather than check if the actual images are colliding, you assign a box around each of your objects and see if the boxes overlap. Although this is not a perfect collision, for most implementations of games it is close enough that it will work.

Lets have an example. Say your Henry image is 50x100 pixels, and your door is 200x200 pixels. We'll assume that H\x and H\y are Henry's x and y coordinates, and D\x and D\y are the doors coordinates. Both will correspond to the top left corner of the image, as usual.

Now we draw an imaginary box around the images, one being 50x100 and the other 200x200. To calculate if these boxes overlap, we want to see if:

1. Henry's X is <= than the Door's X plus its width (its right edge)
2. Henry's X plus his width (the right edge) is >= the Door's X
3. Henry's Y is <= than the Door's Y plus its height (its bottom edge)
4. Henry's Y plus his height (the bottom edge) is >= the Door's Y

The code would look something like this:
If h\x<=d\x+200 And h\x+50>=d\x And h\y<=d\y+200 And h\y+100>=d\y
  ;Collision!
EndIf


Draw some boxes in paint and look at the conditions. You'll notice that all the colliding ones meet this, whereas boxes that do not overlap fail at least one of the conditions. It's certainly an easier thing to visualize than to write about.

Its really late so I hope that is all correct, and I hope it makes a little sense. Basically, simplify collisions, and you simplify your code immensely.


En929(Posted 2009) [#3]
Sauer,

Thanks, that way worked and and I think that way is a better way than using the ImageCollide command and it seems like a way to control collisions better too. I tried what you said with an example code that's below that is set up like this:



HENRY = LoadImage ("Eatum.png")  
DOOR = LoadImage("Door.png")




Type HENRY

	Field x,y
End Type

Type DOOR

	Field x,y
End Type

h.HENRY = New HENRY
h\x = 150 h\y = 300

d.DOOR = New DOOR
d\x = 300 d\y = 300








While Not KeyDown (1)

Cls 


If KeyDown (205) Then 

h\x = h\x + 3

EndIf 



If KeyDown (203) Then

h\x = h\x - 3

EndIf 

If KeyDown (200) Then

h\y = h\y - 3

EndIf 

If KeyDown (208) Then

h\y = h\y + 3

EndIf 


DrawImage (HENRY,h\x,h\y)

DrawImage (DOOR,d\x,d\y)

If h\x<=d\x + 100 And h\x+100>=d\x And h\y <=d\y+150 And h\y+100>=d\y Then

Text 100,100, "You made it in" 
  ;Collision!
EndIf




Flip 

Wend 




It might take some getting used to since it's an invisible rectangle, but it's still pretty good.