From 7a62b573087cde23285195d154c8bb73a760b048 Mon Sep 17 00:00:00 2001 From: elioat Date: Wed, 21 Dec 2022 21:21:19 -0500 Subject: * --- src/pokedex/imgs/blank.txt | 1 + src/pokedex/pokedex.rkt | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 src/pokedex/imgs/blank.txt (limited to 'src/pokedex') diff --git a/src/pokedex/imgs/blank.txt b/src/pokedex/imgs/blank.txt new file mode 100644 index 0000000..87058af --- /dev/null +++ b/src/pokedex/imgs/blank.txt @@ -0,0 +1 @@ +file to force this directory to show up in git diff --git a/src/pokedex/pokedex.rkt b/src/pokedex/pokedex.rkt index 3aebea1..5af1710 100644 --- a/src/pokedex/pokedex.rkt +++ b/src/pokedex/pokedex.rkt @@ -1,4 +1,3 @@ - #lang racket ;; let's try to make a pokedex in racket! @@ -13,11 +12,12 @@ ;; basic GET request -(require net/url) -(require json) +(require net/url + net/url-connect + json + slideshow/pict + racket/draw) -; 🤔 -(require slideshow/pict racket/draw) ;; API URL (define *POKE-API* "https://pokeapi.co/api/v2/") @@ -52,8 +52,30 @@ (inspector (dex-entry 11)) (inspector (dex-entry "bulbasaur")) -(define img (hash-ref (dex-entry 11) "sprite")) - -(define display-image (bitmap (make-object bitmap% img))) - -(frame (scale display-image 0.3)) ; this doesn't actually seem to work with URLs/PNGs +(define (get-image id sprite-file-name) + (define img (hash-ref (dex-entry id) "sprite")) + (define img-url + (string->url img)) + (define the-data + (parameterize ([current-https-protocol 'secure]) + (port->bytes (get-pure-port img-url)))) + (define out (open-output-file sprite-file-name)) + (write-bytes the-data out) + (close-output-port out) + (display-image sprite-file-name)) + +(define (display-image image) + (define display-image (bitmap (make-object bitmap% image))) + (frame (scale display-image 1))) + +(define (see-pokemon id) + (define sprite-file-name + (~a "imgs/" id ".png")) + (if (file-exists? sprite-file-name) + (display-image sprite-file-name) + (get-image id sprite-file-name))) + +(see-pokemon "bulbasaur") +(see-pokemon "butterfree") +(see-pokemon "pikachu") +(see-pokemon 124) \ No newline at end of file -- cgit 1.4.1-2-gfad0