多形状检测 小基本



我想为游戏制作一个可控制的角色,但我不希望地板是一个形状。它将由多个形状组成,并具有不同的 y 和 x 位置。每个形状称为矩形[x][y] x和y,范围从1到40,以形成20x20的网格。我唯一设法做的是一个带有碰撞检测的形状。但我真的希望能够同时拥有所有形状,我是否必须执行 1600 个不同的 If 语句,或者是否有更简单的替代方案?最终程序有望能够创建关卡,然后能够通关,但是到目前为止我只进行了关卡创建。下面是使用一种形状的示例重力程序。

GraphicsWindow.Show()
player = Shapes.AddEllipse(10,10)
Shapes.Move(player, 100, 5)
For i = 1 To 2
  rec[i] = Shapes.AddRectangle(100,10)
  Shapes.Move(rec[i], 100*i, 500)
EndFor
GraphicsWindow.MouseDown = OnMouse
main:
  playertop = Shapes.GetTop(player)
  shapetop = Shapes.GetTop(rec[1])
  Sub OnMouse
    Shapes.Move(player, Shapes.GetLeft(player), playertop-5)
    upvel = 0.5
  EndSub

  If playertop+12 > shapetop Then
    upvel = 0
  Else
    upvel = upvel - 0.0004
  EndIf
  Shapes.Move(player, Shapes.GetLeft(player), playertop-upvel)
  Program.Delay(1)
Goto main

到目前为止的代码:https://pastebin.com/mFuy2Prd提前致谢

下面是一个管理多个对象的过度夸张的示例。除了管理多个对象外,它还显示了标准游戏循环的示例,以及如何管理帧。 不,您不需要 1600 if 语句。您需要的是两个匹配的数组来处理对象的 x 和 y 位置。然后,对于每一帧,你运行一个嵌套的 for 循环,其中 -- 每个项目与其他项目进行比较。

'Drift and bounce-- Created by codingCat aka Matthew L. Parets -- No rights reserved as long as no money is earned
'Demonstrates how to manage multiple moving objects at the same time.
'Circles are added every few ticks until the frame delay is reduced to zero.
'As the circles move around they bounce into each other
'----------------------------------------------------------------------------
'importCode: ZSK870
'At the top of your program - Initial Setup
'Stuff up here only happens once before the main loop
GraphicsWindow.Show()
GraphicsWindow.KeyDown = OnKeyDown 'Let winows know our key event is waiting for presses. 
stop = "false"
pressed = "False" 'No key pressed at the start of the program
frameRate = 50    'Number of milliseonds each frame (trip through the main program) should take
ballAdd = Clock.ElapsedMilliseconds 'Note the time for adding new balls
ballRate = 500 'Add a new ball every half second
balls[1] = Shapes.AddEllipse(5,5)
ballsX[1] = GraphicsWindow.Width
ballsY[1] = Math.GetRandomNumber(GraphicsWindow.Height)
ballSpdX[ballCnt] = (Math.GetRandomNumber(1000) / 100)-500
ballSpdY[ballCnt] = (Math.GetRandomNumber(1000) / 100)-500
ballCnt = 1
'----------------------------------------------------------------------------
'Main Program Loop
'Repeats continuously until the escape key is pressed
frameStart = Clock.ElapsedMilliseconds 'note the time to allow for frame pauses
key = "" 
While key <> "Escape" 'Keep going until the escape key is pressed
  GetKeyPress()       'Check if a key has been pressed
  MoveTheCircles()     'Update circle automatically
  FrameWait()         'Pause and wait for just a tick
endwhile
'----------------------------------------------------------------------------
'Program Close Down. Stuff that is down once after the main loop
GraphicsWindow.FontSize = 90
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.DrawText(100,GraphicsWindow.Height/3,"Good bye")
Program.Delay(2000)
Program.end()
'----------------------------------------------------------------------------
'Events and subroutines.
'Always at the bottom of your program
'No code outside of a subroutine allowed below.
Sub GetKeyPress
  'Get key press, processes all user interaction.
  'Key presses are ignored unless the event tells us that a new key has been pressed
  If pressed = "True" Then 'ignore unless the event tells us there was a press       
    key = GraphicsWindow.LastKey  'Movement keys
    pressed = "False"
  EndIf
EndSub

Sub MoveTheCircles
  If (Clock.ElapsedMilliseconds - ballAdd > ballRate) And stop <> "true" then
    ballCnt = ballCnt + 1
    GraphicsWindow.Title = "Balls = " + ballCnt
    GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
    balls[ballCnt] = Shapes.AddEllipse(5,5)
    ballsX[ballCnt] = Math.GetRandomNumber(GraphicsWindow.Width)
    ballsY[ballCnt] = Math.GetRandomNumber(GraphicsWindow.Height)
    ballSpdX[ballCnt] = (Math.GetRandomNumber(200) / 100)-1
    ballSpdY[ballCnt] = (Math.GetRandomNumber(200) / 100)-1
    ballAdd = Clock.ElapsedMilliseconds
  EndIf
  For i = 1 To ballCnt
    ballsX[i] = ballsX[i] + ballSpdX[i]
    ballsY[i] = ballsY[i] + ballSpdY[i]
    If ballsX[i] < 0 or ballsX[i] > graphicswindow.Width Then
      ballSpdX[i] = ballSpdX[i] * -1
    endif
    If ballsY[i] < 0 or ballsY[i] > graphicswindow.Height Then
      ballSpdY[i] = ballSpdY[i] * -1
    endif
    For j = 1 To ballCnt
      If i <> j Then
        If (ballsX[j] > ballsX[i] And  ballsX[j] < ballsX[i]+5 And ballsy[j] > ballsy[i] And  ballsy[j] < ballsy[i]+5) Or (ballsX[j]+5 > ballsX[i] And  ballsX[j]+5 < ballsX[i]+5 And ballsy[j] > ballsy[i] And  ballsy[j] < ballsy[i]+5) or (ballsX[j] > ballsX[i] And  ballsX[j] < ballsX[i]+5 And ballsy[j]+5 > ballsy[i] And  ballsy[j]+5 < ballsy[i]+5) Or (ballsX[j]+5 > ballsX[i] And  ballsX[j]+5 < ballsX[i]+5 And ballsy[j]+5 > ballsy[i] And  ballsy[j]+5 < ballsy[i]+5) Then
          ballSpdX[i] = ballSpdX[i] * -1
        endif
      endif     
    endfor  
    Shapes.Move(balls[i], ballsX[i], ballsY[i])
  endfor
EndSub  
Sub FrameWait
  in = "false"
  While Clock.ElapsedMilliseconds - frameStart < frameRate
    'Sit here and do nothing until the frame clock has run out.
    'This wait keeps the automatic operations, like moving the ball
    'from running too fast. 
    'This technique is better then using Program.Delay() for two reasons:
    '   1)  The wait time is based on when we started the frame, not
    '        when we ended it. In other words, when one of our trips through
    '        the for loop above takes longer than normal, the delay will be 
    '        reduced, so each frame will be exactly the same length.
    '  2) We have the option (even though we are not using it here) of  
    '        doing some extra processing, such as checking the keyboard. 
    '        With Program.Delay() we are stuck, unable to do anything,
    '       until the  delay is complete.
    in = "true"
  EndWhile
  If in <> "true" Then
    GraphicsWindow.Title = "Stopped -- Balls = " + ballCnt
    stop = "true"
  EndIf
  frameStart = Clock.ElapsedMilliseconds 'Start  a new frame.
EndSub
Sub OnKeyDown
  'Take note of the fact that a key has been pressed. This event is 
  'needed because the GraphicsWindow.lastkey property never
  'clears. Making otherwise impossible to distinguish between
  'multiple presses of the same key
  pressed = "True"
EndSub

相关内容

  • 没有找到相关文章

最新更新