开发者

Rebol/View: How to assign images to layout already created?

开发者 https://www.devze.com 2023-01-23 05:42 出处:网络
Using Rebol/View 2.7.7, I\'m trying to create a card game based on Nick\'s Rebol tutorial at: http://re-bol.com/rebol.html#section-10.18. What I want to do though is read the cards from the binary fil

Using Rebol/View 2.7.7, I'm trying to create a card game based on Nick's Rebol tutorial at: http://re-bol.com/rebol.html#section-10.18. What I want to do though is read the cards from the binary file Nick created, discard some of the data, and use it to layout a tableau of cards, 4 rows of 3 columns, with the 2 center card locations not used.

Here's my code:

protect-system

random/seed now

do %cards.r  ;--include the binary card data

the-tableau: [
 size 320x480 backdrop 0.170.0
 style tabstyle image 80x100 teal
 style holdplace box 80x100 coal
 across
 at 30x20 tc1: tabstyle
 tc2: tabstyle 
 tc3: tabstyle return
 at 30x130 tc4: tabstyle
 tc100: holdplace
 tc5: tabstyle return
 at 30x240 tc6: tabstyle
 tc200: holdplace
 tc7: tabstyle return
 at 30x350 tc8: tabstyle
 tc9: tabstyle
 tc10: tabstyle
]

lc: copy []
lc: [tc1 tc2 tc3 tc4 tc5 tc6 tc7 tc8 tc9 tc10]
deck-cards: copy []  ; The deck holds all of the cards from the binary file
deck-cards-num: copy []
deck-cards-color: copy 开发者_StackOverflow社区[]
lay: layout the-tableau
foreach [card label num color pos] cards [

 dimg: load to-binary decompress (card)
 append deck-cards dimg ;feel movestyle
 throw-away-label: label
 append deck-cards-num num
 append deck-cards-color color
 throw-away-pos: pos
]

random-card: does [pick deck-cards random length? deck-cards]
foreach c lc [set-face get c deck-cards]

view lay

do-events

But this doesn't show the cards at all. I'm not even sure it's reading the correctly? Where is the problem?


Actually you didn't use the random-card function in your for loop at the end... :-)

foreach c lc [get c set-face get c random-card ]

You note that you are not sure if data was loaded correctly...

here is a simple way to find out... just print/probe the TYPE? of that data

dimg: load to-binary decompress (card)
probe type? dimg

In this case it will print out image! in the console... so yep... that's working. :-)

As an added little detail, I noticed you didn't compensate your random for the "back face" image in the card data (which is at its end), so the random-card function should be fixed like so:

random-card: does [pick deck-cards random (length? deck-cards) - 1] ; - 1 since we don't want the back face to be picked.


You only need 'do-events if the event loop is not started.

View/new does not start the event loop .. but View does

I'm not addressing your actual problem though :(


to make the do-events note clear, I added a little answer here so I can add some inline code....

here is an example where you'd want your do-events to be used.

view/new lay ; display the interface right now. (with no cards)

random-card: does [pick deck-cards random (length? deck-cards) - 1] ; - 1 since we don't want the back face to be picked.

; deal cards with a half second delay.
foreach c lc [f: get c set-face get c random-card wait 0.5]


do-events

here, any code you put after 'DO-EVENTS will be executed once all view windows have closed.

which can be things like tmp file cleanup, save on exit, "save changes" dialogs, etc.

additional note:

While building graphics code, its a good habit to place this at the very start of you application:

print " " 

It will open up the console, and then any view windows will show up in front of it.

When ready to share, just comment the line and remove any print statements in your code.

this is useful for 3 things:

1) Its usually highly annoying when the console always pops-up over your application while its tracing (print/probe/etc) some stuff after your window opens.

2) This also has the more useful side-effect of showing you if your application quit correctly since the console will ALSO quit when all waits have terminated correctly.

In your original example, if you add the above print, then you'll see that the console never closes, so this means the application is still running with no more application windows listening to events.

3) It also has the advantage that you can terminate the graphic app directly by closing the console window. This effectively closes all windows and waits immediately and shortcuts any "on application quit" code you might have (code after do-events).

0

精彩评论

暂无评论...
验证码 换一张
取 消