Re: Help please

BlitzMax Forums/BlitzMax Beginners Area/Re: Help please

Joe90bigrat90(Posted 2011) [#1]
Hi there. Im pretty new to programming and im writing a game.
This is my problem (using blitzmax)

I want to create several blocks on a screen at random positions.

ive declared 3 variables like this

Global numberofbuildings
Global buildingx
Global buildingy
number of buildings=rnd(50) 'sets a random number of buildings

later on in my code I have this:

For a=1 To numberofbuildings

buildingx=Rnd(1870) 'store random number in x upto 1870
buildingy=Rnd(970) 'store a random number in y upto 970
Cls 'clear the screen
SetBlend(lightblend) 'set the blend
DrawImage (building,buildingx,buildingy,0) 'draw the block to the screen
Next

However when i run the program its not keeping the block on the screen. Im getting about 40 blocks depending on the number of buildings set all over the screen.

How can i get several random blocks to actually stay on the screen.

Im not that confident about arrays but if you could post some code on maybe how i create an array or how i can do this that would be great. Thanks lot

PS im very new to programming.


Joe90bigrat90(Posted 2011) [#2]
I figure i need to create an array and store the random number in the first element of array and then increase the array by 1 and store the next random number in the array but im not sure how to do this. Any help much appreciated.


degac(Posted 2011) [#3]
Hi

1. welcome
2. get a look to the 'forum codes' to post code... it's handy when you have many source
3. You need arrays (look at the BMax documentation)

Global numberofbuildings:int=20'
Global buildingx:int[numberofbuildings]
Global buildingy:int[numberofbuildings]

For a=0 to numberofbuildings-1 'see help for array

buildingx[a]=Rnd(1870) 'store random number in x upto 1870
buildingy[a]=Rnd(970) 'store a random number in y upto 970
Cls 'clear the screen
SetBlend(lightblend) 'set the blend
DrawImage (building,buildingx[a],buildingy[a],0) 'draw the block to the screen
Next



degac(Posted 2011) [#4]
To be more precise you should separate Initialization (where you create x & y position) and Drawing (Cls, SetBlend and all the coords). This speed up things a lot
Global numberofbuildings:int=20'
Global buildingx:int[numberofbuildings]
Global buildingy:int[numberofbuildings]

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

Cls
SetBlend(lightblend)

For a=0 to numberofbuildings-1 'see help for array
DrawImage (building,buildingx[a],buildingy[a],0) 'draw the block to the screen
Next



Jesse(Posted 2011) [#5]
an alternative in case you decide to explore:
Strict

Type Tbuilding
	Field x:Float
	Field y:Float
End Type

Global list:TList = CreateList()					' create the list to store buildings
Global numberOfBuildings:Int = 50					' variable to store the number of buildings

For Local i:Int = 0 Until numberOfBuildings
	Local building:Tbuilding = New Tbuilding			' create building
	building.x = Rand(1870) 						' fill variables
	building.y = Rand(970)	
	list.addlast(building) 						' add it to the list 
Next

Graphics 1024,768

Repeat
	Cls ' clear the back buffer
	For Local building:Tbuilding = EachIn list  		' grab a building at a time from the list
		DrawRect building.x,building.y,10,10    		' draw the building as a rectangle of 10x10 to the back buffer
	Next 										' next building in the list
	Flip() 										' display the back buffer
Until KeyDown(KEY_ESCAPE)



Joe90bigrat90(Posted 2011) [#6]
thankyou degac and jesse

this is a strange situation. Ok ive copied the code and tested it and its working fine if i start a program afresh.

Thing is ive got a title screen and a while loop. Ill post some code here.
Variables and arrays go here.
SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080

while not keyhit(key_1)
cls
draw text to the screen using drawtext commands

flip
wend

load all my animimages

setclscolor 204,204,204

While Not KeyHit(KEY_ESCAPE)
	'SetAlpha 1
	'SetScale 0.5,0.5

Cls
SetBlend(shadeblend)
draw my images if certain keys pressed

flip
wend
endgraphics
waitkey

thing is when i put the code into a new program it works fine.
But for some reason when i put this code into my program i can sort of see the blocks randomly but everythings flashing. Ive tried an extra flip and cls but i cant actually see the blocks. I just get flashing whilte things. Do i put this code in a while loop???
You see my main game is in a while loop and until you press esc it allows you to play the game. Im not sure where i need t put this code in order for it to work.

But thanks a lot for your help guys. Really great!!! Cheers!!


shinkiro1(Posted 2011) [#7]
This doesn't answer your question, anyway: http://www.2dgamecreators.com/tutorials/gameprogramming/index.html
In my opinion the best way to start with BlitzMax.

Also read other peoples code (the examples which come with bmax). There is nothing bad with 'stealing' other peoples ideas ... o_O

Last edited 2011


Joe90bigrat90(Posted 2011) [#8]
ive typed this code into blitz

SetGraphicsDriver GLMax2DDriver()
Graphics 1920,1080
building=LoadImage("d:\vendetta\sprites\building.png",0)
SeedRnd MilliSecs()
Global numberofbuildings:Int=10
Global buildingx:Int[numberofbuildings]
Global buildingy:Int[numberofbuildings]
  
SetClsColor 240,240,240 

	For a=1 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
		Flip
		SetClsColor 204,204,204
		DrawImage (building,buildingx[a],buildingy[a],0)
	Next


WaitKey
'EndIf


Anyone know why this is drawing only 4 blocks rather than 10???

I cant work it out


shinkiro1(Posted 2011) [#9]
Reduce the Rnd() values.
And also move the flip out of the loop. It only has to be called once after you draw everything.