Racket BSL中的编程突破
我目前正试图在BSL中编写“突破”代码,但我在编写代码时遇到了困难 不知道如何在我的代码中添加欢迎和游戏。它是 假设游戏从欢迎屏幕开始,并在何时开始 用鼠标点击屏幕上的实际游戏应该开始。什么时候 球与屏幕的下边缘相撞,游戏在屏幕上进行 是应该出现的。 我感谢你给我的任何提示Racket BSL中的编程突破,racket,breakout,Racket,Breakout,我目前正试图在BSL中编写“突破”代码,但我在编写代码时遇到了困难 不知道如何在我的代码中添加欢迎和游戏。它是 假设游戏从欢迎屏幕开始,并在何时开始 用鼠标点击屏幕上的实际游戏应该开始。什么时候 球与屏幕的下边缘相撞,游戏在屏幕上进行 是应该出现的。 我感谢你给我的任何提示 (define WIDTH 200) (define HEIGHT 200) (define BALL-RADIUS 10) (define BALL-IMG (circle BALL-RADIUS "so
(define WIDTH 200)
(define HEIGHT 200)
(define BALL-RADIUS 10)
(define BALL-IMG (circle BALL-RADIUS "solid" "red"))
(define MT (empty-scene WIDTH HEIGHT))
(define GAME-OVER
(place-image (text "Game-over" 30 "black")
100 100
MT))
(define WELCOME
(place-image (text "Welcome" 30 "black")
100 100
MT))
(define-struct vel (delta-x delta-y))
; a Vel is a structure: (make-vel Number Number)
; interp. the velocity vector of a moving object
(define-struct ball (loc velocity))
; a Ball is a structure: (make-ball Posn Vel)
; interp. the position and velocity of a object
(define RACKET (rectangle 30 10 "solid" "grey"))
(define-struct world-state (ball racket))
; A WorldState is a structure. (make-world-state Ball Location of
Racket)
; interp. current velocity and location of ball, current location of
racket
; Posn Vel -> Posn
; applies q to p and simulates the movement in one clock tick
(check-expect (posn+vel (make-posn 5 6) (make-vel 1 2))
(make-posn 6 8))
(define (posn+vel p q)
(make-posn (+ (posn-x p) (vel-delta-x q))
(+ (posn-y p) (vel-delta-y q))))
; Ball -> Ball
; computes movement of ball in one clock tick
(define (move-ball ball)
(make-ball (posn+vel (ball-loc ball)
(ball-velocity ball))
(ball-velocity ball)))
; A Collision is either
; - "top"
; - "down"
; - "left"
; - "right"
; - "racket"
; - "none"
; interp. the location where a ball collides with a wall
; Posn -> Collision
; detects with which of the walls (if any) or the racket the ball
collides
(define (collision world-state)
(cond
[(<= (posn-x (ball-loc (world-state-ball world-state))) BALL-
RADIUS) "left"]
[(<= (posn-y (ball-loc (world-state-ball world-state))) BALL-
RADIUS) "top"]
[(>= (posn-x (ball-loc (world-state-ball world-state))) (- WIDTH
BALL-RADIUS)) "right"]
[(>= (posn-y (ball-loc (world-state-ball world-state))) (- HEIGHT
BALL-RADIUS)) "down"]
[(and (>= (posn-y (ball-loc (world-state-ball world-state))) (-
HEIGHT BALL-RADIUS 10))
(<= (- (posn-x (world-state-racket world-state)) 15)
(posn-x (world-state-racket world-state))
(+ (posn-x (world-state-racket world-state)) 15)))
"racket"]
[else "none"]))
; Vel Collision -> Vel
; computes the velocity of an object after a collision
(define (bounce vel collision)
(cond [(or (string=? collision "left")
(string=? collision "right"))
(make-vel (- (vel-delta-x vel))
(vel-delta-y vel))]
[(or (string=? collision "top")
(string=? collision "racket"))
(make-vel (vel-delta-x vel)
(- (vel-delta-y vel)))]
[else vel]))
; render
; WorldState -> Image
; renders ball and
racket at their position
(check-expect (image? (render INITIAL-STATE)) #true)
(define (render world-state)
(place-image BALL-IMG
(posn-x (ball-loc (world-state-ball world-state)))
(posn-y (ball-loc (world-state-ball world-state)))
(place-image RACKET
(posn-x (world-state-racket world-state))
195
(empty-scene WIDTH HEIGHT))))
;tick
; WorldState -> WorldState
; moves ball to its next location
(check-expect (tick INITIAL-STATE) (make-world-state (make-ball (make-posn 21 14) (make-vel 1 2)) (make-posn 20 195)))
(define (tick world-state)
(make-world-state (move-ball (make-ball (ball-loc (world-state-ball world-state))
(bounce (ball-velocity (world-state-ball world-state))
(collision world-state))))
(world-state-racket world-state)))
; A Location is a structure: (make-posn Number Number)
; interp. x and y coordinate of a location on screen.
(define Loc (make-posn 1 1))
; A MouseEvent is one of
; - "button-down"
; - "button-up"
; - "drag"
; - "move"
; - "enter"
; - "leave"
; interp. mouse events, e.g., mouse movements or mouse clicks
(define MOUSE-CLICK "button-down")
; mouse
;
; Game Number Number MouseEvent -> WorldState
; Update position of racket when the mouse moves
;mouse-template
(define (mouse-template world-state mouse-loc-x mouse-loc-y MouseEvent)
(cond
((string=? MouseEvent "button-down")...)
((string=? MouseEvent "button-up")...)
((string=? MouseEvent "drag")...)
((string=? MouseEvent "move")...)
((string=? MouseEvent "enter")...)
((string=? MouseEvent "leave")...)
(else ...)))
(define (mouse world-state mouse-loc-x mouse-loc-y MouseEvent)
(cond
[(and (string=? MouseEvent "move")
(>= mouse-loc-y 180))
(make-world-state (world-state-ball world-state)
(make-posn mouse-loc-x 195))]
[else world-state]))
(define INITIAL-BALL (make-ball (make-posn 20 12)
(make-vel 1 2)))
(define INITIAL-RACKET (make-posn 20 195))
(define INITIAL-STATE (make-world-state INITIAL-BALL INITIAL-RACKET))
; WorldState -> WorldState
; Starts the game
(define (main state)
(big-bang state
(on-tick tick 0.01)
(to-draw render)
(on-mouse mouse)))
; start with INITIAL-STATE
(定义宽度200)
(定义高度200)
(定义球半径10)
(定义BALL-IMG(圆球半径“实心”“红色”))
(定义MT(空场景宽度高度))
(定义游戏结束)
(将图像(文本“游戏”置于“30”黑色上方)
100 100
MT)
(定义欢迎
(放置图像(文本“欢迎”30“黑色”)
100 100
MT)
(定义结构级别(delta-x delta-y))
; Vel是一种结构:(使Vel编号)
; 英普。运动物体的速度矢量
(定义结构球(定位速度))
; 球是一种结构:(使球处于水平位置)
; 英普。物体的位置和速度
(定义球拍(矩形30 10“实心”“灰色”))
(定义结构世界状态(球拍))
; 世界国家是一种结构。(使世界陈述球的位置。)
(球拍)
; 英普。球的当前速度和位置,球的当前位置
球拍
; Posn-Vel->Posn
; 将q应用于p,并在一个时钟周期内模拟移动
(检查预期(位置+标高(位置5-6)(标高1-2))
(做出姿势6-8)
(定义(posn+vel p q)
(制作posn(+(posn-xp)(vel-delta-xq))
(+(posn-yp)(vel-delta-yq)))
; 球->球
; 在一个时钟周期内计算球的运动
(定义(移动球)
(制造钢球(位置+水平(钢球定位钢球)
(球速度球)
(球速度球)
; 碰撞是
; - “顶部”
; - “向下”
; - “左”
; - “对”
; - “球拍”
; - “没有”
; 英普。球与墙碰撞的位置
; Posn->碰撞
; 检测用哪一面墙(如果有的话)或球拍击球
碰撞
(定义(碰撞世界状态)
(续)
[(=(位置N-y(球位置(世界状态球世界状态)))(-高度
球半径);“向下”]
[(和(>=(位置N-y(球位置(世界状态球世界状态)))(-
高度(球半径10))
(水平
;计算碰撞后对象的速度
(定义(反弹水平碰撞)
(条件[(或(字符串=?碰撞“左”)
(字符串=?冲突“右”))
(制造标高(-(标高-delta-x标高))
(vel-delta-y vel))]
[(或(字符串=?碰撞“顶部”)
(字符串=?碰撞“球拍”))
(制造标高(标高-delta-x标高)
((vel-delta-y vel))]
[其他级别])
;渲染
;世界状态->图像
;渲染球和
在他们的位置上拍打
(选中expect(图像?(渲染初始状态))#true)
(定义(渲染世界状态)
(将图像球放置在IMG上
(posn-x(球位置(世界状态球世界状态)))
(posn-y(球位置(世界状态球世界状态)))
(放置影像球拍)
(posn-x(世界国家球拍世界国家))
195
(空场景宽度高度)))
打上钩
;世界国家->世界国家
;将球移动到下一个位置
(选中expect(勾选初始状态)(生成世界状态(生成球(生成位置21 14)(生成级别12))(生成位置20 195)))
(定义(勾选世界状态)
(创建世界状态(移动球(创建球位置(世界状态球世界状态))
(反弹(球速度(世界状态球世界状态))
(世界国家)
(世界国家球拍世界国家)
;位置是一个结构:(生成posn编号)
;屏幕上某个位置的x和y坐标。
(定义Loc(使位置1)
;MouseEvent是其中之一
;-“按下按钮”
;-“扣上按钮”
;-“拖动”
;-“移动”
;-“输入”
“离开”
;鼠标间事件,例如鼠标移动或鼠标点击
(定义鼠标单击“向下按钮”)
老鼠
;
;游戏编号MouseEvent->WorldState
;鼠标移动时更新球拍的位置
;鼠标模板
(定义(鼠标模板世界状态mouse-loc-x mouse-loc-y MouseEvent)
(续)
((字符串=?鼠标事件“按钮按下”)…)
((字符串=?鼠标事件“按钮向上”)…)
((字符串=?鼠标事件“拖动”)…)
((字符串=?MouseEvent“move”)…)
((字符串=?MouseEvent“enter”)…)
((字符串=?MouseEvent“leave”)…)
(其他……)
(定义(鼠标世界状态mouse-loc-x mouse-loc-y MouseEvent)
(续)
[(和(字符串=?MouseEvent“move”)
(>=mouse-loc-y 180))
(创建世界状态(世界状态球世界状态)
(制作posn鼠标-loc-x 195))]
[世界其他国家])
(定义初始球(制造球(制造位置20-12)
(使级别为1级和2级)
(定义初始球拍(制造位置20 195))
(定义初始状态(使世界状态为初始球初始球拍))
;世界国家->世界国家
比赛开始
(定义(主状态)
(大爆炸状态
(勾选0.01)
(绘制渲染)
(在鼠标上)
;从初始状态开始
您现在有三种不同的状态:
- 欢迎光临
- 演奏
- 游戏结束
(define (render w)
(cond
[(welcome? w) (render-welcome w)]
[(world? w) (render-world w)]
[(game-over? w) (render-game-over w)]))