Created
May 2, 2016 05:13
-
-
Save zoek1/9edcf67304bac6c11e2eb337bf4a05ce to your computer and use it in GitHub Desktop.
Quasi FRP game using Hy + Pygame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import pygame) | |
;; Impure function - merge dict2 onto dict1 | |
(defn merge_dicts [dict1 dict2] | |
(do | |
(.update dict1 dict2) | |
dict1)) | |
;; Get the base state - prefered this way instead setv function | |
(defn init [] | |
{ | |
'screen (pygame.display.set_mode [400 300]) ;; Set the window size | |
'is_blue True ;; Verify is the actual color is blue | |
'limit False ;; Set to true only if you close the window | |
'color_rect [0 128 255] ;; Color of the rect | |
'rect_instance (.Rect pygame 30 30 60 60) ;; Rect that will be drawn | |
}) | |
;; Return | |
(defn update [event state] | |
{ | |
'limit (if (= event.type pygame.QUIT) True False) ;; Verify if the close button was pushed | |
'color_rect (if (get state 'is_blue) [0 128 255] [255 100 0]) ;; Verify if the color is blue else return orange color | |
'is_blue (if (and (= event.type pygame.KEYDOWN) | |
(= event.key pygame.K_SPACE)) | |
(not (get state 'is_blue)) | |
(get state 'is_blue)) ;; Change the color if the key space was typed | |
}) | |
(defn draw [state] | |
(pygame.draw.rect | |
(get state 'screen) | |
(get state 'color_rect) | |
(get state 'rect_instance))) ;; Draw the rect | |
(defmain [&rest args] | |
(do | |
(.init pygame) | |
(setv state (init)) | |
(while (not (get state 'limit)) | |
(do | |
(for [event (pygame.event.get)] | |
(draw (merge_dicts state (update event state)))) | |
(pygame.display.flip))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment