Very simple Exit-the-maze game

The place for any Hollywood tutorials
Post Reply
amyren
Posts: 87
Joined: Thu May 02, 2019 11:53 am

Very simple Exit-the-maze game

Post by amyren » Mon Oct 07, 2019 10:12 pm

This is a simple basic exit-the-maze game made for beginners.
The purpose is to show how easy programming in Hollywood can be, so this first code is stripped down to just what is needed to make it run.
I might add more features to in in follow up posts later.

Prerparations: You should make an image at 640x480 pixels, with a black background representing the maze. The maze in the example is made so that the start position will be at position 225, 235, and the exit close to the upper left corner. Apart from this, you can design the maze walls at your own wish. Just make sure that there is enaugh room between the maze walls for the character to move freely.
You must also draw an image for the character, which can be of the size of your choice, but in the example below the character image is 15x17 pixels.
Save these two images as bg_maze_1.png and character.png, in the same folder as you choose to save the script,

I did add the rest of the explaining as comments in the code, so I hope it will be self explaining.

Code: Select all

LoadBrush(1, "bg_maze_1.png", {Transparency = #BLACK}) ; Maze backgrund image to be loaded as brush id1
DisplayBrush(1, 1, 1) ; display the maze background
LoadSprite(1, "character.png") ; the character image is loaded as sprite id1

;initial character position x and y are set to fixed value to match the center of the maze
startposition_x = 225 
startposition_y = 235 

; dynamic variables are set and used to display the sprite at the starting position 
position_x = startposition_x ; this variable will
position_y = startposition_y
DisplaySprite(1, position_x, position_y)

StartTimer(1) ; starts the timer to be used for movement speed later

;program main loop
Repeat
	mouse_pressed = IsLeftMouse() ; check if left mouse button is pressed
	t1 = GetTimer(1) ; gets the timer value in milliseconds
	If t1 > 10 ; this number will affect the movement speed, the lower number the faster the character will move.
		If mouse_pressed 
			;target position x and y are the set to the mouse pointer position, so that the character will always move towards the mouse
			; the -7 and -8 are offsets to make the target position to match the center of the characer of 15x17 pixels
			target_x = MouseX()-7 
			target_y = MouseY()-8
			If target_x > position_x ; if the mouse pointer is at the right side of the character
				position_x = position_x+1 
			Else
				If target_x < position_x Then position_x = position_x-1 ; if the mouse pointer is at the left side of the character
			EndIf
			If target_y > position_y ; if the mouse pointer is lower than the character
				position_y = position_y+1
			Else
				If target_y < position_y Then position_y = position_y-1 ; if the mouse pointer is higher than the character
			EndIf	
			DisplaySprite(1, position_x, position_y) ; will move the character to its new position
			If Collision(#SPRITE_VS_BRUSH, 1, 1, 1, 1) ; will check if the character touches any of the non-transparent parts of the brush
				; reset the character position if it collides with the maze walls
				position_x = startposition_x
				position_y = startposition_y
				DisplaySprite(1, position_x , position_y)
			EndIf
			ResetTimer(1) ; the timer will be reset to zero after each character movement
		EndIf
	EndIf
Forever

User avatar
jPV
Posts: 283
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Very simple Exit-the-maze game

Post by jPV » Tue Oct 08, 2019 6:57 am

Please, please don't do this kind of "busy loops" in your programs. There's absolutely no need to make full CPU load for any kind of program, and especially for this simple that doesn't consume all available processing power to calculations.

If you make this kind of loop to check user activity (instead of using prettier event handling), always add some kind of wait there. For example, VWait() will do and then your program doesn't make full load to the machine all the time (now it does even when idling!).

Add it, for example, as the last line in your loop:

Code: Select all

    VWait()
Forever
That will save users' nerves when they don't have to listen the computer going full blow with fans, and saves electricity bill too :) And most importantly leaves some processing time for other programs too (we are using multitasking operating systems nowadays afterall). Thanks!

amyren
Posts: 87
Joined: Thu May 02, 2019 11:53 am

Re: Very simple Exit-the-maze game

Post by amyren » Tue Oct 08, 2019 12:46 pm

jPV: Thank you for pointing this out. I must admit I have made a similar mistake in some of my other programs, so I will need to update these as well.

For smooth movement I did choose to use only 1 pixel movement per cylcle, and I notice that using vwait() in this example will limit the character maximum speed a bit to much for my liking.
I will follow your advice, but I probably will use something like Wait(5, #Milliseconds) instead of vwait().

tolkien
Posts: 153
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Very simple Exit-the-maze game

Post by tolkien » Tue Oct 08, 2019 11:11 pm

I think is a good habit to wait for the vertical blank.
If your character goes slow with only a pixel you can update it faster (position_x = position_x + 2) or even x = x + 1.5.

jalih
Posts: 254
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Very simple Exit-the-maze game

Post by jalih » Wed Oct 09, 2019 10:56 am

amyren wrote:
Tue Oct 08, 2019 12:46 pm
For smooth movement I did choose to use only 1 pixel movement per cylcle, and I notice that using vwait() in this example will limit the character maximum speed a bit to much for my liking.
I will follow your advice, but I probably will use something like Wait(5, #Milliseconds) instead of vwait().
For games you should use proper event loop with fixed time step for update. It also makes it really easy to handle game states.

Post Reply