DynaBlaster: Htdp version of Dynablaster game
1 How to play
To run the game, type
(require (planet "dynablaster.rkt" ("jeeve" "dynablaster.plt" 1 11))) |
(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 "dynablaster.rkt" ("jeeve" "dynablaster.plt" 1 11))) |
(run move-robot-default move-robot-default) |
2 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) |
(let ([r (f-r d)]) |
(if (empty? r) ; robot is not dead ? |
d |
(if (empty? (sprite-left r (lambda (x) #t) d)) ; something at left of robot ? |
(go-left r d) ; 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]>)
3 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) |
(let* ([r (f-r d)]; robot in decor 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))) (go-right r d)] |
[(not (empty? (sprite-right r danger? d))) (go-left r d)] |
[(not (empty? (sprite-up r danger? d))) (go-down r d)] |
[(not (empty? (sprite-down r danger? d))) (go-up r d)] |
[(not (empty? (search-sprite (sprite-x r) (sprite-y r) bomb? d))) |
(cond [(empty? (sprite-left r something? d)) (go-left r d)] |
[(empty? (sprite-right r something? d)) (go-right r d)] |
[(empty? (sprite-up r something? d)) (go-up r d)] |
[(empty? (sprite-down r something? d)) (go-down r d)] |
[else d])]))]) |
(if (empty? r) |
d |
(cond |
; fear of fire |
[(or (not (empty? (sprite-left r danger? d))) |
(not (empty? (sprite-right r danger? d))) |
(not (empty? (search-sprite (sprite-x r) (sprite-y r) bomb? d)))) |
(if (empty? (sprite-up r something? d)) |
(go-up r d) |
(if (empty? (sprite-down r something? d)) |
(go-down r d) |
(escape r d)))] |
[(or (not (empty? (sprite-up r danger? d))) |
(not (empty? (sprite-down r danger? d))) |
(not (empty? (search-sprite (sprite-x r) (sprite-y r) bomb? d)))) |
(if (empty? (sprite-left r something? d)) |
(go-left r d) |
(if (empty? (sprite-right r something? d)) |
(go-right r d) |
(escape r d)))] |
; intention |
[(and (> x 0) (<= x 10) (empty? (sprite-up r fire? d))) (go-up r d)] |
[(and (> x 10) (<= x 20) (empty? (sprite-down r fire? d))) (go-down r d)] |
[(and (> x 20) (<= x 30) (empty? (sprite-left r fire? d))) (go-left r d)] |
[(and (> x 30) (<= x 40) (empty? (sprite-right r fire? d))) (go-right r d)] |
[(and (> x 40) (<= x 42) (not (blocked? r d))) (drop-bomb r d)] |
[else d])))) |