source: project/wiki/chicken-invaders-with-continuations @ 36409

Last change on this file since 36409 was 31130, checked in by Mario Domenech Goulart, 5 years ago

Chicken Invaders, with Continuations (wiki): moved to chicken-invaders-with-continuations

File size: 2.3 KB
Line 
1<enscript highlight=scheme>
2 ;;; CHICKEN Invaders bm2005-sep-03
3 ;;; Catch the eggs in your basket
4 ;;; usage: csi invader.scm
5 ;;;
6 ;;; I am trying to understand continuations.. but still not sure!
7 ;;; Author: bobmc@bobmc.net            License: GPL
8
9 (use ncurses )
10
11 (define STARTX 5)
12 (define STARTY 3)
13 (define ENDX 25)
14 (define ENDY 14)
15 (define ROW-BASKET 15)
16 (define *ypos* (+ STARTY 2))
17 (define *xpos* STARTX)
18 (define CHICK1 15)
19 (define *done* 0)
20 (define *last-key* 0)
21
22
23 ;;; main loop of the game:-
24 ;;;   cc-play -> cc-drop-eggs -> cc-basket-steps -> cc-play
25 ;;; called from main once, otherwise called from cc-basket-steps
26 (define (cc-play do-other-stuff)
27   (refresh)
28   (update-key)
29   (call/cc cc-drop-eggs))
30
31
32 (define (cc-drop-eggs do-other-stuff)
33   (mvaddstr  (- *ypos* 1)   CHICK1 "   ")
34   (mvaddstr     *ypos*      CHICK1 " 0 ")
35   (mvaddstr  (+ *ypos* 1)   CHICK1 "   ")
36   (set! *ypos* (move-egg *ypos*))
37   (call/cc cc-basket-steps))
38
39 ;;; update the egg-catching basket
40 ;;; the loop here apparently gets aborted and restarted after call/cc
41 (define (cc-basket-steps do-other-stuff)
42   (let loop ()
43     (when ( = *last-key* KEY_RIGHT)
44           ( set! *last-key* 0)
45           ( set! *xpos* ( + *xpos* 1))
46           ( set! *xpos* ( min *xpos* ENDX))
47           )
48     (when ( = *last-key* KEY_LEFT)
49           ( set! *last-key* 0)
50           ( set! *xpos* ( - *xpos* 1))
51           ( set! *xpos* ( max *xpos* STARTX))
52           )
53     (mvaddstr  ROW-BASKET *xpos* " \\__/ ")
54     (call/cc cc-play )
55     (loop)))
56
57 ;;;; look for F1, left-arrow, right-arrow
58 (define ( update-key )
59
60   (set! *last-key* ( char->integer (getch)))
61   (when (= *last-key* (KEY_F 1) )
62         (endwin)                       ; press F1 to exit
63         (exit)))
64
65 ;;; update egg dropping, keep in bounds
66 (define (move-egg y)
67   (if ( > y ENDY )
68       STARTY
69       (+ y 1 )))
70
71 ;;; initialize ncurses, start running ;;; 
72 (define (main)
73   (initscr)
74   (cbreak)
75   (raw)
76   (noecho)
77   (keypad (stdscr) #t)
78   (halfdelay 1)
79   (curs_set 0)
80   (set! *last-key* KEY_RIGHT)
81   (mvaddstr  ROW-BASKET *xpos* " \\__/ ")
82   (mvaddstr  1  15 "(_)>  (_)>")
83   (refresh)
84   (cc-play 0)
85   (endwin) )
86
87 (main)
88 (exit)
89</enscript>
90
91What use are the call/cc statements in this program? It should work exactly the same (or even better, because of less wasted resources) if they were just replaced by normal calls. -- murphy
Note: See TracBrowser for help on using the repository browser.