0

I would like to make a controllable character for a game, however I do not wish the floor to be one shape. It will be made up of multiple shapes and have different y and x positions. Each shape is called rectangle[x][y] x and y ranging from 1 to 40 to make a 20x20 grid. The only thing i have managed to make is one shape with collision detection. But I would really like to be able to have all the shapes at once, would I have to do 1600 different If statements or is there an easier alternative? The final program will hopefully be able to have level creation and then be able to play through the level, however i've only made level creation as of now. Below is a sample gravity program using one shape.

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

Code so far: https://pastebin.com/mFuy2Prd Thanks in advance

EvilTak
  • 7,091
  • 27
  • 36
CharlieG
  • 38
  • 10

1 Answers1

0

Here is an over blown example of managing multiple objects. In addition to maning multiple objects it shows an example of a standard game loop, and how to manage frames. No, you do not need 1600 if statements. What you need is two matched arrays to handle the x and y location of your objects. Then, for each frame you run through a nested for loop where -- each item is compared against each other item.

'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
codingCat
  • 2,396
  • 4
  • 21
  • 27
  • Thank you for this response, I am working around this code to try and make it work for my purpose, currently I am struggling with making movement with the onkeydown and getkeypress as it does not detect when your finger is off of the key. Is there any way I could get keyboard controlled movement with this? – CharlieG Feb 11 '18 at 10:19
  • I have published the file so far with the code: SNZ366 – CharlieG Feb 11 '18 at 10:37
  • Sorry for the delay in this response. The answer is yes. Smallbasic has key up and key down events. This import code will show you how to work the events in tandem: ZSB154 – codingCat May 25 '18 at 12:54