Select / Case not working.

Monkey Targets Forums/HTML5/Select / Case not working.

Stuart(Posted 2014) [#1]
My program works perfectly when, (with the exception of 'Update_Game'), I comment out every line of the Select / Case routine.
With these lines of code included the game does not work; I'm presented with just a black screen and cannot understand why.
I would greatly appreciate the help of the community, the solution to my problem and an explanation of my mistake, thankyou. :)

 
Strict

Import mojo

Function Main:Int()
	New TableTennis
	Return True
End

Class TableTennis Extends App	'Beginning of TableTennis Class declaration

	Const CRT_WIDTH:Float = 630.0
	Const CRT_HEIGHT:Float = 470.0
	Const CRT_LFT:Float = CRT_HEIGHT + CRT_LINE_WIDTH
	Const CRT_RT:Float = CRT_LINE_WIDTH
	Const CRT_OPNT_END:Float = CRT_LINE_WIDTH
	Const CRT_PLR_END:Float = CRT_WIDTH
	Const CRT_LINE_WIDTH:Float = 5.0

	Const PLR_WIDTH:Float = 5.0
	Const PLR_HEIGHT:Float = 40.0
	Const PLR_OUTR_LFT:Float = PLR_HEIGHT - BALL_RDS
	Const PLR_CNTR_LFT:Float = PLR_HEIGHT/2 + BALL_RDS
	Const PLR_CNTR_RT:Float = PLR_HEIGHT/2 - BALL_RDS
	Const PLR_SPD_START:Float = 5.0
	Const PLR_PNTS_START:Int = 0
	
	Const OPNT_WIDTH:Float = 5.0
	Const OPNT_HEIGHT:Float = 40.0
	Const OPNT_CNTR_LFT:Float = OPNT_HEIGHT/2 - BALL_RDS
	Const OPNT_CNTR_RT:Float = OPNT_HEIGHT/2 + BALL_RDS
	Const OPNT_OUTR_RT:Float = OPNT_HEIGHT - BALL_RDS
	Const OPNT_PNTS_START:Int = 0
	
	Const BALL_RDS:Float = 5.0
	Const BALL_X_SPD_START:Float = 3.0
	Const BALL_Y_SPD_START:Float = 1.0 

	Field plr_X_pos:Float = CRT_WIDTH - ( PLR_WIDTH * 2 )
	Field plr_Y_pos:Float = ( CRT_HEIGHT/2 - PLR_HEIGHT/2 ) + CRT_LINE_WIDTH
	Global plr_prev_Y_pos:Float
	Field plr_Y_spd:Float = PLR_SPD_START
	Field plr_pnts:Int = PLR_PNTS_START
	
	Field opnt_X_pos:Float[] = [ CRT_LINE_WIDTH + ( OPNT_WIDTH * 2 ) , CRT_LINE_WIDTH + ( OPNT_WIDTH * 7 ) ]
	Field opnt_Y_pos:Float[] = [ ( CRT_HEIGHT/2 - OPNT_HEIGHT/2 ) + CRT_LINE_WIDTH , ( CRT_HEIGHT/2 - OPNT_HEIGHT/2 ) + CRT_LINE_WIDTH ]
	Field opnt_Y_spd:Float[] = [ -5.0 , 3.0 ]
	Field opnt_pnts:Int = OPNT_PNTS_START
	
	Field ball_X_pos:Float = CRT_WIDTH/2 + BALL_RDS
	Field ball_Y_pos:Float = CRT_HEIGHT/2 + BALL_RDS
	Field ball_X_spd:Float = BALL_X_SPD_START
	Field ball_Y_spd:Float = BALL_Y_SPD_START
	
	Global game_mode:Int = 0			'Gamemode: 0 = Attract Mode , 1 = Game In Play , 2 = Game Over
	Global message_mode:Int = 0		'0 = Message Can Be Displayed
	
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method OnCreate:Int()
		SetUpdateRate(60)
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method OnUpdate:Int()
		Select game_mode
			Case 0
				Attract_Mode()
			Case 1
				Update_Game()
			Case 2
				Game_Over()
		End Select
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method OnRender:Int()
		Cls
		DRAW_TennisCourt()
		DRAW_Player()
		DRAW_Opponent()
		DRAW_Score()
		DRAW_Ball()
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method CONTROL_Player:Int()
		plr_prev_Y_pos = plr_Y_pos
		'If MouseY() > 0 Then plr_Y_pos = MouseY()
	
		If KeyDown ( KEY_UP ) Then
			plr_Y_pos -= plr_Y_spd
			
			If plr_Y_pos < CRT_RT Then
				plr_Y_pos = CRT_RT
			Endif
		Endif
		
		If KeyDown ( KEY_DOWN ) Then
			plr_Y_pos += plr_Y_spd
			
			If plr_Y_pos > ( CRT_LFT - PLR_HEIGHT ) Then
				plr_Y_pos = ( CRT_LFT - PLR_HEIGHT )
			Endif
		Endif
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method MOVEMENT_Ball:Int()
		ball_X_pos += ball_X_spd
		ball_Y_pos += ball_Y_spd
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method MOVEMENT_Opponent:Int()
		For Local opnt:Int = 0 To 1
			opnt_Y_pos[opnt] += opnt_Y_spd[opnt]
			
			If opnt_Y_pos [opnt] < CRT_RT Then
				opnt_Y_pos [opnt] = CRT_RT
				opnt_Y_spd [opnt] *= -1
			Endif
			
			If opnt_Y_pos [opnt] > ( CRT_LFT - OPNT_HEIGHT ) Then
				opnt_Y_pos [opnt] = ( CRT_LFT - OPNT_HEIGHT )
				opnt_Y_spd [opnt] *= -1
			Endif
		Next
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method COLLISION_Ball_With_Court:Int()
		If ball_Y_pos <= ( CRT_RT + BALL_RDS ) Then		'Check Ball Has Hit "Court Right" ( Top Of Play Area )
			ball_Y_pos = ( CRT_RT + BALL_RDS )
			ball_Y_spd *= -1
		Endif
		
		If ball_Y_pos >= ( CRT_LFT - BALL_RDS ) Then	'Check Ball Has Hit "Court Left" ( Bottom Of Play Area )
			ball_Y_pos = ( CRT_LFT - BALL_RDS )
			ball_Y_spd *= -1
		Endif
		
		If ball_X_pos <= ( CRT_OPNT_END + BALL_RDS ) Then	'Check Ball Has Hit "Opponent's End Of Court" ( Left Of Play Area )
			ball_X_pos = ( CRT_OPNT_END + BALL_RDS )
			ball_X_spd *= -1
			plr_pnts += 1		'Award Player Points If Opponent Misses Ball
			If plr_pnts = 3 Then game_mode = 2
		Endif
		
		If ball_X_pos >= CRT_PLR_END Then	'Check Ball Has Hit "Player's End Of Court" ( Right Of Play Area )
			ball_X_pos = CRT_PLR_END
			ball_X_spd *= -1
			opnt_pnts += 1		'Award Opponent Points If Player Misses Ball
			If opnt_pnts = 3 Then game_mode = 2
		Endif
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method COLLISION_Ball_With_Player:Int()
		If ball_X_pos > ( plr_X_pos - BALL_RDS ) And ball_X_spd > 0 Then
			If ( ball_Y_pos >= plr_Y_pos ) And ( ball_Y_pos <= ( plr_Y_pos + PLR_HEIGHT )) Then
				ball_X_pos = ( plr_X_pos - BALL_RDS )
				ball_X_spd *= -1
				If ( ball_Y_pos > plr_Y_pos ) And ( ball_Y_pos < ( plr_Y_pos + PLR_CNTR_RT )) Then
					ball_Y_spd += ( plr_Y_pos - plr_prev_Y_pos ) * 0.25
					If ball_Y_spd > 0 Then ball_Y_spd *= -1
				Endif
				
				If ( ball_Y_pos > ( plr_Y_pos + PLR_CNTR_RT )) And ( ball_Y_pos < ( plr_Y_pos + PLR_CNTR_LFT )) Then ball_Y_spd = 0.0
				
				If ( ball_Y_pos > ( plr_Y_pos + PLR_CNTR_LFT )) And ( ball_Y_pos < ( plr_Y_pos + PLR_HEIGHT )) Then
					ball_Y_spd += ( plr_Y_pos - plr_prev_Y_pos ) * 0.25
					If ball_Y_spd < 0 Then ball_Y_spd *= -1
				Endif
			Endif
		Endif 
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method COLLISION_Ball_With_Opponent_0_Or_1:Int()
		For Local opnt:Int = 0 To 1
			If ( ball_X_pos > ( opnt_X_pos[opnt] - BALL_RDS )) And ( ball_X_pos < ( opnt_X_pos[opnt] + BALL_RDS*2 )) Then
				If ( ball_Y_pos >= opnt_Y_pos[opnt] ) And ( ball_Y_pos <= ( opnt_Y_pos[opnt] + OPNT_HEIGHT )) Then
				Return opnt		
				Endif
			Endif
		Next
		Return -1
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method COLLISION_Ball_With_Opponent:Int()
		Local opnt:Int = COLLISION_Ball_With_Opponent_0_Or_1()
		If opnt >= 0 And ball_X_spd < 0 Then
			ball_X_pos = ( opnt_X_pos[opnt] + ( BALL_RDS*2 ))
			ball_X_spd *= -1
			If ( ball_Y_pos > opnt_Y_pos[opnt] ) And ( ball_Y_pos < ( opnt_Y_pos[opnt] + OPNT_CNTR_LFT )) Then ball_Y_spd = -1.0
			If ( ball_Y_pos > ( opnt_Y_pos[opnt] + OPNT_CNTR_LFT )) And ( ball_Y_pos < ( opnt_Y_pos[opnt] + OPNT_CNTR_RT )) Then ball_Y_spd = 0.0
			If ( ball_Y_pos > ( opnt_Y_pos[opnt] + OPNT_CNTR_RT )) And ( ball_Y_pos < ( opnt_Y_pos[opnt] + OPNT_HEIGHT )) Then ball_Y_spd = 1.0
		Endif
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method Update_Game:Int()
		CONTROL_Player()
		MOVEMENT_Ball()
		MOVEMENT_Opponent()
		COLLISION_Ball_With_Court()
		COLLISION_Ball_With_Player()
		COLLISION_Ball_With_Opponent()
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method DRAW_TennisCourt:Int()
		SetColor ( 255 , 255 , 255 )
		DrawRect ( 0,0 , 640,480 )
		SetColor ( 0 , 160 , 80 )
		DrawRect ( 5,5 , 630,470 )
		SetColor ( 255 , 255 , 255 )
		For Local i:Int = 10 To 460 Step 15
			DrawRect ( 318,i , 5,10)
		Next
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method DRAW_Player:Int()
		SetColor ( 0 , 0 , 255 )
		DrawRect ( plr_X_pos , plr_Y_pos , PLR_WIDTH , PLR_HEIGHT )		'Draw The Player's Bat
		Return True
	End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method DRAW_Opponent:Int()
		SetColor ( 255 , 0 , 0 )
		DrawRect ( opnt_X_pos[0] , opnt_Y_pos[0] , OPNT_WIDTH , OPNT_HEIGHT )	'Draw 1st Opponent
		DrawRect ( opnt_X_pos[1] , opnt_Y_pos[1] , OPNT_WIDTH , OPNT_HEIGHT )	'Draw 2nd Opponent
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method DRAW_Ball:Int()
		SetColor ( 255 , 255 , 0 )	'Change Colour To Yellow For Tennis Ball
		DrawCircle ( ball_X_pos , ball_Y_pos , BALL_RDS )	'Draw Tennis Ball
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
	Method DRAW_Score:Int()
		SetColor ( 255 , 255 , 255 )
		DrawText ( "Opponent's Points: " + opnt_pnts , 85 , 10 )
		DrawText ( "Player's Points: " + plr_pnts , 415 , 10 )
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method Attract_Mode:Int()
		If message_mode = 0 Then
			message_mode = 1
			SetColor ( 0 , 255 , 0 )
			DrawText ( "Welcome to 'Table Tennis'. Press 'Spacebar' to Play. Press 'P' to Pause." , 20 , 320 )
		Endif
		
		If KeyHit ( KEY_SPACE ) Then
			message_mode = 0
			game_mode = 1		'Begin Game
		Endif
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Method Game_Over:Int()
		If message_mode = 0 Then
			message_mode = 1
			DrawText ( "G A M E  O V E R" , 100 , 100 )
			
			If opnt_pnts >= 3 Then
				DrawText ( "The Computer Won This Time  :(" , 80 , 112 )
				DrawText ( "Defeating You By " + ( opnt_pnts - plr_pnts ) + " Points" , 50 , 124 )
			Else
				DrawText ( "Congratulations, You Won!  :)" , 80 , 112 )
				DrawText ( "You Defeated The Computer By " + ( plr_pnts - opnt_pnts ) + " Points" , 50 , 124 )
			Endif
			
			DrawText ( "Press 'Spacebar' To Play Again." , 100 , 136 )	'Game Restart As Yet To Be Implemented ....
		Endif
		Return True
	End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End		'End of TableTennis Class



Leginus(Posted 2014) [#2]
Your select case is working fine.

The problem is that game_mode = 0 is calling Attract_Mode() and in that routine you are trying to do

SetColor ( 0 , 255 , 0 )
DrawText ( "Welcome to 'Table Tennis'. Press 'Spacebar' to Play. Press 'P' to Pause." , 20 , 320 )

which is a render operation only and cannot be called from OnUpdate()

Your Attract_Mode() needs to set a flag e.g. attractmode = 1 and then in the OnRender() check for this and then draw the text


Stuart(Posted 2014) [#3]
Many, many thanks for your invaluable help, Leginus! :) Your knowledge proved successful, much appreciated.


Leginus(Posted 2014) [#4]
No problem! Glad I could help.