Code archives/Graphics/Random & Graphics

This code has been declared by its author to be Public Domain code.

Download source code

Random & Graphics by chwaga2007
Not quite finished, but a lot can be learned from it. It utilizes tons of Rnd commands, texturebuffering on a 3d phase, and examples of using loops inside loops(inside loops?!). Unfortunately, it's incomplete and i left a ton of unfinished code changed to comments. The pixels drawn randomly on phase 1 and 2 could be more efficiently drawn with the for ... next code, I'll do that later.
I added a few comments for newbie's to learn from...kinda.

Post any questions, comments, add-ons (Try to keep it media-free!)

Hope you guys have fun with it.
AppTitle "Time Waster"

Graphics 1280,1024,32,2

SeedRnd MilliSecs()
Global n=0
;SORRY FOR THE CRYPTIC VARIABLE NAMES
;oldx
Global ox
;oldy
Global oy
;newx
Global nx=640
;newy
Global ny=512
;etch-e-sketch mode
Global ees=False 
;keyx (etch-e-sketch)
Global kx=640
;keyy (etch-e-sketch)
Global ky=512
;font...for some reason i had to repeat the font=loadfont() thing every time i changed graphics-mode
Global font=LoadFont("Arial",36,1,0,0)
;incomplete
Global s
;linex1
Global lx1=Rnd(0,1280)
;liney1
Global ly1=Rnd(0,1024)
;linex2
Global lx2=Rnd(0,1280)
;liney2
Global ly2=Rnd(0,1024)
;line drawing speed
Global speed=50
;set the font
SetFont font 


Print "WELCOME TO THE TIME-WASTER"
Print "PRESS ANY KEY (except escape) TO CONTINUE"
Print "FOLLOW GIVEN DIRECTIONS PER 'PHASE' AND" 
Print "PRESS THE ESCAPE KEY To CONTINUE To THE Next PHASE"
Print "AT ANY TIME PRESS F10 TO TAKE A SCREENSHOT (named 'screenshot.bmp')
Print "PRESS KEYS 1-7 TO SWITCH AROUND THE PROGRAM & PHASES"
Print "0 to exit"
WaitKey 

.phase1
While Not KeyHit(1)
	Text 500,950,"PHASE ONE"
	Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
	Rect Rnd(0,1280),Rnd(0,1024),Rnd(0,1280),Rnd(0,1024),Rnd(0,1)
	If KeyHit(68) Then SaveBuffer(FrontBuffer(),"screenshot.bmp")
;every time you see the below goto's, its the code set to use keys 1-7 and 0 to jump around the program using labels
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el



	For n=0 To 1000
		WritePixel Rnd(0,1280),Rnd(0,1024),ReadPixel(Rnd(0,1280),Rnd(0,1024))
		n=n+1

	Next 
n=0
Wend
n=0


.phase2
Cls 
While Not KeyHit(1)
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el
	Text 500,950,"PHASE TWO"
	WritePixel Rnd(0,1280),Rnd(0,1024),ReadPixel(401,401)
	Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
	Rect 400,400,500,500,1
	If KeyHit(68) Then SaveBuffer(FrontBuffer(),"screenshot.bmp")

Wend


.phase3
Cls
While Not KeyHit(1)
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el
	Text 500,950,"PHASE THREE"
	Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
	If KeyHit(68) Then SaveBuffer(FrontBuffer(),"screenshot.bmp")


	For n=0 To 1000
		WritePixel Rnd(0,1280),Rnd(0,1024),ReadPixel(Rnd(0,1280),Rnd(0,1024))
		n=n+1
 
	Next 
	Oval Rnd(0,1280),Rnd(0,1024),Rnd(0,1280),Rnd(0,1024),Rnd(0,1)

	Delay 16.666

Wend 



.phase4
Cls
While Not KeyHit(1)
	Text 500,950,"PHASE FOUR"
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el
	Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
	Rect Rnd(0,1280),Rnd(0,1024),Rnd(0,1280),Rnd(0,1024),Rnd(0,1)
	If KeyHit(68) Then SaveBuffer(FrontBuffer(),"screenshot.bmp")


	For n=0 To 1000
		WritePixel Rnd(0,1280),Rnd(0,1024),ReadPixel(Rnd(0,1280),Rnd(0,1024))
		n=n+1

	Next 
	n=0
	Oval Rnd(0,1280),Rnd(0,1024),Rnd(0,1280),Rnd(0,1024),Rnd(0,1)

Wend 

.phase5
Cls 
MoveMouse 640,512

While Not KeyHit(1)
	If KeyHit(68) Then SaveBuffer(FrontBuffer(),"screenshot.bmp")


	Text 500,0,"PRESS THE SPACE-BAR TO CLEAR THE SCREEN"
	Text 400,40,"PRESS ENTER TO ENTER/EXIT ETCH-E-SKETCH MODE"
	Text 500,950,"PHASE FIVE"
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el

	Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
	Rect 0,0,1,1,1

	If Not KeyDown(20) And ees=False 
		ox=MouseX()
		oy=MouseY()

		connect
		nx=MouseX()
		ny=MouseY()
		connect 

	EndIf 

	If KeyHit(28) And ees=False Then 
		ees=True 
		Cls 
	Else If ees=True And KeyHit(28)
		ees=False 
	EndIf 

	While ees=True
		Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
		Rect 0,0,1,1,1


		WritePixel kx,ky,ReadPixel(0,0)
		If KeyDown(200) Then ky=ky-1
		If KeyDown(208) Then ky=ky+1
		If KeyDown(203) Then kx=kx-1
		If KeyDown(205) Then kx=kx+1

		Delay 10
		If KeyHit(28) Then 
			ees=False 
			Cls
		EndIf 

	Wend 

  

	If KeyHit(57) Then Cls 

Wend 

.phase6

Cls
Graphics3D 1280,1024,32,2
SetBuffer BackBuffer()
font=LoadFont("Arial",36,1,0,0)

SetFont font 

;While Not KeyHit(1)

campivot=CreatePivot()
PositionEntity campivot,0,0,0
camera=CreateCamera(campivot)
PositionEntity camera,0,0,-10

;EntityParent camera,campivot

light=CreateLight()
PositionEntity light,30,70,0

cube=CreateCube()
PositionEntity cube,0,0,0

cube2=CreateCube()
PositionEntity cube2,0,30,0
ScaleEntity cube2,50,50,50

sphere=CreateSphere(20)
PositionEntity sphere,5,0,0

cone=CreateCone()
PositionEntity cone,-5,0,0

FlipMesh cube2


tex=CreateTexture(256,256)
EntityTexture cube,tex
EntityTexture sphere,tex 
EntityTexture cone,tex 
EntityTexture cube2,tex

While Not KeyHit(1)
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el
	If KeyHit(68) Then SaveBuffer(FrontBuffer(),"screenshot.bmp")


	If KeyDown(203) Then TurnEntity campivot,0,1,0
	If KeyDown(205) Then TurnEntity campivot,0,-1,0



;EVERYTHING FROM HERE TO RESETTING TO BACKBUFFER IS PUT INSIDE THE TEXTURE tex
	SetBuffer TextureBuffer(tex)
		Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
		Rect Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,1)
		Oval Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,1)

	SetBuffer BackBuffer()

	TurnEntity cube,3,3,3
	TurnEntity sphere,-4,-3,-2
	TurnEntity cone,-3,3,-3


RenderWorld
	Text 500,950,"PHASE SIX"

	If Not KeyDown(20)  
		ox=MouseX()
		oy=MouseY()

		connect
		nx=MouseX()
		ny=MouseY()
		connect 

	EndIf 


Flip
Wend 


.phase7
Cls 
 
 
Graphics 1280,1024,32,2
font=LoadFont("Arial",36,1,0,0)

SetFont font
While Not KeyHit(1)
	If KeyHit(2) Then Goto phase1
	If KeyHit(3) Then Goto phase2
	If KeyHit(4) Then Goto phase3
	If KeyHit(5) Then Goto phase4
	If KeyHit(6) Then Goto phase5
	If KeyHit(7) Then Goto phase6
	If KeyHit(8) Then Goto phase7
	If KeyHit(11) Then Goto el
	If KeyHit(68) Then 
		SaveBuffer(BackBuffer(),"screenshot.bmp")
	EndIf 

	Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
	Text 500,0,"PRESS THE SPACE-BAR TO CLEAR THE SCREEN"
	Text 400,40,"PRESS NUM + AND NUM - TO DECREASE AND INCREASE "
	Text 500,80,"THE SPEED THAT THE LINES DRAW AT"
	Text 0,0,"DRAWING SPEED = "+speed
	Text 0,40,"(low is fast :D)"

	Text 500,950,"PHASE SEVEN"
	lx2=Rnd(0,1280) 
	ly2=Rnd(0,1024)
	Line lx1,ly1,lx2,ly2
	lx1=lx2
	ly1=ly2

	If KeyDown(78) And speed > 3 Then speed=speed-1
	If KeyDown(74) Then speed=speed+1

	If KeyHit(57) Then Cls 
Delay speed

Wend 
.el
End 


;Cls
;Graphics3D 1280,1024,32,2
;SetBuffer BackBuffer()


Function connect()

	Line ox,oy,nx,ny

End Function

Comments

CS_TBL2007
Not the best code example for beginners tho. There's no indenting, there're too many globals, and .. --w000t?-- GOTO and labels? That's so non-procedural. All blitzes are at least procedural languages. Furthermore: why use while/wend loops when FOR is so much easier/clearer? There's no single FOR loop in that whole source! As a whole, I wonder what it's doing in the archives in the first place.

No, if I were the pope, I wouldn't bless this one.., -sorry- .. :P There's a lot left to learn, and it's certainly not impossible to become good, however your path currently heads into the wrong direction.


chwaga2007
There. I told you it was messy and stuff at the top. I added for ... next 's and did indenting. I don't get what's non-procedural about goto's and labels, it's the best way i can think of for hopping around the program....(the only reason the goto thing is repeated every time is because there's no way to have a global label...as far as i know)


tonyg2007
I don't get what's non-procedural about goto's and labels, it's the best way i can think of for hopping around the program...


Look at creating functions and I agree with CS_TBL.
If I am painfully honest I could believe this uses the worst programming approach as a jape.


chwaga2007
well, I'm a noob to b3d myself, and apparently I'm a messy coder, but if you could put up an improvement that i could use to figure out what i did wrong?


tonyg2007
How about creating a topic for it in the B3D Beginner's forum?


CS_TBL2007
chwaga: everyone has to start at some point. I don't mind people who are slowly progressing, I'm often helping ppl out in the B+ beginners section anyway. The only real problem I'm seeing here is that you've put this code into the archives, which is -according to me- like an section where one can find: 1) functionality that's not in the basic command set of any Blitz, or 2) complete games/demos/things that are written in a way so that they are informative (near-tutorials!). It has happened before that ppl posted your kind of code snippets into the archives, something I completely don't agree on. For those purposes there's a showcase section in the forum. This archives 'forum section' might look like a forum section, but that's only a look-up for the code database, it's very annoying for anyone to have to browse through non-relevant code when searching the archives for something.

Something I'd wish is a BRL/nonBRL archives moderator who will move irrelevant entries to other places. Any chance, BRL?

If the showcase forum is actually for gfx/games/media, then I propose a "beginner's code" section.


chwaga2007
k. If a moderator can move this out or something, I'd appreciate it


Code Archives Forum