On 2/3/20 12:31 PM, Wilzoo wrote:
Hi guys, so I am working on rolling dice game in GUI, the game rules will be something like rolling 3 dices and then giving out points to 2 players based on the rolled values.

Im now stuck on the showing value in GUI. Basically what I need is to show rolled value somewhere in frame.

This is my rolling dice code:

(define (nrandom n)
     (if (= n 0 ) '() (cons (random 1 7) (nrandom (- n 1)))))

this is my callback function which i know is wrong:

(new button% [parent gamew]
              [label "roll dice"]
              [min-width 150]
              ; Callback procedure for a button click:
              [callback (lambda (b e) (send (nrandom 3) show #t))])

So by using this im getting my 3 values in IDE window. Where should I look for information how to get them in frame in some kind of box?

I just started my programming journey and racket documentation is not easy for me to get any information from there tbh. Just figuring out how to make frame and buttons + callback functions took me good few hours.

(Warning: I haven't tested this code.)

One thing you could do is add a new message% widget to the frame. For example, change your callback to the following:

  ;; button callback:
  (lambda (b e)
    ;; creating the widget has the side effect of adding it
    ;; to the parent container (gamew)
    (new message%
      (parent gamew)
      (label (format "Rolled a ~s" (nrandom 3))))
    (void))

Adding a new widget to the game frame may change its size or layout. You can control layout with containers (see pane%, panel%, and their subclasses).

But message% widgets aren't very flexible in how they display text. Another thing you could do is add a single editor canvas and write all of the die-roll messages to its editor. That would look something like this:

  ;; one text% and editor-canvas% per game frame:
  (define t (new text%))
  (define ec (new editor-canvas% (parent gamew) (editor t)))

  ;; the button callback:
  ... (lambda (b e)
        (send t insert (format "Rolled a ~s\n" (nrandom 3)))
        (void))

You can refine this version by disallowing user edits (see `lock`), reliably placing the insertion point at the end of the editor before writing, adding styles to the die roll result, etc. See the text% documentation.

Ryan

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/66512696-4249-88d0-3d17-80942cee2a41%40gmail.com.

Reply via email to