TGC Codebase Backup



Crazy Ball by Mark Garrett

29th Nov 2017 22:41
Summary

'Crazy Ball' bounces around the screen in an erratic manner. The sprites are made from images drawn on the screen.



Description

Simple 2D



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    // Project: Crazy Ball 
// Created: 2017-11-23

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Crazy Ball" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 0, 1) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts



`-----------------------------------------------

`make a ball sprite  **************
color = MakeColor( 0, 127, 127 )

DrawEllipse( 50, 50,    50, 50, color, color, 1 )
GetImage( 1, 0, 0   , 100, 100 )

CreateSprite(1,1)
SetSpritePosition(1,500,500)

ClearScreen()

`********************
`make left border of screen-

color = MakeColor( 255, 255, 255 )   `(pure white)

DrawBox(0,0,10,768,color,color,color,color,1)
GetImage( 3, 0, 0   , 10, 768 )
CreateSprite(100,3)
SetSpritePosition(100,0,0)
setspritecolor(100,255,0,0,255)      `initialize sprite color with red

`********************
`make right border of screen from same image-

CreateSprite(101,3)
SetSpritePosition(101,1014,0)
setspritecolor(101,255,0,0,255)   `initialize sprite color with red

ClearScreen()

`**********************************

`make top border of screen-

DrawBox(0,0,1024,10,color,color,color,color,1)
GetImage( 4, 0, 0   , 1024, 10 )
CreateSprite(102,4)
SetSpritePosition(102,0,0)
setspritecolor(102,255,0,0,255)     `initialize sprite color with red

`make bottom border of screen with same image-

CreateSprite(103,4)
SetSpritePosition(103,0,758)
setspritecolor(103,255,0,0,255)     `initialize sprite color with red

`******************************************
`  Start the crazy ball moving

sync()
loopstart:

SetVSync(1)
if GetRawMouseLeftPressed() = 1 then end 

gosub movesprite
gosub if_color_a_line
sync()
goto loopstart

movesprite:   `initiate first movement  

ww:  
sync()
if  GetRawMouseLeftPressed() = 1 then sleep(10000)

if firstime = 0 
	mult# = 2 : clr_cnt_100 = 0:clr_cnt_101= 0:clr_cnt_102= 0: clr_cnt_103= 0
move_x = random2(1,10): move_y = random2(1,10)
if random2(1,2) = 2 then move_x = move_x - (move_x *2)
if random2(1,2) = 2 then move_y = move_y - (move_y *2)
firstime = 1
endif
`SetSpritePosition( iSpriteIndex, fX, fY)
SetSpritePosition (1, (getspritex(1)+ move_x * mult#), (getspritey(1)+ move_y * mult#))



`--------------------------color the lines yellow-------------------------------------	
`---------------------------and determine 'bounce off' direction-----------------

if GetSpriteCollision( 1, 100 ) = 1  `left_line **********************
	setspritecolor(100,255,255,0,255): clr_cnt_100 = 10
	move_x = move_x - (move_x *2) 
	
		if random2(1,2) = 2 then move_y = move_y - (move_y * 2)
       gosub if_reset_y_val
return
	endif

if GetSpriteCollision( 1, 101 ) = 1  `right_line ***********************
	setspritecolor(101,255,255,0,255): clr_cnt_101 = 10

	move_x = move_x - (move_x *2) 
	
		if random2(1,2) = 2 then move_y = move_y - (move_y * 2)
       gosub if_reset_y_val
		
return
	endif

if GetSpriteCollision( 1, 102 ) = 1  `top_line **************************
	setspritecolor(102,255,255,0,255): clr_cnt_102 = 10

	move_y = move_y - (move_y *2) 
	
		if random2(1,2) = 2 then move_x = move_x - (move_x * 2)
       gosub  if_reset_x_val

return		
	endif
	
if GetSpriteCollision( 1, 103 ) = 1  `bottom_line *************************
	setspritecolor(103,255,255,0,255): clr_cnt_103 = 10
	move_y = move_y - (move_y *2) 
	
		if random2(1,2) = 2 then move_x = move_x - (move_x * 2)
       gosub if_reset_x_val
       
return		
	endif


return

if_color_a_line: 
if clr_cnt_100 > 0 
	dec clr_cnt_100
 if clr_cnt_100 = 0 then setspritecolor(100,255,0,0,255) :return
endif

if clr_cnt_101 > 0 
	dec clr_cnt_101
 if clr_cnt_101 = 0 then setspritecolor(101,255,0,0,255) :return
endif

if clr_cnt_102 > 0 
	dec clr_cnt_102
 if clr_cnt_102 = 0 then setspritecolor(102,255,0,0,255) :return
endif	

if clr_cnt_103 > 0 
	dec clr_cnt_103
 if clr_cnt_103 = 0 then setspritecolor(103,255,0,0,255) :return
endif		

return

`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

if_reset_x_val:

if random2(1,2) = 2
    	 moove_x = random2(1,10)
    	 if move_x < 1 then move_x =    - moove_x
    	 if move_x > 0 then move_x =     moove_x
 endif   	 
return
 


if_reset_y_val:

if random2(1,2) = 2
         moove_y = random2(1,10)
    	 if move_y < 1 then move_y =   - moove_y
    	 if move_y > 0 then move_y =    moove_y    	 
    	 endif
return