DynaBlaster: Htdp version of Dynablaster game
1 How to play
To run the game, type
(require (planet jeeve/dynablaster:1:14)) (run) ; or (run #:sound #f) to play without sound effect
Use left, right, up and down keys to move blue player in the scene.
Drop bombs with space key and attack the red player.
Good luck !
If you want to see the computer play alone, type
(require (planet jeeve/dynablaster:1:14)) (run move-robot-default move-robot-default)
2 Game configuration
(run #:config (game-size 15 10))
3 Customize the behavior of the robot
(run my-move-robot-function)
Before, define a custom move-robot function. For example :
(define (my-move-robot-function f-r d c) (let ([r (f-r d)]) (if (empty? r) ; robot is not dead ? d (if (empty? (sprite-left r (lambda (x) #t) d c)) ; something at left of robot ? (go-left r d c) ; go to left ! d))))
Also, you can start the game in automatically mode, the blue player is controlled by a function like the robot. So you can easily compare the behavior of two algorithms.
(run my-move-robot-function my-move-player-function)
You can use these functions and predicates :
The player and robot sprite : player robot
Sprite informations : sprite-x sprite-y sprite-dx sprite-dy sprite-energy
Sprite actions : go-left go-right go-up go-down drop-bomb
Sprite predicates : bomb? fire? player? robot? blocked? brick? rock?
Sprite search : sprite-left sprite-right sprite-up sprite-down search-sprite
Distance between two sprites : distance
Default move robot function : move-robot-default
Do not hesitate to send me your move-robot functions (jeeve <[email protected]>)
4 Default move-robot function
For information, the default move-robot has only random and defensive behavior. The code corresponding is
(define (move-robot-default f-r d c) (let* ([r (f-r d)] [x (random 100)] [danger? (lambda (x) (or (bomb? x) (fire? x)))] [something? (lambda (x) #t)] [escape (lambda (r d) (cond [(not (empty? (sprite-left r danger? d c))) (go-right r d c)] [(not (empty? (sprite-right r danger? d c))) (go-left r d c)] [(not (empty? (sprite-up r danger? d c))) (go-down r d c)] [(not (empty? (sprite-down r danger? d c))) (go-up r d c)] [(not (empty? (search-sprite (sprite-x r) (sprite-y r) bomb? d c))) (cond [(empty? (sprite-left r something? d c)) (go-left r d c)] [(empty? (sprite-right r something? d c)) (go-right r d c)] [(empty? (sprite-up r something? d c)) (go-up r d c)] [(empty? (sprite-down r something? d c)) (go-down r d c)] [else d])]))]) (if (empty? r) d (cond ; fear of fire [(or (not (empty? (sprite-left r danger? d c))) (not (empty? (sprite-right r danger? d c))) (not (empty? (search-sprite (sprite-x r) (sprite-y r) bomb? d c)))) (if (empty? (sprite-up r something? d c)) (go-up r d c) (if (empty? (sprite-down r something? d c)) (go-down r d c) (escape r d)))] [(or (not (empty? (sprite-up r danger? d c))) (not (empty? (sprite-down r danger? d c))) (not (empty? (search-sprite (sprite-x r) (sprite-y r) bomb? d c)))) (if (empty? (sprite-left r something? d c)) (go-left r d c) (if (empty? (sprite-right r something? d c)) (go-right r d c) (escape r d)))] ; intention [(and (> x 0) (<= x 10) (empty? (sprite-up r fire? d c))) (go-up r d c)] [(and (> x 10) (<= x 20) (empty? (sprite-down r fire? d c))) (go-down r d c)] [(and (> x 20) (<= x 30) (empty? (sprite-left r fire? d c))) (go-left r d c)] [(and (> x 30) (<= x 40) (empty? (sprite-right r fire? d c))) (go-right r d c)] [(and (> x 40) (<= x 42) (not (blocked? r d c))) (drop-bomb r d c)] [else d]))))