User Tools

Site Tools


ugbasic:user:example:the-village
Translations of this page:


ugBASIC User Manual

PURPOSE

SOURCE CODE

 BITMAP ENABLE(16)
 
 CLS
 
 map := LOAD TILEMAP("/usr/src/the-village/resources/village.tmx")
 
 tileset := TILESET(map)
 
 POSITIVE CONST startX = 1
 POSITIVE CONST startY = 1
 POSITIVE CONST tileWidth = TILE WIDTH( TILESET( map ) )
 POSITIVE CONST tileWidth2 = (SCREEN SPRITE RATIO / 100 ) * tileWidth
 POSITIVE CONST tileHeight = TILE HEIGHT( TILESET( map ) )
 POSITIVE CONST screenWidthInTiles = SCREEN WIDTH / tileWidth 
 POSITIVE CONST screenHeightInTiles = SCREEN HEIGHT / tileHeight
 POSITIVE CONST offsetWidthInTiles = ( TILEMAP WIDTH( map ) - screenWidthInTiles ) / 2
 POSITIVE CONST offsetHeightInTiles = ( TILEMAP HEIGHT( map ) - screenHeightInTiles ) / 2
 POSITIVE CONST maxPageX = TILEMAP WIDTH( map ) / screenWidthInTiles
 POSITIVE CONST maxPageY = TILEMAP HEIGHT( map ) / screenHeightInTiles
 
 POSITIVE CONST railLU = TILE ID( TILESET( map ), railLU )
 POSITIVE CONST railLD = TILE ID( TILESET( map ), railLD )
 POSITIVE CONST railRU = TILE ID( TILESET( map ), railRU )
 POSITIVE CONST railRD = TILE ID( TILESET( map ), railRD )
 POSITIVE CONST railLR = TILE ID( TILESET( map ), railLR )
 POSITIVE CONST railUD = TILE ID( TILESET( map ), railUD )
 POSITIVE CONST terrain = TILE ID( TILESET( map ), terrain )
 POSITIVE CONST terrain2 = TILE ID( TILESET( map ), terrain2 )
 POSITIVE CONST score = TILE ID( TILESET( map ), score )
 POSITIVE CONST digit0 = TILE ID( TILESET( map ), digit0 )
 POSITIVE CONST vagonST = TILE ID( TILESET( map ), vagonST )
 POSITIVE CONST railLRbroken = TILE ID( TILESET( map ), railLRbroken )
 POSITIVE CONST railUDbroken = TILE ID( TILESET( map ), railUDbroken )
 
 DIM vagon AS SPRITE
 DIM previousRail AS BYTE
 DIM actualRail AS BYTE
 DIM actualPageX AS BYTE
 DIM actualPageY AS BYTE
 DIM previousPageX AS BYTE = -1
 DIM previousPageY AS BYTE = -1
 DIM tx AS BYTE = startX
 DIM ty AS BYTE = startY
 DIM x AS POSITION = SCREEN BORDER X + startX*tileWidth2
 DIM y AS POSITION = SCREEN BORDER Y + startY*tileHeight
 DIM directionX AS POSITION = 1
 DIM directionY AS POSITION = 0
 DIM jumpAY AS BYTE = 0
 DIM jumpY AS BYTE = 0
 
 vagon = CSPRITE( IMAGE( tileset TILE NAMED vagonLR ) )
 
 vagonUDImage = IMAGE( tileset TILE NAMED vagonUD )
 vagonLRImage = IMAGE( tileset TILE NAMED vagonLR )
 
 GLOBAL vagon, map, tileset, directionX, directionY, tx, ty
 GLOBAL vagonUDImage, vagonLRImage, x, y, directionX, directionY
 GLOBAL actualPageX, actualPageY, previousPageX, previousPageY
 GLOBAL jumpAY, jumpY
 
 PROCEDURE drawVagon ON C128, COLECO
 
 	IF jumpAY > 0 THEN
 		IF jumpAY > ( tileWidth2 / 2 ) THEN
 			jumpY = 4
 		ELSE IF jumpAY > ( tileWidth2 / 4 ) THEN
 			jumpY = 2
 		ELSE
 			jumpY = 1
 		ENDIF
 		
 		DEC jumpAY
 	ELSE
 		jumpY = 0
 	ENDIF
 	
 	SPRITE vagon ENABLE AT x, y - jumpY
 	
 END PROC
 
 PROCEDURE drawVagon ON ALL BUT C128, COLECO
 
 END PROC
 
 PROCEDURE drawScore
 	
 	PUT IMAGE tileset TILE score AT 0, 0
 	PUT IMAGE tileset TILE score+1 AT tileWidth, 0
 
 	PUT IMAGE tileset TILE digit0 AT 2*tileWidth, 0
 	PUT IMAGE tileset TILE digit0 AT 3*tileWidth, 0
 	PUT IMAGE tileset TILE digit0 AT 4*tileWidth, 0
 	PUT IMAGE tileset TILE digit0 AT 5*tileWidth, 0
 	PUT IMAGE tileset TILE digit0 AT 6*tileWidth, 0
 
 END PROC
 
 PROCEDURE changeVagonSpriteAndDirection[ rail AS BYTE, previous AS BYTE ]
 
 	DIM tocheck AS BYTE
 	
 	tocheck = rail
 	
 	IF rail = terrain OR rail = terrain2 THEN
 		tocheck = previous
 	ENDIF
 
 	IF ( directionX > 0 ) AND ( tocheck <> railLR ) THEN
 		directionX = 0
 		directionY = 0
 		IF tocheck = previous THEN
 			DEC tx
 		ENDIF
 		SELECT CASE tocheck
 			CASE railLU
 				directionX = 0
 				directionY = -1
 				vagon = CSPRITE( vagonUDImage, vagon )
 			CASE railLD
 				directionX = 0
 				directionY = 1
 				vagon = CSPRITE( vagonUDImage, vagon )
 		ENDSELECT
 	ELSE IF ( directionX < 0 ) AND ( tocheck <> railLR ) THEN
 		directionX = 0
 		directionY = 0
 		IF tocheck = previous THEN
 			INC tx
 		ENDIF
 		SELECT CASE tocheck
 			CASE railRU
 				directionX = 0
 				directionY = -1
 				vagon = CSPRITE( vagonUDImage, vagon )
 			CASE railRD
 				directionX = 0
 				directionY = 1
 				vagon = CSPRITE( vagonUDImage, vagon )
 		ENDSELECT
 	ELSE IF ( directionY > 0 ) AND ( tocheck <> railUD ) THEN
 		directionX = 0
 		directionY = 0
 		IF tocheck = previous THEN
 			DEC ty
 		ENDIF
 		SELECT CASE tocheck
 			CASE railLU
 				directionX = -1
 				directionY = 0
 				vagon = CSPRITE( vagonLRImage, vagon )
 			CASE railRU
 				directionX = 1
 				directionY = 0
 				vagon = CSPRITE( vagonLRImage, vagon )
 		ENDSELECT
 	ELSE IF ( directionY < 0 ) AND ( tocheck <> railUD ) THEN
 		directionX = 0
 		directionY = 0
 		IF tocheck = previous THEN
 			INC ty
 		ENDIF
 		SELECT CASE tocheck
 			CASE railLD
 				directionX = -1
 				directionY = 0
 				vagon = CSPRITE( vagonLRImage, vagon )
 			CASE railRD
 				directionX = 1
 				directionY = 0
 				vagon = CSPRITE( vagonLRImage, vagon )
 		ENDSELECT
 	ENDIF	
 END PROC
 
 PROCEDURE moveVagonToStart
 	tx = startX
 	ty = startY
 	x = SCREEN BORDER X + startX*tileWidth2
 	y = SCREEN BORDER Y + startY*tileHeight
 	actualPageX = 0
 	actualPageY = 0
 	previousPageX = -1
 	previousPageY = -1
 	directionX = 1
 	directionY = 0	
 	jumpAY = 0
 	jumpY = 0
 	changeVagonSpriteAndDirection[ railLR, terrain ]
 	drawVagon[]
 END PROC
 
 moveVagonToStart[]
 
 DO
 
 	ADD x, directionX
 	IF ( ( x AND (tileWidth2-1) ) = 0 ) AND ( directionX <> 0 ) THEN
 		IF directionX > 0 THEN
 			INC tx
 		ELSE IF directionX < 0 THEN
 			DEC tx
 		ENDIF
 	ENDIF
 
 	ADD y, directionY
 	IF ( ( y AND (tileHeight-1) ) = 0 ) AND ( directionY <> 0 ) THEN
 		IF directionY > 0 THEN
 			INC ty
 		ELSE IF directionY < 0 THEN
 			DEC ty
 		ENDIF
 	ENDIF
 
 	IF ( previousPageX <> actualPageX ) OR ( previousPageY <> actualPageY ) THEN
 		PUT TILEMAP map FROM actualPageX * screenWidthInTiles, actualPageY * screenHeightInTiles
 		drawScore[]
 		previousPageX = actualPageX
 		previousPageY = actualPageY
 	ENDIF
 	
 	actualRail = TILEMAP map AT( tx, ty )
 
 	IF actualRail = railLRbroken OR actualRail = railUDbroken THEN
 		IF jumpY <= 1 THEN
 			moveVagonToStart[]
 		ENDIF
 	ELSE
 		IF previousRail <> actualRail THEN
 			changeVagonSpriteAndDirection[ actualRail, previousRail ]
 			previousRail = actualRail
 		ENDIF
 	ENDIF
 
 	IF ( actualPageX < maxPageX ) AND tx > ( (actualPageX+1) * screenWidthInTiles ) THEN
 		x = SCREEN BORDER X + tileWidth2
 		INC actualPageX
 	ELSE IF ( actualPageX > 0 ) AND tx < ( (actualPageX) * screenWidthInTiles ) THEN
 		x = SCREEN BORDER X + tileWidth2*(screenWidthInTiles-1)
 		DEC actualPageX
 	ELSE IF ( actualPageY < maxPageY ) AND ty > ( (actualPageY+1) * screenHeightInTiles ) THEN
 		y = SCREEN BORDER Y
 		INC actualPageY
 	ELSE IF ( actualPageY > 0 ) AND ty < ( (actualPageY) * screenHeightInTiles ) THEN
 		y = SCREEN BORDER Y + tileHeight*(screenHeightInTiles-1) - tileHeight
 		DEC actualPageY
 	ENDIF
 	
 	drawVagon[]
 
 	'PRINT INKEY;
 	
 	'IF KEY PRESSED( KEY SPACE ) AND jumpAY = 0 THEN
 	'	jumpAY = tileWidth2
 	'ENDIF
 	
 LOOP
 
 
 

SOURCE FILE

HOW TO COMPILE AND RUN

The instructions here refer to compiling the example from the command line. For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to compile the example with just one click.

ATARI 400/800 family

In order to compile and run the example, you need to have the Altirra emulator, and in particular that the altirra executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.atari the-village.bas -o example.xex
 altirra example.xex
 
 # Windows 
 ugbc.atari.exe the-village.bas -o example.xex
 altirra example.xex

ATARI 600XL/800XL/1200XL/XG(SE) family

In order to compile and run the example, you need to have the Altirra emulator, and in particular that the altirra executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.atarixl the-village.bas -o example.xex
 altirra example.xex
 
 # Windows 
 ugbc.atarixl.exe the-village.bas -o example.xex
 altirra example.xex

Commodore 64

In order to compile and run the example, you need to have the VICE emulator, and in particular that the x64sc executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.c64 the-village.bas -o example.prg
 x64sc example.prg
 
 # Windows 
 ugbc.c64.exe the-village.bas -o example.prg
 x64sc example.prg

Commodore PLUS/4

Using YAPE

In order to run the example, you need to have the YAPE emulator. In particular that the yape executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.plus4 the-village.bas -o example.prg
 yape example.prg
 
 # Windows 
 ugbc.plus4.exe the-village.bas -o example.prg
 yape example.prg
Using VICE

In order to run the example, you need to have the VICE emulator. In particular that the xplus4 executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.plus4 the-village.bas -o example.prg
 xplus4 example.prg
 
 # Windows 
 ugbc.plus4.exe the-village.bas -o example.prg
 xplus4 example.prg

Dragon 32

In order to compile and run the example, you need to have the XROAR emulator, and in particular that the xroar executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.d32 the-village.bas -o example.bin
 xroar -rompath (your rom path) example.bin
 
 # Windows 
 ugbc.d32.exe the-village.bas -o example.bin
 xroar.exe -rompath (your rom path) example.bin

Dragon 64

In order to compile and run the example, you need to have the XROAR emulator, and in particular that the xroar executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.d64 the-village.bas -o example.bin
 xroar -rompath (your rom path) example.bin
 
 # Windows 
 ugbc.d64.exe the-village.bas -o example.bin
 xroar.exe -rompath (your rom path) example.bin

PC128 Olivetti Prodest

In order to compile and run the example, you need to have the DCMOTO emulator, and in particular that the dcmoto executable is accessible.

Then, type this command on the command line and on the emulator:

 # Linux 
 ugbc.pc128op the-village.bas -o example.k7
 dcmoto
 (choose BASIC 128)
 CLEAR,&H2FFF: LOADM"CASS:",R: EXEC
 
 # Windows 
 ugbc.pc128op.exe the-village.bas -o example.k7
 dcmoto
 (choose example.k7)
 (choose BASIC 128)
 CLEAR,&H2FFF: LOADM"CASS:",R: EXEC

Thomson MO5

In order to compile and run the example, you need to have the DCMOTO emulator, and in particular that the dcmoto executable is accessible.

Then, type this command on the command line and on the emulator:

 # Linux 
 ugbc.pc128op the-village.bas -o example.k7
 dcmoto
 (choose BASIC 128)
 CLEAR,&H2FFF: LOADM"CASS:",R: EXEC
 
 # Windows 
 ugbc.pc128op.exe the-village.bas -o example.k7
 dcmoto
 (choose example.k7)
 (choose BASIC 128)
 CLEAR,&H2FFF: LOADM"CASS:",R: EXEC

Commodore VIC-20

In order to compile and run the example, you need to have the VICE emulator, and in particular that the xvic executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.vic20 the-village.bas -o example.prg
 xvic --memory 24k example.prg
 
 # Windows 
 ugbc.vic20.exe the-village.bas -o example.prg
 xvic --memory 24k example.prg

ZX Spectrum

In order to compile and run the example, you need to have the Speccy emulator, and in particular that the speccy executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.zx the-village.bas -o example.tap
 Speccy example.tap
 
 # Windows 
 ugbc.zx.exe the-village.bas -o example.tap
 Speccy example.tap

MSX

In order to compile and run the example, you need to have the openMsx or the BlueMSX emulator, and in particular that its executable is accessible.

Then, type this command on the command line:

openMSX
 # Linux 
 ugbc.msx1 the-village.bas -o example.rom
 openmsx -cart example.rom
 
 # Windows 
 ugbc.msx1.exe the-village.bas -o example.rom
 openmsx -cart example.rom
blueMSX
 # Linux 
 ugbc.msx1 the-village.bas -o example.rom
 bluemsx example.rom
 
 # Windows 
 ugbc.msx1.exe the-village.bas -o example.rom
 bluemsx example.rom

ColecoVision

In order to compile and run the example, you need to have the openMsx or the BlueMSX emulator, and in particular that its executable is accessible.

Then, type this command on the command line:

openMSX
 # Linux 
 ugbc.coleco the-village.bas -o example.rom
 openmsx -machine \"COL - ColecoVision\" -cart example.rom
 
 # Windows 
 ugbc.coleco.exe the-village.bas -o example.rom
 bluemsx -machine \"COL - ColecoVision\" example.rom
blueMSX
 # Linux 
 ugbc.coleco the-village.bas -o example.rom
 bluemsx /machine \"COL - ColecoVision\" /rom1 example.rom
 
 # Windows 
 ugbc.coleco.exe the-village.bas -o example.rom
 bluemsx  /machine \"COL - ColecoVision\" /rom1 example.rom

SEGA SC-3000

In order to compile and run the example, you need to have the BlueMSX emulator, and in particular that its executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.sc3000 the-village.bas -o example.rom
 bluemsx /machine \"SEGA - SC-3000\" /rom1 example.rom
 
 # Windows 
 ugbc.sc3000.exe the-village.bas -o example.rom
 bluemsx  /machine \"SEGA - SC-3000\" /rom1 example.rom

SEGA SG-1000

In order to compile and run the example, you need to have the BlueMSX emulator, and in particular that its executable is accessible.

Then, type this command on the command line:

 # Linux 
 ugbc.sg1000 the-village.bas -o example.rom
 bluemsx /machine \"SEGA - SG-1000\" /rom1 example.rom
 
 # Windows 
 ugbc.sg1000.exe the-village.bas -o example.rom
 bluemsx  /machine \"SEGA - SG-1000\" /rom1 example.rom

ANY PROBLEM?

If you have found a problem trying to run this example, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub. Thank you!

POWERED BY