Catch Me! game on CorelSCRIPT

This is a very simple game that uses CorelSCRIPT dynamic dialog boxes. The main idea of the game is to use MOVE function with dynamic dialog controls (a button in our case). This function has the following syntax:

DialogID.ControlID.MOVE left, top, width, height

DialogID and ControlID are the identifiers of the dialog box itself and the control being moved respectively. Left and top are the coordinates of the top left corner of the control with respect to the top left corner of the dialog measured in dialog units. Width and height are the width and height of the control to be set. If these parameters are omitted, the size of the control isn't changed.

CatchMe.gif (1475 bytes)

' Defining global variables
' sx, sy - the size of the dialog box
' x, y - the current position of the "Catch me" button
' cx, cy - the size of the button
' vx, vy - the current speed of the button representing the offset
'          of the button per one step
GLOBAL sx&,sy&,x&,y&,cx&,cy&,vx&,vy&

' Defining dynamic dialog box
BEGIN DIALOG OBJECT FunDialog 205, 77, "Catch Me!", SUB FunHandler
	TEXT  72, 34, 60, 8, .Text1, "Catch the button!!!"
	PUSHBUTTON  5, 5, 49, 14, .Caught, "Catch me"
END DIALOG

' The dialog box event handler subroutine
SUB FunHandler(BYVAL CtrlID%, BYVAL Event%)
SELECT CASE Event
	CASE 0 ' Initialization of the dialog event
		sx&=FunDialog.GetWidth() ' Acquiring the width
		sy&=FunDialog.GetHeight()' and height of the dialog 
		cx&=FunDialog.Caught.GetWidth() ' Width and
		cy&=FunDialog.Caught.GetHeight()' and height of 
						' the button
		x&=FunDialog.Caught.GetLeftPosition()' Horizontal and
		y&=FunDialog.Caught.GetTopPosition() ' vertical position
						     ' of the button
		vx=5 ' Horizontal speed, positive - to the right
		vy=5 ' Vertical speed, positive - downwards
		FunDialog.SETTIMER 30	' Setting timer to generate
					' timer event (5) each time
					' the button must be moved
	CASE 2 ' Clicking in a control event
		if CtrlID=FunDialog.Caught.GetID() then 
			' If the button is clicked
			FunDialog.SETTIMER 0 ' Disable the timer
			Message "Congratulations!" ' Display a message
			FunDialog.CloseDialog 1 ' Close the dialog
		end if
	CASE 5 ' Timer event
		if x+vx+cx>sx or x+vx<0 then vx=-vx
			' If the button is about to hit the right
			' or left margin of the dialog then
			' change the horizontal speed to the opposite
			' direction
		if y+vy+cy>sy or y+vy<0 then vy=-vy
			' If the button is about to hit the top
			' or bottom margin of the dialog then
			' change the vertical speed to the opposite
			' direction
		x=x+vx ' Offset the button position horizontally
		y=y+vy ' Offset the button position vertically
		FunDialog.Caught.Move x,y ' Move the button
END SELECT
END SUB

DIALOG FunDialog ' Display the dialog and start game

A key moment of the script is using the dialog box timer to move the button. When the dialog is displayed, the initialize event (code 0) is sent to the dialog event handler. In response to this event we fetch the size and position of the "Catch me" button and dialog box window as well as initialize the timer and set the time interval to 30 ms. Once this time interval elapses, the timer event (code 5) is sent to the event handler and the timer is reset until the next timer event is generated and so on.

In response to the timer event, we move the button around using MOVE control function. Additional condition must be applied to check whether the button reached an edge of the dialog. If so, the corresponding motion direction must be reversed.

Click here to download the script ready to run.


[ Fun Hour with CorelSCRIPT | Learning CorelSCRIPT | Oberon Home Page ]

Copyright 2000 by Alex Vakulenko. All rights reserved.
This page was last revised on 03/14/00.