re: Lots of if statements

BlitzMax Forums/BlitzMax Beginners Area/re: Lots of if statements

Joe90bigrat90(Posted 2011) [#1]
In my game ive got 2 different men that you control.

i test for different key combinations pressed

like this

[code]
'declare variables
Global player1x 'stores player 1 x position
Global player1y 'stores player 1 y position
Global player1speed 'stores the speed of player 1
Global frameplayer1 'stores the frame of the animimage

player1speed=5 'change this to speed up or slow down the player

'load the animimage
player1movingsouthwest=LoadAnimImage("d:\vendetta\sprites\player1southwest.png",49,49,0,4)

'southwest - downleft
If KeyDown(KEY_S) And KeyDown(KEY_A) And Not KeyDown(KEY_D) And Not KeyDown(KEY_W)
DrawImage (player1movingsouthwest,player1x,player1y,frameplayer1)
player1x=player1x-player1speed
player1y=player1y+player1speed
player1direction=6
frameplayer1:+1
'ResumeChannel channel
If frameplayer1>3 frameplayer1=0
EndIf
[code/]

the only trouble is ive got about 64 blocks like this for every key combination to test if 2 keys are pressed, 3 are pressed, 4 are pressed.
There must be an easy way of writing all these if statements just to check what keys are pressed???

thankyou for your help.


GaryV(Posted 2011) [#2]
Look at some of the examples where Select/Case is used. It should help you a lot.


Redspark(Posted 2011) [#3]
I'm not sure if this would work but off the top of my head, try something like this pseudo code:

In your initialization code for the game create these variables:
movementsprites = array(9)
movementsprites[0] = IdlePose
movementsprites[1] = EastAnimation
movementsprites[2] = SouthWestAnimation
movementsprites[3] = South
movementsprites[4] = SouthEast
movementsprites[5] = NorthWest
movementsprites[6] = North
movementsprites[7] = NorthEast
movementsprites[8] = West

movefactors = array(4)


In your game loop put
movefactors[0] = KeyDown(KEY_A)*-1 ' If going West, store -1
movefactors[1] = KeyDown(KEY_D)*1 ' If going East, store 1
movefactors[2] = KeyDown(KEY_S)*3 ' If going South store 3
movefactors[3] = KeyDown(KEY_W)*-3 ' If going North store -3

'This will give you possible results of 0, 1, 2, 3, 4, -1, -2, -3, -4
movefactor = movefactors[0] + movefactors[1] + movefactors[2] + movefactors[3];

if movefactor<0 then movefactor = movefactor + 9  ' This forces the negative numbers to be in the range of 5 - 8


DrawImage (movementsprites[movefactor],player1x,player1y,frameplayer1)
player1x=player1x+(player1speed * (movefactors[0] + movefactors[1]))
player1y=player1y+ (player1speed * (movefactors[2] + movefactors[3])/3)
player1direction=movefactor
frameplayer1:+1

'I'm not sure if this is what you intended for this part
frameplayer1 = frameplayer1 mod 3


That should be all of the code you need to cover all of the cases. No comparisons necessary. Just use data structures and numerical logic.


Oddball(Posted 2011) [#4]
I'm not quite sure if this is what you're after, because I can't quite understand why you end up with 64 If statements, but it might help.



Joe90bigrat90(Posted 2011) [#5]
hey thanks a lot ill try that later

The rason i end up with 64 blocks of if statements is because each player has 4 keys. For instance there are loads of combinations. Like player moving forward (w) and he may press backward(S) at the same time. You also need to check if they are pressing 3 keys down at the same time or 4 keys down at the same time. The player could move forward but then press backward at the same time. or he may be moving ne but may press down or left.

I have to check for all key presses to mkae sure the correct image is shown. So for instance if player is moving north and then he holds down south as well i set the direction to 5 for south.

So if he is moving foward and then he presses south i make sure to make the character go south.

Hope you understand what i mean


Joe90bigrat90(Posted 2011) [#6]
ok so will the above code also test in case the player is moving forward and he presses down and left at the same time????
as you know whatever direction the player is moving in it draws the correct animimage. Thats why I have to test for all key presses to draw the correct image on the screen. Otherwise if player moving forward and he presses south as well you would get 2 different animimages displaying.


Redspark(Posted 2011) [#7]
I think, the case statements only cover the normal compass points. Unless you list out all of your combinations, it wont cover the unusual ones.

With my code, the opposing compass points cancel each other out. (So forward and backward together give you no movement) Thus, the player does not move when both are pressed.


coffeedotbean(Posted 2011) [#8]
Joe can you post the image your using? easier to understand how to code movements.


Joe90bigrat90(Posted 2011) [#9]
thats pretty cool redspark

on the first line of your code you have:


movementsprites = array(9)

'do i actually type movementsprites=array(9) or do i actually choose my own array name here
movementsprites[0] = IdlePose
movementsprites[1] = EastAnimation
movementsprites[2] = SouthWestAnimation
movementsprites[3] = South
movementsprites[4] = SouthEast
movementsprites[5] = NorthWest
movementsprites[6] = North
movementsprites[7] = NorthEast
movementsprites[8] = West

do i actually type this in as its written i.e movementsprites[0]=idlepose
or should i be typing:
movementsprites[0]=LoadAnimImage ("d:\vendetta\sprites\player1east.png",49,49,0,4)
[code]
movefactors = array(4)

'again do i need to use my own array here as i dont think array(4) will work.

i understand the game loop ok no problem there.
Just need a bit of help with the starting arrays
could you give me a cool example.
Thanks a lot redspark


Joe90bigrat90(Posted 2011) [#10]
hi coffeedotbean

My program goes like this:
SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080

Global frameplayer1 'frame for player 1
Global frameplayer2 'frame for player 2
Global player1x 'store player 1 x position
Global player1y'store player 1 y position
Global player2x'store player 2 x position
Global player2y'store player 2 y position
Global player1direction '1=north, 3=east 5=south 7=west, 2=ne, 4=se,6=sw and 8=nw
Global player2direction 'same as for player1
Global player1speed 'the speed of player1
Global player2speed' the speed of player 2
Global player1name$ 'store name of player1
Global player2name$'store name of player 2

SeedRnd MilliSecs()

player1x=rnd(1870) draw player 1 at random position
player1y=Rnd(970)

player1direction=Rnd(1,8) draw player 1 at random facing direction

player1speed=5 'set the speed of player 1

'load all of the animations - player1
player1movingeast=LoadAnimImage ("d:\vendetta\sprites\player1east.png",49,49,0,4)
player1movingwest=LoadAnimImage ("d:\vendetta\sprites\player1west.png",49,49,0,4)
player1movingnorth=LoadAnimImage ("d:\vendetta\sprites\player1north.png",49,49,0,4)
player1movingsouth=LoadAnimImage ("d:\vendetta\sprites\player1south.png",49,49,0,4)
player1movingnorthwest=LoadAnimImage("d:\vendetta\sprites\player1northwest.png",49,49,0,4)
player1movingnortheast=LoadAnimImage("d:\vendetta\sprites\player1northeast.png",49,49,0,4)
player1movingsouthwest=LoadAnimImage("d:\vendetta\sprites\player1southwest.png",49,49,0,4)
player1movingsoutheast=LoadAnimImage("d:\vendetta\sprites\player1southeast.png",49,49,0,4)

While Not KeyDown(KEY_ESCAPE) 'main loop
Delay(40) 'slow the frames down

'check what key pressed, draw animation And move player1
'north - forward
	If KeyDown(KEY_W) And Not KeyDown(KEY_A) And Not KeyDown(KEY_D) And Not KeyDown(KEY_S)
	DrawImage(player1movingnorth,player1x,player1y,frameplayer1)
	player1y=player1y-player1speed
	player1direction=1
	frameplayer1:+1
	'ResumeChannel channel
	If frameplayer1>3 frameplayer1=0
	EndIf
'and of course for all other directions

'check to see if player 1 is on screen
	If player1x >1870 Then
	player1x=1870
	EndIf
	
	If player1x  <0 Then
	player1x=0
	EndIf
	
	If player1y <0 Then
	player1y=0
	EndIf
	
	If player1y >970 Then
	player1y=970
	EndIf
	
	Flip
Wend

EndGraphics
End

thast basically the majority of my program except for the randomness of displaying buildings on the screen. I left this out as not needed.

Be great if you could help me with this. Im only a beginner but thought id throw myself into the deep end to learn quickly.

Thanks for your help

Kind Regards
Joe


coffeedotbean(Posted 2011) [#11]
I'll post up how I would achieve this in about 2 hours when I get home - You might want to look at "types".

http://www.truplo.com/blitzmaxbeginnersguide/wave1.html - great for beginners.


Redspark(Posted 2011) [#12]
Hi Joe,

That's right. You need to change my fake code into proper Blitzbasic code. I wrote the pseudo code so that it was more understandable than functional. ;)

Let's see. I'm a bit rusty myself. I haven't used Blitz in a while. But an Array of TImages can be defined like this:

array = new type[size]

so

player1moving[] = new TImage[9]

So yes, you need to create proper arrays and load the animation as you wrote above. Each index in the array would contain a different animation. So instead of having the animations assigned to different variable names, you would have just the one array that has each index of the array containing a different direction of animation.

So using what you had above, it would look something like:
player1moving[0]=LoadAnimImage ("d:\vendetta\sprites\player1idle.png",49,49,0,4)
player1moving[1]=LoadAnimImage ("d:\vendetta\sprites\player1east.png",49,49,0,4)
player1moving[8]=LoadAnimImage ("d:\vendetta\sprites\player1west.png",49,49,0,4)
player1moving[6]=LoadAnimImage ("d:\vendetta\sprites\player1north.png",49,49,0,4)
player1moving[3]=LoadAnimImage ("d:\vendetta\sprites\player1south.png",49,49,0,4)
player1moving[5]=LoadAnimImage("d:\vendetta\sprites\player1northwest.png",49,49,0,4)
player1moving[7]=LoadAnimImage("d:\vendetta\sprites\player1northeast.png",49,49,0,4)
player1moving[2]=LoadAnimImage("d:\vendetta\sprites\player1southwest.png",49,49,0,4)
player1moving[4]=LoadAnimImage("d:\vendetta\sprites\player1southeast.png",49,49,0,4)


Do understand what the array is doing? It is a way of storing many pieces of the same type of data.


Redspark(Posted 2011) [#13]
I just realized that I changed the name of the array from movementsprites to player1moving so it was in line with your current code. So, in my first post, you will need to change all references of movementsprites to player1moving.

Also, you would define movefactors as:

movefactors[] = new Int[4]


Midimaster(Posted 2011) [#14]
I think you should separate the code for "turning commands" from the code with "drawing images".

The keys and the their combinations should only result in a new "direction".

In a second pass you draw corresponding to "direction" the correct animation sequence.

And there is also no need for checking the keys for both men simultanously in one "If"-structure. You can do this in two seperate steps. So you will not get 64, but only 18 different cases.

Which keys do you use for Player2?

I used your own code, that you can better understand the structure:
'check what key pressed....
If KeyDown(KEY_W) And Not KeyDown(KEY_A) And Not KeyDown(KEY_D) And Not KeyDown(KEY_S)
	'forward
	player1direction=1
ElseIf...
	player1direction=... ' 9 If's for "men 1" 
ElseIf...
 
EndIf


If KeyDown(KEY_I) And Not KeyDown(KEY_K) And Not KeyDown(KEY_L) And Not KeyDown(KEY_J)
	'forward
	player2direction=1
ElseIf...
	player2direction=... ' 9 If's for "men 2"
ElseIf...
 
EndIf


' 8  1  2
' 
' 7  0  3 
'
' 6  5  4


'...draw animation and move players

Select Player1Direction
	Case 1
		frameplayer1:+1
		If frameplayer1>3 frameplayer1=0
		DrawImage(player1movingnorth,player1x,player1y,frameplayer1)
		player1y=player1y-player1speed	
	Case ....
	
End Select



Select Player2Direction
	Case 1
		frameplayer2:+1
		If frameplayer2>3 frameplayer2=0
		DrawImage(player2movingnorth,player2x,player2y,frameplayer2)
		player2y=player2y-player2speed	
	Case ....
	
End Select

In a second step of course it would be better to optimize the Key-Checking by using the code of Redspark or Oddball....


Joe90bigrat90(Posted 2011) [#15]
ok ive keyed in the code to test it and i keep getting error message.

here is my code:


SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080
Global frameplayer1
Global player1x
Global player1y
Global player1speed
Global player2speed
Global numberofbuildings:Int=50
Global buildingx:Int[numberofbuildings]
Global buildingy:Int[numberofbuildings]

player1moving[]=New TImage[9]
movefactors[]=New Int[4]

SeedRnd MilliSecs()  


player1x=Rnd(1870)
player1y=Rnd(970)


player1speed=5 'change this value to slow or speed up player 1

'load all of the animations - player1
'player1moving[0]=LoadAnimImage ("d:\vendetta\sprites\player1idle.png",49,49,0,4)
player1moving[1]=LoadAnimImage("d:\vendetta\sprites\player1north.png",49,49,0,4)
player1moving[2]=LoadAnimImage("d:\vendetta\sprites\player1northeast.png",49,49,0,4)
player1moving[3]=LoadAnimImage("d:\vendetta\sprites\player1east.png",49,49,0,4)
player1moving[4]LoadAnimImage("d:\vendetta\sprites\player1southeast.png",49,49,0,4)
player1moving[5]LoadAnimImage("d:\vendetta\sprites\player1south.png",49,49,0,4)
player1moving[6]LoadAnimImage("d:\vendetta\sprites\player1southwest.png",49,49,0,4)
player1moving[7]LoadAnimImage("d:\vendetta\sprites\player1west.png",49,49,0,4)
player1moving[8]LoadAnimImage("d:\vendetta\sprites\player1northwest.png",49,49,0,4)



For a=0 To numberofbuildings-1
buildingx[a]=Rnd(1870) 'store random number in x upto 1870	
buildingy[a]=Rnd(970) 'store a random number in y upto 970
Next

SetClsColor 204,204,204

SetBlend(shadeblend)

'slow the game down to show animations
Delay(40)
'draw random buildings on the screen

While Not KeyDown(KEY_ESCAPE)	
	Cls
	typeofbuilding=Rnd(2)

	
		For a=0 To numberofbuildings-1
			SetBlend(solidblend)
			DrawImage (smallbuilding,buildingx[a],buildingy[a],0)
		Next

	SetBlend(shadeblend)	
	
	movefactors[0] = KeyDown(KEY_A)*-1 ' If going West, store -1
	movefactors[1] = KeyDown(KEY_D)*1 ' If going East, store 1
	movefactors[2] = KeyDown(KEY_S)*3 ' If going South store 3
	movefactors[3] = KeyDown(KEY_W)*-3 ' If going North store -3

	'This will give you possible results of 0, 1, 2, 3, 4, -1, -2, -3, -4
	movefactor = movefactors[0] + movefactors[1] + movefactors[2] + movefactors[3];

		If movefactor<0 Then movefactor = movefactor + 9  ' This forces the negative numbers to be in the range of 5 - 8


	DrawImage (player1moving[movefactor],player1x,player1y,frameplayer1)
	player1x=player1x+(player1speed * (movefactors[0] + movefactors[1]))
	player1y=player1y+ (player1speed * (movefactors[2] + movefactors[3])/3)
	player1direction=movefactor
	frameplayer1:+1

	'I'm not sure if this is what you intended for this part
	frameplayer1 = frameplayer1 Mod 3

	'check to see if player 1 is on screen
	If player1x >1870 Then	player1x=1870
	
	
	If player1x  <0 Then player1x=0
		
	If player1y <0 Then player1y=0
	
	
	If player1y >970 Then player1y=970
	
	Flip
Wend

EndGraphics
End


when i run the program i get expecting expression but encountered '='
on this line:

player1moving[]=New TImage[9]



Joe90bigrat90(Posted 2011) [#16]
please ignore the drawimage for small building. I have removed it just to test this program.


Joe90bigrat90(Posted 2011) [#17]
i think i have to put something in player1moving[]=New TImage[9]
movefactors[]=New Int[4]

what do i put in here. so player1moving should have 9 elements and movefactors 4 elements

do i declare it like this:
movefactors[1]= something im not sure what to put here.


Redspark(Posted 2011) [#18]
There are two ways to do it. You can declare the variable like this:

player1moving:TImage[9]

Or you can declare and assign it like:

player1moving[] = new TImage[9]

either way should work. If you are getting an error on one, try the other method.

Last edited 2011


Redspark(Posted 2011) [#19]
Ooops I made a mistake in my declaration. That's why it's blowing up. Try this instead.

Global player1moving:TImage[] =New TImage[9]

Last edited 2011


Joe90bigrat90(Posted 2011) [#20]
ok ive got all the code in now but im getting an error message which reads:

Unhandled Exception:Attempt to index array element beyond array length and pointing to thiss line

[code]
DrawImage (player1moving[movefactor],player1x,player1y,frameplayer1) <<<this is line with error
player1x=player1x+(player1speed * (movefactors[0] + movefactors[1]))
player1y=player1y+ (player1speed * (movefactors[2] + movefactors[3])/3)
player1direction=movefactor
frameplayer1:+1


any help on this much appreciated.


Joe90bigrat90(Posted 2011) [#21]
ok ive figured out what was wrong. Its working great.
Thanks for that redspark. Youre a genius. hehehe

Just one thing though for the player1idle image

if im moving north and i stop moving how do i display the correct idle image so that it looks like hes stopped.

I mean i need 8 different images dont i?

so if youre moving sw and you stop i need to display a sw idle graphic.

Is there anyway i can do this.

Thanks a lot for your help

cheers

Joe


Redspark(Posted 2011) [#22]
LOL! I'm just glad it worked. ;)

Mmmmm... You're right. Do you need any movement in the idle animation? There are a couple of things that can be done. If one of the frames of each of the animations is suitable for an idle frame, then you could just check if you have a positive movefactor and display the normal animation else display only that one frame. Then your DrawImage command would look something like this:

' Check if movefactor is a positive value, 0 being false
if movefactor then
DrawImage (player1moving[movefactor],player1x,player1y,frameplayer1)
else
DrawImage(player1moving[player1direction],player1x,player1y, IdleFrame)
end if


Replace IdleFrame with the proper number of the frame within the animation that you want to use as your idle.

Or you could make a separate idle animation 8 frames in size. Each frame has the player pointing in each of the 8 directions and replace the single DrawImage with something like:

' Check if movefactor is a positive value, 0 being false
if movefactor then
DrawImage (player1moving[movefactor],player1x,player1y,frameplayer1)
else
DrawImage(player1moving[movefactor],player1x,player1y,player1direction)
end if


You may also have to then prevent the player1direction from being replaced each time when you have a 0 movefactor. So change your player1direction=movefactor to:

if movefactor then player1direction = movefactor


That should preserve the direction the player was heading in before he halted and let the last DrawImage to keep the player pointing in that direction when movefactor = 0.

If you need an animated idle, then that is still possible but it complicates things. I would have a second 9 element array specifically for the idle animations (player1idle for instance). Just like player1moving. Then instead of using the player1moving array to draw the image when movefactor=0, you would use the player1idle array.

This is again off the top of my head so there are probably bugs in my theory. Just make sure that player1direction starts with a positive value like 1 and you should be ok. Also, becareful of your frameplayer. It should only increment when the player is moving.

I'm going out tonight so I wont be able to respond anymore today. I'm afraid you'll be on your own, if you get stuck. But it sounds like you can handle it. :)

Last edited 2011


Joe90bigrat90(Posted 2011) [#23]
ok ive got an idle animimage containing 19 frames. If the player doesnt touch a key then i play this animimage. However this is strange

im not sure what mod command is used for but this line
if i leave it as it is only plays a few frames of the animation.

frameplayer1 = frameplayer1 Mod 3

if i rem this line out
it plays the animation then crashes with unhandled exception: attempt to index array element beyond array length and points to this line

DrawImage (player1moving[movefactor1],player1x,player1y,frameplayer1)

its something to do with the frameplayer i think.

Any help much appreciated.


Joe90bigrat90(Posted 2011) [#24]
im just playing one animimage with 19 frames if the player does not touch any keys. The man faces forward and takes off his hat so dont need any direction idle images. Just one animimage with 19 frames.


Redspark(Posted 2011) [#25]
What frameplayer1 = frameplayer1 Mod 3 does is keeps the number between 0 and 2. So as soon as frameplayer1 becomes 3, it changes it to 0.

The Mod operator is known as Modulus. In Math, it's called the remainder. So when you divide a number by another, there is the number of times that number went into the divisible number and a leftover amount.

So, in this case, say frameplayer1 is 2. 3 goes into 2, 0 times with a remainder of 2. So frameplayer1 would continue to be 2. But the next loop through, frameplayer1 is incremented to 3. 3 goes into 3, 1 times with 0 remainder. Therefore, frameplayer1 Mod 3 would return 0 in that case and start the animation from the beginning again.


Redspark(Posted 2011) [#26]
If you are using a 19 frame animation which is much larger than your prior moving animations, you need to allow frameplayer1 to increment to 18 and wrap back to 0 or stop at 18.

In the if movefactor then player1direction = movefactor block of code, change that to:

if movefactor then 
player1direction = movefactor
frameplayer1 = frameplayer1 Mod 3
else
frameplayer1 = frameplayer1 Mod 19
endif


If you don't want the animation to wrap to the beginning, change the frameplayer1 = frameplayer1 Mod 19 statement to:

if frameplayer1>18 then frameplayer1=18


That will hold the animation at the final frame forever. Otherwise, reset the frame back to any appropriate prior frame so that it can keep looping.

Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#27]
ahhh ok i see that makes sense. thankyou.

Last edited 2011


Redspark(Posted 2011) [#28]
If you want to use the 8 frame idle animation instead, then, technically, an animation is just a series of still images. So you can trick the system into using just one frame over and over again as your still image.

You have a variable frameplayer1 that is telling DrawImage which frame of the animation to use for that loop. You also have a variable that stores a number that represents the direction of the player called player1direction. If we swap those variables in the DrawImage command, then it will continually draw that one frame out of the animation because that variable doesn't change value unless the player moves.

So when the player stops moving, we use player1direction as the frame. When they are moving, we use frameplayer1. That's what that IF statement does.

Now, you just have to create the animation so that each frame lines up with the proper direction. Look to the indexes of the player1moving array to see which frame should be in what direction.

Last edited 2011


Joe90bigrat90(Posted 2011) [#29]
yeah ive decided to use your code for a still image in every direction.


' Check if movefactor is a positive value, 0 being false
if movefactor then
DrawImage (player1moving[movefactor],player1x,player1y,frameplayer1)
else
DrawImage(player1moving[movefactor],player1x,player1y,player1direction)
end if

Thing is though if i draw an animimage 8 frames wehn i run this code wont it just play all 8 directions and draw the animimage. Would it be better to use loadimage for a still image. I would prefer using an animimage but i mean thats animation so it would just draw all 8 directions.


Redspark(Posted 2011) [#30]
An animation will work because you can control which frame of the animation is being displayed. What you are going to do with that if statement is keep just 1 frame of the animation being displayed rather than the frames incrementing.

Look at the two DrawImage statements. What variables in those two statements are being used to determine which frame of the animation to play? In the first, it is frameplayer1. In the second, it is player1direction. So the first DrawImage plays each frame of the animation because your frameplayer1 increases by 1 each game loop. Whereas, player1direction only changes each loop when the player hits one of the direction keys (WASD). So, player1direction can be used to say which frame of the idle animation we want to display each loop and it will keep displaying only that frame and not the rest of the animation.

If you want to prove it to yourself to make sure the theory is sound, try using a DrawText statement to display the value of frameplayer1 and player1direction on the screen each loop. Then run the game and move around with your player. Watch how the values change. You will see that frameplayer1 rapidly changes all of the time while player1direction only changes when you change the direction of the player using the keyboard. :)


coffeedotbean(Posted 2011) [#31]
Joe this is how would do it(in the style your using), copy this image to same location as you run this code from (save image as toon.png);



Graphics 640,480,0

' Load player images from a single image
Global Player1_North:TImage 		= LoadAnimImage("toon.png",32,50,28,4)
Global Player1_East:TImage 		= LoadAnimImage("toon.png",32,50,20,4)
Global Player1_South:TImage 		= LoadAnimImage("toon.png",32,50,4,4)
Global Player1_West:TImage		= LoadAnimImage("toon.png",32,50,12,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("toon.png",32,50,24,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("toon.png",32,50,16,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("toon.png",32,50,8,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("toon.png",32,50,0,4)

' Will hold what image needs to be drawn based on keys pressed
Global Player1_Image:TImage = Player1_South

' Some player variables
Global Player1_Moving = False
Global Player1_xPos = GraphicsWidth()/2
Global Player1_yPos = GraphicsHeight()/2
Global Player1_Speed = 2
Global Player1_Frame = 0
Global Player1_MaxFrames = 4
Global Player1_FrameTimer
Global Player1_FrameDelay = 120

While Not KeyDown(KEY_ESCAPE)
Cls

Move()
Draw()

Flip
Wend
End

Function Draw()
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	' Draw the player
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	
End Function

Function Move()
	
	Local Up_Pressed = False
	Local Down_Pressed = False
	Local Right_Pressed = False
	Local Left_Pressed = False
	
	' Get what keys are pressed
	If KeyDown(KEY_UP) Then Up_Pressed = True
	If KeyDown(KEY_Down) Then Down_Pressed = True
	If KeyDown(KEY_Right) Then Right_Pressed = True
	If KeyDown(KEY_Left) Then Left_Pressed = True
	
	' Change player image and move the player based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1_yPos = Player1_yPos - Player1_Speed
		' If up and another key is pressed, ignore above do one of the two below.
		If Right_Pressed = True Player1_Image = Player1_NorthEast ; Player1_xPos = Player1_xPos + Player1_Speed/2
		If Left_Pressed = True Player1_Image = Player1_NorthWest ; Player1_xPos = Player1_xPos - Player1_Speed/2
	ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True Player1_Image = Player1_SouthEast ; Player1_xPos = Player1_xPos + Player1_Speed/2
		If Left_Pressed = True Player1_Image = Player1_SouthWest ; Player1_xPos = Player1_xPos - Player1_Speed/2
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True Player1_Image = Player1_NorthEast ; Player1_yPos = Player1_yPos + Player1_Speed/2
		If Down_Pressed = True Player1_Image = Player1_SouthEast ; Player1_yPos = Player1_yPos - Player1_Speed/2
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True Player1_Image = Player1_NorthWest ; Player1_yPos = Player1_yPos + Player1_Speed/2
		If Down_Pressed = True Player1_Image = Player1_SouthWest ; Player1_yPos = Player1_yPos - Player1_Speed/2
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

End Function



Joe90bigrat90(Posted 2011) [#32]
hey thanks coffeedot bean thats really cool of you to post this. Ive run the program and im getting an error.
heres my code
SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080
Global frameplayer1
Global frameplayer2
Global Player1_moving=False
'Global Player1_Image:TImage =Player1_South


Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120


Global Player1_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1north.png",50,50,0,4)
Global Player1_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1south.png",50,50,0,4)
Global Player1_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player1west.png",50,50,0,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northeast.png",50,50,0,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northwest.png",50,50,0,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southeast.png",50,50,0,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southwest.png",50,50,0,4)

While Not KeyDown(KEY_ESCAPE)	
	Cls
	
	For a=0 To numberofbuildings-1
	SetBlend(solidblend)
	DrawImage (building,buildingx[a],buildingy[a],0)
	Next
	SetBlend(shadeblend)
	
	Move()
	Draw()

	Flip
	Wend
	End

	Function Draw()
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	' Draw the player
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	
End Function

Function Move()
	
	Local Up_Pressed = False
	Local Down_Pressed = False
	Local Right_Pressed = False
	Local Left_Pressed = False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	
	' Change player image and move the player based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1_yPos = Player1_yPos - Player1_Speed
		' If up and another key is pressed, ignore above do one of the two below.
		If Right_Pressed = True Player1_Image = Player1_NorthEast ; Player1_xPos = Player1_xPos + Player1_Speed/2
		If Left_Pressed = True Player1_Image = Player1_NorthWest ; Player1_xPos = Player1_xPos - Player1_Speed/2
	ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True Player1_Image = Player1_SouthEast ; Player1_xPos = Player1_xPos + Player1_Speed/2
		If Left_Pressed = True Player1_Image = Player1_SouthWest ; Player1_xPos = Player1_xPos - Player1_Speed/2
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True Player1_Image = Player1_NorthEast ; Player1_yPos = Player1_yPos + Player1_Speed/2
		If Down_Pressed = True Player1_Image = Player1_SouthEast ; Player1_yPos = Player1_yPos - Player1_Speed/2
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True Player1_Image = Player1_NorthWest ; Player1_yPos = Player1_yPos + Player1_Speed/2
		If Down_Pressed = True Player1_Image = Player1_SouthWest ; Player1_yPos = Player1_yPos - Player1_Speed/2
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

End Function


keeps brinigng up an error message
attempt to access field or method of null object
on this line

DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame

many thanks for posting this coffee. Its helped me a lot to understand the coding. If you could figure out why this isnt working. Probably just something silly i missed. Thanks


Joe90bigrat90(Posted 2011) [#33]
its ok i had a line remmed out. hehehe
Cool it works perfectly and i understand the code much easier. NOt saying you are a better programmer than redspark but its great of both of you to help me out here.

Thanks for your help to both of you.

Much Appreciated.

Cheers

Joe


coffeedotbean(Posted 2011) [#34]
No problem, sometimes it's nice to go back to these "simple" (no offence) programs - we all started here =D

Happy to help with any other questions or question about the code I posted.

Last edited 2011


Joe90bigrat90(Posted 2011) [#35]
thanks a lot. Code is working superbly and I have written adjustments so 2 players can run on the screen in all directions. Working great. I wonder if you could point me in the right direction about doing this.

Currently i draw random blocks to the screen (buildings) and it creates so many buildings depending on what the player selects. Buildings can be 1 to 100.

ive got 2 problems. The blocks obviously are going to overlap sometimes. how do i stop this from happening? Would i need to sort of create a grid somehow and maybe create an array to store the x and y of the blocks. And at the moment the players can walk through the blocks. How do i make the blocks solid. Do i test for collision using imagecollide or do i write code to say when the player is on the block stop them from going on the block. If you could give me a pointer about the best way to do this that be great. Thankyou again for all your help.
Cheers
Joe


coffeedotbean(Posted 2011) [#36]
This a simple array method;

You can change the building size to say 64 for example and the player will still collide correctly, you can also play with player size. Hope this helps you.

To get the buildings I made a 2 dimensional array, decided my building will be 32x32 in size (can be changed of course) and used that and the specified width and height of my graphics call to set the width and height of my array so it fills the screens. Then I run through the array and add a building "1" or no building "0".

It all kicks off in the new Building_Hit() function, this is called just before any move request to see if there will be a building in the way if the player did move in that direction. Function will return True if a building will be in the way and False if not.


Graphics 640,480,0

' Load player images from a single image
Global Player1_North:TImage 		= LoadAnimImage("toon.png",32,50,28,4)
Global Player1_East:TImage 		= LoadAnimImage("toon.png",32,50,20,4)
Global Player1_South:TImage 		= LoadAnimImage("toon.png",32,50,4,4)
Global Player1_West:TImage		= LoadAnimImage("toon.png",32,50,12,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("toon.png",32,50,24,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("toon.png",32,50,16,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("toon.png",32,50,8,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("toon.png",32,50,0,4)

' Will hold what image needs to be drawn based on keys pressed
Global Player1_Image:TImage = Player1_South

' Some player variables
Global Player1_Moving = False
Global Player1_xPos = GraphicsWidth()/2
Global Player1_yPos = GraphicsHeight()/2
Global Player1_Speed = 2
Global Player1_Frame = 0
Global Player1_MaxFrames = 4
Global Player1_FrameTimer
Global Player1_FrameDelay = 120
Global Player1_Size = 32
Global Building_Size = 32
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height = GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]
Make_Buildings(50)

While Not KeyDown(KEY_ESCAPE)
Cls

	Move()
	Draw()

Flip
Wend
End

Function Make_Buildings(Building_Count)
	
	SeedRnd MilliSecs()
	For i = 0 To Building_Count
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			Endif
		Forever
	Next
	
End Function

Function Draw()
	
	' Draw builds - using a DrawRect rather than an image to keep it simple.
	For x = 0 To World_Width
	For y = 0 To World_Height
		If Building_Grid[x , y] = 1 Then DrawRect (x * Building_Size) + 1 , (y * Building_Size) + 1 , Building_Size - 2 , Building_Size - 2
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	' Draw the player
	SetScale 1.0 , 0.64 ' squashed player so it looks like a 32x32 size sprite
	DrawImage Player1_Image , Player1_xPos , Player1_yPos , Player1_Frame
	SetScale 1.0 , 1.0
	
End Function

Function Hit_Building(Direction)
	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	
	Select Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
	End Select
	
	Return False
	
End Function

Function Move()
	
	Local Up_Pressed = False
	Local Down_Pressed = False
	Local Right_Pressed = False
	Local Left_Pressed = False
	
	' Get what keys are pressed
	If KeyDown(KEY_UP) Then Up_Pressed = True
	If KeyDown(KEY_Down) Then Down_Pressed = True
	If KeyDown(KEY_Right) Then Right_Pressed = True
	If KeyDown(KEY_Left) Then Left_Pressed = True
	
	' Change player image and move the player based on what keys are pressed.
	If Up_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_North
		If Hit_Building(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		Endif
	ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		If Hit_Building(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		Endif
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		Endif
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		endif
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

End Function


Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#37]
wow coffee tested and works first time amazing. I just got one problem. Maybe its something to do with my images i dont know.
If i move up sprite moves right up to the block which is great. If i move down sprite moves up to the block. Also cool. If i move right sprite moves up to the block. Cool again. But if i move left(west) there is a gap. Sprite does not get close to the block( well fairly close ) but it does not look cool. You move left to touch the block and there is a gap (probably due to the sprite size being 50x50.) But then why do all the other directions work and left doesnt. I dont think its a problem with your programming. i think its something to do with my sprite.

Any idea - i could make the sprites smaller and see what happens but probably the same thing would happen again.

Its great you posting code like this. I learn a lot from you and I will apply it to other things.
I enjoy reading your code and working out whats going on. I find your code a lot easier to read than looking at loads of types, arrays and stuff.

My sprites are 50x50 and they are in the middle of the image. Shall i create new sprites without a left hand edge so draw the sprites as close as i can get to the left hand side. That might sort the problem out. But then theres also a right hand gap on the sprite but running program it works ok. Strange. So up right and down work. Just got problems with going left.

Thanks for your help cofffeee

Cheers
joe

Last edited 2011


coffeedotbean(Posted 2011) [#38]
My example assumes the sprite fills the 50x50 on all sides.

Best way to test the code is to comment out the line to draw the player and replace it with a drawrect

such as;

Setcolor 255,0,0
drawrect Player1_xPos , Player1_yPos , Player1_Size , Player1_Size
setcolor 255,255,255


If all sides touch then it's the image. But then the code might need to be changed to suite the sprite, right now it's very basic it simply checks for example the top right (Player1_xPos) and top left (Player1_xPos + Player1_Size) it doesn't care what the image is just the space it occupies with is 50x50, so if the image has gaps around the edges then it will look odd.

Changes might need to be made to the Hit_Building() function to better specify the collision box.


Joe90bigrat90(Posted 2011) [#39]
ok instead of a rectangle i want to draw my own image
its just a single frame no animation

so

building=loadimage("building.png")

what do i need to put on this line@

If Building_Grid[x , y] = 1 Then DrawRect (x * Building_Size) + 1 , (y * Building_Size) + 1 , Building_Size - 2 , Building_Size - 2


so instead of drawrect i need to drawimage

would i put this

If Building_Grid[x , y] = 1 Then Drawimage(building,x * Building_Size + 1 , y * Building_Size + 1 , Building_Size - 2 , Building_Size - 2

nah wait a minute drawimage has 3 parameters. image name,x and y.
drawrect has 4 parameters. Hmmmm how do i do this.


coffeedotbean(Posted 2011) [#40]
If Building_Grid[x , y] = 1 Then DrawImage building , x * Building_Size , y * Building_Size


Dont worry about the + 1 and - 2 stuff that was just used to show a gap between the rectangles.

Last edited 2011


ImaginaryHuman(Posted 2011) [#41]
If you have lots of if statements where it's like doing a different thing based on a value - like in a Select-case statement, then it might possibly be faster to have an array of function pointers and then call the function based on the index - a variation of old-school `jump tables`.

Last edited 2011


Joe90bigrat90(Posted 2011) [#42]
Hi coffeedot bean

Ive copied that line into my program and i keep getting error message. Attempt to access field of null object.

If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size, y * Building_Size,0

Am I doing something wrong???

thanks


coffeedotbean(Posted 2011) [#43]
In my example make sure your image is called 'building'.

 
Global building = loadimage("...")



Joe90bigrat90(Posted 2011) [#44]
Ok excellent. I changed the Player1_Size to 50 and now its working ok. Im putting the character in a random position each time and thats working fine. However the blocks arnt being randomly placed on the screen. Every time i run it they are in the same location.
How do i get random blocks.


also if the user presses 5 on the title screen i want to increase the number of buildings.
currently Make_Buildings is equal to 50.
Make_Buildings(50)

when i press number 5 on the title screen i want to change the number of buildings. How do i increase or redim an array.

Make_Buildings(50) = Make_buildings(50+1) i know wont work.
Is there a way of increasing the array number when user presses '5'.

this is the code you gave me for Make_Buildings.
It doesnt seem to be randomly placing buildings.
They are always in the same place.


Function Make_Buildings(Building_Count)

For i = 0 To Building_Count
Repeat
Local x = Rand(0 , World_Width)
Local y = Rand(0 , World_Height)
If Building_Grid[x , y] = 0
Building_Grid[x , y] = 1
Exit
EndIf
Forever
Next

End Function


coffeedotbean(Posted 2011) [#45]
As to increasing the number of buildings... The array will never change (in this example), we just need to call the Make_Buildings() function with a different value of "Building_Count". Example below shows this.

As to the buildings not being placed randomly is cus you've taken out of the seedrnd call in the function.

Graphics 640 , 480 , 0

Global AValue = 50
Global AValue_Max = 200
Global AValue_Min = 0
Global Game_Started = False

Global Building_Size = 32
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height = GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]

While Not KeyDown(KEY_ESCAPE)
Cls

	TitleScreen()
	Draw()
	Game()
Flip
Wend
End

Function TitleScreen()
	
	If Game_Started = True Then Return ' Exit this function NOW as the game has started
	
	DrawText "Title Screen" , 10 , 10
	DrawText "Press 5 to increase value" , 10 , 30
	DrawText "Press 6 to decrease value" , 10 , 50
	DrawText "Value = "+AValue , 10 , 70
	DrawText "Press SPACE to start" , 10 , 90
	
	Local Value_Increase_Step = 5
	
	If KeyHit(KEY_5)
		AValue = AValue + Value_Increase_Step
		If AValue > AValue_Max Then AValue = AValue_Max
	ElseIf KeyHit(KEY_6)
		AValue = AValue - Value_Increase_Step
		If AValue < AValue_Min Then AValue = AValue_Min
	ElseIf KeyHit(KEY_SPACE)
		Game_Started = True
		Make_Buildings(AValue)
	Endif
	
End function

Function Game()
	
	If Game_Started = False Then Return ' Exit this function NOW as the game has not started
	
	SetColor 155,155,0
	DrawText "Game Screen" , 10 , 10
	DrawText "Number of buildings = " + AValue , 10 , 30
	DrawText "Press SPACE to got to the titel screen" , 10 , 50
	SetColor 255,255,255

	
	If KeyHit(KEY_SPACE) Then Game_Started = False
	
End Function

Function Draw()
	
	If Game_Started = False Then Return ' Exit this function NOW as the game has not started
	
	' Draw builds - using a DrawRect rather than an image to keep it simple.
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetColor 55,55,55
		If Building_Grid[x , y] = 1 Then DrawRect (x * Building_Size) + 1 , (y * Building_Size) + 1 , Building_Size - 2 , Building_Size - 2
		SetColor 255,255,255
	Next
	Next

End Function

Function Make_Buildings(Building_Count)
	
	Clear_Buildings() ' Clear our buildings
	
	' Set a random seed value and place out buildings
	SeedRnd MilliSecs()
	For i = 0 To Building_Count
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function


Last edited 2011


Joe90bigrat90(Posted 2011) [#46]
cool thanks a lot. Right i think thats all the help i need. I will try and do the other stuff myself. Thanks for all of your help. Youve been great. Its nice looking at your code and learning stuff. Ive learnt so much just from looking at your examples. Really helped me a lot. I will use these examples in other programs. But now onto my game. hehehe thankyou so much for your time and patience. Its nice to know you help people with code. Not many bother with newbies but you have been fantastic. I wish you well for the future coffee. Keep up the programming. Hey do you work as a programmer. you should go into it. Get into a games company in london or something. I reckon you would be cool at writing games for a major company. OK have fun if i get stuck ill let you know. hehe seeya coffee


Joe90bigrat90(Posted 2011) [#47]
jesus christ I had a hard disk crash and lost my game. Well gutted. Got my drive back up and running and reentered as much code as I could. Everything working how it should except the buildings arnt being displayed on the screen. Ive tried to code as much back as I had before.(Took me all day) to start again. Ive made backups this time on cd in case i lose it. If you dont mind coffee can you take a look at this for me and try and tell me why it isnt displaying the buildings. Thanks. Cheers.

SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080

'variables for player 1
Global Player1_moving=False
Global Player1_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120
Global Player1_Size = 50

'variables for player 2
Global Player2_moving=False
Global Player2_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player2west.png",50,50,0,4)
Global Player2_xPos
Global Player2_yPos
Global Player2_Speed=5
Global Player2_Frame=0
Global Player2_MaxFrames=4
Global Player2_FrameTimer
Global Player2_FrameDelay=120
Global Player2_Size = 32

'general variables

Global numberofbuildings = 50
Global numberofbuildings_Max = 100
Global numberofbuildings_Min = 0
Global Building_Size = 50
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]
Global playerspeed
Global grid:String
Global vehicles:String

Local tickerx#=1920
Local font:TImageFont = LoadImageFont("d:\vendetta\fonts\impact.ttf", 50) 
Local font2:TImageFont =LoadImageFont("d:\vendetta\fonts\impact.ttf",30) 

'load the images into the variables
Global Player1_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1north.png",50,50,0,4)
Global Player1_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1south.png",50,50,0,4)
Global Player1_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player1west.png",50,50,0,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northeast.png",50,50,0,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northwest.png",50,50,0,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southeast.png",50,50,0,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southwest.png",50,50,0,4)


'load the images into the variables
Global Player2_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player2north.png",50,50,0,4)
Global Player2_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player2east.png",50,50,0,4)
Global Player2_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player2south.png",50,50,0,4)
Global Player2_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player2west.png",50,50,0,4)
Global Player2_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2northeast.png",50,50,0,4)
Global Player2_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2northwest.png",50,50,0,4)
Global Player2_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2southeast.png",50,50,0,4)
Global Player2_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2southwest.png",50,50,0,4)

Global building=LoadImage("d:\vendetta\sprites\smallbuilding.png",0)

'load all of the icons and screen on title screen 
titlescreen=LoadImage("d:\vendetta\screens\title.bmp")
player1icon=LoadImage("d:\vendetta\sprites\player1east1.bmp")
player2icon=LoadImage("d:\vendetta\sprites\player2west1.bmp")
bonuspointicon=LoadImage("d:\vendetta\sprites\bonuspoint.bmp")
bouncingbulleticon=LoadImage("d:\vendetta\sprites\bouncingbullet.bmp")
choiceicon=LoadImage("d:\vendetta\sprites\choice.bmp")
laserbulleticon=LoadImage("d:\vendetta\sprites\laserbullet.bmp")
radarjamicon=LoadImage("d:\vendetta\sprites\radarjam.bmp")
seekingmissilebulleticon=LoadImage("d:\vendetta\sprites\seekingmissilebullet.bmp")
speedybootsicon=LoadImage("d:\vendetta\sprites\speedyboots.bmp")
speedybulleticon=LoadImage("d:\vendetta\sprites\speedybullet.bmp")
supermapicon=LoadImage("d:\vendetta\sprites\supermap.bmp")
tankicon=LoadImage("d:\vendetta\sprites\tank.bmp")
blockicon=LoadImage("d:\vendetta\sprites\block.bmp")
teleporticon=LoadImage("d:\vendetta\sprites\teleport.bmp")
turningmissilebulleticon=LoadImage("d:\vendetta\sprites\turningmissilebullet.bmp")
wallbulleticon=LoadImage("d:\vendetta\sprites\wallbullet.bmp")
buildingicon=LoadImage("d:\vendetta\sprites\smallbuilding.png")
titlemusic=LoadSound("d:\vendetta\sounds\vendetta.wav",True)
channel=PlaySound(titlemusic)

While Not KeyHit(KEY_ESCAPE)


Move()
Draw()
Conditions()

Flip
Wend
End

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function


Function Draw()
	SetClsColor 204,204,204
	Cls
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		'SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetBlend(shadeblend)
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	'DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'DrawRect Player1_xPos , Player1_yPos , Player1_Size , Player1_Size

	'SetScale 1.0,1.0
End Function

Function Make_Buildings(Building_Count)
	
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function


Function Hit_Building(Direction)
	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	
	Select Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
	End Select
	
	Return False
	
End Function


Function Move()
	
	Local Up_Pressed = False
	Local Down_Pressed = False
	Local Right_Pressed = False
	Local Left_Pressed = False
	
	Local Up_Pressed2=False
	Local Down_Pressed2=False
	Local Right_Pressed2=False
	Local Left_Pressed2=False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	
	If KeyDown(KEY_UP) Then Up_Pressed2 = True
	If KeyDown(KEY_DOWN) Then Down_Pressed2 = True
	If KeyDown(KEY_RIGHT) Then Right_Pressed2 = True
	If KeyDown(KEY_LEFT) Then Left_Pressed2 = True

	
	
	' Change player image and move the player based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		If Hit_Building(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		If Hit_Building(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	


Function Conditions()
	'check to see if player 1 is on screen
	If player1_xPos >1870 Then player1_xPos=1870
	If player1_xPos  <0 Then player1_xPos=0
	If player1_yPos <0 Then player1_yPos=0
	If player1_yPos >970 Then player1_yPos=970
	If player2_xPos >1870 Then player2_xPos=1870
	If player2_xPos  <0 Then player2_xPos=0
	If player2_yPos <0 Then player2_yPos=0
	If player2_yPos >970 Then player2_yPos=970

End Function
	


I cant figure out why its not drawing any buildings on the screen.
Ive checked through all the code and cant find anything thats missing.
Any help be great so i can get my program to what is was before.
Cheers
Kind Regards
Joe


coffeedotbean(Posted 2011) [#48]
From a quick look I don't think you have called the Make_Buildings() function.

Also a tip for your Conditions() function, as you know the width and height of the player "player_size" you can make it so no matter the screen resolution you'll never need to edit this function again, just change the player_size if it changes.

Always good to get into the habit of not hardcoding numbers cus things can change as a project grows.

Function Conditions()

If Player1_xPos + Player1_Size > GraphicsWidth() then Player1_xPos = GraphicsWidth() - Player1_Size 
If Player1_xPos < 0 then Player1_xPos = 0
If Player1_yPos + Player1_Size > GraphicsHeight() then Player1_yPos = GraphicsHeight() - Player1_Size 
If Player1_yPos < 0 then Player1_yPos = 0

If Player2_xPos + Player2_Size > GraphicsWidth() then Player2_xPos = GraphicsWidth() - Player2_Size 
If Player2_xPos < 0 then Player1_xPos = 0
If Player2_yPos + Player2_Size > GraphicsHeight() then Player2_yPos = GraphicsHeight() - Player2_Size 
If Player2_yPos < 0 then Player2_yPos = 0

End Function



Joe90bigrat90(Posted 2011) [#49]
cool thanks for those tips ive edited the code to support this. However im still having problems with the buildings. If i put a line saying MakeBuildings(Building_Count)
it just continously draws a random building over and over again. Looking at your code i cant see anywhere where you call Make_Buildings
.
You call functions Titlescreen() Draw() and Game() but you do not call function Make_Buildings.

I dont understand this cos ive copied your code exactly and its not working. Weird. Here is my entire code. The players on the screen dont even get stuck (say the blocks were invisible) so i know its not drawing any buildings. I have renamed Global AValue = 50 to numberofbuildings=50
Global AValue_Max = 200 to global numberofbuildings_Max =200 and
Global AValue_Min = 0 to Global numberofbuildings=0

Here is my entire code. I still cant figure out why its not drawing the buildings when ive copied the code exactly and just changed a few variables.

Weird!!!!

SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080

'variables for player 1
Global Player1_moving=False
Global Player1_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120
Global Player1_Size = 50
Global Player1_Score=0

'variables for player 2
Global Player2_moving=False
Global Player2_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player2west.png",50,50,0,4)
Global Player2_xPos
Global Player2_yPos
Global Player2_Speed=5
Global Player2_Frame=0
Global Player2_MaxFrames=4
Global Player2_FrameTimer
Global Player2_FrameDelay=120
Global Player2_Size = 50
Global Player2_Score=0
'general variables

Global numberofbuildings = 50
Global numberofbuildings_Max = 100
Global numberofbuildings_Min = 0
Global Building_Size = 50
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]
Global playerspeed
Global grid:String
Global vehicles:String
Global music:String
Global sfx:String

Local tickerx#=1920
Local font:TImageFont = LoadImageFont("d:\vendetta\fonts\impact.ttf", 50) 
Local font2:TImageFont =LoadImageFont("d:\vendetta\fonts\impact.ttf",30) 

'load the images into the variables
Global Player1_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1north.png",50,50,0,4)
Global Player1_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1south.png",50,50,0,4)
Global Player1_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player1west.png",50,50,0,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northeast.png",50,50,0,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northwest.png",50,50,0,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southeast.png",50,50,0,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southwest.png",50,50,0,4)


'load the images into the variables
Global Player2_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player2north.png",50,50,0,4)
Global Player2_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player2east.png",50,50,0,4)
Global Player2_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player2south.png",50,50,0,4)
Global Player2_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player2west.png",50,50,0,4)
Global Player2_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2northeast.png",50,50,0,4)
Global Player2_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2northwest.png",50,50,0,4)
Global Player2_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2southeast.png",50,50,0,4)
Global Player2_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player2southwest.png",50,50,0,4)

Global building=LoadImage("d:\vendetta\sprites\smallbuilding.png",0)

'load all of the icons and screen on title screen 
titlescreen=LoadImage("d:\vendetta\screens\title.bmp")
mapscreen=LoadImage("d:\vendetta\screens\mapscreen.bmp")
player1icon=LoadImage("d:\vendetta\sprites\player1east1.bmp")
player2icon=LoadImage("d:\vendetta\sprites\player2west1.bmp")
bonuspointicon=LoadImage("d:\vendetta\sprites\bonuspoint.bmp")
bouncingbulleticon=LoadImage("d:\vendetta\sprites\bouncingbullet.bmp")
choiceicon=LoadImage("d:\vendetta\sprites\choice.bmp")
laserbulleticon=LoadImage("d:\vendetta\sprites\laserbullet.bmp")
radarjamicon=LoadImage("d:\vendetta\sprites\radarjam.bmp")
seekingmissilebulleticon=LoadImage("d:\vendetta\sprites\seekingmissilebullet.bmp")
speedybootsicon=LoadImage("d:\vendetta\sprites\speedyboots.bmp")
speedybulleticon=LoadImage("d:\vendetta\sprites\speedybullet.bmp")
supermapicon=LoadImage("d:\vendetta\sprites\supermap.bmp")
tankicon=LoadImage("d:\vendetta\sprites\tank.bmp")
blockicon=LoadImage("d:\vendetta\sprites\block.bmp")
teleporticon=LoadImage("d:\vendetta\sprites\teleport.bmp")
turningmissilebulleticon=LoadImage("d:\vendetta\sprites\turningmissilebullet.bmp")
wallbulleticon=LoadImage("d:\vendetta\sprites\wallbullet.bmp")
buildingicon=LoadImage("d:\vendetta\sprites\smallbuilding.png")
titlemusic=LoadSound("d:\vendetta\sounds\vendetta.wav",SOUND_LOOP)
channel=PlaySound(titlemusic)

'scrolling text for title screen
text$="Welcome to Vendetta!!! Press '1' to start game, '2' to redefine keys, '3' to set points to win,"
text:+"'4' to set player names, '5' to set the number of buildings, '6' to redraw map, '7' to set player speed, '8' to turn the grid on/off, '9' to turn vehicles on/off and '0' to see High Score Table!!!!"
text:+" Programming by Trevor Stanton. (c) Copyright 2011 - Hope you enjoy this game and look out for further updates coming soon.!!!!"

'scrolling text for in game
text2$="Let the Battle Commence!!"

playerspeed=5
pointstowin=10
player1name$="player1"
player2name$="player2"
grid="off"
vehicles="off"
music="on"
sfx="on"
'SetChannelRate channel,2
While Not KeyHit(KEY_1)

		
SetClsColor 0,0,0
Cls	
	Delay(100)

	SetImageFont(font)
	
	DrawText text,tickerx#,980

	SetImageFont(font2)
		
	DrawImage(titlescreen,0,0,0)
	DrawImage(player1icon,500,380,0)
	DrawImage(player2icon,1300,380,0)
	DrawImage(bonuspointicon,600,500,0)
	DrawText "=Bonus Point",660,500
	DrawImage(bouncingbulleticon,600,560,0)
	DrawText "=Bouncing Bullet",660,560
	DrawImage(choiceicon,600,620,0)
	DrawText "=Choice",660,620
	DrawImage (laserbulleticon,600,680,0)
	DrawText "=Laser Bullet",660,680
	DrawImage (radarjamicon,600,740,0)
	DrawText "=Radar Jam",660,740
	DrawImage (seekingmissilebulleticon,600,800,0)
	DrawText "=Seeking Missile Bullet",660,800
	DrawImage (speedybootsicon,960,500,0)
	DrawText "=Speedy Boots",1020,500
	DrawImage (speedybulleticon,960,560,0)
	DrawText "=Speedy Bullet",1020,560
	DrawImage (supermapicon,960,620,0)
	DrawText "=Super Map",1020,620
	DrawImage (teleporticon,960,680,0)
	DrawText "=Teleport",1020,680
	DrawImage (turningmissilebulleticon,960,740,0)
	DrawText "=Turning Missile Bullet",1020,740
	DrawImage (wallbulleticon,960,800,0)
	DrawText "=Wall Bullet",1020,800
	
	DrawText "Number Of            = " + numberofbuildings,730,280
	
	DrawText "Points To Win = " +pointstowin,740,240
	
	DrawImage (buildingicon,870,280,0)
	DrawText "Player Speed = " +playerspeed,750,340
	DrawText "Grid ="+grid,800,380
	DrawText "Vehicles = "+vehicles,765,420
	
	DrawText "Up:    "+"W",250,680
	DrawText "Down:  "+"S",250,720
	DrawText "Left:  "+"A",250,760
	DrawText "Right: "+"D",250,800
	DrawText "Fire:  "+"G",250,840
	
	DrawText "'1' - Start Game",250,440
	DrawText "'2' - Keys",250,480
	DrawText "'3' - Set Points To Win",250,520
	DrawText "'4' - Set Player Names",250,560
	DrawText "'5' - Increase Buildings",250,600
	
	DrawText "'6' - Decrease Buildings",1400,440
	DrawText "'7' - Set Player Speed",1400,480
	DrawText "'8' - Turn Grid On/Off",1400,520
	DrawText "'9' - Vehicles On/Off",1400,560
	DrawText "'0' - High Score Table",1400,600
	
	DrawText "Up:    "+"Up",1400,680
	DrawText "Down:  "+"Down",1400,720
	DrawText "Left:  "+"Left",1400,760
	DrawText "Right: "+"Right",1400,800
	DrawText "Fire:  "+"CTRL",1400,840

	DrawText "Name:   "+player1name,250,380
	DrawText "Name:   "+player2name,1400,380

	
	DrawText "Press 'F1' In game to Redraw Map!!!!",700,900
	
	DrawText "Music = "+music,250,250
	DrawText "(Press 'F2' to toggle Music)",250,290
	DrawText "SFX = "+sfx,1400,250
	DrawText "(Press 'F3' to toggle SFX)",1400,290
	
	tickerx=tickerx-40
	If tickerx<-TextWidth(text) tickerx=1920

	Local Value_Increase_Step=1
	
	'if KeyDown(KEY_4)
	'player1name$ = Input("Enter name Player 1")
	'player2name$ = Input("Enter Name Player 2")
	'EndIf
	
	If KeyDown(KEY_3) Then
	pointstowin=pointstowin+1
		If pointstowin>100 pointstowin=1
	EndIf	

	If KeyDown(KEY_5)
		numberofbuildings = numberofbuildings + Value_Increase_Step
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = 0
	ElseIf KeyDown(KEY_6)
		numberofbuildings = numberofbuildings - Value_Increase_Step
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings=100
	EndIf
	
	If KeyDown(KEY_7) Then
	playerspeed=playerspeed+1;player1_Speed=playerspeed;player2_Speed=playerspeed
	If playerspeed>20 playerspeed=1
	EndIf

	If KeyDown(KEY_8) And grid="off" Then
	grid="on"
	ElseIf grid="on" And KeyDown(KEY_8) Then
	grid="off"
	EndIf

	If KeyDown(KEY_9) And vehicles="off" Then
	vehicles="on";'DrawImage (tankicon,960,800,0)
	ElseIf vehicles="on" And KeyDown(KEY_9) Then
	vehicles="off"';DrawImage (blockicon,960,800,0)
	EndIf

	If KeyDown(KEY_F2) And music="on" Then
	music="off";StopChannel channel
	ElseIf KeyDown(KEY_F2) And music="off" Then
	music="on";channel=PlaySound(titlemusic)
	EndIf 

	If KeyDown(KEY_F3) And sfx="on" Then
	sfx="off"
	ElseIf KeyDown(KEY_F3) And sfx="off" Then
	sfx="on"
	EndIf

Flip
Wend

SeedRnd MilliSecs()  

Player1_xPos=Rnd(1870)
Player1_yPos=Rnd(875)
PLayer2_xPos=Rnd(1870)
Player2_yPos=Rnd(875)


While Not KeyDown(KEY_ESCAPE)



'Clear_Buildings()
'Hit_Building(Direction)
Move()
Draw()
Conditions()


'SetColor 204,204,204
DrawImage(mapscreen,0,935,0)
SetImageFont(font)
SetBlend(maskblend)
DrawText "PLAYER1 = "+Player1_Score,560,935
DrawText "PLAYER2 = "+Player2_Score,1120,935
SetImageFont(font2)
DrawText text2,tickerx#,990
tickerx=tickerx-10
If tickerx<-TextWidth(text) tickerx=1800


Flip
Wend
End

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function

Function Draw()
	SetClsColor 204,204,204
	Cls
	
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetBlend(shadeblend)
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'DrawRect Player1_xPos , Player1_yPos , Player1_Size , Player1_Size

	'SetScale 1.0,1.0
End Function

Function Make_Buildings(Building_Count)
	
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function


Function Hit_Building(Direction)
	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	
	Select Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
	End Select
	
	Return False
	
End Function

Function Move()
	
	Local Up_Pressed = False
	Local Down_Pressed = False
	Local Right_Pressed = False
	Local Left_Pressed = False
	
	Local Up_Pressed2=False
	Local Down_Pressed2=False
	Local Right_Pressed2=False
	Local Left_Pressed2=False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	
	If KeyDown(KEY_UP) Then Up_Pressed2 = True
	If KeyDown(KEY_DOWN) Then Down_Pressed2 = True
	If KeyDown(KEY_RIGHT) Then Right_Pressed2 = True
	If KeyDown(KEY_LEFT) Then Left_Pressed2 = True

	
	
	' Change player image and move player1 based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		If Hit_Building(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		If Hit_Building(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

	
	' Change player image and move player 2 based on what keys are pressed.
	If Up_Pressed2 = True
		' If only up is pressed
		Player2_Moving = True
		Player2_Image = Player2_North
		Player2_yPos = Player2_yPos - Player2_Speed
		' If up and another key is pressed, ignore above do one of the two below.
		If Right_Pressed2 = True Player2_Image = Player2_NorthEast ; Player2_xPos = Player2_xPos + Player2_Speed/2
		If Left_Pressed2 = True Player2_Image = Player2_NorthWest ; Player2_xPos = Player2_xPos - Player2_Speed/2
	ElseIf Down_Pressed2 = True
		Player2_Moving = True
		Player2_Image = Player2_South
		Player2_yPos = Player2_yPos + Player2_Speed
		If Right_Pressed2 = True Player2_Image = Player2_SouthEast ; Player2_xPos = Player2_xPos + Player2_Speed/2
		If Left_Pressed2 = True Player2_Image = Player2_SouthWest ; Player2_xPos = Player2_xPos - Player2_Speed/2
	ElseIf Right_Pressed2 = True
		Player2_Moving = True
		Player2_Image = Player2_East
		Player2_xPos = Player2_xPos + Player2_Speed
		If Up_Pressed2 = True Player2_Image = Player2_NorthEast ; Player2_yPos = Player2_yPos + Player2_Speed/2
		If Down_Pressed2 = True Player2_Image = Player2_SouthEast ; Player2_yPos = Player2_yPos - Player2_Speed/2
	ElseIf Left_Pressed2 = True
		Player2_Moving = True
		Player2_Image = Player2_West
		Player2_xPos = Player2_xPos - Player2_Speed
		If Up_Pressed2 = True Player2_Image = Player2_NorthWest ; Player2_yPos = Player2_yPos + Player2_Speed/2
		If Down_Pressed2 = True Player2_Image = Player2_SouthWest ; Player2_yPos = Player2_yPos - Player2_Speed/2
	Else
		' no keys pressed
		Player2_Frame = 0
		Player2_Moving = False
		
	EndIf	

End Function


Function Conditions()
	If Player1_xPos + Player1_Size > GraphicsWidth() Then Player1_xPos = GraphicsWidth() - Player1_Size 
	If Player1_xPos < 0 Then Player1_xPos = 0
	If Player1_yPos + Player1_Size > GraphicsHeight()-140 Then Player1_yPos = GraphicsHeight()-140 - Player1_Size 
	If Player1_yPos < 0 Then Player1_yPos = 0

	If Player2_xPos + Player2_Size > GraphicsWidth() Then Player2_xPos = GraphicsWidth() - Player2_Size 
	If Player2_xPos < 0 Then Player2_xPos = 0
	If Player2_yPos + Player2_Size > GraphicsHeight()-140 Then Player2_yPos = GraphicsHeight()-140 - Player2_Size 
	If Player2_yPos < 0 Then Player2_yPos = 0

End Function



I really need help with this - im tearing my hair out trying to figure out why its not working as it was before. I mean I havent changed that much so i dont get it.


Joe90bigrat90(Posted 2011) [#50]
ok this is my code ive made corrections.

SetGraphicsDriver GLGraphicsDriver()
Graphics 1920,1080
'variables for player 1
Global Player1_moving=False
Global Player1_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120
Global Player1_Size = 50
Global Player1_Score=0

Make_Buildings(50)

'general variables
Global numberofbuildings = 50
Global numberofbuildings_Max = 100
Global numberofbuildings_Min = 0
Global Building_Size = 50
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]
Global playerspeed
Global screengrid:String
Global vehicles:String
Global music:String
Global sfx:String


'load the images into the variables for Player 1
Global Player1_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1north.png",50,50,0,4)
Global Player1_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1south.png",50,50,0,4)
Global Player1_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player1west.png",50,50,0,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northeast.png",50,50,0,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northwest.png",50,50,0,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southeast.png",50,50,0,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southwest.png",50,50,0,4)

'load the building image
Global building=LoadImage("d:\vendetta\sprites\smallbuilding.png",0)

Player1_xPos=Rnd(1870)
Player1_yPos=Rnd(875)

While Not KeyDown(KEY_ESCAPE)


Move()
Draw()
Conditions()

Flip
Wend
End

Function Make_Buildings(Building_Count)
	
	SeedRnd MilliSecs()
	For i = 0 To Building_Count	
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function


Function Draw()
	SetClsColor 204,204,204
	Cls
	
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf

' Draw the player
	'SetScale 1.0,0.64
	SetBlend(shadeblend)
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
End Function

Function Hit_Building(Direction)
	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	
	Select Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
	End Select
	
	Return False
	
End Function

Function Move()
	
	Local Up_Pressed = False
	Local Down_Pressed = False
	Local Right_Pressed = False
	Local Left_Pressed = False
	
	Local Up_Pressed2=False
	Local Down_Pressed2=False
	Local Right_Pressed2=False
	Local Left_Pressed2=False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	
	If KeyDown(KEY_UP) Then Up_Pressed2 = True
	If KeyDown(KEY_DOWN) Then Down_Pressed2 = True
	If KeyDown(KEY_RIGHT) Then Right_Pressed2 = True
	If KeyDown(KEY_LEFT) Then Left_Pressed2 = True

	
	
	' Change player image and move player1 based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		If Hit_Building(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		If Hit_Building(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		If Hit_Building(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		If Hit_Building(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			If Hit_Building(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			If Hit_Building(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

End Function


Function Conditions()
	If Player1_xPos + Player1_Size > GraphicsWidth() Then Player1_xPos = GraphicsWidth() - Player1_Size 
	If Player1_xPos < 0 Then Player1_xPos = 0
	If Player1_yPos + Player1_Size > GraphicsHeight()-140 Then Player1_yPos = GraphicsHeight()-140 - Player1_Size 
	If Player1_yPos < 0 Then Player1_yPos = 0

End Function



when i run the program i get unhandled exception. Attempt to index array element beyond array length and poiints to this line:

If Building_Grid[x , y] = 0



coffeedotbean(Posted 2011) [#51]
Going back to my post #45 I called Make_Buildings() in the TitleScreen() Function, called after "Space" is pressed - which in my code starts the game.

Your getting the error "unhandled exception" because your calling Make_Buildings() before you have defined the array

SetGraphicsDriver GLGraphicsDriver()
Graphics 1920,1080
'variables for player 1
Global Player1_moving=False
Global Player1_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120
Global Player1_Size = 50
Global Player1_Score=0

'Make_Buildings(50)   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< should not be here

'general variables
Global numberofbuildings = 50
Global numberofbuildings_Max = 100
Global numberofbuildings_Min = 0
Global Building_Size = 50
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]

'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< should be here or later (after you have defined the array and other variables that Make_Buildings() looks for.

Global playerspeed
Global screengrid:String
Global vehicles:String
Global music:String
Global sfx:String


Last edited 2011


Joe90bigrat90(Posted 2011) [#52]
cool thanks coffee duh silly mistake hahaha.
Ok so whatever i change the buildings to i am using 5 to increase the buildings and 6 to decrease the buildings.
When i press 1 to start the game it is working but its drawing one extra building all the time.

So if i select say 10 buildings it will draw 11 buildings.
And if i draw 1 building it will draw 2 buildings.

Ive checked the code out and it seems logical to me. How could it draw an extra building if im actually choosing the number of buildings.

So my code:

Global numberofbuildings = 50
Global numberofbuildings_Max = 100
Global numberofbuildings_Min = 0
Global Building_Size = 50
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]

Make_Buildings(Building_Count)

im not posting all the code as its not needed, Just the buildings part.then in my title screen code i got this:

If KeyDown(KEY_5)
		numberofbuildings = numberofbuildings + Value_Increase_Step
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Max
		Make_Buildings(numberofbuildings)
	ElseIf KeyDown(KEY_6)
		numberofbuildings = numberofbuildings - Value_Increase_Step
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Min
		Make_Buildings(numberofbuildings)
	EndIf

Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count	
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function

Function Draw()
	SetClsColor 204,204,204
	Cls
	
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetBlend(shadeblend)
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'SetScale 1.0,1.0
End Function


So thats basically all the building code i have. I wonder why its drawing an extra building. Is it something to do with the array. I was thinking a 0 could cause problems. But sometimes I want to play the game without buildings so I want 0 included. Hope this makes sense!! By the way thanks for sorting out that problem. I saw what was wrong when you said about it. hehehe darn silly.


Joe90bigrat90(Posted 2011) [#53]
hmmm i think i may have found what im looking for.

Its this line isnt it

If Building_Grid[x , y] = 0
Building_Grid[x , y] = 1

so if the x and y =0 then change it to 1

so if i need say 0 so it draws 0 buildings shall i change this to:

If Building_Grid[x , y] = 0
Building_Grid[x , y] = 0

Last edited 2011


coffeedotbean(Posted 2011) [#54]
ah its drawing 1 extra building because computers count '0' as a number,

below code fixes that.


Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1	' <<<<<<<<<<<<<< change here '-1'
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function


Last edited 2011


Joe90bigrat90(Posted 2011) [#55]
excellent. Yes that solved it. Now drawing correct number of buildings. Ok Theres one more thing i need to know. At the present time its drawing buildings off and to the right of the screen and below. It seems that its drawing buildings correctly from the left.

Graphics 1920,1080 'my graphics mode

Global Building_Size = 50
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size
Global Building_Grid[World_Width+1,World_Height+1]

Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function

For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next


The area that both players are allowed to move on is from left to right 0 to 1870 and up and down from 0 to 875.

I think it must be the worldwidth and worldheight values.

Ive also got another bit of code which stops the player moving off the screen. Ive also included this so you can see what im doing.#

If Player1_xPos + Player1_Size > GraphicsWidth() Then Player1_xPos = GraphicsWidth() - Player1_Size 
	If Player1_xPos < 0 Then Player1_xPos = 0
	If Player1_yPos + Player1_Size > GraphicsHeight()-140 Then Player1_yPos = GraphicsHeight()-140 - Player1_Size 
	If Player1_yPos < 0 Then Player1_yPos = 0


Notice the -140 after the graphicsheight. This is because i dont want the players moving to the bottom of the screen as I have a mini map and player scores at the bottom sort of like a border at the bottom.

Thankyou for all your help coffee. i wish I could program as well as you but im learning every day just by looking at your code. Sometimes its hard to tell whats actually going on. I didnt realise you can call a function within a function. Thats pretty cool. Id love to see some of your work or have u made any games?? Obviously i dont want the source code but id like to see maybe a game youve made. Is that your work programming??


Joe90bigrat90(Posted 2011) [#56]
ive also got a key so when you press it you can turn a grid on and off.

Im trying to create a simple grid which overlays the buildings.

I just want to draw lines on the screen the same width as the building size.

So the building size is set to 50 so the lines will be 50 wide apart.

Do i need to use another array for this or can i do it somehow like this
'my variables for line data
Global linex=0
Global liney=0
Global linex2=0
Global liney2=935


Function Grid()
            For i=o to 50 ' ok so a loop that draws a line 50 times. Is there any way of drawing lines so that it draws lines to the width of the building size
                      setlinewidth 5
                      setcolor 0,0,0
                      linex=linex+50
                      liney=liney
                      linex2=linex2+50
                      liney2=liney2+50
                      drawline linex,liney,linex2,liney2
            Next
end function


Hmmm this doesnt seem to work. It quickly flashes the lines on the screen
and then dissapear so i see the lines for a split second and then vanish.

What am i doing wrong????

Thankyou. Kind Regards
Joe


coffeedotbean(Posted 2011) [#57]
Your on the right track with the coding for the lines - but I feel you don't quite understand how the array is working and how it's being interacted with - The best way to understand this is if I write you a very basic array based level editor - I'll do that when I get home in the next 4 hours or so.

I have written several games - most recent in my sig below - theres a game called tweakie tweak on the alawar website (thats my first "indie game" releaed in early 2000.

Also here is a list of most of my games, sadly didin't keep the source code for some and the links have now expired for probably all downloads. http://www.blitzbasic.com/Community/posts.php?topic=67269#751127

Last edited 2011


Joe90bigrat90(Posted 2011) [#58]
ok cool thanks.

It be really cool if you could show me how to just create a simple grid for my game where if the user presses g it toggles a grid on and off on the main game screen. It wont really do anything. It is just there in case you want to turn a grid on.

But more importantly is the blocks. They are being drawn halfway on the edge of the screen and below the screen. The top of the screen is working fine and so is the left. Its just the right and the bottom of the screen.

The right side of the screen players can move upto 1870 so i guess it needs to draw a building 50 below this number in order to fit the building on the right side of the screen.

The bottom of the screen players can move to 875 so again I guess a building needs to be drawn 50 below this as buildings are 50 x 50.

Yes it is tricky learning all this stuff. Im throwing myself in the deep end but i feel i learn a lot more from looking at others code. I sit and read it and try to work out whats happening. Ive learnt so much from you. Its working out the best way to do something for me. I can write a program and think yeah thats cool and then look at someone elses and they cut the code down by half. So there are loads of different ways to write the same code. But you have taught me a lot. Yeah arrays I find quite difficult to understand. Variables are pretty staright forward sometimes. I am learning every day reading peoples code and looking at the way things are done. Ive never programmed in this language before so its quite a challenge. But i love blitzmax I think its really cool. Fast as well. And I have the GUI as well for windows and stuff. Cool!!!


Joe90bigrat90(Posted 2011) [#59]
Wow ive taken a look at your games. They look pretty professional. Did you create all the graphics yourself?????

Yeah shame you cant download them anymore but the graphics look pretty cool. Im crap at drawing graphics. Might ask a mate if he can draw some pretty cool graphics for my game.

Well its a really crap game but i dont intend to write games to make money. Im really writing a game to learn blitxmax. I want to finish this project with as much help as i can get. Then im gonna print of all the code and store it in a folder so i can read it and learn.

Ive been looking at types. Trying to learn them. I might create a type for bullets but i need to learn more. Is it best to have an image for a bullet or use a full stop to show machine gun fire???

You're obviously a really good programmer. Well i can tell that anyway from the code youve written.

Why dont you write a game and start selling it. Youre obviously talented at programming and i reckon if you had 2 or 3 games written you could make a lot of money. Youre good at teaching I know that. Couldnt you get a job for a games company or teach programming to students.

I'd love to be at your level. Think of all the cool games I could make.
I wish I was as good as you at programming. Im new to programming really. I wanted to find a language that was fast and quite easy to use and i saw blitzmax and bought it. I'm glad i did. Im learning so much about variables, arrays, types, functions, Methods and stuff. Its all cool stuff!!!


coffeedotbean(Posted 2011) [#60]
If anything it's more fun when you starting out with games programming, you'll get those cool "light-bulb" moments where suddenly something make sense. With Santa's workshop I hired some one to do the graphics, hang around some pixel art forums such as pixel joint and you can usually find people willing to make graphics some for free other for a price.

I have released a few games to portals etc over the years with mild success but the indie scene is no longer indie - indie games today have budgets of $25,000 or even more not like the old days =D.

Types are a must for almost any project so they are worth learning.

Here is a basic array/level editor - should help you get your head around arrays.

Here's some idea's

- edit it so it's drawing images instead of rectangles.
- Make it so if the mouse is within the array area the cell it is over is highlighted in some way
- Press keys 1-9 will change the colour of the rectangle to be draw / or image

' ---------------------------------------------------------------------------------------------
' EDIT THESE
' ---------------------------------------------------------------------------------------------
Global Full_Screen		 = False
Global Tile_Size 			 = 48	' What size are our tiles in this world?
Global World_Width 		 = 11	' How many tiles Wide is our World? (Remeber computers count '0' as a number so for example setting this to 9 will actualy mean 10)
Global World_Height 		 = 8	' How many tlles Tall is our World? (Remeber computers count '0' as a number so for example setting this to 0 will actualy mean 1)

' ---------------------------------------------------------------------------------------------

' Set up our screen and resolution
Graphics 800 , 600 , Full_Screen * 32 , 60

' Variables
Global World_Grid[World_Width + 1 , World_Height + 1]
Global Enable_Grid = True

' ---------------------------------------------------------------------------------------------
' Main loop
While Not KeyDown(KEY_ESCAPE)
Cls

	Edit_World()
	Draw_World()
	Draw_Grid()
	Draw_Info()

Flip
Wend
End

' ---------------------------------------------------------------------------------------------
' Draw some text to the screen with some usful info
Function Draw_Info()
	
	Local Mx = MouseX() / Tile_Size	' Translate mouse position so it relates to the array, eg a if mouse is at 64 (pixels) it will translate it to 1 (2nd cell in our array if tile_size=32)
	Local My = MouseY() / Tile_Size	' Translate mouse position so it relates to the array, eg a if mouse is at 128 (pixels) it will translate it to 3 (4th cell in our array if tile_size=32)
	
	' Find where our World ends on the horizontal in pixles (used for drawing text to the Right of our world space)
	Local Offset_x = World_Width * Tile_Size + Tile_Size
	
	' Find where our World ends on the vertical in pixles (used for drawing text to the Bottom of our world space)
	Local Offset_y = World_Height * Tile_Size + Tile_Size
	
	' Draw position of mouse in pixels
	DrawText "Pixel (x:y) = " + MouseX() + ":" + MouseY() , Offset_x + 10 , 10
	
	' Draw position of mouse as if it was in our array - note it will not knowif it have moved outside our array we need to check that manualy
	DrawText "Array (x:y) = " + Mx + ":" + My , Offset_x + 10 , 30
	
	' Display text based on wether the grid if on or off
	If Enable_Grid = True Then DrawText "Grid = On" , Offset_x + 10 , 50 Else DrawText "Grid = Off" , Offset_x + 10 , 50
	
	' World Size info
	DrawText "Width (Tiles)   = " + World_Width , Offset_x + 10 , 90
	DrawText "Height (Tiles)  = " + World_Height , Offset_x + 10 , 110
	DrawText "Width (Pixesl)  = " + ( (World_Width * Tile_Size) + Tile_Size) , Offset_x + 10 , 130
	DrawText "Height (Pixels) = " + ( (World_Height * Tile_Size) + Tile_Size) , Offset_x + 10 , 150
	
	' Instructions
	DrawText "Press SPACE to turn grid On or Off" , 10 , Offset_y + 10
	DrawText "Press Mouse 1(red),3(green) and 2(none/delete) to edit our World" , 10 , Offset_y + 30
	
End Function

' ---------------------------------------------------------------------------------------------
' Use mouse to add tiles
Function Edit_World()
	
	Local Mx = MouseX() / Tile_Size	' Translate mouse position so it relates to the array, eg a if mouse is at 64 (pixels) it will translate it to 1 (2nd cell in out array if tile_size=32)
	Local My = MouseY() / Tile_Size	' Translate mouse position so it relates to the array, eg a if mouse is at 128 (pixels) it will translate it to 3 (4th cell in out array if tile_size=32)
	
	' Turn Grid on/off if SPACE is pressed
	If KeyHit(KEY_SPACE) = True
		If Enable_Grid = True Then Enable_Grid = False Else Enable_Grid = True
	EndIf
	
	' Exit function NOW if we are outside our array size World_Width or World_Height or we'll get a crash 'out of bounds error'
	If Mx > World_Width Or My > World_Height Then Return
	
	' Change array value based on what mouse button is pressed
	If MouseHit(1) = True
		World_Grid(Mx , My) = 1 ' Red
	ElseIf MouseHit(3) = True
		World_Grid(Mx , My) = 2 ' Green
	ElseIf MouseHit(2) = True
		World_Grid(Mx , My) = 0 ' Nothing
	Endif
	
End Function

' ---------------------------------------------------------------------------------------------
' Draw our Tiles
Function Draw_World()
	
	' When ever we draw anything based on it's array postion we need to '*' it by the tile_Size so it translate to the screen correctly.
	' Array does not know, need to know or wants to know the screen size or tile size.
	
	' Loop through our array and draw some coloured rectangles based on the value stored in the array
	For x = 0 To World_Width
	For y = 0 To World_Height
		If World_Grid(x , y) = 1 ' Red
			SetColor 155 , 0 , 0 ; DrawRect(x * Tile_Size , y * Tile_Size , Tile_Size , Tile_Size)
		ElseIf World_Grid(x , y) = 2 ' Grenn
			SetColor 0 , 155 , 0 ; DrawRect(x * Tile_Size , y * Tile_Size , Tile_Size , Tile_Size)
		ElseIf World_Grid(x , y) = 0 ' Nothing
			' Do nothing - does not need to be called but here for consistancy	
		Endif
		SetColor 155 , 155 , 155 ; DrawText World_Grid(x , y) , 	x * Tile_Size + 5 , y * Tile_Size + 5	' Array value
	Next
	Next
	SetColor 255 , 255 , 255
	
End Function

' ---------------------------------------------------------------------------------------------
' Draw our Grid (lines)
Function Draw_Grid()
	
	If Enable_Grid = False Then Return ' We dont want to draw the grid so exit this function NOW!
	
	SetColor 155 , 155 , 155
	' Horizontal lines
	For x = 0 To World_Width+1
		DrawLine x * Tile_Size , 0 , x * Tile_Size , (World_Height * Tile_Size) + Tile_Size
	Next
	' Vertical lines
	For y = 0 To World_Height+1
		DrawLine 0 , y * Tile_Size , (World_Width * Tile_Size) + Tile_Size , y * Tile_Size
	Next
	SetColor 255 , 255 , 255
	
End Function


Last edited 2011

Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#61]
Cool thats a pretty neat program. Ive saved it on my hard drive so i can study the code. Very interesting indeed. Thankyou very much for that.

Ive managed to draw a grid on my screen. The blocks are being displayed correctly on the screen except for the bottom of the screen. This is because of the graphicsheight. Say I want to reduce a line of blocks from the bottom of the screen. I dont want to change the graphicsheight which is 1080 as the screen will be smaller. Something I need to change which I think is from this part of code. Its creating a random block from 0 to world_width and World_Height

Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height)
			If Building_Grid[x , y] = 0
				Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function


World_Width and Height are currently set at:

Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size


So it looks like i need to change something here. Now I need to reduce the buildings from the bottom of the screen. The building size is currently set to 55.

However i cant make any changes to the above 2 lines as it will produce an error from this line:

[code]
If Building_Grid[x , y] = 0

Its just that at the bottom of the screen I have a mini map and player scores but obviously its drawing blocks there.
How can i change the height to reduce the buildings at the bottom of the screen 2 rows.

Thanks a lot.


Joe90bigrat90(Posted 2011) [#62]
Well I think the World_Height I need to change. As I understand it World_Height=GraphicsHeight which is 1080. Divided by Building size which is 55 = 19. I need to get this down to 17 so my blocks are drawn on the screen properly. Ive got the width working its just the height.


Joe90bigrat90(Posted 2011) [#63]
Its ok dont worry i sorted it out. All i did was add this to the line
Global World_Height =GraphicsHeight()/Building_Size-2

Its now working correctly.

Thanks

Joe


Joe90bigrat90(Posted 2011) [#64]
Damit I have another problem. I need to make sure that when the blocks are created that the blocks dont start in both the players locations.

Ok so i have player1_Xpos and Player1_Ypos to figure out the position of the player and I know that the Building_Size =55.

Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next
	
End Function

Maybe I need to write an if statement in here to say that if a block is drawn and it touches the player then draw it elsewhere or would it be better to draw the player at another location where there wasnt a block.
How do i go about doing this???

Last edited 2011


coffeedotbean(Posted 2011) [#65]
The best way it is randomly pick an array location x&y and see if there is a building.. if there is then pick another, if a building does not exist then you use the selected array x and y to position the player, heres some rough code (wont run but should show you how to code it).

Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next

	PositionPlayer()   ' <<<<<<<<<<<<<<<< new function call

End Function

Function PositionPlayer()

SeedRnd MilliSecs()
' Setup a loop so we can keep searching for a place to put a player
Repeat
' choose a random array x and y location to check
   Local x = Rand(0 , World_Width)
   Local y = Rand(0 , World_Height)
   ' Theres a building at this location so lets do nothing and let the loop repeat.		
   If Building_Grid[x , y] = 1
      ' do nothing
   ' ahh good no building here so lets place the player here
   Elseif Building_Grid[x,y] = 0
      Player_x = x* Tile_Size
      Player1_y = y* Tile_Size
      Building_Grid[x,y]=3 ' set this array location to 3, this way when he place player 2 in the same way this location will also be ignored.
      Exit
   Endif
Forever

' do same for player 2.

End Function


Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#66]
Cool that worked a treat!!! Thanks a lot!!!I made a few adjustments as my world_Height is less than full screen. But ive tested it and its working fine. Brilliant.


coffeedotbean(Posted 2011) [#67]
Idealy you would setup a list of positions checked so the code does not check the same position over and over, imagine you had a world 500 wide and 500 tall and only 4 places a player could placed - the program might take a few seconds to a few hours to find a free place - but don't worry about that just something to think about.


Joe90bigrat90(Posted 2011) [#68]
Ok thankyou hey coffee i was editing my program and i decided to write a new function and then i changed my mind and deleted the function. Now when i run my program i keep getting an error yet i havent deleted any code i started with so i dont get it.
When i run the program i keep getting this error

Expression of type 'Int' cannot be indexed and pointing to this line

If Building_Grid[x , y] = 0

What should i check for thats giving me this error.

I cant even run the program as the error comes up staright away.


coffeedotbean(Posted 2011) [#69]
Only thing I can think of is that you have removed or changed.

Global Building_Grid[World_Width+1,World_Height+1]


If you have not defined your array you'll typically get that error.


Joe90bigrat90(Posted 2011) [#70]
Haha yeah i found it. I went through my code and found i didnt have it. I must have deleted it. You were exactly right though. You got it staright away. It was that line hehehehe thankyou.


Joe90bigrat90(Posted 2011) [#71]
Thanks cofee. Hey how would you write bullet code for firing a shot. How would you go about programming this. Would you use a type for the bullets???
Then i guess i need a while loop and to draw the bullet moving.
Would you use an array for the bullet or a variable???
I guess i need a bullet image although it would be pretty cool if i could draw full stops coming out of the weapon like a machine gun.
so i need 8 bullet images for the bullet for 8 directions.

Whats the command for detecting collisions????
So i guess i need loads of if statements like errrrr
if <bullet> collide with <player> then erase the bullet and draw an image of the player dying.
If you draw something to the screen whats the command for deleting the bullet. Is there an erase image function????

So i could create a type for the bullet.
I want to have several different types of bullets in my game.
If you could start me off on the right track that would be very much appreciated. Once you have shown me how i can create a bullet i can then use that code for the other bullets.
Hopefully this will be the last post on here for a while as I can do the rest of the stuff myself. (I think heehe)
Just need to work out how to draw a bullet moving and how collision works.

I guess the first thing you need to know is what direction the player is moving. Then once you know the direction of the player and he presses fire to draw a bullet from the player1xpos or player1ypos. Move it until it hits a building or another player then destroy it. if it hits a building then nothing happens except destroy bullet but if it hits a player draw image for player (dead image) destroy the player and increase the score for player who shot him. Hmmm will i need some sort of frame timer to slow down the bullet as it will draw it too fast and you wont see it????
yes i think i will need a timer. It would be cool that if you pressed fire another bullet is shot rather than not being able to press fire if a bullet is on the screen. That would be silly, i mean you could fire a shot and then you have to wait if it travels right across the screen. It needs to be so that when you press fire another bullet comes out. I think I know what to do i just need to maybe have a bit of code to get me on the right track. Thanks coffee by the way its a shame you cant download your games anymore. The graphics look awesome.

Last edited 2011


coffeedotbean(Posted 2011) [#72]
It's not bullet code but this should show you how types work how to create them, delete them and collisions.

Graphics 800 , 600 , 0 , 60
SetBlend ALPHABLEND

' ---------------------------------------------------------------------------------------------
' Edit these
Global MouseBoxSize = 20

' ---------------------------------------------------------------------------------------------
' Variables
Global RockList:TList = CreateList() ' List to hold all our Rock types (objects)
SeedRnd MilliSecs()
HideMouse()

' ---------------------------------------------------------------------------------------------
' Quickly add 10 Rocks to our Screen so there's something to start with
For i = 0 To 9
	Rock.Create(Rand(0,GraphicsWidth()),Rand(0,GraphicsHeight()),Rand(20,50))
Next

' ---------------------------------------------------------------------------------------------
' Main loop
While Not KeyDown(KEY_ESCAPE)
Cls

	Rock.Draw() ' Call the function inside our Rock type called Draw()
	DrawMouse()
	UseMouse()

	DrawText "Number of Rocks: " + CountList(Rocklist) , 10 , 10
	DrawText "Press Mouse 1 to add a rock, Mouse 2 to delete a rock" , 10 , 30

Flip
Wend
End

' ---------------------------------------------------------------------------------------------
Function UseMouse()
	
	If MouseHit(1) = True
		Rock.Create(MouseX() , MouseY() , Rand(10 , 60) ) ' Call the function inside our Rock type called Create()
	ElseIf MouseHit(2) = True
		Rock.Destroy(MouseX() , MouseY() , MouseBoxSize) ' Call the function inside our Rock type called Destroy()
	Endif
	
End Function

' ---------------------------------------------------------------------------------------------
Function DrawMouse()
	
	' Draw the mouse and it's collision box
	SetColor 255 , 255 , 255
	SetAlpha 0.5
	DrawRect MouseX() , MouseY() , MouseBoxSize , MouseBoxSize
	SetAlpha 1.0
	
End Function

' ---------------------------------------------------------------------------------------------
Type Rock
	' Each rock we create will have the following variables it can use
	Field xPos
	Field yPos
	Field Size
	
	' Function to create a new Rock and add it to our list of types (objects)
	Function Create(x , y , size)
		Local NewRock:Rock = New Rock
		NewRock.xPos = x
		NewRock.yPos = y
		NewRock.Size = size
		ListAddLast(RockList,NewRock)
	End Function
	
	' Function to loop through our list of Rocks (objects) and draw them based on their own variables
	' Useing rect's for this but can easily be images
	Function Draw()
		If RockList = Null Then Return
		For Local ThisRock:Rock = EachIn RockList
			SetColor 255 , 255 , 255 ; DrawRect ThisRock.xPos , ThisRock.yPos , ThisRock.Size , ThisRock.Size
			SetColor 155 , 155 , 0 ; DrawRect ThisRock.xPos + 1 , ThisRock.yPos + 1 , ThisRock.Size - 2 , ThisRock.Size - 2	
		Next
	End Function
	
	' Loop through our list of Rocks (objects) and see if their x and y positions are within the mouse box area, if they are then delete that Rock
	Function Destroy(Mx , My , Ms)
		For Local ThisRock:Rock = EachIn RockList
			If (Mx > ThisRock.xPos And Mx < ThisRock.xPos + ThisRock.Size) Or (Mx + Ms > ThisRock.xPos And Mx + Ms < ThisRock.xPos + ThisRock.Size)
			If (My > ThisRock.yPos And My < ThisRock.yPos + ThisRock.Size) Or (My + Ms > ThisRock.yPos And My + Ms < ThisRock.yPos + ThisRock.Size)
				ListRemove(RockList , ThisRock)
			Endif
			Endif
		Next
	End Function
	
End Type


Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#73]
Cool ive tested the program. Its really good!!!
Ive coded some bullet code but i cant see the bullet on the screen??
Is it drawing it too fast???

Global bulletImg:TImage=LoadImage("d:\sprites\bullet.png")
Type Tbullet
Field x:Float
Field y:Float
Field image:TImage
Field Speed:Float
End Type

Global bulletList:TList = CreateList()

Local bullet:Tbullet=New Tbullet
bullet.x = Player1_Xpos
bullet.y = Player1_Ypos
bullet.image=bulletImg

Global Up_Pressed = False
Global Down_Pressed = False
Global Right_Pressed = False
Global Left_Pressed = False
Global Fire_Pressed =False

If KeyDown(KEY_G) Then Fire_Pressed = True

If Fire_Pressed = True Then
FireBullet_Player1()

Function FireBullet_Player1()
For bullet:Tbullet = EachIn bulletList
SetBlend(maskblend)
DrawImage bullet.image,bullet.x,bullet.y
SetBlend(solidblend)
Next
End Function



Obviously im not listing the whole program here. Just my bullet code.
I run the program and its not bringing up any errors.
I just cant see the bullet being drawn.
I think im doing something wrong. I have had a read of types in teh tutorials. So im learning slowly. hehe
Im just wondering if its drawing it so fast that i cant see it.
Please ignore the lack of code. My program does work so ignore the lack of flip wend and end etc. This is just the bullet code. I think im doing something wrong. Will I need a frametimer to slow down the bullet.
I dont think its even creating a bullet at the moment.


coffeedotbean(Posted 2011) [#74]
add this to your code some place

drawtext listcount(bulletlist),10,10


Lets see if the bullets are being created to start with.

Are they being told to move? I don't see any code where the bullets are told to move? Also I don't see any code where the bullet is being added to the bulletlist.


Joe90bigrat90(Posted 2011) [#75]
Nah it says identifier listcount not found so obviously something wrong there.


Joe90bigrat90(Posted 2011) [#76]
nah i know the bullets wont move. Im just trying to display a stationary bullet on the screen. Once i got it stationary i can then try and move the bullet. i try and do one thing at a time and test it and then move onto the next problem. jeez I never realised it was so hard just to create a bullet. Why do i have to use a type or list. Cant i just say if player 1 presses fire then drawimage bullet and move it.
I can see types being useful and lists and stuff but is it really neccessary????


coffeedotbean(Posted 2011) [#77]
Sorry meant

drawtext countlist(bulletlist),10,10


Types are very VERY important, each bullet needs to be a separate object with it's own variables and information which is why we need to keep a list of them so we can check each objects details when needed.


coffeedotbean(Posted 2011) [#78]
Also don't forget the bullet might be getting drawn under the player image - make sure the function to draw the bullet is called after the function to draw the player.


Joe90bigrat90(Posted 2011) [#79]
DrawText "Countlist(bulletList)="+CountList(bulletlist),500,540

Hmmmm its value is 0 all the time which I guess means that the bullet is not being added to the bulletlist

Ok ive added this line of code
ListAddLast(bulletList,bullet)

Now countlist(bulletlist) = 1

ok i press right control for player 2 to see whats happening.

heres a bit of code

Function Player2_Shooting()
If fire_Pressed2=False Then Return
SetBlend(maskblend)
DrawText "Player 2 Shooting",0,0
For bullet:Tbullet=EachIn bulletList
SetBlend(maskblend)
DrawImage bullet.image,bullet.x,bullet.y
'SetBlend(shadeblend)
Next
SetBlend(shadeblend)
End Function

ok so im pressing right control. The bullet is appearing on the screen but its completely black. My bullet image is white with a red bullet in the middle. Whys it displaying as black. Ive tried changing the blend but it doesnt seem to make any difference. Ok so how do i make the bullet move from say player1's x position and y position which is Player1_xPos and Player2_yPos. I guess changing the line from drawimage bullet.image, bullet.x, bullet.y to this: drawimage bullet.image, Player1_xPos,Player1_yPos will start the bullet from player 1's location.
How do i actually move the bullet and to stop it appearing black on the screen.


Joe90bigrat90(Posted 2011) [#80]
Ok ive stopped the bullet from going black by adding a setcolor command. Seems last color command was black. Also removed an unncessary setblend. Bullet image now being displayed correctly. how do i move the bullet without it going to fast and also how do i work out what bullet image to display if theres 8 directions.
Do i need 8 different if statements
Nah wait cant i use your array like you did to test the key presses. Maybe i can find out what direction the players moving from the Hit_Building code.

Last edited 2011


Joe90bigrat90(Posted 2011) [#81]
ok this is basically the code i have:
Type Tbullet
Field x:Float
Field y:Float
Field image:TImage
Field Speed:Float
End Type

Global bulletList:TList=CreateList()

Local bullet:Tbullet=New Tbullet
bullet.x= Player1_Xpos
bullet.y= Player1_Ypos
bullet.image=bulletImg

ListAddLast(bulletList,bullet)

While Not KeyDown(KEY_ESCAPE)

Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Redraw_Map()
Buildings_And_Grid_Ingame()
Help_And_Variables()
Conditions()

Flip
Wend
End

Function Player1_Shooting()
	If fire_pressed=False Then Return
	SetColor 255,255,255
	SetBlend(maskblend)
	DrawText "Player 1 Shooting",0,0
	For bullet:Tbullet=EachIn bulletList
	SetBlend(shadeblend)
	DrawImage bullet.image,bullet.x,bullet.y
	Next
End Function

Function Player2_Shooting()
	If fire_Pressed2=False Then Return
	SetColor 255,255,255
	SetBlend(maskblend)
	DrawText "Player 2 Shooting",0,0
	For bullet:Tbullet=EachIn bulletList
	SetBlend(shadeblend)
	DrawImage bullet.image,bullet.x,bullet.y
	Next
End Function


I want to see what the values of bullet.x and bullet.y are?? I try and drawthem in a text line drawtext "bullet.x="+bullet.x,0,0 but it displays an error saying cant find x.

Ive also set the bullet starting position to Player1_xPos and Player1_yPos but the bullet doesnt appear where Player1_xPos or Player1_yPos is. It appears somewhere else which is a bit weird. Thats why i wanna know what the values of bullet.x and bullet.y are.


Joe90bigrat90(Posted 2011) [#82]
Ok worked out how to display bullet.x and bullet.y. Changed local bullet to global bullet thats why it wasnt seeing it. However the values of bullet.x and bullet.y are not the same as Player1_xPos and Player2_xPos. Why is this????


Joe90bigrat90(Posted 2011) [#83]
ok ive sorted it. Code now looks like this.

Type Tbullet
Field x:Int
Field y:Int
Field image:TImage
Field Speed:Int
End Type

Global bulletList:TList=CreateList()

Global bullet:Tbullet=New Tbullet
ListAddLast(bulletList,bullet)

While Not KeyDown(KEY_ESCAPE)

	bullet.x= Player1_xPos
	bullet.y= Player1_yPos
	bullet.image=bulletImg

Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Redraw_Map()
Buildings_And_Grid_Ingame()
Help_And_Variables()
Conditions()

Function Player1_Shooting()
	If fire_pressed=False Then Return
	SetColor 255,255,255
	SetBlend(maskblend)
	DrawText "Player 1 Shooting",0,0
	For bullet:Tbullet=EachIn bulletList
	SetBlend(shadeblend)
	DrawImage bullet.image,bullet.x,bullet.y
	Next
End Function


Ok so now my bullet is appearing at player1_Xpos and player1_Ypos and displaying correctly. I can see the values of bullet.x and bullet.y and they tie up with Player1_xPos and Player1_yPos.

I guess I need 2 seperate types 1 for player 1 and 1 for player 2. hmmm hadnt thought about that. cant i just use the same type for both players. all i need to do now is to move the bullet from player1_xpos or player1_ypos in teh correct direction and move the bullet. Will i need a frame timer??? i reckon i probably do to slow the bullet down a bit.
Any help with this coffee would be great. Im learning pretty fast and things are beginning t make a little more sense. not a lot but im getting better at finding errors and stuff.


coffeedotbean(Posted 2011) [#84]
Hum.. I feel you still don't understand how types work, took me a while back in the day to.

But then again I am not seeing all your code so I cannot be sure. I'll see If I can write up another example shortly.


Joe90bigrat90(Posted 2011) [#85]
hmmm this is getting more complex as i go on. Firstly at the present time if i press G which is fire for player 1 and right control for player 2. I have 2 different types. 1 for each player. If i just press g the bullet is displayed at Player1_xPos +17 and Player1_yPos+3 which displays the bullet in the correct position to start off with. However this only works if im facing right(3). obviously if i move down and press G the bullet appears to the right of the player which is incorrect as it should start down.(5). So I know the Player1_direction and i know the Player2_direction. Should i write several if statements saying if direction = 5 then bullet.x=Player1_xPos+17 etc etc
I would need 8 if statements.
Thing is though in this code supplied by coffee: i just added Player1direction=5 etc
i know what direction the player is moving all the time.
Function Move()
	
	Up_Pressed = False
	Down_Pressed = False
	Right_Pressed = False
	Left_Pressed = False
	Fire_Pressed = False
	
	Up_Pressed2=False
	Down_Pressed2=False
	Right_Pressed2=False
	Left_Pressed2=False
	Fire_Pressed2=False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	If KeyDown(KEY_G) Then Fire_Pressed = True
	
	If KeyDown(KEY_UP) Then Up_Pressed2 = True
	If KeyDown(KEY_DOWN) Then Down_Pressed2 = True
	If KeyDown(KEY_RIGHT) Then Right_Pressed2 = True
	If KeyDown(KEY_LEFT) Then Left_Pressed2 = True
	If KeyDown(KEY_RCONTROL) Then Fire_Pressed2 = True
	
	
	' Change player image and move player1 based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1direction=1
		If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1direction=5
		If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1direction=3
		If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		Player1direction=7
		If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
			EndIf 'no keys pressed
		Else
			Player1_Frame = 0
			Player1_Moving = False
			EndIf

i guess instead of if statements i could have case selects.
But the thing is we know what Player1direction is all the time with these statements. Is there an easy way of referencing the direction of player 1 instead of if statements or case.
Could i with the type i created put a field direction in there. Maybe thats the answer. hmmm i will have to think about this but my brain is frazzled with learning too much code. hahahaha


Joe90bigrat90(Posted 2011) [#86]
yeah im learning slowly coffeedotbean. But im programming all day. Not just writing my program but im reading tutorials and copying code and testing it, changing the values, see what they do. All day I am learning new stuff. Ive only been programming for about 2 weeks in blitzmax so im new to all this. But i love learning at how things have been done and your code has been top notch for me. And that editor you wrote helped me a lot understanding arrays. Yeah sometimes I program really well and other times i have a mental block. Its not hard for me to program but its hard for me to work out the best procedure and the best way to go out about things. I will learning for a long time yet and ive still got loads to learn but thanks a lot for your code. When i see your code I think christ why didnt i think of that and it is so logical. You've been a great help to me so far and i thankyou very much for your code. its been emotional!!!! hehe


Joe90bigrat90(Posted 2011) [#87]
I love the way you customize programs. U know so you can change values and stuff so the program still works. Like that array editor. You can change the number of cells. That was cool. Ive learnt not just to write a program but to make it as customizable as you can using arrays and variables. I do understand arrays a little bit better thanks to your editor. I guess im going to have to keep learning till i get to your stage of expertise but i will get there eventually. I guess things just take time. Today is the first time ive learnt about types. Cool. I think they could be quite useful in the future. Tommorrow i might write a short program just with types and arrays to get a better understanding of them. I'm gonna take some time out from writing my program to learn things that i need to know.


.rIKmAN.(Posted 2011) [#88]
I really think reading the "Beginners Guide to BlitzMax" would help you out learning about loops, arrays, types, the basics etc.

http://www.blitzbasic.com/Community/posts.php?topic=42519
(PDF and HTML version available)

Also, once you have your head around the basic programming principles, this a good guide on OOP in BlitzMax.

http://www.blitzbasic.com/Community/posts.php?topic=59233

Also, following something like this guides which starts from the beginning, and ends with (I think) a Breakout game will help you understand the various concepts and how each element works together to make a complete game.

http://www.blitzbasic.com/Community/posts.php?topic=59509

Hope that helps.

Last edited 2011


coffeedotbean(Posted 2011) [#89]
Here I put this together - this shows types and collisions - though I tend to not use the built in image collision but as your starting out it's good.

You'll need these images.







Graphics 800 , 600 , 0 , 60
SetBlend ALPHABLEND

' ---------------------------------------------------------------------------------------------
' Ship variables
Global Ship_xPos = GraphicsWidth()/2
Global Ship_yPos = GraphicsHeight() - 100
Global Ship_Speed = 4

' ---------------------------------------------------------------------------------------------
' Other variables
Global Score = 0
Global AsteriodList:TList = CreateList() ' List to hold all our Asteroid types (objects)
Global BulletList:TList = CreateList() ' List to hold all our Bullet types (objects)
SeedRnd MilliSecs()

' ---------------------------------------------------------------------------------------------
' Images
SetMaskColor 255,0,255
Global IMGAstro:TImage = LoadAnimImage("astro.png" , 40 , 40 , 0 , 8)
Global IMGShip:TImage = LoadImage("ship.png") ; MidHandleImage(IMGShip) 
Global IMGBullet:TImage = LoadImage("bullet.png") ; MidHandleImage(IMGBullet) 

' ---------------------------------------------------------------------------------------------
' Start
SpawnAsteriods()

' ---------------------------------------------------------------------------------------------
' Main loop
While Not KeyDown(KEY_ESCAPE)
Cls

	Asteriod.Draw()
	Asteriod.Update()
	Bullet.Draw()
	Bullet.Update()
	DrawShip()
	UpdateShip()
	Collisions()
	
	DrawText "Number of Asteroids: " + CountList(AsteriodList) , 10 , 10
	DrawText "Number of Bullets: " + CountList(BulletList) , 10 , 30
	DrawText "Space to shoot, Cursor keys to move, Enter to spawn asteroids" , 10 , 50
	DrawText "Score: " + Score , GraphicsWidth() - 200 , 10

Flip
Wend
End

' ---------------------------------------------------------------------------------------------
Function SpawnAsteriods()
	
	' Create 30 Asteroids
	For i = 0 To 29
		Asteriod.Create()	
	Next	
	
End Function

' ---------------------------------------------------------------------------------------------
Function Collisions()
	
	' Select our first bullet in the list
	For Local ThisBullet:Bullet = EachIn BulletList 
		' Loop through our asteroids and see if this bullet collides with any asteroids
		For Local ThisAstro:Asteriod = EachIn AsteriodList 
			If ImagesCollide(ThisBullet.Image , ThisBullet.xPos , ThisBullet.yPos , 0 , ThisAstro.Image , ThisAstro.xPos , ThisAstro.yPos , ThisAstro.Frame) = True
				' There is a collision so lets remove the bullet and asteroid and go back and select the next bullet and repeat till all bullets have been checked.
				ListRemove(BulletList , ThisBullet)
				ListRemove(AsteriodList , ThisAstro)
				Score = Score + 100
				' Important to force an exit cus this bullet no longer exists and will throw an error if we dont exit this loop.
				' If there were no collisions it would go back and select the next bullet.
				Exit
			EndIf
		Next
	Next
	
End Function

' ---------------------------------------------------------------------------------------------
Function UpdateShip()
	
	' Move ship left and right
	If KeyDown(KEY_RIGHT)
		Ship_xPos = Ship_xPos + Ship_Speed
	ElseIf KeyDown(KEY_LEFT)
		Ship_xPos = Ship_xPos - Ship_Speed
	Endif
	
	' Move ship up and down
	If KeyDown(KEY_UP)
		Ship_yPos = Ship_yPos - Ship_Speed
	ElseIf KeyDown(KEY_DOWN)
		Ship_yPos = Ship_yPos + Ship_Speed
	Endif
	
	' Press space to make a new bullet
	If KeyHit(KEY_SPACE)
		Bullet.Create(Ship_xPos,Ship_yPos)
	EndIf	
	
	' Press Enter to spawn more asteroids
	If KeyHit(KEY_ENTER)
		SpawnAsteriods()
	Endif
	
End Function

' ---------------------------------------------------------------------------------------------
Function DrawShip()
	
	' Draw our ship
	DrawImage IMGShip , Ship_xPos , Ship_yPos
	
End Function

' ---------------------------------------------------------------------------------------------
Type Bullet
	' Variables for our bullets
	Field xPos
	Field yPos
	Field Speed = 8
	Field Image:TImage = IMGBullet
	
	Function Create(x , y)
		' Make a new bullet
		Local NewBullet:Bullet= New Bullet
		NewBullet.xPos = x
		NewBullet.yPos = y
		ListAddLast(BulletList , NewBullet)
	End Function
	
	Function Update()
		If BulletList = Null Then Return
		' Update each bullet and decrease their ypos by speed
		For Local ThisBullet:Bullet = EachIn BulletList 
			ThisBullet.yPos = ThisBullet.yPos - ThisBullet.Speed
		Next
	End Function
	
	Function Draw()
		If BulletList = Null Then Return
		' Draw each of out bullets in our list
		For Local ThisBullet:Bullet = EachIn BulletList 
			DrawImage ThisBullet.Image , ThisBullet.xPos , ThisBullet.yPos
		Next
		Bullet.OffScreen()
	End Function
	
	Function OffScreen()
		If BulletList = Null Then Return
		' Delete any Bullets that go off the screen
		For Local ThisBullet:Bullet = EachIn BulletList 
			If ThisBullet.yPos < -100 Then ListRemove(BulletList , ThisBullet)
		Next
	End Function
	
End Type
' ---------------------------------------------------------------------------------------------
Type Asteriod
	' Varaibles for our asteriods
	Field xPos#
	Field yPos#
	Field xSpeed#
	Field ySpeed#
	Field Image:TImage = IMGAstro
	Field Frame
	Field AnimTimer
	
	Function Create()
		' Make a new asteroid - randomise some variables to make each a little different
		Local NewAstro:Asteriod = New Asteriod
		NewAstro.xPos = Rnd(0 , GraphicsWidth() )
		NewAstro.yPos = - Rand(100,300)
		NewAstro.ySpeed = Rnd(1 , 3)
		NewAstro.Frame = Rand(0 , 7)
		ListAddLast(AsteriodList , NewAstro)
	End Function
	
	Function Draw()
		If AsteriodList = Null Then Return
		' Draw each asteriod in our list
		For Local ThisAstro:Asteriod = EachIn AsteriodList 
			DrawImage ThisAstro.Image , ThisAstro.xPos , ThisAstro.yPos , ThisAstro.Frame
		Next
	End Function
	
	Function Update()
		If AsteriodList = Null Then Return
		' Update and animate each asteroid
		For Local ThisAstro:Asteriod = EachIn AsteriodList 
			' Move it down the screen by it's speed 
			ThisAstro.yPos = ThisAstro.yPos + ThisAstro.ySpeed
			' Simple animation timer
			If MilliSecs() > ThisAstro.AnimTimer
				ThisAstro.AnimTimer = MilliSecs() + 100
				ThisAstro.Frame = ThisAstro.Frame + 1
				If ThisAstro.Frame => 8 Then ThisAstro.Frame = 0
			Endif
		Next
		Asteriod.OffScreen()
	End Function
	
	Function OffScreen()
		If AsteriodList = Null Then Return
		' Delete asteriod if it goes off the bottom of the screen, then make a new one.
		For Local ThisAstro:Asteriod = EachIn AsteriodList 
			If ThisAstro.yPos > GraphicsHeight()
				ListRemove(AsteriodList , ThisAstro)
				Asteriod.Create()	
			Endif
		Next
	End Function
	
End Type


Last edited 2011

Last edited 2011

Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#90]
yes thats a good program but it still doesnt help me with the direction of the player. The thing im stuck with is to be able to draw the bullet in the correct direction depending on what direction the player is facing.
I saved the pics and run the program so i can see how collision works which is great. I will use that in my code later. I just need help with the player1direction and player2direction. Thats what im confused about. Here is some code of the program.

SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080


'variables for player 1
Global Player1_moving=False
Global Player1_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player1east.png",50,50,0,4)
Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120
Global Player1_Size = 50
Global Player1_Score=0

'general variables
Global numberofbuildings = 50
Global numberofbuildings_Max = 200
Global numberofbuildings_Min = 0
Global Building_Size = 55
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size-2
Global Building_Grid[World_Width+1,World_Height+1]

Make_Buildings(Building_Count)

Global Value_Increase_Step=5
Global tickerx#=1920
Global font:TImageFont = LoadImageFont("d:\impact.ttf", 50) 
Global font2:TImageFont =LoadImageFont("d:\impact.ttf",30) 

'load the images into the variables for Player 1
Global Player1_North:TImage 		= LoadAnimImage("d:\player1north.png",50,50,0,4)
Global Player1_East:TImage 		= LoadAnimImage("d:\player1east.png",50,50,0,4)
Global Player1_South:TImage 		= LoadAnimImage("d:\player1south.png",50,50,0,4)
Global Player1_West:TImage		= LoadAnimImage("d:\player1west.png",50,50,0,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("d:\player1northeast.png",50,50,0,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("d:\player1northwest.png",50,50,0,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("d:\player1southeast.png",50,50,0,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("d:\player1southwest.png",50,50,0,4)
Global building=LoadImage("d:\smallbuilding.png",0)

Global player1name:String="Player 1"
Global player2name:String="Player 2"

Global Enable_Grid= True

Global Player1direction
Global Player2direction

Global Up_Pressed = False
Global Down_Pressed = False
Global Right_Pressed = False
Global Left_Pressed = False
Global Fire_Pressed =False

Make_Buildings(numberofbuildings)

While Not KeyDown(KEY_ESCAPE)


Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Redraw_Map()
Buildings_And_Grid_Ingame()
Help_And_Variables()
Conditions()

ScrollingText2()


Flip
Wend
End


Function PositionPlayer1()

	SeedRnd MilliSecs()
	Repeat
		Local x =Rand(0,World_Width)
		Local y =Rand(0,World_Height-1)
		If Building_Grid[x, y] = 1
			ElseIf Building_Grid[x,y] =0
			Player1_xpos=x* Building_Size
			Player1_ypos=y* Building_Size
			Building_Grid[x,y]=3
			Exit	
		EndIf
	Forever

End Function

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function

Function Draw()
	SetClsColor 204,204,204
	Cls
	
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetBlend(shadeblend)
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'SetScale 1.0,1.0
End Function

Function Hit_Building_Player1(Player1_Direction)

	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	'Local Player2_Box = Player2_Size - Player2_Speed
	
	Select Player1_Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
		
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
			
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
			
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
			

	End Select
	
	Return False
	
End Function

Function Move()
	
	Up_Pressed = False
	Down_Pressed = False
	Right_Pressed = False
	Left_Pressed = False
	Fire_Pressed = False
	
	Up_Pressed2=False
	Down_Pressed2=False
	Right_Pressed2=False
	Left_Pressed2=False
	Fire_Pressed2=False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	If KeyDown(KEY_G) Then Fire_Pressed = True
	
		
	' Change player image and move player1 based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1direction=1
		If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1direction=5
		If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1direction=3
		If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		Player1direction=7
		If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
			EndIf 'no keys pressed
		Else
			Player1_Frame = 0
			Player1_Moving = False
			EndIf

Function Redraw_Map()
	If KeyHit(KEY_F1)
	Make_Buildings(Building_Count)
	Make_Buildings(numberofbuildings)
	EndIf
End Function

Function Buildings_And_Grid_Ingame()
	If KeyDown(KEY_F2)
		Delay(60)
		numberofbuildings=numberofbuildings+1
		SetBlend(maskblend)
		SetColor 0,0,0
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(shadeblend)
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
		Make_Buildings(numberofbuildings)
	ElseIf KeyDown(KEY_F3)
		Delay(60)
		numberofbuildings = numberofbuildings-1
		SetBlend(maskblend)
		SetColor 0,0,0
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(Shadeblend)
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Max
		Make_Buildings(numberofbuildings)
	ElseIf KeyDown(KEY_F4) 
		Enable_Grid=False
		screengrid="off"
		SetColor 0,0,0
	ElseIf KeyDown(KEY_F5) 
		Enable_Grid=True
		screengrid="on"
	EndIf
		
End Function

Function Conditions()
	If Player1_xPos + Player1_Size > GraphicsWidth() Then Player1_xPos = GraphicsWidth() - Player1_Size 
	If Player1_xPos < 0 Then Player1_xPos = 0
	If Player1_yPos + Player1_Size > GraphicsHeight()-130 Then Player1_yPos = GraphicsHeight()-130 - Player1_Size 
	If Player1_yPos < 0 Then Player1_yPos = 0

	If Player2_xPos + Player2_Size > GraphicsWidth() Then Player2_xPos = GraphicsWidth() - Player2_Size 
	If Player2_xPos < 0 Then Player2_xPos = 0
	If Player2_yPos + Player2_Size > GraphicsHeight()-130 Then Player2_yPos = GraphicsHeight()-130 - Player2_Size 
	If Player2_yPos < 0 Then Player2_yPos = 0
End Function

Function GetPlayer1Name() 
	Cls
	FlushKeys
	Local done = 0
	Local c
	Repeat
		Cls
		SetBlend(maskblend)
		DrawText "Input Player 1 Name:> " + player1name:String, 700, 500
		c = GetChar() 
		If c <> 0
			If c = 8
				If Len(player1name:String) > 0 Then player1name:String = Left:String(player1name:String, Len(player1name:String) - 1) 
			EndIf
			Select c
				Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72,  ..
					73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,  ..
					97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,  ..
					110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122
						player1name = player1name + Chr(c)
			End Select
			
		EndIf
		
		If KeyHit(KEY_ENTER) 
			FlushKeys
			done = 1
		End If
		Flip
	Until done = 1
End Function

Function ScrollingText()
	'scrolling text For title screen
	text$="Welcome to Vendetta!!!     Select your 'Options' and press '1' to start game!!!     Press 'F1' to redraw the map in game!!!     "
	text:+"Look at the Mini-Map to find the bonus pickups!!!     Try to take the pickups before your opponent!!!                                                                                                               "
	text:+"                                                                                                                                                                                                                    "
	tickerx=tickerx-40
	If tickerx<-TextWidth(text) tickerx=1920
	SetImageFont(font)
	
	DrawText text,tickerx#,980


	SetImageFont(font2)

End Function

Function ScrollingText2()
'scrolling text for in game
	text2$="Let the Battle Commence!!!                                                                                                                                                                                   Who will get the first Kill!!!  "+player1name
	text2:+"  or "+player2name                                                                                                                                                                                                        
	text2:+"???"
	text2:+"                                                                                                                                                                     Shoot to Kill and Blow his 'Fuckin' head off!!!"
	text2:+"                                                                                                                                                                     It's the first to "+pointstowin                                    
	text2:+" points!!!					                                                                                                                                                                                                        "
			
	SetImageFont(font2)
	DrawText text2,tickerx#,990
	tickerx=tickerx-4
	If tickerx<-TextWidth(text2) tickerx=1920

Function Player1_Shooting()
If fire_pressed=False Then Return
	SetColor 255,255,255
	SetBlend(maskblend)
	DrawText "Player 1 Shooting",0,0
	SetBlend(shadeblend)
End Function

Ok thats the end of the code. In fact I think you wrote most of it. Its a pretty crap game really. Its good code from you but my graphics are really crap. It looks so poor but still its a learning process.

Can you help me with the direction of the player and then to display a bullet correctly. I dont know how to go about doing this??????


coffeedotbean(Posted 2011) [#91]
Your almost there, you have the "Player1direction" variable so use that when you create your bullet. You know that if Player1direction = 1 then the playing is facing north so the bullet only need to travel along the y axis, if Player1direction = 2 then it needs to travel along the x and the y, if Player1direction = 3 then the bullet only need to travel along the y .. etc.

Use what ever Player1direction equals when you create your bullet to give it speed along the x and/or y axis.

... for example

Bullet.Create( Player1_xPos , Player1_yPos, Player1direction )

Type Bullet
	Field xPos
	Field yPos
	Field xSpeed
        Field ySpeed
        Field Speed = 8
	Field Image:TImage = IMGBullet
	
	Function Create(x , y , direction)
		' Make a new bullet
		Local NewBullet:Bullet= New Bullet
		NewBullet.xPos = x
		NewBullet.yPos = y
                Select direction
                Case 1
                   NewBullet.xSpeed = 0
                   NewBullet.ySpeed = Speed
                Case 2
                   NewBullet.xSpeed = Speed/2
                   NewBullet.ySpeed = Speed/2
                Case 3
                   NewBullet.xSpeed = Speed
                   NewBullet.ySpeed = 0
                End Select
		ListAddLast(BulletList , NewBullet)
	End Function



Joe90bigrat90(Posted 2011) [#92]
Ok yeah that seems better. Ok i press g now to fire from player 1 and im getting my bullet image drawing on the screen and moving up the screen which is great. Everythings working except for the direction of the bullets.
At the moment its always shooting a bullet to the north(direction1).

Interesting how you actually create a bullet. I will practice coding types later on today and read those tutorials. Its pretty clever stuff. So i could have loads of fields in my type and just refer to them with a fullstop in front. Pretty cool.

Heres my code. I think ive made a silly mistake somewhere. I think i may have missed player1direction somewhere. I wont post all the code just my bullet code.

Heres my code.
Global Player1direction

Global IMGBullet:TImage=LoadImage("d:\bullet.png");MidHandleImage(IMGBullet)
Global Up_Pressed = False
Global Down_Pressed = False
Global Right_Pressed = False
Global Left_Pressed = False
Global Fire_Pressed =False

Global BulletList:TList=CreateList()

While Not KeyDown(KEY_ESCAPE)

Move()'no need to show code
Draw()'no need to show code
Draw_Grid()'no need to show code
Player1_Shooting()
Player2_Shooting()
Bullet.Create(x,y,direction)
Bullet.Update()
Bullet.DrawBullet()
Redraw_Map()'no need to show code
Buildings_And_Grid_Ingame()'no need to show code
Help_And_Variables()'no need to show code
Conditions()'no need to show code

Type Bullet
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Field Speed=5
	Field Image:TImage=IMGBullet
	
Function Create(x,y,direction)
	Local NewBullet:Bullet=New Bullet
	NewBullet.xPos = x
		NewBullet.yPos = y
                Select direction
                Case 1
                   NewBullet.xSpeed = 0
                   NewBullet.ySpeed = Speed
                Case 2
                   NewBullet.xSpeed = Speed/2
                   NewBullet.ySpeed = Speed/2
                Case 3
                   NewBullet.xSpeed = Speed
                   NewBullet.ySpeed = 0
                End Select
		ListAddLast(BulletList , NewBullet)
End Function

Function Update()
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
		ThisBullet.yPos=ThisBullet.yPos-ThisBullet.Speed

	Next
End Function

Function DrawBullet()
	SetColor 255,255,255
	SetBlend(maskblend)
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
	DrawImage ThisBullet.Image, ThisBullet.xPos, ThisBullet.yPos
	SetBlend(shadeblend)
	Next
	Bullet.OffScreen()
End Function	

Function OffScreen()
If BulletList = Null Then Return
		' Delete any Bullets that go off the screen
		For Local ThisBullet:Bullet = EachIn BulletList 
			If ThisBullet.yPos >1920 Or thisbullet.xpos <0 Then ListRemove(BulletList , ThisBullet)
		Next
End Function

End Type

Function Player1_Shooting()
If fire_pressed=False Then Return
	Bullet.Create(Player1_xPos,Player1_yPos, Player1direction)
End Function


Cool things are starting to look up. I will apply this code for other bullets as well.

Ok so when i fire bullet is moving up even when i move in all directions. Why is this.

hmmm somewhere im missing a player1direction.


Joe90bigrat90(Posted 2011) [#93]
hmmm im just noticing the function Update()
Seems that theres only a ypos value. I guess i need an xpos in here as well.


Joe90bigrat90(Posted 2011) [#94]
Nah wait a minute that wont work right. yeah so looking at code it creates the bullet at player1_xPos, Player1_yPos and the direction of the player.
So its definatley creating the bullet correctly.
Must be the draw image call.
Do i need to specify a direction with the drawimage call.


Joe90bigrat90(Posted 2011) [#95]
DrawImage ThisBullet.Image, ThisBullet.xPos, ThisBullet.yPos


would this work
DrawImage ThisBullet.Image, ThisBullet.xPos(Player1direction), ThisBullet.yPos(Player1direction)




Joe90bigrat90(Posted 2011) [#96]
the bullet is firing ok from the x and y position. It just doesnt seem to recognise direction.
My 2 variables for player 1 and player 2 are Player1direction and Player2direction.

I think its somwething to do with this bit of code:
Function Create(x,y,direction)
	Local NewBullet:Bullet=New Bullet
	NewBullet.xPos = x
		NewBullet.yPos = y
                Select direction
                Case 1
                   NewBullet.xSpeed = 0
                   NewBullet.ySpeed = Speed
                Case 2
                   NewBullet.xSpeed = Speed/2
                   NewBullet.ySpeed = Speed/2
                Case 3
                   NewBullet.xSpeed = Speed
                   NewBullet.ySpeed = 0
                End Select
		ListAddLast(BulletList , NewBullet)
End Function


If i rem all the lines out the bullet still fires to the north(0) up!!
I wonder if this function is being called at all???
I will test it and see!!


Joe90bigrat90(Posted 2011) [#97]
ahhh i see i didnt read your line above the code.

Hehehe
I need to add more case statements.
Cool
I do that now!!!


Joe90bigrat90(Posted 2011) [#98]
yeah i do understand that but for example it doesnt seem to be reading the case statements. The bullet always fires up to the north. Up!!!
I understand about the x and the y to change where the bullet is firing but no matter what direction i am moving in it always fires up!!!


Joe90bigrat90(Posted 2011) [#99]
yes the function is being called but i dont understand why the bullet is always firing up. it doesnt seem to be reading the direction???
Weird!!


coffeedotbean(Posted 2011) [#100]
Have you change the Update() function to take into account the xspeed?

Function Update()
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
		ThisBullet.yPos=ThisBullet.yPos-ThisBullet.Speed

	Next
End Function



Joe90bigrat90(Posted 2011) [#101]
yes i have. I have that line of code already.
here is my bullet code

SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080


Global Player1direction
Global Player2direction
Global IMGBullet:TImage=LoadImage("d:\bullet.png");MidHandleImage(IMGBullet)

While Not KeyDown(KEY_ESCAPE)

Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Bullet.Create(x,y,direction)
Bullet.Update()
Bullet.DrawBullet()
Redraw_Map()
Buildings_And_Grid_Ingame()
Help_And_Variables()
Conditions()

Flip
Wend
End

Type Bullet
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Field Speed=6
	Field Image:TImage=IMGBullet
	

Function Create(x,y,direction)
	Local NewBullet:Bullet=New Bullet
	NewBullet.xPos = x+25
		NewBullet.yPos = y
		Select direction
		Case 1
		NewBullet.xSpeed = 0
		NewBullet.ySpeed = Speed
		Case 2
		NewBullet.xSpeed = Speed/2
		NewBullet.ySpeed = Speed/2
		Case 3
		NewBullet.xSpeed = Speed
		NewBullet.ySpeed = 0
		End Select
		ListAddLast(BulletList , NewBullet)
End Function

Function Update()
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
		ThisBullet.yPos=ThisBullet.yPos-ThisBullet.Speed
		
	Next
End Function

Function DrawBullet()
	SetColor 255,255,255
	SetBlend(maskblend)
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
	DrawImage ThisBullet.Image, ThisBullet.xPos, ThisBullet.yPos
	SetBlend(shadeblend)
	Next
	Bullet.OffScreen()
End Function	

Function OffScreen()
If BulletList = Null Then Return
		' Delete any Bullets that go off the screen
		For Local ThisBullet:Bullet = EachIn BulletList 
			If ThisBullet.yPos >1920 Or thisbullet.xpos <0 Then ListRemove(BulletList , ThisBullet)
		Next
End Function

Function Move()
	
	Up_Pressed = False
	Down_Pressed = False
	Right_Pressed = False
	Left_Pressed = False
	Fire_Pressed = False
	
	Up_Pressed2=False
	Down_Pressed2=False
	Right_Pressed2=False
	Left_Pressed2=False
	Fire_Pressed2=False
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	If KeyHit(KEY_G) Then Fire_Pressed = True
	
	If KeyDown(KEY_UP) Then Up_Pressed2 = True
	If KeyDown(KEY_DOWN) Then Down_Pressed2 = True
	If KeyDown(KEY_RIGHT) Then Right_Pressed2 = True
	If KeyDown(KEY_LEFT) Then Left_Pressed2 = True
	If KeyHit(KEY_RCONTROL) Then Fire_Pressed2 = True

If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1direction=1
		If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1direction=5
		If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1direction=3
		If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		Player1direction=7
		If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
			EndIf 'no keys pressed
		Else
			Player1_Frame = 0
			Player1_Moving = False
			EndIf
Function Player1_Shooting()

If fire_pressed=False Then Return
	Bullet.Create(Player1_xPos,Player1_yPos, Player1direction)
	channel2=PlaySound(gunshot)
	SetChannelRate channel2,0.5
	SetChannelVolume channel2,0.2

End Function

Function Player2_Shooting()
If fire_pressed2=False Then Return
	Bullet.Create(Player2_xPos,Player2_yPos, Player2direction)
	channel3=PlaySound(gunshot)
	SetChannelRate channel3,1
	SetChannelVolume channel3,0.2
End Function



Last edited 2011

Last edited 2011


coffeedotbean(Posted 2011) [#102]
Problem is the Update() function.... You have not told the bullet to travel along the x and your not using xSpeed and ySpeed just Speed which will always be 6 which is why it only ever traveled up the screen.

Function should be like this.

Function Update()
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
		ThisBullet.yPos=ThisBullet.yPos + ThisBullet.xSpeed
		ThisBullet.xPos=ThisBullet.xPos + ThisBullet.ySpeed

	Next
End Function





Joe90bigrat90(Posted 2011) [#103]
ahhhh i see coffee yes that makes sense to move the x and the y. Ooops yes i didnt pick that up. ok ive updated the code and run the program.

hmmm nah it doesnt work. i press fire for player 1 and now i dont get a moving bullet. I just get a stationary bullet pointing to the north of player1

The bullet appears on the screen but always above the player. Still if i move in any direction the bullet appears to the north but this time its not moving like a bullet. Just stationary and is drawn on the screen.

Ill have a look through the code and see whats going wrong!!!


Joe90bigrat90(Posted 2011) [#104]
Hey coffee thanks for your help on this. I think once this bullet situation is fixed i will be able to start coding the rest of the game myself. Mind you it will take months before it is ready. Ive still got tons to do. Random sprites appearing, vehicles, different bullets and stuff. Its gonna take about a year. hheheee

Ive had a look through the code and ive run your program again but in your program you dont have 8 directions for bullets so i cant really compare it to my program although yours is much better coded than mine.

I think after this bullet is sorted im gonna take time out for about 2 weeks and start learning those tutorials you gave me. I wont bother working on the program until ive learnt some stuff. I dont want to keep asking you for help all the time cos you got your own projects to be getting on with.

If you could just help me find this problem( you will be able to see whats happening im sure) ill leave you alone in peace and start learning those tutorials.


coffeedotbean(Posted 2011) [#105]
found an error in my code that could be the problem;

xpeed and yspeed should be the other way round.

Function Update()
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
		ThisBullet.yPos=ThisBullet.yPos + ThisBullet.ySpeed
		ThisBullet.xPos=ThisBullet.xPos + ThisBullet.xSpeed

	Next
End Function



Joe90bigrat90(Posted 2011) [#106]
nah its not that. I have changed it though to what youve got above but thats not the problem. Still when i press fire button for player 1 im still getting a still image of the bullet above player1's head in every direction i move in. I cant seem to work out whats causing the problem. Before when i had this code in:

Function Update()
	If BulletList=Null Then Return
	For Local ThisBullet:Bullet=EachIn BulletList
		ThisBullet.yPos=ThisBullet.yPos-ThisBullet.Speed
		
	Next
End Function


the bullet was actually moving on the screen although yes it is incorrect dure to xspeed and yspeed.

Now i just get a dot above the players head which is my bullet image.
Pretty strange huh. Its got to be something to do with xspeed and yspeed
not moving the bullet.

I have got the correct code here:

Function Update()
If BulletList=Null Then Return
For Local ThisBullet:Bullet=EachIn BulletList
ThisBullet.yPos=ThisBullet.yPos + ThisBullet.ySpeed
ThisBullet.xPos=ThisBullet.xPos + ThisBullet.xSpeed
Next
End Function

Last edited 2011


coffeedotbean(Posted 2011) [#107]
Do some tests to see what values your bullet has.. should help find the issue.

eg;

For Local ThisBullet:Bullet=EachIn BulletList
   DrawText ThisBullet.xSpeed+"/"+ThisBullet.ySpeed,ThisBullet.xPos,ThisBullet.yPos
Next




Joe90bigrat90(Posted 2011) [#108]
hey ive just tried something to see what happens

Case 1
		NewBullet.xSpeed = 0
		NewBullet.ySpeed = Speed


If i change the NewBullet.xSpeed=4
then if im facing up then i get a moving bullet going to the right
if i dont move up and go in any other direction i just get a still bullet image above my head.

Last edited 2011


Joe90bigrat90(Posted 2011) [#109]
Case 3
		NewBullet.xSpeed = Speed
		NewBullet.ySpeed = 0
		End Select

if i change NewBullet.ySpeed to 4
then if im facing right i get a moving bullet going down.
If i dont move down and go in any other direction i just get a still bullet image above my head


Joe90bigrat90(Posted 2011) [#110]
ok i pasted that line into my code and when i press fire button its printing 0/0.


coffeedotbean(Posted 2011) [#111]
Don't forget in the code we only have 3 directions programmed 1(North),2(North East) & 3(East) - go in any other direction and the bullet is not being assigned x and y speeds.

Last edited 2011


Joe90bigrat90(Posted 2011) [#112]
yeah i understand that. if i move north and press fire i get a still bullet above my head. If i move east i get a still bullet above my head and if i move north east i get a still bullet above my head. In fact every direction is giving me a still bullet. no moving bullets at all unless i change NewBullet.ySpeed=4 then i only get a moving bullet if im going right and then bullet moves down.


coffeedotbean(Posted 2011) [#113]
ah i see the problem. You need to use;

NewBullet.xSpeed = NewBullet.Speed


Not...

NewBullet.xSpeed = Speed



Joe90bigrat90(Posted 2011) [#114]
cool nice one. Now when i fire if i move right and fire i get moving bullet to the right.
If i move up and fire i get a moving bullet going down (the opposite direction hehehe)
and if i move north east i get a bullet moving south east.

Cool now i need to add the rest of the case statements and adjust where the bullet is firing from. i can do that myself.

Are you sure that code you gave me for the case statements is correct. Just wondered why the bullets are going in opposite directions. Thats ok should be just a + or - problem.

Thanks a lot coffee.
After today I will end your misery for my lack of programming knowledge. Time to start looking at those tutorials. Thanks


coffeedotbean(Posted 2011) [#115]
Just need to use put a "-" where appropriate, for example,

south...
NewBullet.xSpeed = 0
NewBullet.ySpeed = NewBullet.Speed ' = +4


north...
NewBullet.xSpeed = 0
NewBullet.ySpeed = -NewBullet.Speed ' = -4



Joe90bigrat90(Posted 2011) [#116]
yeah cool coffee i done it for all 8 directions and its working neat. hehehe
Mind you still a crappy game but its working. hehe
Ok my last question before I dissapear for 2 weeks learning tutorials.
I also have Player 2 setup firing as well.
Just want to know this one question
By creating that type in the future im obviously going to have to check for collisions.
Will I need 2 types (1 for player 1 and 1 for player 2) to determine who shot the bullet.
I mean the code is running fine at the mo. Both players firing and no clashes what so ever. Im just thinking about the future if i need to be able to tell who shot what bullet.
Thankyou for all of your help. You've been superb and i am very grateful for your time and effort. Its made me realise how much i need to know and therfore I must crack on with learning as much as i can before i continue with my program.
Thankyou very much once again.

Your help has been a huge guidance for me


coffeedotbean(Posted 2011) [#117]
Just have a new field varible called "owner" or similar and attach that to a bullet when it's created that way both players can use the same type but when checking for collisions check who the owner is and do what is needed.


Joe90bigrat90(Posted 2011) [#118]
ahhh i see. Ok ive got this now in my code. There was a problem with the bullets which i have sorted out now. The problem was if the player didnt move a bullet was being drawn to the screen when the fire button was pressed and stayed there permanently. I had to include code to fix it so that at the start of the game if the user presses fire button no button is drawn until they have actually moved. I put this code in
If KeyHit(KEY_G) And Player1_HasMoved=True Then Fire_Pressed = True
	If KeyHit(KEY_G) And Player1_HasMoved=False Then Fire_Pressed = False

Well thats sorted now which is good. Can you just check for me to make sure im doing this right. I think it is right. Ive included owner in the type. Just like you to check for me. thanks


If fire_pressed=False Then Return
	Bullet.Create(owner=player1,Player1_xPos,Player1_yPos, Player1direction)
EndIf	

If fire_pressed2=False Then Return
	Bullet.Create(owner=player2,Player2_xPos,Player2_yPos, Player2direction)

and my type is now:

Type Bullet
	Field owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Field Speed=8
	Field Image:TImage=IMGBullet
	
	
Function Create(owner,x,y,direction)
	Local NewBullet:Bullet=New Bullet
		NewBullet.xPos = x
		NewBullet.yPos = y
		Select direction
		Case 1 'I have more cases but no need to display them here
		NewBullet.xPos=x+25
		NewBullet.yPos=y+15
		NewBullet.xSpeed = 0
		NewBullet.ySpeed = -NewBullet.Speed
End Select
		ListAddLast(BulletList , NewBullet)
End Function


So im creating my bullet with owner at the front of it.

If i want to check what bullet comes from which player how do i refer to the owner field

Is it Bullet.owner????
thanks


Joe90bigrat90(Posted 2011) [#119]
hmmmm how do i actually refer to a type field so i can use it to change it.

For instance Speed=8

How do i refer to this in a text statement so i can view it.

Drawtext "Speed of Bullet = " + Bullet.Speed doesnt work.

How do i actually refer to a field in a type.

Thankyou.


Joe90bigrat90(Posted 2011) [#120]
im trying to change the value of Speed with a keypress outside of the type decleration.

So
If keyhit(KEY_6) Bullet.speed=Bullet.speed + 1

however it doesnt recognise Bullet.speed nor deos it recognise speed. There must be a way of actually accessing a field type within a use defined type.


coffeedotbean(Posted 2011) [#121]
This doesn't look right ...
If fire_pressed=False Then Return
	Bullet.Create(owner=player1,Player1_xPos,Player1_yPos, Player1direction)
EndIf	

If fire_pressed2=False Then Return
	Bullet.Create(owner=player2,Player2_xPos,Player2_yPos, Player2direction)
Endif


This should be ...
If fire_pressed=False Then Return
	Bullet.Create(1,Player1_xPos,Player1_yPos, Player1direction)
EndIf	

If fire_pressed2=False Then Return
	Bullet.Create(2,Player2_xPos,Player2_yPos, Player2direction)
Endif


And then ...
Type Bullet
	Field owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Field Speed=8
	Field Image:TImage=IMGBullet
	
	
Function Create(owner,x,y,direction)
	Local NewBullet:Bullet=New Bullet
		NewBullet.xPos = x
		NewBullet.yPos = y
                NewBullet.owner = owner		
                Select direction
		Case 1 'I have more cases but no need to display them here
		   NewBullet.xPos=x+25
		   NewBullet.yPos=y+15
		   NewBullet.xSpeed = 0
		   NewBullet.ySpeed = -NewBullet.Speed
                End Select
		ListAddLast(BulletList , NewBullet)
End Function


You cannot change a set field Value on a Global level (at least I don't think you can). You do this instead.

Type Bullet
        Global Whatever = 8	
        Field owner
	Field xPos
	Field yPos
	Field xSpeed
End Type


Then call it and change it with for example..

Bullet.Whatever = 8

This way you know it belongs to your bullet and is easier to read.


Joe90bigrat90(Posted 2011) [#122]
Ahhh i see ok thanks for that.
Hey im trying to make my bullets dissapear when the bullet hits a building.

Ive got this code
'my building image
Global building=LoadImage("d:\vendetta\sprites\smallbuilding.png",0)
'my bullet image
Global IMGBullet:TImage=LoadImage("d:\bullet.png");MidHandleImage(IMGBullet)

'my collisions function

Function Collisions()

             For Local ThisBullet:Bullet= EachIn BulletList
                          If ImagesCollide(ThisBullet.Image,ThisBullet.xPos,ThisBullet.yPos,0,building,x,y,0)=True
				ListRemove(BulletList, ThisBullet)
				Exit
			EndIf
	Next
End Function

'So im saying here for every bullet shot
if images collide(my bullet and the building image)=true
then remove the bullet and exit the function otherwise it would report error.
So it knows the building image and the bullet image so why isnt this working properly. I think the bullet is ok but looking at this maybe something wrong with the building data. It knows the image of the building but am i specifying x and y correctly.

My make buildings data is
Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()	
End Function

I see that x and y for the buildings is only local. Do i need to set this to global so that the x and y of the buildings could be accessed. Its got to be something to do with that x and y of the building.


Joe90bigrat90(Posted 2011) [#123]
Its pretty obvious why this isnt working.
Becasue x and y are equal to random numbers so its not gonna work out where the building is.
when it draws the buildings in the draw function:
If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size, y * Building_Size

it specifies x*Building_size which would be a random number * 50 and same for y.

I dont need to test for direction of the bullet as any bullet that hits a building i want to destroy.

I think i need to specify as x and y for building something to do with the Building_Grid.


Joe90bigrat90(Posted 2011) [#124]
Hmmm following your example code of trying to access a type field. Im trying to work out why owner isnt changing.

I have this code: This is the order in which i call my functions
Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Bullet.Create(owner,x,y,direction)
Bullet.Update()
Bullet.DrawBullet()
Collisions()
Redraw_Map()
Buildings_And_Grid_Ingame()
Help_And_Variables()
Conditions()

Type Bullet
	Global owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Global Speed=8
	Field Image:TImage=IMGBullet
	

Function Create(owner,x,y,direction)
	Local NewBullet:Bullet=New Bullet
		NewBullet.xPos = x
		NewBullet.yPos = y
		NewBullet.owner=owner
		NewBullet.Speed=NewBullet.Speed
		Select direction
		Case 1
		NewBullet.xPos=x+25
		NewBullet.yPos=y+15
		NewBullet.xSpeed = 0
		NewBullet.ySpeed = -NewBullet.Speed

Function Player1_Shooting()

If fire_pressed=False Then Return
	Bullet.Create(1,Player1_xPos,Player1_yPos, Player1direction)
DrawText "Bullet Owner="+Bullet.owner,500,480

EndIf
End Function

If i put a drawtext "Bullet Owner=" +Bullet.owner,0,0 in the Function Player1_Shooting() i get a result of 1 or 2 which is correct.

However if i put a drawtext in Help_And_Variables() when i press fire for both players Bullet.owner is not being updated. It seems that only when i drawtext in the Player1_shooting() Function it displays the correct result. Why is this if ive got global owner. It should give me a result no matter where i put a drawtext????????????
???????????


coffeedotbean(Posted 2011) [#125]
Owner needs to be a field not a global, owner is unique to each bullet.

This will show you who owns each bullet/.
For Local ThisBullet:Bullet= EachIn BulletList
Drawtext ThisBullet.Owner,ThisBullet.xPos,ThisBullet.yPos
Next


As for collision - fastest way is to make a building type then in the function where you make the buildings in the array you also call the function to make a building type - this type will just hold the image and x,y position on screen (array is still used for player vs building collitions) so you can do buillet vs building collisions - when the building is destroyed ensure you set the array at that buildings position to 0 so players can no longer collide with it.


Joe90bigrat90(Posted 2011) [#126]
ok thanks coffee. Sorry for the late reply. I lost my internet connection. back online now. Ive been reading the tutorials that you gave me the links for. Very interesting.
Im gonna try coding today so that when the bullets hit the buildings they are destroyed.
I will create a building type as you suggested. I will see how it goes. If I get any problems ill post here.
Thankyou coffee for your help.
Kind Regards
Joe


Joe90bigrat90(Posted 2011) [#127]
ok so i have the bullets working for both players in all directions.
If any bullet hits a building i want to delete it and delete the bullet. Ive followed your suggestions as above.
I have created a new type called building.

Type Building
Field Image:TImage=building
Field X 'stores x location of building
Field Y 'stores y location of building


I have this code for creating the buildings.

Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()
               CreateBuilding(Image,x,y)
End Function


So this function actually creates the buildings.

Im having problems referring to x and y of the buildings.

So in my type i store the image for the building which is ok.
But how do i actually get the x and y of the building.
In the make buildings function x=random number from 0 to world_width and y=random number from 0 to world_height.

Would i need to create another array to store the x and y of the buildings?

Im finding this quite difficult to understand.

So i have my type but what am i supposed to do with it. I mean for the bullet type it creates a bullet when you fire the fire button but i do not want to create any more buildings. I just want to store the image, x and y. This is a lot more difficult than i first thought. lol.

Ive got my type created and am calling a function CreateBuilding(Image,x,y) from my make buildings function but this is different from my bullet function as the bullet function is creating a bullet. I dont want to create any more buildings. I just want to store the image and the x and y of each building. Im not sure how to go about doing this. Its confusing to say the least. After reading the tutorials its made things even more complicated. lol

Cant i just write a simple line of code to say that if the bullet hits any building delete the bullet and erase the building.

I know how to delete the bullet. thats not a problem but trying to work out how to obtain the x and y of the buildings is giving me a headache.


coffeedotbean(Posted 2011) [#128]
Think of types as a list of "Like" items on a sheet of paper

Visualised as...

  |Image | xPos | yPos | << fields
-----------------------
1.
2.
3.
4.
.. etc ..


So first we need to create our sheet of paper where we'll write down our list.

Global MyList:TList=CreateList()


Now we have a sheet of paper but it's blank.

Lets decide what information we want to list. Lets make a type and add our fields.

Type Item
    Field Image:TImage
    Field xPos
    Field yPos
End Type


Our paper (list) is still blank at this point.

Lets add an item;

Local NewItem:Item = New Item
    NewItem.xPos = 30
    NewItem.yPos = 60
    NewItem.Image = img_1
    ListAddLast(MyList,NewItem)


So Above we refer to this new item as just <b>NewItem</b> assign values to our fields and then add it to the list.

List now looks like this;

  |Image  | xPos | yPos |
------------------------
1.| img_1 |  30  |  60  |


Lets add another item..

Local NewItem:Item = New Item
    NewItem.xPos = 10
    NewItem.yPos = 90
    NewItem.Image = img_2
    ListAddLast(MyList,NewItem)


List looks like this;

  |Image  | xPos | yPos |
------------------------
1.| img_1 |  30  |  60  |
2.| img_2 |  10  |  90  |


We'll add 3 more items so our list looks like this;

  |Image  | xPos | yPos |
------------------------
1.| img_1 |  30  |  60  |
2.| img_2 |  10  |  90  |
3.| img_1 |  20  |   0  |
4.| img_5 |  80  |  50  |
5.| img_9 |  0   |  10  |


We can loop through our list with;

For Local a:Item = EachIn MyList
     DrawText a+". "+a.xPos , 10 , a*15
Next


The above code will go through the list one item at a time from first to last and draw in the screen the xPos of each item, To find the values it will use the variable <b>a</b> as a pointer.

OutPut would be;

1. 30
2. 10
3. 20
4. 80
5. 0


So let find the item at position 80:50, and if we find it draw the image.

For Local a:Item = EachIn MyList
     If a.xPos = 80 and a.yPos = 50
         Drawimage a.image , a.xPos , a.yPos
     Endif
Next


Hope this helps.


Joe90bigrat90(Posted 2011) [#129]
ahhh i see that makes things a lot clearer. Ok let me see if i understand this correctly. Ok so i think i know how i can refer to the different fields.

ok so first i create a variable like so:

Global BuildingList:TList=CreateList()

So my type is:

Type Building
Field x
Field y
Field Image:TImage
end type

This is where i get confused. Ok so i know how to add something to the list:
Local NewItem:Item = New Item
NewItem.x = 10
NewItem.y = 90
NewItem.Image = building
ListAddLast(BuildingList,NewItem)

and if i want to loop through the list and display stuff i use:

For Local a:Item = EachIn BuildingList
DrawText a+". "+a.x , 10 , a*15
Next

Ok thats cool i think i understand that. Ahhh i see ok i understand how to find a value as well that makes sense.

Ok i think i understand that much better. Thankyou

Ok so in the make buildings function:


Function Make_Buildings(Building_Count)
Clear_Buildings()
SeedRnd MilliSecs()
For i = 0 To Building_Count-1
Repeat
Local x = Rand(0 , World_Width)
Local y = Rand(0 , World_Height-1)

If Building_Grid[x , y] = 0
Building_Grid[x , y] = 1
Exit
EndIf
Forever
Next

PositionPlayer1()
PositionPlayer2()
CreateBuilding(Image,x,y)
End Function

i see that local x and y = random number from 0 to the world width.

So in my code for the type how do i reference x and y because they are local to the function. I need them to be global really. So in my list would i code something like this:

Local NewItem:Item = New Item
NewItem.x = Rand(0 , World_Width)'random x number
NewItem.y = Rand(0 , World_Height-1)'random y number
NewItem.Image = building
ListAddLast(BuildingList,NewItem)

my maximum number of buildings is set to 200.
If i had 200 buildings how would i add all these.
I guess i need a loop to add 200 buildings.

For Local a:Item = EachIn MyList
newitem.x= Rand(0 , World_Width)'random x number
NewItem.y = Rand(0 , World_Height-1)'random y number
NewItem.Image = building
Next

does that look correct??


coffeedotbean(Posted 2011) [#130]
It's better to have functions that deal wil types inside the type;

Type Building
  Field x
  Field y
  Field Image:TImage

   Function Create(grab_x,grab_y)
    
    Local NewItem:Building= New Building
    NewItem.x = grab_x   ' <<<<<<<< taken from function call 
    NewItem.y = grab_y   ' <<<<<<<< taken from function call
    NewItem.Image = img_building
    ListAddLast(BuildingList,NewItem)

   End Function

End type



Then in your makebuilding function;

Function Make_Buildings(Building_Count)
    Clear_Buildings()
    SeedRnd MilliSecs()
    For i = 0 To Building_Count-1
        Repeat
            Local x = Rand(0 , World_Width)
            Local y = Rand(0 , World_Height-1)
            If Building_Grid[x , y] = 0
                Building_Grid[x , y] = 1
                Building.Create(x,y)             ' <<<<<<< make our building type here passing the x and y values into the function call.
                Exit
            EndIf
        Forever
    Next

PositionPlayer1()
PositionPlayer2()
End Function


Last edited 2011


Joe90bigrat90(Posted 2011) [#131]
cool ok so always create functions inside the type. Ok i remember that.
Thankyou very much.
Ive coded in some code for detecting when a bullet hits a player.

My code is this
Function HitPlayer()
	For Local ThisBullet:Bullet=EachIn BulletList
		If ImagesCollide (IMGBullet:TImage,ThisBullet.xPos,ThisBullet.yPos,0,Player2_Image,Player2_xPos,Player2_yPos,0) And ThisBullet.owner=1
			SetBlend(maskblend)
			SetColor 0,0,0
			DrawText "bullet collided with player2",0,0
			SetBlend(shadeblend)
		EndIf
	Next	
End Function

Its working ok and the text is appearing when player1 shoots player 2 so i know that its working. Weird thing is though is this: The game starts off at normal speed and then with this function in it plays really slow and starts to lag.
If i remove this function gameplay stays at the right speed. fast!!!
Why would this function cause major slowdown or lag????

Could it be something to do with this function being called at the wrong time.


coffeedotbean(Posted 2011) [#132]
Function it's self looks fine.

I don't know how fast the imagescollide function is as I have never used it - it might just not be very good.

However It all depends on the rest of your code, where are you calling the HitPlayer() function?

You might also try addding the code to delete the bullet type into the function too to see if that helps.

Last edited 2011


Joe90bigrat90(Posted 2011) [#133]
yeah ok i will do some tests later.

Ive been testing for collision with bullets. I had to edit my sprite as it was too big. My sprites are now 38 high by 16 wide.

However i got this really annoying problem. i have 4 sprites as a png and there is 1 pixel between them. 16 wide each. It doesnt matter how i design the sprites 50x50, 10x10 I always have a gap when im moving east between my sprites and the block. If you move south sprite is great - right up to the block, north sprite is great right up to the block, south sprite is great - right up to the block. East man everytime i have a gap so its nothing to do with the sprites. Building size is 55. Player1_Size is 38 the same as the sprite height. Strange that north, south and west are ok but only east has a problem. Any idea as to why this is coffee. Ive tried adjusting things in this.
Function Hit_Building_Player1(Player1_Direction)

	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	
	
	Select Player1_Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
		
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed +Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed +Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
			
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
			
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
			

	End Select
	
	Return False
	
End Function


I tried changing a few things in this like adding or subtracting x but then you get stuck next to the block and cant move. However it does seem to be much better when i adjust this. the player moves up to the block but gets stuck everytime and cant move. hehehe any ideas on how i can sort this out. Cheers


coffeedotbean(Posted 2011) [#134]
can you post the image?


Joe90bigrat90(Posted 2011) [#135]
ive also tested when trying to shoot the opponent. If you shoot up north and the player is above you it hits the player if you aim a little to the right of the player. But the sprite is exactly the correct size. In other words there are no gaps to the right of the sprite so perfect pixel collision should be possible. Very strange indeed.


coffeedotbean(Posted 2011) [#136]
As the sprite is not square any more looks like you'll need to change the function so it uses player1_width and player1_height rather then player1_size.

Last edited 2011


coffeedotbean(Posted 2011) [#137]
Then again try changing..

Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little


to

Local Player1_Box = Player1_Size - Abs(Player1_Speed) ' Reduce size of the player collision box a little



Joe90bigrat90(Posted 2011) [#138]
Hey how do i upload images on here. I dont have a website????


Joe90bigrat90(Posted 2011) [#139]
yeah i tried that. It doesnt work still the same.
Its a shame you cant upload images on here or can you?


Joe90bigrat90(Posted 2011) [#140]
if i change this to this:

Local Player1_Box = Player1_Size - Player1_Speed-20

then when i move right its perfect up to the block but now when i move south the player moves through the block - about halfway down.


Joe90bigrat90(Posted 2011) [#141]
hmmm something strange is going on with my function.

Function HitPlayer()
If BulletList=Null Then Return
For Local ThisBullet:Bullet=EachIn BulletList
If ImagesCollide (IMGBullet:TImage,ThisBullet.xPos,ThisBullet.yPos,0,Player2_Image,Player2_xPos,Player2_yPos,0) And Thisbullet.owner=1 Then ListRemove(BulletList, ThisBullet)
Next
End Function

When i run the program it doesnt matter where i put this function it runs at normal speed and then after about 30 seconds i get major slow down. If i remove the function gameplay is normal no delay. But as soon as i call it and ive tried putting it eveywhere it causes major lagg. I dont even have to shoot the player. No matter where i put it its slowing my program down big time after about 20 seconds. Rem the function out and it runs fine.

Here is what im calling.

Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Bullet.Create(owner,x,y,direction)
Bullet.Update()
Bullet.DrawBullet()
Bullet.Update()
Bullet.HitPlayer()
Redraw_Map()
Buildings_And_Grid_Ingame()
Help_And_Variables()
Conditions()

I must be doing something wrong. I dont understand why its lagging so much even when i havent shot the opponent. I could understand if i shot the person maybe. But the images collide isnt even coming into effect. Weird!!!!


Joe90bigrat90(Posted 2011) [#142]
i tried adding the code to delete the bullet type into the function too but it still remains the same. Very strange. If i rem the function out i get no slowdown whatsoever and ive tested it many times with the function and without. It cant be the if imagecollide command because im not even shooting at the player. If i leave the game running 20 seconds into the game and i get major lagg. If i rem it out leave the game i get no lag at all. Its weird.


Joe90(Posted 2011) [#143]
Cool ive sorted out the lag problem. I adjusted my code to the following.

If ThisBullet.owner=1 And ImagesCollide (IMGBullet:TImage,ThisBullet.xPos,ThisBullet.yPos,0,Player2_Image,Player2_xPos,Player2_yPos,Player2_frame) And Player2_Moving=False
ListRemove(BulletList,ThisBullet)

Seems to work perfectly.
I was trying things out in the game and ive come across a major bug.
When player2 moves northwest, player 1 cannot move south. Also if player 1 moves south player 2 cannot move northwest.

Ive gone over and over the code time and time again and i cannot find out where this bug is coming from. Ive checked the code loads of times and it all seems to be ok. This is a major flaw and cant continue unless i get this sorted.

Would anyone be able to take a look at the code and see if they can find out whats happening cos im baffled by this.


coffeedotbean(Posted 2011) [#144]
prob not the code its a limitation of keyboards http://answers.yahoo.com/question/index?qid=20081224211045AAfkVvM

Best bet go buy some cheap £5 usb game pads. http://www.play.com/Search.html?searchstring=gamepad&searchtype=allproducts&searchfilters=s%257bgamepad%257d%252b&pa=search&page=search&d=1&ob=4


Joe90(Posted 2011) [#145]
yeah coffee but its really strange. If player 2 moves north east and player 1 moves south it all works fine. The only time i get a problem is when player 2 moves north west and player 1 moves south. All the other directions work fine. If it was the keyboard then i would have thought that if player 2 moved north east and player 1 moves south then it wouldnt work either but it is. When player 2 is moving north west and player 1 cant move south i get a beep coming from somewhere. Sounds as if its coming from the keyboard. But my keyboard is pretty cool. Surely it can handle 3 keys down at one time. I mean i play loads of games and never had any problems. so you think it could be the keyboard???


Joe90(Posted 2011) [#146]
hey coffee you were right. it is the keyboard. Its something to do with the arrow keys. I changed the keys over and its working fine.
It doesnt like the arrow keys. Hehehe But it likes the num pad keys.
Thanks a lot.


Joe90(Posted 2011) [#147]
ok cool so when any player hits another player with the bullet it dissapears and is working ok.
I need to try and sort out when any player hits a building with a bullet it dissapears. Ive written code but it doesnt seem to be working. Would i need to put the function for hitting a building in the building type or would i need to put it into the bullet type.

Heres my code:
Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					Buildingposition.Create(x,y) ' here i call the function for the x and y of buildings.
				Exit
			EndIf
		Forever
	Next
	PositionPlayer1()
	PositionPlayer2()	
	
End Function

'Heres my type

Type Buildingposition
Field x
Field y
Field Image:TImage

Function Create(grab_x,grab_y)

Local NewItem:Buildingposition=New Buildingposition'
	NewItem.x=grab_x
	NewItem.y=grab_y
	NewItem.Image=building
	ListAddLast(BuildingList,NewItem)
	'SetColor 0,0,0
BulletHitBuilding()

End Function

BulletHitBuilding()
End Function

Function BulletHitBuilding()
If BulletList=Null Then Return
		For Local ThisBullet:Bullet=EachIn BulletList
			If ThisBullet.owner=1 And ImagesCollide (IMGBullet:TImage,ThisBullet.xPos,ThisBullet.yPos,0,building:TImage,x,y,0) Then
			ListRemove(BulletList,ThisBullet)
			EndIf
		Next

End Function
End Type



Im trying to say that if the bullet image collides with the building image then delete the bullet. But im not sure that x and y are working properly.
Do i need to put the function BulletHitBuilding() in the BulletType or the Building Type.

thanks a lot
Cheers
Joe


coffeedotbean(Posted 2011) [#148]
Your looping through your list of bullets but not your buildings;

.. try this.

Function BulletHitBuilding()
If BulletList=Null Then Return
		For Local ThisBullet:Bullet=EachIn BulletList
                For Local ThisBuilding:Buildingposition=EachIn Buildinglist
			If ThisBullet.owner=1 And ImagesCollide (ThisBullet.Image,ThisBullet.xPos,ThisBullet.yPos,0,ThisBuilding.Image,ThisBuilding.x,ThisBuilding.y,0) Then
			ListRemove(BulletList,ThisBullet)
			EndIf
		Next
                Next
End Function
End Type



Joe90(Posted 2011) [#149]
ahhh i see yes i can see what im doing wrong. I need to loop through the bullets and the buildings.
ive entered in the code but the bullets from player1 are still going through the buildings. In actual fact I dont really need to know the owner of the bullet at this stage as all bullets fired should be deleted if they hit a building. If i were to do other bullets (Say bouncing or ricochet) then yes maybe i would need to know the owner.

So after its created a building im calling the BulletHitBuilding Function so it should work.
Hmmmm
Ill have a look over the code and see if i can find anything out of place.
Thanks a lot for that. Heres the code if you do find something out of place let me know and that would be great.
Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					Buildingposition.Create(x,y)
				Exit
			EndIf
		Forever
	Next
	PositionPlayer1()
	PositionPlayer2()	
	
End Function

Type Buildingposition
Field x
Field y
Field Image:TImage

Function Create(grab_x,grab_y)

Local NewItem:Buildingposition=New Buildingposition'
	NewItem.x=grab_x
	NewItem.y=grab_y
	NewItem.Image=building
	ListAddLast(BuildingList,NewItem)
	'SetColor 0,0,0
BulletHitBuilding()
End Function

Function BulletHitBuilding()
If BulletList=Null Then Return
		For Local ThisBullet:Bullet=EachIn BulletList
		For Local ThisBuilding:Buildingposition=EachIn BuildingList
			If ThisBullet.owner=1 And ImagesCollide (ThisBullet.Image,ThisBullet.xPos,ThisBullet.yPos,0,ThisBuilding.Image,ThisBuilding.x,ThisBuilding.y,0) Then
			ListRemove(BulletList,ThisBullet)
			EndIf
		Next
		Next
End Function
End Type


Strange its not working. However im sure its not too hard to locate. i will work out whats going wrong.


Jesse(Posted 2011) [#150]
need to exit the building for loop after the bullet is removed.

Last edited 2011


Joe90(Posted 2011) [#151]
Ok Cheers Jesse. I was looking at your tile editor earlier. Its pretty cool. ive exited the loop. Still bullets are shooting through the buildings from both players. ????????


H&K(Posted 2011) [#152]
ThisBullet.owner=1
Whats "1"


Joe90(Posted 2011) [#153]
1 is the owner of the bullet. 1=Player1 and 2=player2
I dont really need to know who fired the bullet at this stage but it will be useful later on in case i need to use different types of bullets and i will need to know who fired them.


Joe90(Posted 2011) [#154]
ive tried shooting at the buildings just with player 1 but the bullets go straight through the buildings???? Any ideas as to what im doing wrong???
Cheers


coffeedotbean(Posted 2011) [#155]
I had some time to kill so I wrote the game for you =D

http://www.getperplexed.com/joe_game.zip

Did not comment it much but should help.

Keys are W,A,S,D,F for Player 1 Num 8,4,5,6,+ for player 2

Last edited 2011


Joe90(Posted 2011) [#156]
wow thats cool coffee. Cant belive you wrote that in such short time. Hehehe thanks for that. I will examine the code and try and get my bullets dissapearing when hitting the buildings.
Many thanks thats helped me a lot.


Joe90(Posted 2011) [#157]
hi coffee i been messing about with your game. I have found a couple of bugs in it. The first bug is when player 1 is at the top of the screen and you are pressing key to go up, ne or nw player 2 cant move. How can i rectifty this. Try it and you will see. The second bug is if there are barrels on the line below the top line then if you are moving on the top line say east and there is a barrel underneath you on the line below it wont let you move east or west past the barrel underneath you.

Do you think you know the solution to get rid of these bugs. The first one is the major one. I dont know why player 2 cant move when player 1 is at top of screen and moving up ne or nw.

Cheers coffee for your help


Joe90(Posted 2011) [#158]
hiya coffee hows u??? I got a slight problem and wondered if you could help me. Do you reckon you could fix those bugs above?? I dunno how to fix them. Anyway when i press a key i want to randomize the players.
Its working most of the time but about 2 out of 10 times it displays 2 player 2's instead of 1 player 1 and 1 player 2.

Heres my code.
Function Randomize_Player1()
	'SeedRnd MilliSecs()
	For Local a:Player= EachIn Player_List
	
			'ListRemove Player_List,a
			x=Rand(0,World_Width/2)
			y=Rand(0,World_Height-2)
				If Building_Grid[x , y]=0
					ListRemove Player_List,a
					Player.Create(1, x* Building_Size+20,y * Building_Size+10)
					Exit
					'ListRemove Player_List,a

						
				EndIf
		
		Next
	
End Function

Function Randomize_Player2()
	'SeedRnd MilliSecs()
	For Local a:Player= EachIn Player_List
	
			'ListRemove Player_List,a
			x=Rand(World_Width/2, World_Width)
			y=Rand(0, World_Height-2)
				If Building_Grid[x , y]=0
					ListRemove Player_List,a
					Player.Create(2, x* Building_Size+20,y* Building_Size+10)
					'ListRemove Player_List,a

				Exit		
				EndIf
		
		Next
	
End Function


ok so i press f8 and it calls these functions to randomize the players. As i said earlier most of the time it works but about 2 or 3 out of 10 times it displays 2 player 2's. It never displays 2 player 1's. Always 2 player 2's but only does it occasionally.

Im thinking it could be something to do with the grid. There are 35 grid spaces going across. If you half this it equals 17.5 so im wondering when it tries to position the players if its having problems due to not being able to display the image in teh centre grid lines. I guess it might not know which image to display in the middle. Any help much appreciated. Cheers coffee.

Last edited 2011


coffeedotbean(Posted 2011) [#159]
Ill take a look my code later, didin't realy test it for bugs.

The issue is your looping through your player list for some blizzare reason :p what is happening is the following;

Randomize_Player1() > loops through player list (which is 0 so does it once) resulting in 1 x player 1 being created.

Randomize_Player2() > loop through player list (which is now 1 cus it added a player in Randomize_Player1() so loops through twice resulting in 2 x player 2's.

Thy this, call Spawn_Players() when hitting F8.

Function Respawn_Players()

	' Delete players
	ClearList(Player_List)
	
	' Spawn player 1
	Randomize_Player1()
	
	' Spawn player 2
	Randomize_Player2()

End function

Function Randomize_Player1()
	
	' loop forever until player 1 finds a free space on the grid
	Repeat
		x=Rand(0,World_Width/2)
		y=Rand(0,World_Height-2)
		
		If Building_Grid[x , y]=0
			ListRemove Player_List,a
			Player.Create(1, x* Building_Size+20,y * Building_Size+10)
			Exit ' exit loop
		EndIf
		
	Forever
	
End Function

Function Randomize_Player2()
	
	' loop forever until player 2 finds a free space on the grid
	Repeat
	
		x=Rand(World_Width/2, World_Width)
		y=Rand(0, World_Height-2)
		
		If Building_Grid[x , y]=0
			ListRemove Player_List,a
			Player.Create(2, x* Building_Size+20,y* Building_Size+10)
			Exit ' exit loop
		EndIf
		
	Forever
	
End Function



coffeedotbean(Posted 2011) [#160]
Ok found the issue with the player not moving when player 1 was on the limits of the screen, just need to call flushkeys(), however I have resolved both issues by putting a barrel border around the play area.

The fix the issue would have involved more code and amny chnages so this is an easy fix.

The only code change is a few lines at the start of Make_World() function.

link: http://www.getperplexed.com/joe_game2.zip


Joe90(Posted 2011) [#161]
cool coffee thankyou. If you dont mind id like you look at this code for me. Ive gone back to my main game i was working on and recoded a lot of the code with the finished game you showed me.

I run my game and im still trying to work out bullets vs buildings.
When i shoot my bullets they do dissapear into the buildings which is good but the trouble ive got is that my bullets are also dissapearing on empty spaces on the grid and i cant work out why. It obviously thinks theres a building in an empty space for some reason and i dont know why.

Heres my code.
'my make buildings function.
Function Make_Buildings(Building_Count)
	Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
			Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					Buildings.Create(x*Building_Size,y*Building_Size) ' Here i am calling my type to create x and y
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()	
End Function

'Heres my update bullet and check for collision with building code
Function Update()
		If Bullet_List=Null Then Return
		For Local ThisBullet:Bullet=EachIn Bullet_List
			ThisBullet.yPos=ThisBullet.yPos+ThisBullet.ySpeed
			ThisBullet.xpos=ThisBullet.xPos+ThisBullet.xSpeed
		Next
	
	' Collide with building
		For Local b:Bullet= EachIn Bullet_List
			For Local c:Buildings= EachIn Building_List
				If b.owner=1 And ImagesCollide(b.Image , b.xPos , b.yPos , 0 , c.Image , c.xPos , c.yPos , 0)
					ListRemove(Bullet_List , b)
					Exit
				EndIf
			Next
		Next
		
'heres my building type
Type Buildings
	Field xPos
	Field yPos
	Field Image:TImage = Building
		
	' Create Building
	Function Create(x , y)
	Local a:Buildings= New Buildings
		a.xPos 	 = x
		a.yPos 	 = y
		ListAddLast(Building_List , a)
	End Function
End Type

I dont really want to create a building from this as its creating buildings fine from the Make_Buildings function.
I just need to call the buildings type to hold the x,y and image of the building.
As I say it is working when bullets hit buildings. its just making bullets dissapear when there is no building on the grid.
Thanks for your examples coffee. That game you wrote has helped me a lot with types definately so thanks a lot for that.
Can you work out why bullets are dissapearing in empty spaces.
thanks a lot.
Kind Regards
Joe

End Function


Joe90(Posted 2011) [#162]
Cool ive sorted out bullets vs player collisions and its working fine. Awesome!!! Ive just got 1 small problem.
When player 1 or player 2 shoots the opponent his health decreases (thanks to coffee) and i delete the bullet and the players score is increased. it then checks to see if the players score is equal to the pointstowin and if it is prints a message and exits the game. If it isnt then it then resets the map, positions the players again and resets the health. This is the problem i have. If i shoot say 6 bullets and 3 of those bullets cause the players health to decrease to zero it prints a message which is fine. It then resets the map,positions the players and resets health. But the other 3 bullets that were still on the screen continue to move even after it resets everything. How can i remove those bullets. Even a cls doesnt remove them. Its not a case of removing the bullet from the list cos ive already doen that when they hit the player. I need to destroy those bullets left over after the player is killed.
Any ideas how can i do this. Cheers

Last edited 2011


Jesse(Posted 2011) [#163]
ClearList(BulletList)



Joe90(Posted 2011) [#164]
Ahhhh cheers jesse that works. Sorted!!! Hehehe thankyou very much for your help. Its great to have a bit of support when things arnt going well.
Now all i need now is to sort out why the bullets are dissapearing when there isnt a building in the grid.
I hope coffee can give me a solution.
Im damned if i can work out why this is happening so if youre out there coffee please help me.
Ive rewritten the entire program thanks to your full game you sent me.
Took me hours to get it all working but im getting there.
I nearly have a working game now but it is a bit crap. Hehehe
But its a great learning curve for me. Im used to types now and thanks to you ive learnt so much. Its awesome.
Please help me with this building problem. I cant work it out. hehe I will have a look tommorrow and try and see whats going on.


coffeedotbean(Posted 2011) [#165]
The only oddity I can see is this function, surely any bullet regardless of owner should be deleted when it hits a building?

	' Collide with building
		For Local b:Bullet= EachIn Bullet_List
			For Local c:Buildings= EachIn Building_List
				If b.owner=1 And ImagesCollide(b.Image , b.xPos , b.yPos , 0 , c.Image , c.xPos , c.yPos , 0)
					ListRemove(Bullet_List , b)
					Exit
				EndIf
			Next
		Next


change it to;

	' Collide with building
		For Local b:Bullet= EachIn Bullet_List
			For Local c:Buildings= EachIn Building_List
				If ImagesCollide(b.Image , b.xPos , b.yPos , 0 , c.Image , c.xPos , c.yPos , 0)
					ListRemove(Bullet_List , b)
					Exit
				EndIf
			Next
		Next


Although not sure if that will help.


Joe90(Posted 2011) [#166]
nah it doesnt help but thankyou anyway. Instead of deleting the bullet im Drawtext "Bullet Has Collided with Building",0,0
When I fire the bullet its brining up that text message when the bullet passes through some empty spaces so I know that there is a problem somewhere where its not recognising where an actual building is??. Ill post some code so you can get an idea of whats being called where and see why its not recognizing where a building is.

Graphics 1920,1080

'variables for player 1
Global Player1_moving=False
Global Player1_Image:TImage = LoadAnimImage("d:\vendetta\sprites\player1eastfinal.png",16,38,0,4)
Global Player1_xPos=GraphicsWidth()/2
Global Player1_yPos=GraphicsHeight()/2
Global Player1_Speed=5
Global Player1_Frame=0
Global Player1_MaxFrames=4
Global Player1_FrameTimer
Global Player1_FrameDelay=120
Global Player1_Size = 35
Global Player1_Score=0

'general variables
Global numberofbuildings = 50
Global numberofbuildings_Max = 200
Global numberofbuildings_Min = 0
Global Building_Size = 55
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size-2
Global Building_Grid[World_Width+1,World_Height+1]

Make_Buildings(Building_Count)

Global Value_Increase_Step=5
Global tickerx#=1920
Global font:TImageFont = LoadImageFont("d:\vendetta\fonts\impact.ttf", 50) 
Global font2:TImageFont =LoadImageFont("d:\vendetta\fonts\impact.ttf",30) 
Global font3:TImageFont =LoadImageFont("d:\vendetta\fonts\impact.ttf",15)

Global player1name:String="Player 1"
Global player2name:String="Player 2"

'load the images into the variables for Player 1
Global Player1_North:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1northfinal.png",16,38,0,4)
Global Player1_East:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1eastfinal.png",16,38,0,4)
Global Player1_South:TImage 		= LoadAnimImage("d:\vendetta\sprites\player1southfinal.png",16,38,0,4)
Global Player1_West:TImage		= LoadAnimImage("d:\vendetta\sprites\player1westfinal.png",16,38,0,4)
Global Player1_NorthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northeastfinal.png",16,38,0,4)
Global Player1_NorthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1northwestfinal.png",16,38,0,4)
Global Player1_SouthEast:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southeastfinal.png",16,38,0,4)
Global Player1_SouthWest:TImage 	= LoadAnimImage("d:\vendetta\sprites\player1southwestfinal.png",16,38,0,4)

Global IMG_Bullet:TImage=LoadImage("d:\vendetta\sprites\bullet.png");MidHandleImage (IMG_Bullet)
Global Building:TImage=LoadImage("d:\vendetta\sprites\smallbuilding.png")
Global Bullet_List:TList 	= CreateList()
Global Building_List:TList 	= CreateList()
Global Up_Pressed = False
Global Down_Pressed = False
Global Right_Pressed = False
Global Left_Pressed = False
Global Fire_Pressed =False

While Not KeyDown(KEY_ESCAPE)

Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Bullet.Update()
Bullet.DrawBullet()
Redraw_Map()
Help_And_Variables
Conditions()

Flip
Wend
End

Function Make_Buildings(Building_Count)
Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
		Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					Buildings.Create(x*Building_Size,y*Building_Size)
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()	
End Function

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function

 Bullet
Type Bullet
	Field owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Global Speed=8
	Field Image:TImage=IMG_Bullet
		
	' Create Bullet
	Function Create(owner,x , y , direction)
		Local NewBullet:Bullet= New Bullet
		NewBullet.xPos 	 = x
		NewBullet.yPos 	 = y
		NewBullet.owner=owner
		Select direction
		Case 1
		NewBullet.xPos=x+7
		NewBullet.yPos=y+10
		Newbullet.xSpeed=0
		NewBullet.ySpeed= -NewBullet.Speed
		Case 2
		NewBullet.xPos=x+12
		NewBullet.ypos=y+18
		NewBullet.xSpeed= +NewBullet.Speed/2
		newbullet.yspeed= -NewBullet.Speed/2
		Case 3
		NewBullet.xpos=x+8
		NewBullet.ypos=y+23
		NewBullet.xSpeed= NewBullet.Speed
		Newbullet.yspeed=0
		Case 4
		NewBullet.xpos=x+8
		NewBullet.ypos=y+26
		NewBullet.xSpeed= +NewBullet.Speed/2
		Newbullet.yspeed= +NewBullet.Speed/2
		Case 5
		NewBullet.xPos=x+7
		NewBullet.yPos=y+18
		NewBullet.xSpeed=0
		NewBullet.ySpeed=NewBullet.Speed
		Case 6
		NewBullet.xpos=x+7
		NewBullet.ypos=y+26
		NewBullet.xSpeed= -NewBullet.Speed/2
		Newbullet.yspeed= +NewBullet.Speed/2
		Case 7
		NewBullet.xPos=x+7
		NewBullet.yPos=y+23
		NewBullet.xSpeed=-NewBullet.Speed
		NewBullet.ySpeed=0
		Case 8
		NewBullet.xpos=x+5
		NewBullet.ypos=y+18
		NewBullet.xSpeed= -NewBullet.Speed/2
		Newbullet.yspeed= -NewBullet.Speed/2

		End Select
		ListAddLast (Bullet_List, NewBullet)
	End Function

Function Update()
		If Bullet_List=Null Then Return
		For Local ThisBullet:Bullet=EachIn Bullet_List
			ThisBullet.yPos=ThisBullet.yPos+ThisBullet.ySpeed
			ThisBullet.xpos=ThisBullet.xPos+ThisBullet.xSpeed
		Next

' Bullet Hit player 1
		For Local e:Bullet= EachIn Bullet_List
			If e.owner=2 And ImagesCollide(e.Image , e.xPos , e.yPos , 0 , Player1_Image , Player1_xPos , Player1_yPos , 0) Then
						ListRemove(Bullet_List , e)
						'reduce the health here
						player1health=player1health-10
						'ListRemove(Bullet_List , e)
						If player1health<=0 Then
						ClearList (Bullet_List)
						player2_score=player2_score+1
						Notify Player1name+" "+"has Died!!!"
						player1health=player1starthealth
						player2health=player2starthealth
						Make_Buildings(Building_Count)
						Make_Buildings(numberofbuildings)
						channel4=PlaySound(intro) 
						If player2_Score=pointstowin Then
						Notify player1name+" "+"Has Won The Game!!!"
						End
						EndIf
						EndIf
						Exit
			EndIf
			
		Next

	'Bullet Hit player 2
		For Local f:Bullet= EachIn Bullet_List
			If f.owner=1 And ImagesCollide(f.Image , f.xPos , f.yPos , 0 , Player2_Image , Player2_xPos , Player2_yPos , 0) Then
						ListRemove(Bullet_List , f)
						'reduce the health here
						player2health=player2health-10
						'ListRemove(Bullet_List , f)
						If player2health<=0 Then
						ClearList (Bullet_List)
						player1_score=player1_Score+1
						Notify Player2name+" "+"has Died!!!"
						player2health=player2starthealth
						player1health=player1starthealth
						Make_Buildings(Building_Count)
						Make_Buildings(numberofbuildings)
						channel4=PlaySound(intro) 
						If player1_score=pointstowin Then
						Notify player2name+" "+"Has Won The Game!!!"
						End
						EndIf
						EndIf
						Exit
					EndIf
			
		Next
	
		
	End Function

' Draw the Bullet sprite
	Function DrawBullet()
		SetColor 255,255,255
		SetBlend(maskblend)
		If Bullet_List=Null Then Return
		For Local ThisBullet:Bullet=EachIn Bullet_List
		DrawImage ThisBullet.Image, ThisBullet.xPos, ThisBullet.yPos
		SetBlend(shadeblend)
		Next
		Bullet.HitBuilding()
		Bullet.OffScreen()
	End Function
	
	Function OffScreen()
		If Bullet_List = Null Then Return
		' Delete any Bullets that go off the screen
		For Local ThisBullet:Bullet = EachIn Bullet_List 
			If ThisBullet.xPos <=0 Or ThisBullet.xpos >1900 Then ListRemove(Bullet_List , ThisBullet)
			If ThisBullet.ypos <=0 Or ThisBullet.ypos >925 Then ListRemove(Bullet_List, ThisBullet)
		Next
	End Function

	Function HitBuilding()
	' Collide with building
		For Local b:Bullet= EachIn Bullet_List
			For Local c:Buildings= EachIn Building_List
				If ImagesCollide(b.Image , b.xpos , b.ypos , 0 , c.Image , c.x , c.y , 0)
					SetBlend(maskblend)
					DrawText "Bullet Collided With Building",0,0
					'ListRemove(Bullet_List , b)
					Exit
				EndIf
			Next
		Next
	End Function	
	
End Type

' Buildings
Type Buildings
	Field x
	Field y
	Field Image:TImage = Building
		
	' Create Building
	Function Create(grab_x , grab_y)
	Local NewItem:Buildings= New Buildings
		NewItem.x= grab_x
		NewItem.y= grab_y
		NewItem.Image=Building
		ListAddLast(Building_List , Newitem)
	End Function
	
			
End Type

Function PositionPlayer1()
	
	SeedRnd MilliSecs()
	Repeat
		Local x =Rand(0,World_Width)
		Local y =Rand(0,World_Height-1)
		If Building_Grid[x, y] = 1
			ElseIf Building_Grid[x,y] =0
			Player1_xpos=x* Building_Size+20
			Player1_ypos=y* Building_Size+5
			Building_Grid[x,y]=3
			Exit	
		EndIf
	Forever

End Function


Function Draw_Grid()
            If Enable_Grid=False Then Return
		SetColor 0,0,0
		For x = 0 To World_Width+1
			SetLineWidth 1
			DrawLine x * Building_Size , 0 , x * Building_Size, (World_Height * Building_size-55) + Building_Size
		Next
			
		For y = 0 To World_Height
			SetLineWidth 1
			DrawLine 0 , y * Building_Size , (World_Width * Building_size) + Building_Size , y * Building_Size
		Next
End Function

Function Hit_Building_Player1(Player1_Direction)
	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	'Local Player2_Box = Player2_Size - Player2_Speed
	
	Select Player1_Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
			
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
			
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
			

	End Select
	
	Return False
	
End Function

Function Move()
	
	Up_Pressed = False
	Down_Pressed = False
	Right_Pressed = False
	Left_Pressed = False
	Fire_Pressed = False
	
	Up_Pressed2=False
	Down_Pressed2=False
	Right_Pressed2=False
	Left_Pressed2=False
	Fire_Pressed2=False 
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	If KeyHit(KEY_G) Then Fire_Pressed =True
	
	If KeyDown(KEY_NUM8) Then Up_Pressed2 = True
	If KeyDown(KEY_NUM5) Then Down_Pressed2 = True
	If KeyDown(KEY_NUM6) Then Right_Pressed2 = True
	If KeyDown(KEY_NUM4) Then Left_Pressed2 = True
	If KeyHit(KEY_RCONTROL) Then Fire_pressed2=True
	
	
	' Change player image and move player1 based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1direction=1
		If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1direction=5
		If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1direction=3
		If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		player1direction=7
		If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

	
	' Change player image and move player 2 based on what keys are pressed.
	If Up_Pressed2 = True
		' If only up is pressed
		Player2_Moving = True
		Player2_Image = Player2_North
		Player2direction=1
		If Hit_Building_Player2(1) = False Then Player2_yPos = Player2_yPos - Player2_Speed
		If Right_Pressed2 = True
			Player2_Image = Player2_NorthEast
			player2direction=2
			If Hit_Building_Player2(3) = False Then Player2_xPos = Player2_xPos + Player2_Speed / 2
		ElseIf Left_Pressed2 = True
			Player2_Image = Player2_NorthWest
			Player2direction=8
			If Hit_Building_Player2(7) = False Then Player2_xPos = Player2_xPos - Player2_Speed / 2
		EndIf
ElseIf Down_Pressed2 = True
		Player2_Moving = True
		Player2_Image = Player2_South
		Player2direction=5
		If Hit_Building_Player2(5) = False Then Player2_yPos = Player2_yPos + Player2_Speed
		If Right_Pressed2 = True
			Player2_Image = Player2_SouthEast
			Player2direction=4
			If Hit_Building_Player2(3) = False Then Player2_xPos = Player2_xPos + Player2_Speed / 2
		ElseIf Left_Pressed2 = True
			Player2_Image = Player2_SouthWest
			Player2direction=6
			If Hit_Building_Player2(7) = False Then Player2_xPos = Player2_xPos - Player2_Speed / 2
		EndIf
	ElseIf Right_Pressed2 = True
		Player2_Moving = True
		Player2_Image = Player2_East
		Player2direction=3
		If Hit_Building_Player2(3) = False Then Player2_xPos = Player2_xPos + Player2_Speed
		If Up_Pressed2 = True
			Player2_Image = Player2_NorthEast
			Player2direction=2
			If Hit_Building_Player2(1) = False Then Player2_yPos = Player2_yPos + Player2_Speed / 2
		ElseIf Down_Pressed2 = True
			Player2_Image = Player2_SouthEast
			Player2direction=4
			If Hit_Building_Player2(5) = False Then Player2_yPos = Player2_yPos - Player2_Speed / 2
		EndIf
	ElseIf Left_Pressed2 = True
		Player2_Moving = True
		Player2_Image = Player2_West
		Player2direction=7
		If Hit_Building_Player2(7) = False Then Player2_xPos = Player2_xPos - Player2_Speed
		If Up_Pressed2 = True
			Player2_Image = Player2_NorthWest
			Player2direction=8
			If Hit_Building_Player2(1) = False Then Player2_yPos = Player2_yPos + Player2_Speed/2
		ElseIf Down_Pressed2 = True
			Player2_Image = Player2_SouthWest
			Player2direction=6
			If Hit_Building_Player2(5) = False Then Player2_yPos = Player2_yPos - Player2_Speed / 2
		EndIf
	Else

		' no keys pressed
		Player2_Frame = 0
		Player2_Moving = False
		
	EndIf	

End Function

Function Conditions()
	If Player1_xPos + Player1_Size > GraphicsWidth() Then Player1_xPos = GraphicsWidth() - Player1_Size 
	If Player1_xPos < 0 Then Player1_xPos = 0
	If Player1_yPos + Player1_Size > GraphicsHeight()-140 Then Player1_yPos = GraphicsHeight()-140 - Player1_Size 
	If Player1_yPos < 0 Then Player1_yPos = 0

	If Player2_xPos + Player2_Size > GraphicsWidth() Then Player2_xPos = GraphicsWidth() - Player2_Size 
	If Player2_xPos < 0 Then Player2_xPos = 0
	If Player2_yPos + Player2_Size > GraphicsHeight()-140 Then Player2_yPos = GraphicsHeight()-140 - Player2_Size 
	If Player2_yPos < 0 Then Player2_yPos = 0
End Function

Function Player1_Shooting()

If fire_pressed=False Then Return
	Bullet.Create(1,Player1_xPos,Player1_yPos, Player1direction)
	'SetBlend(maskblend)
	'SetColor 0,0,0
	If sfx="off" StopChannel channel2
	If sfx="on" channel2=PlaySound(gunshot)
	SetChannelRate channel2,0.5
	SetChannelVolume channel2,1	
End Function


Ok thats not all of the code but its the majority of it. I havent included some functions as they are not neccessary.

Yeah so when i fire a bullet its thinking that there is a building in an empty space and i dont know why. I seem to have written the code correctly as it was in your game that you made but i dont know why its not working. It cant be getting x and y of the buildings if its making bullets dissapear when there isnt a building there.
Strange
Hopefully someone can find something that would be causing a problem.
Cheers for your help coffee. Damn if i can get this working it will be cool.


coffeedotbean(Posted 2011) [#167]
Can you post your Draw() function?


Joe90(Posted 2011) [#168]
sure i can. Here it is:
Function Draw()
	'SetClsColor 204,204,204
	'Cls
	
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetImageFont(font3)
	SetColor 0,0,0
	SetBlend(maskblend)
	If player1health >0 DrawText Player1Label+" "+Player1Health+"%", Player1_xPos - 15 , Player1_yPos - 15
	If player1health <=0 DrawText "",Player1_xpos -15, Player1_ypos -15
	SetBlend(shadeblend)
	SetColor 255,255,255
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	SetColor 0,0,0
	SetBlend(maskblend)
	If player2health > 0 DrawText Player2Label+" "+Player2Health+"%", Player2_xPos - 15 , Player2_yPos - 15
	If player2health <=0 DrawText "",Player2_xpos -15, Player2_yPos -15
	SetBlend(shadeblend)
	SetColor 255,255,255
	DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'SetScale 1.0,1.0
End Function


Hope that helps.
Cheers Kind Regards
Joe


coffeedotbean(Posted 2011) [#169]
Hum you should be using the building list to draw the buildings rather then the array.

Replace your function with this, what you should see when running is on top of every bulding image a pink [x] if not then we're further to understanding what is going.

Function Draw()
	'SetClsColor 204,204,204
	'Cls
	

        ' Draw the buildings using our list
        For Local a:Buildings= EachIn Building_List
                 Drawimage a.Image,a.xPos,a.yPos
        Next

        ' Show a '[x]' if there is a building
	For x = 0 To World_Width
	For y = 0 To World_Height
                SetColor 255,0,255

				If Building_Grid[x , y]=1 Then DrawText "[x]", x * Building_Size,  y * Building_Size
		SetColor 255,255,255
	Next
	Next

	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetImageFont(font3)
	SetColor 0,0,0
	SetBlend(maskblend)
	If player1health >0 DrawText Player1Label+" "+Player1Health+"%", Player1_xPos - 15 , Player1_yPos - 15
	If player1health <=0 DrawText "",Player1_xpos -15, Player1_ypos -15
	SetBlend(shadeblend)
	SetColor 255,255,255
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	SetColor 0,0,0
	SetBlend(maskblend)
	If player2health > 0 DrawText Player2Label+" "+Player2Health+"%", Player2_xPos - 15 , Player2_yPos - 15
	If player2health <=0 DrawText "",Player2_xpos -15, Player2_yPos -15
	SetBlend(shadeblend)
	SetColor 255,255,255
	DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'SetScale 1.0,1.0
End Function


Last edited 2011

Last edited 2011

Last edited 2011


Joe90(Posted 2011) [#170]
For Local a:Buildings= EachIn Building_List
Drawimage a.Image,a.xPos,a.yPos
Next

Ok it didnt recognise xpos and ypos so i replaced it with x and y

Now the amount of buildings is incorrect. When it should be drawing 50 buildings it now draws 100 and 50 of the buildings have pink x's on and the other 50 do not.

Last edited 2011


coffeedotbean(Posted 2011) [#171]
Are you clearing the building_list when you remake the game? should be called as part of the Clear_Buildings() function I guess.


Joe90(Posted 2011) [#172]
How do u mean when i remake the game???
Do i need to call the Clear_Buildings function from somewhere?
I am clearing the buildings in the Make_Buildings function here
Function Make_Buildings(Building_Count)
Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
		Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					Buildings.Create(x*Building_Size,y*Building_Size)
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()	
End Function


Or do i need to call it somewhere else as well??


coffeedotbean(Posted 2011) [#173]
In an ealier post you said you press F8 to remake the game, does that include the placement of building? If so you need to clear the building_list at some point or your just adding 50 more buildings to a list that already has 50.

Either way you at least know the cause is that twice as many buildings are being made. Buildings do not need to be drawn for collisions to happen. which is why your bullets where disspearing when they appeared to be hitting nothing. You only saw half the buildings cus you were drawing the buildings based on the array rather then the list.


Joe90(Posted 2011) [#174]
Yeah but the thing is I have this code as well.
[code]
Function Help_And_Variables()
If KeyDown(KEY_F2)
Delay(60)
numberofbuildings=numberofbuildings+1
SetBlend(maskblend)
SetColor 0,0,0
SetImageFont(font2)
DrawText "Number Of buildings ="+numberofbuildings,0,0
SetBlend(shadeblend)
If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
Make_Buildings(numberofbuildings)

'This code increases the buildings and ive got some code that decreases the buildings. If i use the list everything messes up.

F8 randomizes the players in game when you press it.
F1 redraws the map.

Is there no way to detect bullet vs buildings using the array rather than the list?????


Joe90(Posted 2011) [#175]
would i be able to remove drawing the buildings from the draw function and instead use a list?????


Joe90(Posted 2011) [#176]
you see from this function
Function Make_Buildings(Building_Count)
Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
		Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					Buildings.Create(x*Building_Size,y*Building_Size)
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()	
End Function


All i want to do is grab the x and y of the building and the image.
Where it says Buildings.create is that actually creating a building. I thought that this call would look at the buildings type and get the x and y and image of the building. It doesnt actually draw a building does it?


Joe90(Posted 2011) [#177]
Ok if i rem this line out
'Buildings.Create(x*Building_Size,y*Building_Size)

It creates the correct number of buildings and displays pnk x's on the grid but now the bullet doesnt collide with the building. Ive still got this function in the bullet type.
Function HitBuilding()
	' Collide with building
		For Local b:Bullet= EachIn Bullet_List
			For Local c:Buildings= EachIn Building_List
				If ImagesCollide(b.Image , b.xpos , b.ypos , 0 , c.Image , c.x , c.y , 0)
					SetBlend(maskblend)
					DrawText "Bullet Collided With Building",0,0
					ListRemove(Bullet_List , b)
					Exit
				EndIf
			Next
		Next
	End Function	



Joe90(Posted 2011) [#178]
yes i see what the problem is. If i unrem the line Buildings.Create(x*Building_size,y*building_size) its creating an additional double the amount of buildings.

So that line Buildings.create is actually drawing more buildings but you can move through them and bullets dissapear when they hit them.

Isnt there any way to stop this from happening.

If i rem the line out i get the correct number of buildings but then the bullets go through the buildings. And if i unrem the line it creates an extra 50 buildings or whatever and then the bullets dissapear.

Isnt there any way of correcting this unless i have to recode the whole program???????????


coffeedotbean(Posted 2011) [#179]
I see you Press F2 and it increases numberofbuildings, but then it adds that amount of buildings to the list;

So with your code if you press F2..

Once. numberofbuildings=1 ; Make_Buildings(1) = total building 1
Twice. numberofbuildings=2 ; Make_Buildings(2) = total building 3 ( 1 already made plus 2 more)
Thris. numberofbuildings=3 ; Make_Buildings(3) = total building 6 ( 3 already made plus 3 more)
...etc

Thats your problem.

You need to call Make_Buildings() once after the numberofbuildings has been decided/finalised or it will spirl out of control, a quick fix is ....

Function Help_And_Variables()
If KeyDown(KEY_F2)
Delay(60)
numberofbuildings=numberofbuildings+1
SetBlend(maskblend)
SetColor 0,0,0
SetImageFont(font2) 
DrawText "Number Of buildings ="+numberofbuildings,0,0
SetBlend(shadeblend)
If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
Make_Buildings(1)                                                ' <<<< change here make 1 buildig every time F2 is pressed. 


Last edited 2011

Last edited 2011


Joe90(Posted 2011) [#180]
Hi coffee but thats strange because when this line is remmed out
everything works as it should do. Its producing the correct number of buildings to whatever ive set the numberofbuildings to.
If i set it to Make_Buildings(1) it doesnt work.
Buildings.Create(x*Building_Size,y*Building_Size)

I put Make_Buildings(1) but it doesnt work.

This is my code for every time i Make_Buildings(numberofbuildings)
Ive listed the title screen code as well so you can see whats happening.

'my variables
Global numberofbuildings = 50
Global numberofbuildings_Max = 200
Global numberofbuildings_Min = 0
Global Building_Size = 55
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size-2
Global Building_Grid[World_Width+1,World_Height+1]

Make_Buildings(Building_Count)

'title screen loop
While Not KeyHit(KEY_1)

If KeyDown(KEY_5)
		numberofbuildings = numberofbuildings + Value_Increase_Step
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
	ElseIf KeyDown(KEY_6)
		numberofbuildings = numberofbuildings - Value_Increase_Step
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Max
	EndIf

Make_Buildings(numberofbuildings)

Flip

Wend
'main game loop
While Not KeyDown(KEY_ESCAPE)
Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Bullet.Update()
Bullet.DrawBullet()
Redraw_Map()
Help_And_Variables
Conditions()

Flip
Wend
End

Function Make_Buildings(Building_Count)
Clear_Buildings()
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
		Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					'Buildings.Create(x*Building_Size,y*Building_Size) 'currently remmed out
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()
	PositionPlayer2()	
End Function

Function Clear_Buildings()
	
	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function
 Bullet
Type Bullet
	Field owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Global Speed=8
	Field Image:TImage=IMG_Bullet
		
	' Create Bullet
	Function Create(owner,x , y , direction)
		Local NewBullet:Bullet= New Bullet
		NewBullet.xPos 	 = x
		NewBullet.yPos 	 = y
		NewBullet.owner=owner
		Select direction
		Case 1
		NewBullet.xPos=x+7
		NewBullet.yPos=y+10
		Newbullet.xSpeed=0
		NewBullet.ySpeed= -NewBullet.Speed
'lots more case statements not listed here
End Select
		ListAddLast (Bullet_List, NewBullet)
	End Function


	' Bullet Hit player 1
		For Local e:Bullet= EachIn Bullet_List
			If e.owner=2 And ImagesCollide(e.Image , e.xPos , e.yPos , 0 , Player1_Image , Player1_xPos , Player1_yPos , 0) Then
						ListRemove(Bullet_List , e)
						'reduce the health here
						player1health=player1health-10
						'ListRemove(Bullet_List , e)
						If player1health<=0 Then
						ClearList (Bullet_List)
						player2_score=player2_score+1
						Notify Player1name+" "+"has Died!!!"
						player1health=player1starthealth
						player2health=player2starthealth
						Make_Buildings(Building_Count)
						Make_Buildings(numberofbuildings)
						channel4=PlaySound(intro) 
						If player2_Score=pointstowin Then
						Notify player1name+" "+"Has Won The Game!!!"
						End
						EndIf
						EndIf
						Exit
			EndIf
			
		Next
End Function
			
' Draw the Bullet sprite
	Function DrawBullet()
		SetColor 255,255,255
		SetBlend(maskblend)
		If Bullet_List=Null Then Return
		For Local ThisBullet:Bullet=EachIn Bullet_List
		DrawImage ThisBullet.Image, ThisBullet.xPos, ThisBullet.yPos
		SetBlend(shadeblend)
		Next
		Bullet.HitBuilding()
		Bullet.OffScreen()
	End Function
	
	Function OffScreen()
		If Bullet_List = Null Then Return
		' Delete any Bullets that go off the screen
		For Local ThisBullet:Bullet = EachIn Bullet_List 
			If ThisBullet.xPos <=0 Or ThisBullet.xpos >1900 Then ListRemove(Bullet_List , ThisBullet)
			If ThisBullet.ypos <=0 Or ThisBullet.ypos >925 Then ListRemove(Bullet_List, ThisBullet)
		Next
	End Function

'hitbuilding() remmed out
'	Function HitBuilding()
	' Collide with building
	'	For Local b:Bullet= EachIn Bullet_List
	'		For Local c:Buildings= EachIn Building_List
	'			If ImagesCollide(b.Image , b.xpos , b.ypos , 0 , c.Image , c.x , c.y , 0)
	'				SetBlend(maskblend)
	'				DrawText "Bullet Collided With Building",0,0
	'				ListRemove(Bullet_List , b)
	'				Exit
	'			EndIf
	'		Next
	'	Next
	'End Function	
	
' Buildings
Type Buildings
	Field x
	Field y
	Field Image:TImage = Building
	
	' Create Building
	Function Create(grab_x , grab_y)
	Local NewItem:Buildings= New Buildings
		NewItem.x= grab_x
		NewItem.y= grab_y
		NewItem.Image=Building
		ListAddLast(Building_List , Newitem)
	End Function
	
			
End Type
Function PositionPlayer1()
	
	SeedRnd MilliSecs()
	Repeat
		Local x =Rand(0,World_Width)
		Local y =Rand(0,World_Height-1)
		If Building_Grid[x, y] = 1
			ElseIf Building_Grid[x,y] =0
			Player1_xpos=x* Building_Size+20
			Player1_ypos=y* Building_Size+5
			Building_Grid[x,y]=3
			Exit	
		EndIf
	Forever

End Function
Function Draw_Grid()
            If Enable_Grid=False Then Return
		SetColor 0,0,0
		For x = 0 To World_Width+1
			SetLineWidth 1
			DrawLine x * Building_Size , 0 , x * Building_Size, (World_Height * Building_size-55) + Building_Size
		Next
			
		For y = 0 To World_Height
			SetLineWidth 1
			DrawLine 0 , y * Building_Size , (World_Width * Building_size) + Building_Size , y * Building_Size
		Next
End Function
Function Help_And_Variables()
	If KeyDown(KEY_F2)
		Delay(60)
		numberofbuildings=numberofbuildings+1
		SetBlend(maskblend)
		SetColor 0,0,0
		SetImageFont(font2) 
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(shadeblend)
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
		Make_Buildings(numberofbuildings)
	ElseIf KeyDown(KEY_F3)
		Delay(60)
		numberofbuildings = numberofbuildings-1
		SetBlend(maskblend)
		SetColor 0,0,0
		SetImageFont(font2)
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(Shadeblend)
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Max
		Make_Buildings(numberofbuildings)
	ElseIf KeyDown(KEY_F4) 
		Enable_Grid=False
	ElseIf KeyDown(KEY_F5) 
		Enable_Grid=True
	EndIf
Function Draw()
	'SetClsColor 204,204,204
	'Cls
	
	For x = 0 To World_Width
	For y = 0 To World_Height
		SetBlend(solidblend)
		If Building_Grid[x , y]=1 Then DrawImage building, x*Building_Size,  y*Building_Size
		SetColor 255,255,255
	Next
	Next
	
	' Simple timer will increase frame count after Player1_FrameDelay (millisecs)
	' Will loop frame back to 0 if frame goes over the max frame count.
	If MilliSecs() > Player1_FrameTimer And Player1_Moving = True
		Player1_FrameTimer = MilliSecs()+Player1_FrameDelay
		Player1_Frame = Player1_Frame + 1
		If Player1_Frame => Player1_MaxFrames Then Player1_Frame = 0
	EndIf
	
	If MilliSecs() > Player2_FrameTimer And Player2_Moving = True
		Player2_FrameTimer = MilliSecs()+Player2_FrameDelay
		Player2_Frame = Player2_Frame + 1
		If Player2_Frame => Player2_MaxFrames Then Player2_Frame = 0
	EndIf

	' Draw the player
	'SetScale 1.0,0.64
	SetImageFont(font3)
	SetColor 0,0,0
	SetBlend(maskblend)
	If player1health >0 DrawText Player1Label+" "+Player1Health+"%", Player1_xPos - 15 , Player1_yPos - 15
	If player1health <=0 DrawText "",Player1_xpos -15, Player1_ypos -15
	SetBlend(shadeblend)
	SetColor 255,255,255
	DrawImage Player1_Image,Player1_xPos,Player1_yPos,Player1_Frame
	SetColor 0,0,0
	SetBlend(maskblend)
	If player2health > 0 DrawText Player2Label+" "+Player2Health+"%", Player2_xPos - 15 , Player2_yPos - 15
	If player2health <=0 DrawText "",Player2_xpos -15, Player2_yPos -15
	SetBlend(shadeblend)
	SetColor 255,255,255
	DrawImage Player2_Image,Player2_xPos,Player2_yPos,Player2_Frame
	'SetScale 1.0,1.0
End Function
Function Hit_Building_Player1(Player1_Direction)
	
	Local Player1_Box = Player1_Size - Player1_Speed ' Reduce size of the player collision box a little
	'Local Player2_Box = Player2_Size - Player2_Speed
	
	Select Player1_Direction
		Case 1 ' Up
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos - Player1_Speed) / Building_Size] = 1 Then Return True
			
		Case 3 ' Right
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Speed + Player1_Box) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True
			
		Case 5 ' Down
			If Building_Grid[Player1_xPos / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True
			If Building_Grid[(Player1_xPos + Player1_Box) / Building_Size , (Player1_yPos + Player1_Speed + Player1_Box) / Building_Size] = 1 Then Return True	
			
		Case 7 ' Left
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , Player1_yPos / Building_Size ] = 1 Then Return True
			If Building_Grid[(Player1_xPos - Player1_Speed) / Building_Size , (Player1_yPos + Player1_Box) / Building_Size ] = 1 Then Return True	
			

	End Select
	
	Return False
	
End Function
Function Move()
	
	Up_Pressed = False
	Down_Pressed = False
	Right_Pressed = False
	Left_Pressed = False
	Fire_Pressed = False
	
	Up_Pressed2=False
	Down_Pressed2=False
	Right_Pressed2=False
	Left_Pressed2=False
	Fire_Pressed2=False 
	
	' Get what keys are pressed
	If KeyDown(KEY_W) Then Up_Pressed = True
	If KeyDown(KEY_S) Then Down_Pressed = True
	If KeyDown(KEY_D) Then Right_Pressed = True
	If KeyDown(KEY_A) Then Left_Pressed = True
	If KeyHit(KEY_G) Then Fire_Pressed =True
	
	If KeyDown(KEY_NUM8) Then Up_Pressed2 = True
	If KeyDown(KEY_NUM5) Then Down_Pressed2 = True
	If KeyDown(KEY_NUM6) Then Right_Pressed2 = True
	If KeyDown(KEY_NUM4) Then Left_Pressed2 = True
	If KeyHit(KEY_RCONTROL) Then Fire_pressed2=True
	
	
	' Change player image and move player1 based on what keys are pressed.
	If Up_Pressed = True
		' If only up is pressed
		Player1_Moving = True
		Player1_Image = Player1_North
		Player1direction=1
		If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos - Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
ElseIf Down_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_South
		Player1direction=5
		If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos + Player1_Speed
		If Right_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed / 2
		ElseIf Left_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed / 2
		EndIf
	ElseIf Right_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_East
		Player1direction=3
		If Hit_Building_Player1(3) = False Then Player1_xPos = Player1_xPos + Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthEast
			Player1direction=2
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed / 2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthEast
			Player1direction=4
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	ElseIf Left_Pressed = True
		Player1_Moving = True
		Player1_Image = Player1_West
		player1direction=7
		If Hit_Building_Player1(7) = False Then Player1_xPos = Player1_xPos - Player1_Speed
		If Up_Pressed = True
			Player1_Image = Player1_NorthWest
			Player1direction=8
			If Hit_Building_Player1(1) = False Then Player1_yPos = Player1_yPos + Player1_Speed/2
		ElseIf Down_Pressed = True
			Player1_Image = Player1_SouthWest
			Player1direction=6
			If Hit_Building_Player1(5) = False Then Player1_yPos = Player1_yPos - Player1_Speed / 2
		EndIf
	Else
		' no keys pressed
		Player1_Frame = 0
		Player1_Moving = False
	EndIf	

End Function

Function Redraw_Map()
	If KeyHit(KEY_F1)
	Make_Buildings(Building_Count)
	Make_Buildings(numberofbuildings)
	EndIf
End Function


If i just put (1) in Make_Buildings it doesnt work. I cant then increase the number of buildings.
It just creates 1 building. My variable increases as per usual but its just displaying 1 building.


coffeedotbean(Posted 2011) [#181]
Oh your creating buildings on the fly, well that's different.
Changes start with '<<<<<<<<<<<<<<<<<<<

'my variables
Global numberofbuildings = 50
Global numberofbuildings_Max = 200
Global numberofbuildings_Min = 0
Global Building_Size = 55
Global World_Width = GraphicsWidth()/Building_Size
Global World_Height =GraphicsHeight()/Building_Size-2
Global Building_Grid[World_Width+1,World_Height+1]

Make_Buildings(Building_Count)                                            '<<<<<<<<<<<<<<<<<<< REMOVE! Take out it's not needed

'title screen loop
While Not KeyHit(KEY_1)

If KeyDown(KEY_5)
		numberofbuildings = numberofbuildings + Value_Increase_Step
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
	ElseIf KeyDown(KEY_6)
		numberofbuildings = numberofbuildings - Value_Increase_Step
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Max
	EndIf

Make_Buildings(numberofbuildings)                                         '<<<<<<<<<<<<<<<<<<< REMOVE! Take this out, call this later

Flip
Wend

Make_Buildings(numberofbuildings)                                         '<<<<<<<<<<<<<<<<<<< ADD! Move to here so it runs ONCE after you exit the title and before you enter the main loop
PositionPlayer1()                                                     '<<<<<<<<<<<<<<<<<<< ADD! Moved out of the Make_Buildings() function, so call here
PositionPlayer2()                                                   '<<<<<<<<<<<<<<<<<<< ADD! Moved out of the Make_Buildings() function so call here

'main game loop
While Not KeyDown(KEY_ESCAPE)
Move()
Draw()
Draw_Grid()
Player1_Shooting()
Player2_Shooting()
Bullet.Update()
Bullet.DrawBullet()
Redraw_Map()
Help_And_Variables
Conditions()

Flip
Wend
End

Function Make_Buildings(Building_Count)
        Clear_Buildings()                                            '<<<<<<<<<<<<<<<<<<< REMOVE! As we're adding buildings on the fly we need to remove this, only call if you want to delete All buildings    
	SeedRnd MilliSecs()
	For i = 0 To Building_Count-1
		Repeat
		Local x = Rand(0 , World_Width)
			Local y = Rand(0 , World_Height-1)
			
				If Building_Grid[x , y] = 0
					Building_Grid[x , y] = 1
					'Buildings.Create(x*Building_Size,y*Building_Size) 'currently remmed out
				Exit
			EndIf
		Forever
	Next

	PositionPlayer1()                                 '<<<<<<<<<<<<<<<<<<< REMOVE! Dont want players being moved everytime we run this function, so remove
	PositionPlayer2()                                            '<<<<<<<<<<<<<<<<<<< REMOVE! Dont want players being moved everytime we run this function, so remove
End Function

Function Clear_Buildings()

	ClearList(Building_List)                                          '<<<<<<<<<<<<<<<<<<< ADD! Clear the list as well as the array below                                 

	' Clear our array prior so no buildings are present
	For x = 0 To World_Width
	For y = 0 To World_Height
		Building_Grid[x , y] = 0
	Next
	Next
	
End Function

Function Help_And_Variables()
	If KeyDown(KEY_F2)
		Delay(60)
		numberofbuildings=numberofbuildings+1
		SetBlend(maskblend)
		SetColor 0,0,0
		SetImageFont(font2) 
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(shadeblend)
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
		Make_Buildings(1)                                         '<<<<<<<<<<<<<<<<<<< CHANGE! Make one more building
	ElseIf KeyDown(KEY_F3)
		Delay(60)
		numberofbuildings = numberofbuildings-1
		SetBlend(maskblend)
		SetColor 0,0,0
		SetImageFont(font2)
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(Shadeblend)
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Max
		Make_Buildings(1)                                         '<<<<<<<<<<<<<<<<<<< REMOVE! We want to remove a building not make one so delete this
                Buildings.Remove()                                      '<<<<<<<<<<<<<<<<<<< ADD! Remove the first Building on our list (new function added to building type)
	ElseIf KeyDown(KEY_F4) 
		Enable_Grid=False
	ElseIf KeyDown(KEY_F5) 
		Enable_Grid=True
	EndIf
End Function

Type Buildings
	Field x
	Field y
	Field Image:TImage = Building
	
	' Create Building
	Function Create(grab_x , grab_y)
	Local NewItem:Buildings= New Buildings
		NewItem.x= grab_x
		NewItem.y= grab_y
		NewItem.Image=Building
		ListAddLast(Building_List , Newitem)
	End Function
	
        Function Remove()                                             '<<<<<<<<<<<<<<<<<<< ADD/NEW! New function delete first building in list and change array value back to Zero
              For b:Buildings = Eachin Building_List
                   Building_Grid[b.x/Building_Size , b.y/Building_Size] = 0
                   ListRemove(Building_List,b)
                   Exit
              Next
        End Function
			
End Type


Last edited 2011


Joe90(Posted 2011) [#182]
ahhh i see ok cool thanks coffee. Well ive put in the new code and I have 2 problems.
Function Help_And_Variables()
	If KeyDown(KEY_F2)
		Delay(60)
		numberofbuildings=numberofbuildings+1
		SetBlend(maskblend)
		SetColor 0,0,0
		SetImageFont(font2) 
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(shadeblend)
		If numberofbuildings > numberofbuildings_Max Then numberofbuildings = numberofbuildings_Min
		Make_Buildings(1)
	ElseIf KeyDown(KEY_F3)
		Delay(60)
		numberofbuildings = numberofbuildings-1
		SetBlend(maskblend)
		SetColor 0,0,0
		SetImageFont(font2)
		DrawText "Number Of buildings ="+numberofbuildings,0,0
		SetBlend(Shadeblend)
		If numberofbuildings < numberofbuildings_Min Then numberofbuildings= numberofbuildings_Max
		Buildings.Remove()
	ElseIf KeyDown(KEY_F4) 
		Enable_Grid=False
	ElseIf KeyDown(KEY_F5) 
		Enable_Grid=True
	EndIf

The first problem is if i increase the buildings when the numberofbuildings >Numberofbuildings_Max so 200 it doesnt get rid of the buildkings when it resets to 0.
So i press F2 to increase them and when numberofbuildings is 200 it resets to 0 but obviously its not going to delete the buildings as its always making a building.

Its the same when i press F3 to delete buildings. It gets to 0 and then resets to 200 and the buildings dont display.
I guess cos its always calling the Buildings.Remove function.
The second problem I have is that when it draws buildings to the screen if i increase the number of buildings its drawing them in columns rather then randomly putting a building on the screen. It looks a bit silly i mean when i press f2 to increase the buildings i want a random generator rather than it adding 1 building in columns.

It was working really cool before as it randomly increased the builidngs at different places on the grid. Now its just adding buildings from left to the right side of screen when i press F2.


Joe90(Posted 2011) [#183]
also i had this code to redraw the map
Function Redraw_Map()
	If KeyHit(KEY_F1)
	Make_Buildings(Building_Count)
	Make_Buildings(numberofbuildings)
	EndIf
End Function

Now when i redraw the map it produces double the amount of buildings.
I just want this redraw funtion to not add any buildings but just to redraw the current builidngs again.
Would i make a call to Remove Buildings function to get rid of them
. Ive tried making a call to this function and then adding Make_buildings(numberofbuildings) but it still doubles the amount of buildings.
Wait a minute can i use Listremove to get rid of them from my buildings type.

Ive also found another problem. When the players health reaches 0 its doubling the amount of buildings because of these 2 lines.
Make_Buildings(Building_Count)
Make_Buildings(numberofbuildings)

'Bullet Hit player 2
		For Local f:Bullet= EachIn Bullet_List
			If f.owner=1 And ImagesCollide(f.Image , f.xPos , f.yPos , 0 , Player2_Image , Player2_xPos , Player2_yPos , 0) Then
						ListRemove(Bullet_List , f)
						'reduce the health here
						player2health=player2health-10
						'ListRemove(Bullet_List , f)
						If player2health<=0 Then
						ClearList (Bullet_List)
						player1_score=player1_Score+1
						'Notify Player2name+" "+"has Died!!!"
						player2health=player2starthealth
						player1health=player1starthealth
						Make_Buildings(Building_Count)
						Make_Buildings(numberofbuildings)
						channel4=PlaySound(intro) 
						If player1_score=pointstowin Then
						Notify player2name+" "+"Has Won The Game!!!"
						End
						EndIf
						EndIf
						Exit
					EndIf
			
		Next
	
		
	End Function

How do i make it so it just redraws the map with the same amount of buildings but placed randomly again.

Last edited 2011

Last edited 2011


Joe90(Posted 2011) [#184]
oh awesome man the bullets are now dissaperaring when hitting the bullets and not dissapearing when there isnt a building there. Omg thanks so much. Thats awesome!! hehehe its still a crappy game though. And anyway you helped me with most of it but wow i have certainly learnt a lot from you. I would never have learnt this just reading text. I now know from you about functions, methods, types(although im still trying to get my head round these)arrays(getting better slowly),variables. Drawing images, creating lists. Wow you have taught me so much. I now feel much more confident with what i am doing but ive still got a long way to go. Wow the bullets are working now. Thankyou very much.


Joe90(Posted 2011) [#185]
cool everythings working except for:
* I cant redraw the map with the same amount of buildings placed randomly.
*When buildings reaches 200 it goes back to 0 but not working as when it reaches 0 the buildings are still on the screen.
*When buildings reaches 0 then goes to 200 its not displaying the buildings.
*Its drawing buildings to the screen in columns rather than placed at a random location.

Damn not much is working from the menu. That change has screwed up loads of stuff but i guess it had to be done so the bullets hitting the buildings would be correct.


coffeedotbean(Posted 2011) [#186]
In your Help_And_Variables() call Clear_Buildings() when numberofbuildings = 0
In your Help_And_Variables()when numberofbuildings = 200 call Make_Buildings(200).

When you redraw the map again add Clear_Buildings() to clear all buildings and the array.

Function Redraw_Map()
	If KeyHit(KEY_F1)
        Clear_Buildings()                              ' <<<<<<< ADD!
	Make_Buildings(Building_Count)                 ' <<<<<<< REMOVE! not needed
	Make_Buildings(numberofbuildings)
	EndIf
End Function



I urge you re-write this game from scratch, there is code all over the shop :p but don't be lazy and copy'n'paste from this project - take what you know and start from stratch planning it out step by step. You know where you got tripped up before so it should be a breeze.


Joe90(Posted 2011) [#187]
cool thanks coffee
Its redrawing the map fine now.

Im still having problems with the increasing buildings when i press F2and decreasing buildings when i press f3. Ive put this line in:

if numberofbuildings = 200
Make_Buildings(200)
endif

and ive got this in as well
if numberofbuildings =0 then
Clear_Buildings
endif

It doesnt work though. im drawing text to the screen and if its on 200 buildings and i press f2 its drawing text saying that there are 201 buildings and then it goes back to 0.

I dont know why its displaying 201 buildings when i have this line in.

If numberofbuildings > numberofbuildings_Max Then numberofbuildings=numberofbuildings_Min

numberofbuildings_max is set to 200
numberofbuildings_min is 0

so why is it displaying 201 buildings in the text before it goes back to 0.
Weird.

is there no way of when i increase the buildings to draw the blocks randomly on the screen instead of drawing the blocks in columns. It works its way from left to right drawing blocks on every line till it reaches width of screen and then starts again. I like it how i had it before where it was actually drawing blocks to the screen at random grid spaces.

How do you use the goto command???

When the player wins i want to go back to the main title screen but thats not working.

Ive got this line in my function

goto title

and then ive got before the first loop
#title

It says it cant find title???? i dont get it. Why cant it find title when ive clearly got it listed correctly.
Does it not like goto from functions or something???


Joe90(Posted 2011) [#188]
my brains not thinking today. i still got a hangover from last night. In fact im still feeling a bit ill. If i could coffee i'd buy you a pint but i cant.
But when i get paid on the 12th july i will pay you a tenner for all your help youve given me. You deserve it!!!
If you give me your email address ill pay you a tenner on the 12th as a thanks for all your help!!!


Joe90(Posted 2011) [#189]
yeah i know there is code all over the shop. Its pretty badly written.
I got functions jumping all over the place.
It does need to be rewritten. youre not wrong there.
I mean your program you wrote was awesome. Not much code either and it works better than my shitty game. haha
Cant belive you wrote that in one night.
I been doing this on and off for weeks. How do you know so much about programming????
Is blitz the only language you write or do you know other languages. c or c++ maybe.
How long have you been programming for and how did you learn so much stuff????
Yeah ill definately give you some cash for the help youve given me.
As i said before ill pay you a tenner on the 12th. Just gimme your email address and ill paypal it to ya.


Joe90(Posted 2011) [#190]
Ok ive sorted out the increase buildings and decrease buildings. Just had a brainstorm earlier thats all. Ok thats good. everything is working.

Is there any way of instead of drawing the blocks by columns from left to right to randomly place the blocks in spaces.

I will have a look at the functions and see if i can change it.

How do u use the goto command. I cant get it to work at all.
ive got this label at the start of the title screen.
#title

and im calling it after the players score is equal to the pointstowin variable.
Trouble is it keeps coming up identifier not found.
Can you not use goto in functions???


Joe90(Posted 2011) [#191]
Its ok ive sorted it. Ok everythings working now but as you said earlier i will rewrite the program so its not all over the place.

Just need help with the goto command now.
I want to return to the title screen after the player has won.
Heres a little of my code.
#title
While Not KeyHit(KEY_1)

		
SetClsColor 0,0,0
Cls	
	Delay(100)

	HideMouse
	
	ScrollingText()
		
	DrawImage(titlescreen,0,0,0)
	etc
etc


and this bit of code when im trying to return to title screen.

If player2_Score=pointstowin Then
						Notify player1name+" "+"Has Won The Game!!!"
						Goto title
endif


Why doesnt it regognize the command. Am i doing something wrong??
Cheers


Joe90(Posted 2011) [#192]
Im trying to display an image when the player dies.
Global deadimage=loadimage("c:\deadimage.png")

Trouble is when i try and display the deadimage it runs so fast i cant see it and i dont want to put a delay into the program as it halts program execution.
Is there any command i can use to pause the screen for about 3 seconds???


Joe90(Posted 2011) [#193]
Type Bullet
	Field owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Global Speed=8
	Field Image:TImage=IMG_Bullet
		
	' Create Bullet
	Function Create(owner,x , y , direction)
		Local NewBullet:Bullet= New Bullet
		NewBullet.xPos 	 = x
		NewBullet.yPos 	 = y
		NewBullet.owner=owner
		Select direction
		Case 1
		NewBullet.xPos=x+7
		NewBullet.yPos=y+10
		Newbullet.xSpeed=0
		NewBullet.ySpeed= -NewBullet.Speed

If i press a key combination i want to change the bullet speed for each player.
Currently in the type i have global speed.
I can change the bullet speed for both players with this code:
If KeyDown(KEY_F7) And KeyDown(KEY_F11) Then
	Delay(160)
	Bullet.Speed=Bullet.Speed+1
	If Bullet.Speed>20 Then Bullet.Speed =1
	EndIf	


Say i wanted 2 seperate speeds for both players so 1 player can have fast bulelts and the other slow.

Would i add to the bullet type:
Global Player1_Bulletspeed
Global Player2_Bulletspeed

Im not sure i actually need to do this. Can i reference 2 seperate bullet speeds with the owner in the bullet type or would i have to put in 2 seperate fields.


coffeedotbean(Posted 2011) [#194]
Goto is evil stay away for it, instead..

Global Show_TitleScreen = True

While

	If Show_TitleScreen = True
		Title_Screen_Function()
	Else
		Other_Game_Function_1()
		Other_Game_Function_2()
		Other_Game_Function_3()
	EndIf

Wend


===========================================================================

Use another type to dislay a dead player;

Global DeadPlayer_List:TList 	= CreateList()

Type DeadPlayer
	Field xPos
	Field yPos
	Field image:TImage
	Field life
	
	Function Create(player_number,x,y)
		Local a:DeadPlayer= New DeadPlayer
		a.xPos	= x
		a.yPos	= y
		a.life 	= 5 * 1000 ' 5 seconds
		If player_number = 1 Then a.image = 'whatever
		If player_number = 2 Then a.image = 'whatever
		ListAddLast(DeadPlayer_List , a)
	End Function
	
	Function Update()
		For a:DeadPlayer = EachIn DeadPlayer_List
			' Draw dead player image
			DrawImage a.image,a.xPos,a.yPos
			' Delete dead player from list after life is exceeded.
			If a.life < MilliSecs()
				ListRemove(DeadPlayer_List,a)
			EndIf
		Next
	End Function
	
End Type


When a player dies just call DeadPlayer.Create() using the appropraite players variables. Dont forget to call DeadPlayer.Update in your main loop some place so that function can run.

===========================================================================

Bullet speed can be assigned like;

Type Bullet
	Field owner
	Field xPos
	Field yPos
	Field xSpeed
	Field ySpeed
	Field Speed                    ' <<<<<<<<<< change to field
	Field Image:TImage=IMG_Bullet
		
	' Create Bullet
	Function Create(owner,x , y , direction , bulletspeed)   ' <<<<<<<< add a new argument to the function call
		Local NewBullet:Bullet= New Bullet
		NewBullet.xPos 	 = x
		NewBullet.yPos 	 = y
		NewBullet.owner=owner
		NewBullet.speed = bulletspeed             ' <<<<<<<<< set the bullet speed for THIS bullet
		Select direction
		Case 1
		NewBullet.xPos=x+7
		NewBullet.yPos=y+10
		Newbullet.xSpeed=0
		NewBullet.ySpeed= -NewBullet.Speed


Now when you call Bullet.Create just add the extra speed variable.

===========================================================================

I don't want any cash but that's all the same.


Joe90(Posted 2011) [#195]
cool thanks coffee ive sorted out the bullet speeds. Its all working fine. Im still having problems returning to the title screen.

So at the start of my code ive got all my variables.
Global
Global
Global etc etc


'Then ive got my title screen loop
'title screen loop
Function Titlescreen()
While Not KeyHit(KEY_1)

'blah blah blah title screen code

Flip
Wend
end function 'end the title screen function

'Now ive got my main loop here
Function Main_Loop
While Not KeyHit(KEY_ESCAPE)

Flip
Wend
End

'All of my functions here

And im trying to return to the title screen from here

For Local e:Bullet= EachIn Bullet_List
			If e.owner=2 And ImagesCollide(e.Image , e.xPos , e.yPos , 0 , Player1_Image , Player1_xPos , Player1_yPos , 0) Then
						ListRemove(Bullet_List , e)
						'reduce the health here
						player1health=player1health-10
						'ListRemove(Bullet_List , e)
						If player1health<=0 Then
						ClearList (Bullet_List)
						player2_score=player2_score+1
						Notify Player1name+" "+"has Died!!!"
						player1health=player1starthealth
						player2health=player2starthealth
						Clear_Buildings()
						Make_Buildings(numberofbuildings)
						PositionPlayer1()
						PositionPlayer2()
						If sfx="on" Then
						channel4=PlaySound(intro)
						ElseIf sfx="off" 
						StopChannel channel4 
						EndIf
						If player2_Score=pointstowin Then
						Notify player2name+" "+"Has Won The Game!!!"
						Title_Screen()
						EndIf
						EndIf
						Exit
			EndIf
			
		Next
end function


Trouble is when i return to the title screen function i just get a black screen. And nothing seems to work.
I didnt really wanna put the call title screen in another loop as i though it might causes conflicts. I remember not to use goto as it is evil so im trying to do it your way.
With your code earlier i didnt really understand where to put the loop.
After this I am going to rewrite the code like in your finished game cos i got code going all over the place. Butu at least with all your examples I have a good framework to use.

Last edited 2011


coffeedotbean(Posted 2011) [#196]

' ---------------------------------------------------------------------------------------------
' Setup the screen
Const Display_Width	 = 800
Const Display_Height	 = 600
Const FullScreen_Mode	 = False
Graphics Display_Width , Display_Height , FullScreen_Mode * 60

' ---------------------------------------------------------------------------------------------
' Lists
Global Thing_List:TList 	= CreateList()

' ---------------------------------------------------------------------------------------------
' Other
Global Show_Title_Screen = True
Global Max_Number_Of_Things = 200
Global Min_Number_Of_Things = 10
Global Number_Of_Things = Min_Number_Of_Things

' ---------------------------------------------------------------------------------------------
' Main Loop
While Not KeyDown(KEY_ESCAPE)
Cls
	If Show_Title_Screen = True
		Title_Screen()
	Else
		Thing.Update()
		Game_Text()
		Exit_To_Title_Screen()
		' Other game functions here etc
	EndIf

Flip
Wend
End

' ---------------------------------------------------------------------------------------------
' Text on the game screen
Function Game_Text()
	
	SetColor 255,0,255
	DrawText "",10,10
	DrawText "Number of Things created: "+CountList(Thing_List),10,30
	DrawText "Press Space to go to the Title Screen",10,50
	SetColor 255,255,255
	
End Function

' ---------------------------------------------------------------------------------------------
' Exit game and bring up title screen
Function Exit_To_Title_Screen()
	
	If KeyHit(KEY_SPACE) = True
		' Clear our lists and reset any variables
		ClearList(Thing_List)
		Number_Of_Things = Min_Number_Of_Things
		' We want to show the title screen
		Show_Title_Screen = True
	EndIf
	 
End Function

' ---------------------------------------------------------------------------------------------
' Title Screen function
Function Title_Screen()
	
	DrawText "[Title Screen]",10,10
	DrawText "Number of Things: "+Number_Of_Things,10,30
	DrawText "Press Space to Start the game",10,50
	' Increase Thing count
	If KeyHit(KEY_RIGHT)=True Then Number_Of_Things = Number_Of_Things + 1
	If Number_Of_Things > Max_Number_Of_Things Then Number_Of_Things = Min_Number_Of_Things
	
	' Decrease Thing count
	If KeyHit(KEY_LEFT)=True Then Number_Of_Things = Number_Of_Things - 1
	If Number_Of_Things < Min_Number_Of_Things Then Number_Of_Things = Max_Number_Of_Things
	
	' Exit Title screen and create things
	If KeyHit(KEY_SPACE)=True Then 
		' Make our things
		For i = 0 Until Number_Of_Things
			Thing.Create()
		Next
		' I dont want to show title screen any more
		Show_Title_Screen = False
	EndIf
	
End Function

' ---------------------------------------------------------------------------------------------
' Thing type
Type Thing
	Field xPos#
	Field yPos#
	Field SpeedX#
	Field SpeedY#
	Field id
	Field Size = 16
		
	' Create Thing
	Function Create()
		Local a:Thing = New Thing
		a.xPos 	= Rand(0,Display_Width)
		a.yPos 	= Rand(0,Display_Width)
		a.SpeedX	= Rnd(-2,2)
		a.SpeedY	= Rnd(-2,2)
		a.id		= CountList(Thing_List)
		ListAddLast(Thing_List , a)
	End Function
	
	' Update thing
	Function Update()
		For a:Thing = EachIn Thing_List
			a.xPos = a.xPos + a.Speedx
			If a.xPos > Display_Width Then a.xPos = 0
			If a.xPos < 0 Then a.xPos = Display_Width
			a.yPos = a.yPos + a.Speedy
			If a.yPos > Display_Height Then a.yPos = 0
			If a.yPos < 0 Then a.yPos = Display_Height
			DrawOval a.xPos,a.yPos,a.Size,a.Size
			DrawText a.id,a.xPos,a.yPos-a.Size
		Next
	End Function
End Type



.rIKmAN.(Posted 2011) [#197]
I've not been involved with this thread, but I have to say big respect to coffeedotbean for all the help and patience he has given in this thread.

Far above and beyond the usual, respect sir! :)


coffeedotbean(Posted 2011) [#198]
Thanks rIKmAN.. yeah it turned into quite an epic thread. Keeped me busy in work though.. you know rather then doing real work.


Joe90(Posted 2011) [#199]
yeah same here thats what i say massive respect for coffee. Theres not many people that would help so extensivley with code like this. Its very much appreciated. I thankyou so much for all your help. Youve shown me a great deal and ive learnt so much. Cheers Coffee. If every youre in maidstone kent let me know and ill buy you a drink. hehe


iaqsuk(Posted 2012) [#200]
Hi all, I converted Post #31, from blitzmax to blitzplus, well atleast the character only. Here's the codes:

;begin code
Global screenx = 640
Global screeny = 480

Graphics screenx,screeny,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global AValue = 50
Global AValue_Max = 200
Global AValue_Min = 0


Global Player_North = LoadAnimImage("gfx/toonn.png",32,50,28,4)
Global Player_East = LoadAnimImage("gfx/toone.png",32,50,20,4)
Global Player_South = LoadAnimImage("gfx/toons.png",32,50,4,4)
Global Player_West = LoadAnimImage("gfx/toonw.png",32,50,12,4)
Global Player_NorthEast = LoadAnimImage("gfx/toonne.png",32,50,24,4)
Global Player_NorthWest = LoadAnimImage("gfx/toonnw.png",32,50,16,4)
Global Player_SouthEast = LoadAnimImage("gfx/toonse.png",32,50,8,4)
Global Player_SouthWest = LoadAnimImage("gfx/toonsw.png",32,50,0,4)


; Will hold what image needs To be drawn based on keys pressed
Global Player_Image = Player_South

; Some player variables
Global Player_Moving = True
Global Player_xPos = screenx/2
Global Player_yPos = screeny/2
Global Player_Speed = 2
Global Player_Frame = 0
Global Player_MaxFrames = 4
Global Player_FrameTimer
Global Player_FrameDelay = 120
Global Player_Size = 32

While Not KeyDown(1)
Cls
Move()
Draw()
Conditions()
Flip
Wend
End

Function Draw()
; Draw builds - using a DrawRect rather than an image To keep it simple.
For x = 0 To World_Width
For y = 0 To World_Height
Next
Next

; Simple timer will increase frame count After Player1_FrameDelay (MilliSecs)
; Will loop frame back To 0 If frame goes over the max frame count.
If MilliSecs() > Player_FrameTimer And Player_Moving = True
Player_FrameTimer = MilliSecs()+Player_FrameDelay
Player_Frame = Player_Frame + 1
If Player_Frame => Player_MaxFrames Then Player_Frame = 0
EndIf

; Draw the player
DrawImage Player_Image,Player_xPos,Player_yPos,Player_Frame
MaskImage Player_Image,255,255,255
End Function


Function Move()
Local Up_Pressed = False
Local Down_Pressed = False
Local Right_Pressed = False
Local Left_Pressed = False

; Get what keys are pressed
If KeyDown(200) Then Up_Pressed = True
If KeyDown(208) Then Down_Pressed = True
If KeyDown(205) Then Right_Pressed = True
If KeyDown(203) Then Left_Pressed = True

; Change player image And move the player based on what keys are pressed.
If Up_Pressed = True
Player_Moving = True
Player_Image = Player_North
Player_yPos = Player_yPos - Player_Speed
If Right_Pressed = True
Player_Image = Player_NorthEast
Player_xPos = Player_xPos + Player_Speed
Player_yPos = Player_yPos - Player_Speed
ElseIf Left_Pressed = True
Player_Image = Player_NorthWest
Player_xPos = Player_xPos - Player_Speed
Player_yPos = Player_yPos - Player_Speed
EndIf
ElseIf Down_Pressed = True
Player_Moving = True
Player_Image = Player_South
Player_yPos = Player_yPos + Player_Speed
If Right_Pressed = True
Player_Image = Player_SouthEast
Player_xPos = Player_xPos + Player_Speed
Player_yPos = Player_yPos + Player_Speed
ElseIf Left_Pressed = True
Player_Image = Player_SouthWest
Player_xPos = Player_xPos - Player_Speed
Player_yPos = Player_yPos + Player_Speed
EndIf
ElseIf Right_Pressed = True
Player_Moving = True
Player_Image = Player_East
Player_xPos = Player_xPos + Player_Speed
If Up_Pressed = True
Player_Image = Player_NorthEast
Player_xPos = Player_xPos + Player_Speed
Player_yPos = Player_yPos - Player_Speed
ElseIf Down_Pressed = True
Player_Image = Player_SouthEast
Player_xPos = Player_xPos + Player_Speed
Player_yPos = Player_yPos + Player_Speed
EndIf
ElseIf Left_Pressed = True
Player_Moving = True
Player_Image = Player_West
Player_xPos = Player_xPos - Player_Speed
If Up_Pressed = True
Player_Image = Player_NorthWest
Player_xPos = Player_xPos + Player_Speed
Player_yPos = Player_yPos - Player_Speed
ElseIf Down_Pressed = True
Player_Image = Player_SouthWest
Player_xPos = Player_xPos - Player_Speed
Player_yPos = Player_yPos + Player_Speed
EndIf
Else
; no keys pressed
Player_Frame = 0
Player_Moving = False
EndIf
End Function

Function Conditions()
;check To see If player is on screen
If Player_xPos >600 Then Player_xPos=600
If Player_xPos <0 Then Player_xPos=0
If Player_yPos <0 Then Player_yPos=0
If Player_yPos >480 Then Player_yPos=480
End Function
;end code

My question is how can I make the character from walk speed to slowly run speed, then after slowly to complete stop once key is depressed?
I know I'll need to add a global run, maybe global speedadj? Please help. Thanks.


Midimaster(Posted 2012) [#201]
if you change the value Player_FrameDelay#....

Global Player_FrameDelay = 120
Global Player_Speed = 2


...the player's frame speed will change and the X-Y-moving speed you can change with Player_Speed#.

If you change this values during the function Move() the speed will change during the game


iaqsuk(Posted 2012) [#202]
Hi Midmaster,
Thanks for quick reply. I tried your suggestions and it worked, but I was thinking more like a walk speed to run speed slowly going up longer you hold the key. Here is what I was thinking of doing but the character just made frame movements but not moving up? Here is the code:

;adding new Globals:
Global Player_MaxSpeed# = 4.0
Global Player_AdjSpeed# = 0.90

;movement codes here(just showing 1 movement for now to test):

; Change player image And move the player based on what keys are pressed.
If Up_Pressed = True
Player_Moving = True
Player_Image = Player_North
-----my new codes here-------------------------------------------------------
if Player_Speed < Player_MaxSpeed#
Player_yPos = Player_yPos - Player_Speed + Player_adjSpeed#
Elseif Player_Speed > Player_MaxSpeed#
Player_yPos = Player_MaxSpeed#
endif

---existing code here----------------------------------------------------------
If Right_Pressed = True
Player_Image = Player_NorthEast
etc........

I tried above and it moves frame movements but doesn't move up. Not sure if this is correct? Thanks.


iaqsuk(Posted 2012) [#203]
Hi all,
it's easy to add another:
Global Run = 4 (faster then walk speed)

then add another key to press like:
If KeyDown(29) Then Up_Pressedrun = True (control key)

then add more codes for movement key arrows like here:
ElseIf Up_Pressedrun = True
Player_Moving = True
Player_Image = Player_North
Player_yPos = Player_yPos - Player_Run

and it will work and makes the character looks like running, since speed is faster, but it is not realistic since you have to press another key to run instead of just holding down existing movement arrow key. Anyone? help please? Thanks.


Midimaster(Posted 2012) [#204]
I would suggest to use a variable SpeedFactor#, which can have effects on Player_FrameDelay# and Player_Speed#. When standing still the SpeedFactor#=0, when pressing a key the SpeedFactor# is rising from 0 to 1, when pressing no key it comes back to 0.

Now you need a RisingAdd#=0.05, which rises the SpeedFactor# and a RisingStepTimer# which cares about the time to rise the SpeedFactor#

SpeedFactor#=0
RisingAdd#=0.05
PlayerMaxSpeed#=2
Repeat
     If KeyDown(208)=TRUE
           RisingAdd=0.1
     Else
          RisingAdd=-0.2
     Endif

     If RisingStepTimer<Millisecs()
          RisingStepTimer=Millisecs()+100
          SpeedFactor=SpeedFactor+ RisingAdd
          If SpeedFactor>1 then SpeedFactor=1
          If SpeedFactor<0 then SpeedFactor=0
     Endif

     PlayerX=PlayerX + PlayerMaxSpeed*SpeedFactor
 
     Print "PlayerX=" + PlayerX     
     Print "PlayerSpeed=" + (PlayerMaxSpeed*SpeedFactor)    
Until KeyHit(Key_Escape)



iaqsuk(Posted 2012) [#205]
Hi Midimaster, thank you for your reply. It worked like a charm. Now I can safely erase the original Global Player_Speed# since PlayerMaxSpeed replace it.

My next question to continue this code is: I noticed after unpressing the keydown(208)(or whatever the key is), is just goes to a stop pretty fast after reaching PlayerMaxSpeed. Is there a way to code another part to make the player decrease speed slowly just like slowly raising speed?

I was thinking of creating a "DecreasingAdd = -0.1, then just repeat above code to see if it will work? Thanks.


iaqsuk(Posted 2012) [#206]
Hi Midimaster or anyone, I don't know if my last post is possible, I thinking it automatically just goes back to "0" at regular speed, but it would be nice to see it down like some car codes I've see in blitzmax and I've haven't beed able to figure it out on blitzplus.

Like this post that once the car reaches maxspeed then when you release the keydown speed it slowly idles to 0: http://www.blitzbasic.com/Community/posts.php?topic=87085#988284 on post #1. I know many different factors needs to be in play, but that's what I was trying to do in this character once it reaches maxspeed to slow down slowly like after running really fast then slowly going to walk then stop speed.

I also noticed with the speed factors Midimaster, the speedfactor only works in the beginning, once it reaches maxspeed it stays in maxspeed even if you stop for a long while. Is there a way for a timer to reset and go back to beginning after maybe 2-3 seconds of idle stop like the car script? Anyone, thanks.


Midimaster(Posted 2012) [#207]
Well,... you have to play around with the values... The code is correct, but the values need to be adjusted to your conception. Play around with PlayerMaxSpeed# and the both RisingAdd#

Here is a again the code as a runable BltzPlus:

Graphics 800,600
SetBuffer BackBuffer()
SpeedFactor#=0
RisingAdd#=0.05
PlayerMaxSpeed#=2
FPS=CreateTimer(200)
PlayerX#=0
RisingAdd#=0
SpeedFactor#=0
PlayerMaxSpeed#=4
Repeat
	Cls
     If KeyDown(205)=True
           RisingAdd=0.05
     Else
          RisingAdd=-0.1
     EndIf

     If RisingStepTimer<MilliSecs()
          RisingStepTimer=MilliSecs()+100
          SpeedFactor=SpeedFactor+ RisingAdd
          If SpeedFactor>1 Then SpeedFactor=1
          If SpeedFactor<0 Then SpeedFactor=0
     EndIf
      PlayerX=PlayerX + PlayerMaxSpeed*SpeedFactor
 	If PlayerX>800 Then PlayerX=0
	Oval PlayerX,200,10,10  
	Flip 0
	WaitTimer FPS 
Until KeyHit(1)



The movement is very smooth... but the player will stop faster if you set...
Repeat
	Cls
     If KeyDown(205)=True
           RisingAdd=0.05
     Else
          RisingAdd=-0.2
     EndIf
...



And now the player will reduce speed very slowly, if you set...
Repeat
	Cls
     If KeyDown(205)=True
           RisingAdd=0.05
     Else
          RisingAdd=-0.05
     EndIf
...



A second way to make the slow speed more realistic, is not to work with a SpeedFactor# between 0 und 1, but between 1 and 2. The RisingAdd# will now not longer change the SpeedFactor# additiv, but with multipication:

Graphics 800,600
SetBuffer BackBuffer()
SpeedFactor#=0
RisingAdd#=0.05
PlayerMaxSpeed#=2
FPS=CreateTimer(200)
PlayerX#=0
RisingAdd#=0
SpeedFactor#=1
PlayerMaxSpeed#=4
Repeat
	Cls
     If KeyDown(205)=True
           RisingAdd=1.05
     Else
          RisingAdd=0.97
     EndIf

     If RisingStepTimer<MilliSecs()
          RisingStepTimer=MilliSecs()+100
          SpeedFactor=SpeedFactor * RisingAdd
          If SpeedFactor>2 Then SpeedFactor=2
          If SpeedFactor<1 Then SpeedFactor=1
     EndIf
      PlayerX=PlayerX + PlayerMaxSpeed*(SpeedFactor-1)
 	If PlayerX>800 Then PlayerX=0
	Oval PlayerX,200,10,10  
	Flip 0
	WaitTimer FPS 
Until KeyHit(1)




And if the player did not stop in your code, it is no problem of my code sample. Perhaps you made a mistake in implementing it.

Last edited 2012


iaqsuk(Posted 2012) [#208]
Hi Midimaster, thank you so much for the updated codes, yes your updated codes works like a charm. Also youre right that is might be my codes and thats why its not working on my codes. On Post#200 is my codes which I was trying to implement your arrow codes movements. I'll have to recheck and retest again and again until I see it works like your above post.

I noticed on you oval above codes, I've been trying to add Y axis movements and I tried to replace your existing X axis with Y and it still just goes left to right. I tried keydown(200) uparrow. Why? I am trying to implement your oval codes to myPo codes on post #200. Thanks again.


Midimaster(Posted 2012) [#209]
The biggest problem you will get with speed manipulation is the user, who releases the keys not exactly in the same moment. This alone would not be a problem, but if you also want to have diagonal movement... those diagonal movements would often end in a last horizontal movement, because the user is not able to release the keys exactly at the same time.

So I wrote a demontration for you:

first the keys are checked, but not immediately accepted. After a timer of 50msec without any changes the new state becomes true:




This code solves a lot of your problems:

1.
The direction is checked without doing anything to the player. it ends in a variable Direction%, which is a binare combination 8+4+2+1 of the four keys.

2.
It waits 50msec before accepting new key situation. For this you need 3 variables: Direction% to kep the state of the keys, PreDirection% to know the last state of the keys, and StabilDirection% which contains the accepted direction.

3.
Now it has a variable RightLeft# which is positiv or negativ depending on the direction of the player. This helps to get the code short: you need only one line to add everything necessary to the PlayerX#:
PlayerX=PlayerX + PlayerMaxSpeed*(SpeedFactor-1)*RightLeft


4.
It compensate the diagonal speed with using 0.7 instead of 1.0. Because when the player does a diagonal movement he would move 1 to top and 1 to left. and this would look faster (=1.4) like real speed. Pythagoras: sqr(1²+1²)=1.41

5.
It smooths the starting or end of a movement with slow speed because of using multiplication of factor.

Last edited 2012


iaqsuk(Posted 2012) [#210]
Hi Midimaster, thank you so much for your codes. It's been a great help learing from you. I knew it was possible to have decling speed but wasnt sure how.

I'll try maing your code into my codes and play it to make it work, or if adding your codes to my codes, I'll add my codes to your, anyways, lol I sound funny there, I'll play with the codes to make it work.

Another question, to complete the code, how about making "Brakes", like if you press keydown(57), i think thats space key, to make the character brake after PlayerMaxSpeed if needed? Thanks.


Midimaster(Posted 2012) [#211]
Isn't "Brakes" the same as a very strong slowdown? Try to play with RisingAdd#

    If Break=TRUE
           RisingAdd=0.8
     ElseIf StabilDirection >0
           RisingAdd=1.05
     Else
          RisingAdd=0.95
     EndIf



iaqsuk(Posted 2012) [#212]
Hi Midimaster, thank you so much for all your help in aswering my questions, i know you didn't have to help me, but I'm glad there is folks like you willing to help newbies like me.

I'll play around with the braking system. Once I figure that out, I think that will be good. I have a idea of creating bullets for the player. I'll just look at the above codes to see if I can figure it out.

On your post #209, you have all your movement codes under Cls. I tried to implement the movement codes in a function like in my codes on post# 200. Here is what I tried:
while not keydown(1)
cls
UpdatePlayer()
flip
wend
end

function UpdatePlayer()
----your movement codes----
end function

but it didn't move the player? If you can figure that out? Thanks.


Midimaster(Posted 2012) [#213]
perhaps you forgot to make now all variables GLOBAL. Because inside function the variables only live, if they are GLOBAL

A%=2
GLOBAL B%=2

Print "at start: A=" + A
Print "at start: B=" + B
Print

DoSomething()

Print "outside: A=" + A
Print "outside: B=" + B
Print
waitkey()

Function DoSomething()
     Print "inside before: A=" + A
     Print "inside before: B=" + B
     Print
     A=A+10
     B=B+10
     Print "inside after: A=" + A
     Print "inside after: B=" + B
     Print
End Function


Last edited 2012


iaqsuk(Posted 2012) [#214]
Hi Midimaster, thanks for that function code. That was a great example and I figured it out. I was able to create functions for:
While not Keydown(1)
Cls
DrawPlayer()
UpdatePlayer()
PlayerConditions()
Flip 0
Wend
WaitTimer FPS
End
--------------Functions here-------------------------
and it worked. I noticed I forgot to global couple of words. I also moved the arrow keys to the numberkeys with your directions and predirections codes and it also works like a charm. thanks. again.

I was wondering if my codes starts to get more larger which it is, is it better to put the "Player" to a Type? Especially if I started to add background and ammo, enemies, so I won't have so many globals? Also, from other examples of codes, I only see in types:
Type Player
Field PlayerX = 0
Field PlayerY = 0
Field Speed = 0
End Type
All the ther other players globals don't go in the the type? Just wondering. Thanks.


Midimaster(Posted 2012) [#215]
If you write code here in the forum, you should use the forum codes. There are Code Blocks and Code Boxes to differ between text, small code and large code!

Your last post would look like this (at first a codebox then a code block):

Hi Midimaster, thanks for that function code. That was a great example and I figured it out. I was able to create functions for:

and it worked. I noticed I forgot to global couple of words. I also moved the arrow keys to the numberkeys with your directions and predirections codes and it also works like a charm. thanks. again.

I was wondering if my codes starts to get more larger which it is, is it better to put the "Player" to a Type? Especially if I started to add background and ammo, enemies, so I won't have so many globals? Also, from other examples of codes, I only see in types:
Type Player
    Field PlayerX = 0
    Field PlayerY = 0
    Field Speed = 0
End Type

All the ther other players globals don't go in the the type? Just wondering. Thanks.


To your question:

You can create types like you want. If you add also the other parameters like PlayerMaxSpeed# to the type it is a good decision!


iaqsuk(Posted 2012) [#216]
Will do forum codes next time if I post codes again. Thanks. I will start recoding my codes to types.

I was wondering if you have used Mappy a map editor? I dabbled at a little but trying to have a large map and have Player can scroll the whole map, but can figure it out. If you can, if not, that's ok. I'll probably figure ti out. thanks.