回 帖 发 新 帖 刷新版面

主题:[原创]青蛙过河(游戏)

DECLARE SUB Show (row!, col!)
DECLARE SUB Clr (row!, col!)
DECLARE SUB MoveZ (s!)
DECLARE SUB MoveM (md!)
DECLARE SUB Lose ()
DECLARE SUB Win ()
CONST Up = -2, Down = 2, LEFT = -1, RIGHT = 1
CONST Quick = 1, Normal = 2, Slow = 3, ToTurn = Quick * Normal * Slow
CONST R0 = 3, C0 = 30          '左上角位置

KeyUp$ = CHR$(0) + CHR$(72)
KeyDown$ = CHR$(0) + CHR$(80)
KeyLeft$ = CHR$(0) + CHR$(75)
KeyRight$ = CHR$(0) + CHR$(77)
KeySpace$ = CHR$(32)
KeyEnter$ = CHR$(13)
KeyEsc$ = CHR$(27)

DIM SHARED room(4 TO 20, 0 TO 11) AS INTEGER
DIM SHARED mrow, mcol, mc

CLS
RANDOMIZE TIMER
mrow = 18: mcol = INT(10 * RND + 1)  '产生一个1-10的列数
COLOR 10
CALL Show(mrow, mcol)

REM 边界标记为1,阻碍物标记为1,目标无标记(0)
FOR row = 4 TO 20
  room(row, 0) = 1
  room(row, 11) = 1
NEXT row
FOR col = 1 TO 10
  room(20, col) = 1
NEXT col

REM 画出边界
COLOR 11
LOCATE R0, C0 + 1: PRINT CHR$(201) + STRING$(20, 205) + CHR$(187)
FOR row = 1 TO 4
  LOCATE R0 + row, C0 + 1: PRINT CHR$(186) + STRING$(20, 32) + CHR$(186)
NEXT row
FOR row = 5 TO 17 STEP 2
  LOCATE R0 + row, C0 + 1: PRINT CHR$(204) + STRING$(20, 205) + CHR$(185)
  LOCATE R0 + row + 1, C0 + 1: PRINT CHR$(186) + STRING$(20, 32) + CHR$(186)
NEXT row
LOCATE R0 + 19, C0 + 1: PRINT CHR$(200) + STRING$(20, 205) + CHR$(188)

REM 画出阻碍物  --移动的方块
COLOR 12
FOR row = 6 TO 16 STEP 2
  FOR col = 1 TO 10
    READ room(row, col)
    IF room(row, col) = 1 THEN CALL Show(row, col) ELSE CALL Clr(row, col)
  NEXT col
  FOR col = 1 TO 3
    READ room(row + 1, col)
  NEXT col
NEXT row
DATA 1,1,0,0,0,1,1,0,0,0,3,-1,5
DATA 1,1,0,0,1,1,0,0,1,1,2, 1,4
DATA 1,1,1,0,0,1,1,1,0,0,3,-1,5
DATA 1,1,0,0,0,1,1,0,0,0,2, 1,5
DATA 1,1,0,0,1,1,0,0,1,1,1,-1,4
DATA 1,1,1,0,0,0,1,1,1,0,3, 1,6
REM DATA数据说明前10个1,0为有无阻碍的方块,后三个分别为速度,方向,周期
REM 第1个DATA中1,1,0,0,0为一周期 五个元素
REM 第2个DATA中1,1,0,0  为一周期 四个元素

REM Quick = 1, Normal = 2, Slow = 3, ToTurn = Quick * Normal * Slow
REM ToTurn本应为Quick, Normal, Slow的最小公倍数,这里以乘积代替
REM zc  0  1  2  3  4  5  6  7  8  9 ...
REM     Q  Q  Q  Q  Q  Q  Q  Q  Q  Q ...
REM     N     N     N     N     N    ...
REM     S        S        S        S ...
REM 以上表示ZC依次取0-9时那种速度的方块应该移动一格,以形成不同的速度的方块
REM 可见ZC是以0-5为一周期(Q, N, S的最小公倍数为6)
REM MoveZ(Quick)就表示移动速度为Quick的那一行方块
zc = 0
DO
  IF zc MOD Quick = 0 THEN CALL MoveZ(Quick)
  IF zc MOD Normal = 0 THEN CALL MoveZ(Normal)
  IF zc MOD Slow = 0 THEN CALL MoveZ(Slow)
  zc = zc + 1: IF zc = ToTurn THEN zc = 0

  tim = TIMER + .5  '改变.5 就可改变游戏速度
  DO
    key$ = INKEY$
    SELECT CASE key$
      CASE KeyUp$, KeySpace$
        CALL MoveM(Up)
      CASE KeyDown$
        CALL MoveM(Down)
      CASE KeyLeft$
        CALL MoveM(LEFT)
      CASE KeyRight$
        CALL MoveM(RIGHT)
      CASE KeyEnter$
        DO WHILE INKEY$ = ""
        LOOP
      CASE KeyEsc$
        END
    END SELECT
  LOOP WHILE TIMER < tim
LOOP
END

回复列表 (共6个回复)

沙发

SUB MoveM (md)
  COLOR 10
  IF ABS(md) = 1 THEN         '如果是左右移动
    IF room(mrow, mcol + md) = 0 THEN
      CALL Clr(mrow, mcol)
      mcol = mcol + md
      CALL Show(mrow, mcol)
    END IF
  ELSEIF ABS(md) = 2 THEN     '如果是上下移动
    IF room(mrow + md, mcol) = 0 THEN
      CALL Clr(mrow, mcol)
      mrow = mrow + md
      CALL Show(mrow, mcol)
     
      IF mrow = 4 THEN        '如果移动到最上一层
        room(mrow, mcol) = 1
        mc = mc + 1: IF mc = 5 THEN CALL Win
                              '如果有五个移动到最上一层,则胜利
        mrow = 18: mcol = INT(10 * RND + 1)
        CALL Show(mrow, mcol) '产生一个新目标
      END IF
    END IF
  END IF
END SUB

SUB MoveZ (s)
  COLOR 12
  FOR zrow = 6 TO 16 STEP 2
    zs = room(zrow + 1, 1)     'z.speed
    IF zs = s THEN
      zd = room(zrow + 1, 2)   'z.direction
      zl = room(zrow + 1, 3)   'z.lenth

      REM 移动ROOM中标记
      IF zd = LEFT THEN
        FOR zcol = 1 TO 9
          room(zrow, zcol) = room(zrow, zcol + 1)
        NEXT zcol
        room(zrow, 10) = room(zrow, 9 - zl)
      ELSEIF zd = RIGHT THEN
        FOR zcol = 10 TO 2 STEP -1
          room(zrow, zcol) = room(zrow, zcol - 1)
        NEXT zcol
        room(zrow, 1) = room(zrow, 2 + zl)
      END IF

      REM 按ROOM中标记,重新输出阻碍方块
      FOR zcol = 1 TO 10
        IF room(zrow, zcol) <> 0 THEN
          CALL Show(zrow, zcol)
        ELSE
          CALL Clr(zrow, zcol)
        END IF
      NEXT zcol
    END IF
  NEXT zrow
  IF room(mrow, mcol) = 1 THEN CALL Lose
  
  REM 刷新阻碍物时 擦掉了目标
  COLOR 10: CALL Show(mrow, mcol)
END SUB

SUB Show (row, col)
  LOCATE R0 + row, C0 + col * 2: PRINT "[]"
END SUB

SUB Clr (row, col)
  LOCATE R0 + row, C0 + col * 2: PRINT "  "
END SUB

SUB Win
  LOCATE 23, 1: PRINT "Win"
  END
END SUB

SUB Lose
  LOCATE 23, 1: PRINT "Lose"
  END
END SUB 

板凳

程序中字符说明
z    阻碍物    在每一层飘动的 方块
m    目标      自己控制的 方块
d    direction 方向
c    count     记数
l    lenth     长度
s    speed     速度
row            行
col            列
其他由以上字符组成:
有 zd,zc,zl,zs,zrow,zcol
及 md,mc,mrow,mcol

3 楼

好像不对

4 楼

强!![em11]

5 楼

[em2]

6 楼

不好运行

我来回复

您尚未登录,请登录后再回复。点此登录或注册