首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多形状检测Smallbasic

多形状检测Smallbasic
EN

Stack Overflow用户
提问于 2018-02-02 02:29:54
回答 1查看 216关注 0票数 0

我想为游戏制作一个可控制的角色,但我不希望地板是一个形状。它将由多个形状组成,并具有不同的y和x位置。每个形状被称为矩形x和y,范围从1到40,形成一个20x20的网格。我做的唯一的事情就是一个带有碰撞检测的形状。但是我真的希望能够一次拥有所有的形状,我需要做1600个不同的If语句,还是有一个更简单的替代方案?最终的程序将希望能够有关卡创建,然后能够玩完关卡,但我现在只做了关卡创建。下面是一个使用一个形状的重力程序示例。

代码语言:javascript
复制
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提前表示感谢

EN

回答 1

Stack Overflow用户

发布于 2018-02-09 01:24:49

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

代码语言:javascript
复制
'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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48569511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档