Code-Beispiel
libtcod Beispiel
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
GPLv3 | TJF | 19.09.2012 |
libtcod Beispiel
Dieses Beispiel betrifft die Anwendung der Bibliothek libtcod, welche zur Erstellung von 'roguelike' Spielen dient. Es wurde ein Teil des Quelltexts aus diesem Tutorial von Python nach FB übersetzt. Der interessierte Leser wird die syntaktischen Unterschiede schnell erarbeiten und kann so die Übersetzung vervollständigen (und hier veröffentlichen).
Mit ein paar Ergänzungen entsteht ein Programm, welches in einem Fenster zwei Räume mit einer Verbindung darstellt. Eine Figur (silber) kann mit den Cursortasten zwischen den Räumen bewegt werden. Neun Geister laufen planlos herum und der zehnte bewegt sich in Richtung der Figur.
Im folgenden Quelltext müssen die Zugriffspfade an die individuelle Installation angepasst werden:
#DEFINE __TCOD_OLD_1_5_1__ ' try to uncomment if you get error 'undefined reference to ...'
#LIBPATH "libtcod-1.5.1"
#INCLUDE ONCE "libtcod.bi"
TYPE Tile
AS INTEGER Block = TRUE, Block_Sight = TRUE
END TYPE
DIM SHARED AS TCOD_console_t con
CONST AS INTEGER MAP_WIDTH = 60, MAP_HEIGHT = 35
DIM SHARED AS Tile map(MAP_WIDTH, MAP_HEIGHT)
CONST SCREEN_WIDTH = MAP_WIDTH, SCREEN_HEIGHT = MAP_HEIGHT + 3, LIMIT_FPS = 20
STATIC SHARED AS TCOD_color_t color_dark_wall, color_dark_ground
SUB Rect(byval X AS INTEGER, BYVAL Y AS INTEGER, BYVAL W AS INTEGER, BYVAL H AS INTEGER)
FOR c AS INTEGER = X + 1 TO X + W
FOR r AS INTEGER = Y + 1 TO Y + H
map(c, r).Block = FALSE
map(c, r).Block_Sight = FALSE
NEXT
NEXT
END SUB
TYPE Objct
DECLARE CONSTRUCTOR(BYVAL Vx AS INTEGER, BYVAL Vy AS INTEGER, BYVAL Vc AS INTEGER, BYVAL Cv AS TCOD_color_t)
DECLARE SUB move(BYVAL dx AS INTEGER, BYVAL dy AS INTEGER)
DECLARE SUB draw()
DECLARE SUB clear()
AS INTEGER X, Y, Char, Count = RND() * 2 * LIMIT_FPS
AS TCOD_color_t Col
END TYPE
CONSTRUCTOR Objct(BYVAL Vx AS INTEGER, BYVAL Vy AS INTEGER, BYVAL Vc AS INTEGER, BYVAL Cv AS TCOD_color_t)
X = Vx
Y = Vy
Char = Vc
Col = Cv
END CONSTRUCTOR
SUB Objct.move(BYVAL dx AS INTEGER, BYVAL dy AS INTEGER)
IF map(X + dx, Y + dy).Block THEN BEEP : EXIT SUB
map(X, Y).Block = FALSE
X += dx
Y += dy
map(X, Y).Block = TRUE
END SUB
SUB Objct.draw(BYVAL )
TCOD_console_set_default_foreground(con, Col)
TCOD_console_put_char(con, X, Y, Char, TCOD_BKGND_NONE)
END SUB
SUB Objct.clear()
TCOD_console_put_char(con, X, Y, ASC(" "), TCOD_BKGND_NONE)
END SUB
' some objects to move
DIM SHARED AS Objct obj(10) => { _
TYPE<Objct>(11, 23, ASC("@"), TCOD_silver) _
, TYPE<Objct>(12, 21, ASC("1"), TCOD_yellow) _
, TYPE<Objct>(13, 21, ASC("2"), TCOD_red) _
, TYPE<Objct>(14, 21, ASC("3"), TCOD_blue) _
, TYPE<Objct>(15, 21, ASC("4"), TCOD_red) _
, TYPE<Objct>(35, 21, ASC("5"), TCOD_yellow) _
, TYPE<Objct>(36, 21, ASC("6"), TCOD_red) _
, TYPE<Objct>(37, 21, ASC("7"), TCOD_yellow) _
, TYPE<Objct>(38, 21, ASC("8"), TCOD_red) _
, TYPE<Objct>(39, 21, ASC("9"), TCOD_yellow) _
, TYPE<Objct>(40, 21, ASC("0"), TCOD_gold) _
}
SUB make_map()
' rooms and tunnel
Rect( 3, 8, MAP_WIDTH - 38, MAP_HEIGHT - 12) ' room 1
Rect(MAP_WIDTH - 30, 4, MAP_WIDTH - 34, MAP_HEIGHT - 7) ' room 2
Rect(MAP_WIDTH - 35, 25, 5, 1) ' tunnel
' the objects (can move)
FOR i AS INTEGER = 0 TO UBOUND(obj)
map(obj(i).X, obj(i).Y).Block = TRUE
NEXT
'#go through all tiles, and set their background color
FOR y AS INTEGER = 0 TO MAP_HEIGHT
FOR x AS INTEGER = 0 TO MAP_WIDTH
IF map(x, y).Block_Sight THEN
TCOD_console_set_char_background(con, x, y, color_dark_wall, TCOD_BKGND_SET)
ELSE
TCOD_console_set_char_background(con, x, y, color_dark_ground, TCOD_BKGND_SET)
END IF
NEXT
NEXT
END SUB
FUNCTION handle_keys() AS bool
VAR key = TCOD_console_check_for_keypress(TRUE) ' real-time
'VAR key = TCOD_console_wait_for_keypress(FALSE) ' turn-based
SELECT CASE AS CONST key.vk
CASE TCODK_ENTER : IF key.lalt THEN _
TCOD_console_set_fullscreen(IIF(TCOD_console_is_fullscreen(), FALSE, TRUE))
CASE TCODK_ESCAPE : RETURN TRUE
END SELECT
IF TCOD_console_is_key_pressed(TCODK_UP) THEN
obj(0).move(0, -1)
ELSEIF TCOD_console_is_key_pressed(TCODK_DOWN) THEN
obj(0).move(0, 1)
ELSEIF TCOD_console_is_key_pressed(TCODK_LEFT) THEN
obj(0).move(-1, 0)
ELSEIF TCOD_console_is_key_pressed(TCODK_RIGHT) THEN
obj(0).move(1, 0)
END IF : RETURN FALSE
END FUNCTION
SUB render_all()
FOR i AS INTEGER = 0 TO UBOUND(obj)
obj(i).draw()
NEXT
TCOD_console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0)
END SUB
SUB move_ghosts()
VAR i = 0
FOR i = 1 TO UBOUND(obj) - 1
obj(i).Count -= 1
IF obj(i).Count <= 0 THEN
obj(i).Count = CUINT(LIMIT_FPS * (1 + RND()))
obj(i).move(CINT(RND() * 2 - 1), CINT(RND() * 2 - 1))
END IF
NEXT
obj(i).Count -= 1
IF obj(i).Count <= 0 THEN
obj(i).Count = CUINT(LIMIT_FPS * (1 + RND()))
VAR dx = SGN(obj(0).X - obj(i).X)
VAR dy = SGN(obj(0).Y - obj(i).Y)
obj(i).move(dx, dy)
END IF
END SUB
' ***** main *****
TCOD_console_set_custom_font("libtcod-1.5.1/data/fonts/arial12x12.png", _
TCOD_FONT_TYPE_GREYSCALE OR TCOD_FONT_LAYOUT_TCOD, FALSE, FALSE)
TCOD_console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, "Rogue2", FALSE, TCOD_RENDERER_SDL)
TCOD_sys_set_fps(LIMIT_FPS)
con = TCOD_console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
color_dark_wall = TCOD_color_RGB(0, 0, 100)
color_dark_ground = TCOD_color_RGB(50, 50, 150)
make_map()
RANDOMIZE
VAR c = 2 * LIMIT_FPS
WHILE 0 = TCOD_console_is_window_closed()
render_all()
TCOD_console_flush()
FOR i AS INTEGER = 0 TO UBOUND(obj)
obj(i).clear()
NEXT
move_ghosts()
IF handle_keys() THEN EXIT WHILE
WEND
Bei der Bearbeitung ist mir ein Fehler in libtcod.bi aufgefallen. Bitte die aktuelle Version des Headers verwenden.
Für wndiows Benutzer: Download mit Beispielprojekt
English
See english forum thread.
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|