Rolling dice game

50 views
Skip to first unread message

Wilzoo

unread,
Feb 3, 2020, 6:31:52 AM2/3/20
to Racket Users
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.

Thanks for help

Ryan Culpepper

unread,
Feb 3, 2020, 7:51:20 AM2/3/20
to Wilzoo, Racket Users
(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

John Clements

unread,
Feb 15, 2020, 1:52:36 PM2/15/20
to Wilzoo, Racket Users
Have you taken a look at How To Design Programs? At the end of section one, you should have what you need to build these games and others like them:

https://htdp.org/2019-02-24/part_one.html

John Clements
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/da23d01b-5422-4fe9-b40d-0ca9a904fed9%40googlegroups.com.



Reply all
Reply to author
Forward
0 new messages