📜 ⬆️ ⬇️

Scheme (Lisp) Games in DrRacket

This article uses the DrRacket development environment . To begin, consider the relationship of the state machine and the gameplay. The control object in the game can be represented as a finite state machine. Consider a program that simulates a traffic light. This example was described in a previous article .

The transition to another steady state is the switching signal of the traffic light. The state diagram can be represented as follows.

image

In order to create a traffic light, draw a circle in the center of an empty scene.
')
#lang racket (require picturing-programs) (define (DOT s) (circle 100 "solid" s)) 

s is a variable responsible for color. The transition to another state can be represented
following construction:

 (define (traffic-light-next s) (cond [(string=? "red" s) "green"] [(string=? "green" s) "yellow"] [(string=? "yellow" s) "red"])) 

In order to simulate the switching signal, use the function big-bang.
 (big-bang "red" [on-tick traffic-light-next 1] [to-draw DOT]) 

Now the traffic light goes to the next steady state 1 time per second.
Full text of the program
 #lang racket (require picturing-programs) (define (DOT s) (circle 100 "solid" s)) (define (traffic-light-next s) (cond [(string=? "red" s) "green"] [(string=? "green" s) "yellow"] [(string=? "yellow" s) "red"])) (big-bang "red" [on-tick traffic-light-next 1] [to-draw DOT]) 


Platforms, obstacles, enemies, etc. can also have control over games. For example, in some games, an object may “jump” to the next canvas, i.e. approaching the border of the canvas, the object disappears and appears on the opposite border. The transition condition (“skipping”) is determined by comparing the coordinates of the object and the edge of the canvas.

  [(> (+ x DELTA) WIDTH) 0] 

If the condition is not met, we remain on the same screen.

 (cond [(> (+ x dx) WIDTH) 0] [else (+ x dx)] ) 

Motion is determined by incrementing DELTA to the x coordinate. Let's write the program entirely:

 #lang racket (require 2htdp/image) (require 2htdp/universe) (define WIDTH 100) (define DELTA 1) (define BALL (circle 5 "solid" "red")) (define MT (empty-scene WIDTH 10)) (define (main x0) (big-bang x0 [to-draw render] [on-tick bounce])) (define (bounce x) (cond [(> (+ x DELTA) WIDTH) 0] [else (+ x DELTA)] )) (define (render x) (place-image BALL x 5 MT)) (main 50) 

This page provides examples of programs using big-bang. You can run programs in the "beginner student" mode by adding the necessary packages.

Next, write a program in which the control object
keys "left" and "right".

Here we need a function for handling the keys.

 #lang racket (require 2htdp/image) (require 2htdp/universe) (define BACKGROUND (empty-scene 100 100)) (define DOT (circle 10 "solid" "red")) (define (place-dot-at x) (place-image DOT x 50 BACKGROUND)) (define (change-func pk) ;   (cond [(string=? "left" k) (- p 5)] [(string=? "right" k) (+ p 5)] [else p])) ( big-bang 50 [to-draw place-dot-at] [on-key change-func] ) 

If we also need to handle the keystrokes “up”, “down”, then the coordinates of the object should be stored in the structure of the form:

 (define-struct posn (xy)) (define INIT-WORLD (make-posn 100 100)) 

We write a program in which the object can move horizontally and vertically.

 #lang racket (require picturing-programs) (define WIDTH 200) (define HEIGHT 200) (define BACKGROUND (empty-scene WIDTH HEIGHT)) (define obj1 (circle 10 "solid" "red")) (define-struct posn (xy)) ;  (define INIT-WORLD (make-posn 100 100) ) (define (change-current-world-key current-world a-key-event) ; ""  (cond [(key=? a-key-event "up") (make-posn (posn-x current-world) (- (posn-y current-world) 5))] [(key=? a-key-event "down") (make-posn (posn-x current-world) (+ (posn-y current-world) 5))] [(key=? a-key-event "left") (make-posn (-(posn-x current-world)5) (posn-y current-world) )] [(key=? a-key-event "right") (make-posn (+(posn-x current-world)5) (posn-y current-world) )] [else current-world])) (define (redraw current-world) (place-image obj1 (posn-x current-world) (posn-y current-world) BACKGROUND)) (big-bang INIT-WORLD (on-key change-current-world-key) (on-draw redraw) ) 

Yes, but usually there are several objects in games. We write a program in which there are two objects. We use two structures: world and posn.

 #lang racket (require picturing-programs) (define WIDTH 300) (define HEIGHT 300) (define BACKGROUND (empty-scene WIDTH HEIGHT)) (define obj1 (circle 10 "solid" "red")) (define obj2 (circle 10 "solid" "green")) (define-struct posn (xy)) (define-struct world [obj1 obj2]) ;     world (define INIT-WORLD (make-world (make-posn 100 100) (make-posn 200 100))) (define (draw-game world) (place-image obj1 (posn-x (world-obj1 world)) (posn-y (world-obj1 world)) (place-image obj2 (posn-x (world-obj2 world)) (posn-y (world-obj2 world)) BACKGROUND))) (big-bang INIT-WORLD [to-draw draw-game]) 

Now, in order to manage one of the objects, add the function of processing keys.

 #lang racket (require picturing-programs) (define WIDTH 300) (define HEIGHT 300) (define BACKGROUND (empty-scene WIDTH HEIGHT)) (define obj1 (circle 10 "solid" "red")) (define obj2 (circle 10 "solid" "green")) (define-struct posn (xy)) (define-struct world [obj1 obj2]) (define INIT-WORLD (make-world (make-posn 50 150) (make-posn 250 150))) (define (change-current-world-key current-world a-key-event) (make-world (change-obj1 (world-obj1 current-world) a-key-event) (world-obj2 current-world))) (define (change-obj1 a-posn a-key-event) (cond [(key=? a-key-event "up") (make-posn (posn-x a-posn) (- (posn-y a-posn) 5))] [(key=? a-key-event "down") (make-posn (posn-x a-posn) (+ (posn-y a-posn) 5))] [(key=? a-key-event "left") (make-posn (-(posn-x a-posn) 5) (posn-y a-posn) )] [(key=? a-key-event "right") (make-posn (+(posn-x a-posn)5) (posn-y a-posn) )] [else a-posn])) (define (draw-game world) (place-image obj1 (posn-x (world-obj1 world)) (posn-y (world-obj1 world)) (place-image obj2 (posn-x (world-obj2 world)) (posn-y (world-obj2 world)) BACKGROUND))) (big-bang INIT-WORLD (on-key change-current-world-key) [to-draw draw-game] ) 

More information about creating games can be found in the book How to Design Worlds .

Source: https://habr.com/ru/post/328924/


All Articles