RCRPG/Julia: Difference between revisions

m
Fixed syntax highlighting.
mNo edit summary
m (Fixed syntax highlighting.)
 
(8 intermediate revisions by one other user not shown)
Line 1:
This [[Julia]] version of [[RCRPG]] implementscontains two versions of the game code: one version with a text interface. Whenand runa withinsecond Julia'sone commandwith linea REPL,GUI interface.
it will display many of the game items in color.
 
===Language Idioms===
 
This program illustrates some of the interesting aspects of Julia:
Line 10 ⟶ 9:
* functional programming with map and other functions using anonymous functions on arrays
 
<br />
In addition, the GUI version code illustrates the following additional aspects of Julia:
* The use of Channels to create a communication between game code and display code in a way that could allow the game to run as a client-server version with little change
* The use of @async to allow co-routine based multitasking with Channels to communicate between co-routines
 
== =Objective ===
The objective of the game is to find your way to the treasure room, which is located in the upper left of level 5
(against the dungeon level 5 corner at (1, 1, 5)), and then to exit the game by ascending above level 1.
 
===Commands===
 
Direction commands:<pre>north, south, east, west, up, down</pre>Type the first letter of these commands
Line 40:
removing inventory drops items on the dungeon floor.
 
Rooms for each level remain only while you are on that level. Levels generate new rooms each time you renterre-enter them.
 
View inventory: Letter i.
 
Current rroom in(of)om informationormation: Letter of.
 
Aliasing: Letter l. Restrictions are that the keys are case insensitive and cannot be extended keys.
 
Help: Letter h.
 
===Code===
<langsyntaxhighlight lang="julia">using Crayons
 
struct Point
Line 570 ⟶ 572:
 
rungame()
</syntaxhighlight>
</lang>
 
==GUI Code==
<syntaxhighlight lang="julia">using Gtk.ShortNames, Colors, Cairo, Graphics
<lang julia>
using Gtk.ShortNames, Colors, Cairo, Graphics
 
#============== GUI CODE ===================#
Line 598 ⟶ 599:
push!(vbox, textdisplay)
 
mainchoices = ["N", "S", "E", "W", "Up", "Down", "Inven", "inFo", "Take", "Remove", "eQuip", "Attack", "aLiases", "Help"]
aliases = Dict{String, String}()
directionchoices = ["N", "S", "E", "W", "U", "D"]
yesnochoices = ["Yes", "No"]
yesnoquitchoices = ["Yes", "No", "Quit"]
aliases = Dict{String, String}()
 
struct DisplayUpdate
Line 612 ⟶ 615:
 
inputchan = Channel{String}(1000)
kbinput(w, event) = (push!(inputchan, Stringstring(Char(event.keyval))); 1)
inputhid = signal_connect(kbinput, win, "key-press-event") do widget, event
 
signal_connect(win, "key-press-event") do widget, event
println("hit key $(String(event.keyval))")
push!(inputchan, String(event.keyval))
end
dchan = Channel{DisplayUpdate}(100)
lchan = Channel{String}(20)
Line 692 ⟶ 693:
 
#============== INPUT INTERFACE BETWEEN GUI AND GAME PLAY VIA CHANNELS =======================#
 
function aliasing(player)
aliaschan = Channel{String}(4)
aliaskeyhit(w, event) = push!(aliaschan, string(Char(event.keyval)))
signal_handler_block(win, inputhid)
hid = signal_connect(aliaskeyhit, win, "key-press-event")
logln("Hit the CURRENTLY DEFAULT key for function:")
oldch = take!(aliaschan)
logln("Hit the TO BE AN ALIAS key for the last key:")
newch = take!(aliaschan)
aliases[newch] = oldch
signal_handler_disconnect(win, hid)
signal_handler_unblock(win, inputhid)
end
 
function queryprompt(menu, options, txt="")
Line 700 ⟶ 715:
while true
choice = uppercase(take!(inputchan))
if haskey(aliases, choice)
choice = uppercase(aliases[choice])
end
if choice in options
swapforlastinbox(oldmenu, vbox)
Line 958 ⟶ 976:
 
help(player=nothing) = push!(lchan,
"n north, s south, w west, e east, d down, u up, i inventory, f roominfo, t take, r drop, q equip, a attack/dig, l aliases")
 
playerdisplayinventory(player) = push!(lchan, "Inventory: $(player.inventory)\nWielding: $(player.wielding)\n")
Line 1,182 ⟶ 1,200:
"U" => playerup, "D" => playerdown, "I" => playerdisplayinventory,
"F" => playerroominfo, "T" => playertake, "R" => playerremove,
"Q" => playerequip, "A" => playerdig, "L" => aliasing, "H" => help)
 
function rungame()
Line 1,195 ⟶ 1,213:
end
 
rungame()</syntaxhighlight>
</lang>
9,476

edits