diff options
Diffstat (limited to 'src/macroknight/game.hy')
-rw-r--r-- | src/macroknight/game.hy | 161 |
1 files changed, 24 insertions, 137 deletions
diff --git a/src/macroknight/game.hy b/src/macroknight/game.hy index 7227f76..53e6fac 100644 --- a/src/macroknight/game.hy +++ b/src/macroknight/game.hy @@ -2,148 +2,33 @@ (require hyrule *) (import pygame pytmx.util_pygame [load_pygame] - pprint [pprint]) + entities [Player LevelTile] + tiles [TileSet draw-tile] + utils [neg] + text [render-text] + systems [apply-gravity apply-collisions]) (pygame.init) - (setv TILE_SCALING 1) (setv TILE_SIZE (* TILE_SCALING 16)) -(setv GRAVITY 5) - -(defn neg [value] - (* -1 value)) - -(defn invert [move] - #((neg (get move 0)) - (neg (get move 1)))) - -(defn merge-moves [moves] - (let [end-move #(0 0)] - (for [move moves] - (setv end-move - #((+ (get end-move 0) (get move 0)) - (+ (get end-move 1) (get move 1))))) - end-move)) - -(defclass TileSet [] - (defn __init__ [self image tile-w tile-h [padding 0]] - - (setv self.sheet - (let [surf (pygame.image.load "assets/monochrome-transparent.png")] - (if (!= TILE_SCALING 1) - (pygame.transform.scale - surf - #((* (.get_width surf) TILE_SCALING) - (* (.get_height surf) TILE_SCALING))) - surf))) - - (setv self.tiles - (lfor y (range 0 (.get_height self.sheet) (+ tile-h (* padding TILE_SCALING))) - x (range 0 (.get_width self.sheet) (+ tile-w (* padding TILE_SCALING))) - (let [tile (pygame.Surface #(tile-w tile-h))] - (.blit tile self.sheet #(0 0) #(x y tile-w tile-h)) - tile))))) - - -;; Define systems here -(defn apply-gravity [entity entities] - (when (not entity.fixed) - (.move entity #(0 GRAVITY)))) - -(defn apply-collisions [entity entities] - (for [ent entities] - (when (and (!= ent.id entity.id) - (.colliderect entity.rect ent.rect) - (not entity.fixed)) - (let [collision-rect (.clip entity.rect ent.rect) - move-x (get entity.total-move 0) - move-y (get entity.total-move 1)] - (when (!= move-x 0) - (.move entity #((* (if (> move-x 0) -1 1) collision-rect.width) 0))) - (when (!= move-y 0) - (.move entity #(0 (* (if (> move-y 0) -1 1) collision-rect.height))))) - (.ground entity)))) - -;; Define entities here -(defclass LevelTile [] - (setv fixed True) - - (defn __init__ [self id x y surf] - (setv self.id id) - (setv self.surf - (if (!= TILE_SCALING 1) - (pygame.transform.scale - surf - #((* (.get_width surf) TILE_SCALING) - (* (.get_height surf) TILE_SCALING))) - surf)) - - (setv self.rect (.get_rect surf :left (* x TILE_SIZE) :top (* y TILE_SIZE))))) - -(defclass MiniSprite [pygame.sprite.Sprite] - (defn __init__ [self tile x y] - (.__init__ (super)) - (setv self.surf (pygame.Surface #(TILE_SIZE TILE_SIZE))) - (.blit self.surf tile #(0 0)) - (setv self.rect (.get_rect self.surf :left (* x TILE_SIZE) :top (* y TILE_SIZE))))) - -(defclass Player [pygame.sprite.Sprite] - (setv fixed False) - (setv SPEED 3) - (setv JUMP_IMPULSE 10) - (setv MAX_JUMPING 100) - - (defn __init__ [self tile initial-x initial-y] - (.__init__ (super)) - (setv self.id 1) - (setv self.surf (pygame.Surface #(TILE_SIZE TILE_SIZE))) - (setv self.jumping False) - (setv self.jump_move 0) - (.blit self.surf tile #(0 0)) - (setv self.rect (.get_rect self.surf :left (* 5 TILE_SIZE)))) - - (defn move [self move] - (.append self.moves move) - (.move_ip self.rect (get move 0) (get move 1))) - - (defn [property] total-move [self] - (merge-moves self.moves)) - - (defn jump [self] - (setv self.jumping True) - (when (< self.jump_move self.MAX_JUMPING) - (setv self.jump_move (+ self.jump_move self.JUMP_IMPULSE)) - (.move self #(0 (neg self.JUMP_IMPULSE))))) - - (defn ground [self] - (setv self.jump_move 0)) - - (defn flush [self] - (setv self.moves []))) (setv screen (pygame.display.set_mode #((* TILE_SCALING 640) (* TILE_SCALING 480)))) (setv clock (pygame.time.Clock)) (setv running True) - -(setv tiles (TileSet "assets/monochrome-transparent.png" TILE_SIZE TILE_SIZE 1)) - -(setv player (Player (get tiles.tiles 28) 0 0)) +(setv tileset (TileSet "assets/monochrome-transparent.png" TILE_SCALING TILE_SIZE TILE_SIZE 1)) +(setv player (Player 1 (get tileset.tiles 28) TILE_SIZE 5 5)) (setv sprites-group []) (.append sprites-group player) -(setv level (load_pygame "assets/level-1.tmx")) +(setv level (load_pygame "assets/level-0.tmx")) (setv macro-input-mode False) (setv macro-commands [None None None]) (for [tiledef (enumerate (.tiles (get level.layers 0)))] - (.append sprites-group (LevelTile (get tiledef 0) #* (get tiledef 1)))) + (.append sprites-group (LevelTile (get tiledef 0) TILE_SIZE TILE_SCALING #* (get tiledef 1)))) (setv ongoing_inputs []) -(defn draw-tile [target tile-id x y] - (let [tile (MiniSprite (get tiles.tiles tile-id) x y)] - (.blit target tile.surf tile.rect))) - (while running (for [event (pygame.event.get)] (case event.type @@ -152,7 +37,6 @@ (setv running False) (if macro-input-mode (when (in event.key [pygame.K_a pygame.K_w pygame.K_a pygame.K_s pygame.K_d]) - (print event.key) (setv (get macro-commands (.index macro-commands None)) event.key)) (case event.key pygame.K_SPACE (setv macro-input-mode True) @@ -162,32 +46,36 @@ (.fill screen "#000000") + (render-text screen tileset "USE A MACRO MOVE TO JUMP HIGHER" 1 10) + (render-text screen tileset "HIT SPACE AND ENTER A COMBO" 1 11) + (if macro-input-mode ;; If the commands list is full (if (get macro-commands -1) ;; Process commands (do - (pprint (lfor command macro-commands :if command command)) (let [#(command-id command) (get (lfor command (enumerate macro-commands) :if (get command 1) command) 0)] (case command pygame.K_a (.move player #((neg TILE_SIZE) 0)) pygame.K_s (.move player #(0 1)) - pygame.K_w (.move player #(0 (neg player.MAX_JUMPING))) + pygame.K_w (.move player #(0 (neg (/ player.MAX_JUMPING 2)))) pygame.K_d (.move player #(TILE_SIZE 0))) (if (= command-id (- (len macro-commands) 1)) (do (setv macro-commands [None None None]) (setv macro-input-mode False)) (setv (get macro-commands command-id) None)))) - (do (draw-tile screen 774 3 1) - (for [#(num command) (enumerate macro-commands)] - (let [x-pos (+ 4 num)] - (case command - pygame.K_w (draw-tile screen 1057 x-pos 1) - pygame.K_d (draw-tile screen 1058 x-pos 1) - pygame.K_s (draw-tile screen 1059 x-pos 1) - pygame.K_a (draw-tile screen 1060 x-pos 1)))))) + ;; If there's still space in the commands list + (for [#(num command) (enumerate macro-commands)] + (let [x-pos (+ 4 num)] + (case command + pygame.K_w (draw-tile screen tileset 1057 x-pos 5) + pygame.K_d (draw-tile screen tileset 1058 x-pos 5) + pygame.K_s (draw-tile screen tileset 1059 x-pos 5) + pygame.K_a (draw-tile screen tileset 1060 x-pos 5) + None (draw-tile screen tileset 725 x-pos 5))))) + ;; Else (do (for [inp ongoing_inputs] (case inp pygame.K_a (.move player #((neg player.SPEED) 0)) @@ -213,7 +101,6 @@ (.blit screen sprite.surf sprite.rect)) (pygame.display.flip) - (.tick clock 60) - ) + (.tick clock 60)) (pygame.quit) |