Skip to content

Commit

Permalink
Separated procedures into multiple files.
Browse files Browse the repository at this point in the history
  • Loading branch information
packetpirate committed Apr 25, 2015
1 parent 37652b3 commit b29c572
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 147 deletions.
90 changes: 90 additions & 0 deletions constants.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#lang racket
(require sgl/gl
rsound
racket/runtime-path)

; Constants
(define F_WIDTH 600)
(define F_HEIGHT 600)
(define GRID_SIZE 15)
(define NEIGHBORS '((0 -1) (1 -1) (1 0) (1 1) (0 1) (-1 1) (-1 0) (-1 -1)))
(define nil '())

; Globals
(define TILE_SIZE (/ F_HEIGHT GRID_SIZE))
(define GRID_OFF (/ (abs (- F_WIDTH F_HEIGHT)) 2))
(define A_TIMER 0)
(define PATH '())

; Setup the sound settings and load the guitar sounds.
(define-runtime-path demos "demos")
(define song (rs-read (build-path demos "Guitar.C4E4.wav")))
(define Sample-rate 44100.0)
(define (s sec) (* sec Sample-rate))

; The following are some pre-defined guitar sounds.
(define n1 (clip song (s 0) (s 1)))
(define n2 (clip song (s 9) (s 10)))
(define n3 (clip song (s 19) (s 20)))
(define n4 (clip song (s 25) (s 26)))
(define n5 (clip song (s 31) (s 32)))
(define clips (list n1 n2 n3 n4 n5))

; Define the start and end position.
(define start (cons 3 4))
(define goal (cons 10 9))

; Define the player's current position. Subject to change throughout execution.
(define player (cons 3 4))

; Resize the display window.
(define (resize w h)
(glViewport 0 0 w h)
(set! F_WIDTH w)
(set! F_HEIGHT h)
(set! TILE_SIZE (/ F_HEIGHT GRID_SIZE))
(set! GRID_OFF (/ (abs (- F_WIDTH F_HEIGHT)) 2)))

; Compute G score. (14 if moving from the current tile to this one requires a diagonal movement, 10 otherwise)
(define (compG p t)
(let ([xOff (- (send t getCol) (send p getCol))] ; The column offset between the two tiles.
[yOff (- (send t getRow) (send p getRow))]) ; The row offset between the two tiles.
(define (cg)
(if (and (not (= xOff 0)) (not (= yOff 0)))
14 ; If the movement from the current tile to this neighbor is a diagonal one, the cost is 14...
10)) ; ...otherwise, its cost is 10.
(cg)))

; Compute H score. ((vertical offset + horizontal offset) * 10)
(define (compH t e)
(let ([xOff (abs (- (send t getCol) (send e getCol)))] ; Horizontal offset from goal tile.
[yOff (abs (- (send t getRow) (send e getRow)))]) ; Vertical offset from goal tile.
(define (ch)
(* (+ xOff yOff) 10)) ; Multiply the sum of the two offsets by 10 to get the H score.
(ch)))

; Compute F score. (G score + H score)
(define (compF g h)
(+ g h))

; Used to move the player around the grid.
(define (move rOff cOff)
(set! player (cons (+ (car player) rOff) (+ (cdr player) cOff))) ; Modifies the player's position to reflect the specified movement.
(play (list-ref clips (random 4)))) ; Plays one of the guitar sound clips.

; Called after the window renders. Used to update objects.
(define (update)
(if (>= A_TIMER 1/3) ; We want the player to move every 0.5 seconds, so the timer stops at 1/2 second and then acts.
(begin (set! A_TIMER 0) ; Reset the timer, then check to see where the player is and move accordingly.
(cond ((< (car player) (car goal)) ; If the player is above the goal, move them one down.
(move 1 0)) ; Move down.
((> (car player) (car goal)) ; If the player is below the goal, move them one up.
(move -1 0)) ; Move up.
((< (cdr player) (cdr goal)) ; If the player is to the left of the goal, move them one to the right.
(move 0 1)) ; Move right.
((> (cdr player) (cdr goal)) ; If the player is to the right of the goal, move them one to the left.
(move 0 -1)) ; Move left.
(else #f)))
(set! A_TIMER (+ A_TIMER 1/60)))) ; If a half a second hasn't gone by yet, add the time delay to the timer.

(provide (all-defined-out))
71 changes: 71 additions & 0 deletions tile.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#lang racket
(require sgl/gl)
(require "constants.rkt")

; Helper function for drawing tiles at a given row and column.
(define (drawTile r c)
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF) (* r TILE_SIZE) 0.0) ; Draw the vertex at the top-left of the box.
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF TILE_SIZE) (* r TILE_SIZE) 0.0) ; Draw the vertex at the top-right of the box.
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF TILE_SIZE) (+ (* r TILE_SIZE) TILE_SIZE) 0.0) ; Draw the vertex at the bottom-right of the box.
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF) (+ (* r TILE_SIZE) TILE_SIZE) 0.0)) ; Draw the vertex at the bottom-left of the box.

; Class to represent grid tiles.
(define tile%
(class object%
(init row col walkable)
(super-new)

(define myRow row) ; The row that this tile is in.
(define myCol col) ; The column that this tile is in.
(define canWalk walkable) ; Can the "player" traverse this tile?
(define parent nil) ; Used to determine which tile this tile was a neighbor of.

(define F 0) ; This particular tiles F score. (G+H)
(define G 0) ; This tile's G score. (10 or 14, depending on movement to get to this tile)
(define H 0) ; This tile's H score. ((vertical offset + horizontal offset) * 10)

; Field getters/setters.
(define/public (getRow)
myRow)
(define/public (getCol)
myCol)

(define/public (isWalkable)
canWalk)
(define/public (setWalk v)
(set! canWalk v))

(define/public (hasParent)
(not (equal? parent nil)))
(define/public (getParent)
parent)
(define/public (setParent p)
(set! parent p))

(define/public (getF)
F)
(define/public (setF f)
(set! F f))
(define/public (getG)
G)
(define/public (setG g)
(set! G g))
(define/public (getH)
H)
(define/public (setH h)
(set! H h))

; Draws the appropriate color for the tile.
(define/public (draw)
(cond ((not canWalk)
(begin (glColor3f 0.4 0.4 0.4) ; If this is a wall, make the color of the tile gray.
(drawTile myRow myCol)))
((and (= myRow (cdr start)) (= myCol (car start)))
(begin (glColor3f 0.0 1.0 0.0) ; If this is the start tile, make the color green.
(drawTile myRow myCol)))
((and (= myRow (cdr goal)) (= myCol (car goal)))
(begin (glColor3f 1.0 0.0 0.0) ; If this is the goal tile, make the color red.
(drawTile myRow myCol)))
(else #f)))))

(provide (all-defined-out))
174 changes: 27 additions & 147 deletions tune_traveler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,128 +5,8 @@
rsound
racket/runtime-path)

; Constants
(define F_WIDTH 600)
(define F_HEIGHT 600)
(define GRID_SIZE 15)
(define NEIGHBORS '((0 -1) (1 -1) (1 0) (1 1) (0 1) (-1 1) (-1 0) (-1 -1)))
(define nil '())

; Setup the sound settings and load the guitar sounds.
(define-runtime-path demos "demos")
(define song (rs-read (build-path demos "Guitar.C4E4.wav")))
(define Sample-rate 44100.0)
(define (s sec) (* sec Sample-rate))

; The following are some pre-defined guitar sounds.
(define n1 (clip song (s 0) (s 1)))
(define n2 (clip song (s 9) (s 10)))
(define n3 (clip song (s 19) (s 20)))
(define n4 (clip song (s 25) (s 26)))
(define n5 (clip song (s 31) (s 32)))
(define clips (list n1 n2 n3 n4 n5))

; Globals
(define TILE_SIZE (/ F_HEIGHT GRID_SIZE))
(define GRID_OFF (/ (abs (- F_WIDTH F_HEIGHT)) 2))
(define A_TIMER 0)
(define PATH '())

; Define the start and end position.
(define start (cons 3 4))
(define goal (cons 10 9))

; Define the player's current position. Subject to change throughout execution.
(define player (cons 3 4))

; Resize the display window.
(define (resize w h)
(glViewport 0 0 w h)
(set! F_WIDTH w)
(set! F_HEIGHT h)
(set! TILE_SIZE (/ F_HEIGHT GRID_SIZE))
(set! GRID_OFF (/ (abs (- F_WIDTH F_HEIGHT)) 2)))

; Compute G score. (14 if moving from the current tile to this one requires a diagonal movement, 10 otherwise)
(define (compG p t)
(let ([xOff (- (send t getCol) (send p getCol))] ; The column offset between the two tiles.
[yOff (- (send t getRow) (send p getRow))]) ; The row offset between the two tiles.
(define (cg)
(if (and (not (= xOff 0)) (not (= yOff 0)))
14 ; If the movement from the current tile to this neighbor is a diagonal one, the cost is 14...
10)) ; ...otherwise, its cost is 10.
(cg)))

; Compute H score. ((vertical offset + horizontal offset) * 10)
(define (compH t e)
(let ([xOff (abs (- (send t getCol) (send e getCol)))] ; Horizontal offset from goal tile.
[yOff (abs (- (send t getRow) (send e getRow)))]) ; Vertical offset from goal tile.
(define (ch)
(* (+ xOff yOff) 10)) ; Multiply the sum of the two offsets by 10 to get the H score.
(ch)))

; Compute F score. (G score + H score)
(define (compF g h)
(+ g h))

; Class to represent grid tiles.
(define tile%
(class object%
(init row col walkable)
(super-new)

(define myRow row) ; The row that this tile is in.
(define myCol col) ; The column that this tile is in.
(define canWalk walkable) ; Can the "player" traverse this tile?
(define parent nil) ; Used to determine which tile this tile was a neighbor of.

(define F 0) ; This particular tiles F score. (G+H)
(define G 0) ; This tile's G score. (10 or 14, depending on movement to get to this tile)
(define H 0) ; This tile's H score. ((vertical offset + horizontal offset) * 10)

; Field getters/setters.
(define/public (getRow)
myRow)
(define/public (getCol)
myCol)

(define/public (isWalkable)
canWalk)
(define/public (setWalk v)
(set! canWalk v))

(define/public (hasParent)
(not (equal? parent nil)))
(define/public (getParent)
parent)
(define/public (setParent p)
(set! parent p))

(define/public (getF)
F)
(define/public (setF f)
(set! F f))
(define/public (getG)
G)
(define/public (setG g)
(set! G g))
(define/public (getH)
H)
(define/public (setH h)
(set! H h))

; Draws the appropriate color for the tile.
(define/public (draw)
(cond ((not canWalk)
(begin (glColor3f 0.4 0.4 0.4) ; If this is a wall, make the color of the tile gray.
(drawTile myRow myCol)))
((and (= myRow (cdr start)) (= myCol (car start)))
(begin (glColor3f 0.0 1.0 0.0) ; If this is the start tile, make the color green.
(drawTile myRow myCol)))
((and (= myRow (cdr goal)) (= myCol (car goal)))
(begin (glColor3f 1.0 0.0 0.0) ; If this is the goal tile, make the color red.
(drawTile myRow myCol)))
(else #f)))))
(require "constants.rkt")
(require "tile.rkt")

; Used to access elements of a one-dimensional array representing a two-dimensional array.
(define (get row col)
Expand Down Expand Up @@ -208,31 +88,31 @@
; (unless (equal? current ((get (cdr start) (car start)) GRID)) (pathfind)))))))

; Helper function for drawing tiles at a given row and column.
(define (drawTile r c)
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF) (* r TILE_SIZE) 0.0) ; Draw the vertex at the top-left of the box.
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF TILE_SIZE) (* r TILE_SIZE) 0.0) ; Draw the vertex at the top-right of the box.
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF TILE_SIZE) (+ (* r TILE_SIZE) TILE_SIZE) 0.0) ; Draw the vertex at the bottom-right of the box.
(glVertex3f (+ (* c TILE_SIZE) GRID_OFF) (+ (* r TILE_SIZE) TILE_SIZE) 0.0)) ; Draw the vertex at the bottom-left of the box.

; Used to move the player around the grid.
(define (move rOff cOff)
(set! player (cons (+ (car player) rOff) (+ (cdr player) cOff))) ; Modifies the player's position to reflect the specified movement.
(play (list-ref clips (random 4)))) ; Plays one of the guitar sound clips.

; Called after the window renders. Used to update objects.
(define (update)
(if (>= A_TIMER 1/2) ; We want the player to move every 0.5 seconds, so the timer stops at 1/2 second and then acts.
(begin (set! A_TIMER 0) ; Reset the timer, then check to see where the player is and move accordingly.
(cond ((< (car player) (car goal)) ; If the player is above the goal, move them one down.
(move 1 0)) ; Move down.
((> (car player) (car goal)) ; If the player is below the goal, move them one up.
(move -1 0)) ; Move up.
((< (cdr player) (cdr goal)) ; If the player is to the left of the goal, move them one to the right.
(move 0 1)) ; Move right.
((> (cdr player) (cdr goal)) ; If the player is to the right of the goal, move them one to the left.
(move 0 -1)) ; Move left.
(else #f)))
(set! A_TIMER (+ A_TIMER 1/60)))) ; If a half a second hasn't gone by yet, add the time delay to the timer.
;(define (drawTile r c)
; (glVertex3f (+ (* c TILE_SIZE) GRID_OFF) (* r TILE_SIZE) 0.0) ; Draw the vertex at the top-left of the box.
; (glVertex3f (+ (* c TILE_SIZE) GRID_OFF TILE_SIZE) (* r TILE_SIZE) 0.0) ; Draw the vertex at the top-right of the box.
; (glVertex3f (+ (* c TILE_SIZE) GRID_OFF TILE_SIZE) (+ (* r TILE_SIZE) TILE_SIZE) 0.0) ; Draw the vertex at the bottom-right of the box.
; (glVertex3f (+ (* c TILE_SIZE) GRID_OFF) (+ (* r TILE_SIZE) TILE_SIZE) 0.0)) ; Draw the vertex at the bottom-left of the box.

;; Used to move the player around the grid.
;(define (move rOff cOff)
; (set! player (cons (+ (car player) rOff) (+ (cdr player) cOff))) ; Modifies the player's position to reflect the specified movement.
; (play (list-ref clips (random 4)))) ; Plays one of the guitar sound clips.

;; Called after the window renders. Used to update objects.
;(define (update)
; (if (>= A_TIMER 1/2) ; We want the player to move every 0.5 seconds, so the timer stops at 1/2 second and then acts.
; (begin (set! A_TIMER 0) ; Reset the timer, then check to see where the player is and move accordingly.
; (cond ((< (car player) (car goal)) ; If the player is above the goal, move them one down.
; (move 1 0)) ; Move down.
; ((> (car player) (car goal)) ; If the player is below the goal, move them one up.
; (move -1 0)) ; Move up.
; ((< (cdr player) (cdr goal)) ; If the player is to the left of the goal, move them one to the right.
; (move 0 1)) ; Move right.
; ((> (cdr player) (cdr goal)) ; If the player is to the right of the goal, move them one to the left.
; (move 0 -1)) ; Move left.
; (else #f)))
; (set! A_TIMER (+ A_TIMER 1/60)))) ; If a half a second hasn't gone by yet, add the time delay to the timer.

; Render everything to the frame.
(define (draw-gl)
Expand Down

0 comments on commit b29c572

Please sign in to comment.