none
Does using Shapes.AddImage conflict with KeyUp and KeyDown? RRS feed

  • Question

  • My program runs fine when I use 

    t=Shapes.AddRectangle (40,10)

    to create a shape that I use for a target.  I use

    GraphicsWindow.KeyDown = keydown
    GraphicsWindow.KeyUp = keyup

    to access key strokes in my game

    But when I add

    imagepath1="C:\Small Basic\redman.png"
    t = Shapes.AddImage(imagepath1)

    -or just-

    t = Shapes.AddImage("C:\Small Basic\redman.png")

    to create a different target Shape,

    Other parts of the program work, ie target appears, but

    GraphicsWindow.KeyDown = keydown
    GraphicsWindow.KeyUp = keyup

    no longer activate the subroutines keydown and keyup

    Thanks

    Dan

    Friday, October 19, 2018 3:38 PM

Answers

  • ...  to create a different target Shape  OR  o create an additional target Shape ??

    You'll need a different name for an additional shape, like  t[1] and t[2]   or  t1 and t2    (.... t[3] or t3  aso.)

    But if you simply replace

    ' t = Shapes.AddRectangle(40,10)

    by

    t = Shapes.AddImage("C:\Small Basic\redman.png")

    then this alone will make NO diffence!

    x = 100
    y = 100
    d = 5
    GraphicsWindow.BrushColor = "Red"
    
    t = Shapes.AddRectangle(40,10)   '' red  Rectangle1
    't = Shapes.AddImage(LDImage.NewImage(40,10, "Blue"))  '' blue  Image1  (Forgot to COMMENT OUT ??)
    
    Shapes.Move(t, x,y)
    
    GraphicsWindow.KeyDown = OnKeyDown
    
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey
      If lastKey = "Up" Then
        y = y - d
      ElseIf lastKey = "Down" Then
        y = y + d
      ElseIf lastKey = "Left" Then
        x = x - d
      ElseIf lastKey = "Right" Then
        x = x + d
      EndIf
      Shapes.Move(t, x,y)
    EndSub


    • Edited by Pappa LapubEditor Friday, October 19, 2018 4:28 PM
    • Proposed as answer by litdevModerator Friday, October 19, 2018 4:55 PM
    • Marked as answer by Rizdek Friday, October 19, 2018 5:30 PM
    Friday, October 19, 2018 4:25 PM
    Answerer

All replies

  • ...  to create a different target Shape  OR  o create an additional target Shape ??

    You'll need a different name for an additional shape, like  t[1] and t[2]   or  t1 and t2    (.... t[3] or t3  aso.)

    But if you simply replace

    ' t = Shapes.AddRectangle(40,10)

    by

    t = Shapes.AddImage("C:\Small Basic\redman.png")

    then this alone will make NO diffence!

    x = 100
    y = 100
    d = 5
    GraphicsWindow.BrushColor = "Red"
    
    t = Shapes.AddRectangle(40,10)   '' red  Rectangle1
    't = Shapes.AddImage(LDImage.NewImage(40,10, "Blue"))  '' blue  Image1  (Forgot to COMMENT OUT ??)
    
    Shapes.Move(t, x,y)
    
    GraphicsWindow.KeyDown = OnKeyDown
    
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey
      If lastKey = "Up" Then
        y = y - d
      ElseIf lastKey = "Down" Then
        y = y + d
      ElseIf lastKey = "Left" Then
        x = x - d
      ElseIf lastKey = "Right" Then
        x = x + d
      EndIf
      Shapes.Move(t, x,y)
    EndSub


    • Edited by Pappa LapubEditor Friday, October 19, 2018 4:28 PM
    • Proposed as answer by litdevModerator Friday, October 19, 2018 4:55 PM
    • Marked as answer by Rizdek Friday, October 19, 2018 5:30 PM
    Friday, October 19, 2018 4:25 PM
    Answerer
  • Also if you are going to use the image several times in different parts of the game then load it once using ImageList (especially if it is a downloaded image).

    Another varient using Imagelist for 10 identical image shape and using a game loop for smoother movement (no auto repeat key delay and can move diagonally with 2 keys pressed) - the game loop idea will lead to better interactive game.

    Initialise()

    ' GAME LOOP (LOGIC UPDATED EACH TIME Step)

    While "True"
      start = Clock.ElapsedMilliseconds
     
      MoveShapes()
     
      'Maintain steady 50 fps
      delay = 20 - (Clock.ElapsedMilliseconds-start)
      If delay > 0 Then
        Program.Delay(delay)
      EndIf
    EndWhile

    'SUBROUTINES

    Sub Initialise
      gw = 500
      gh = 500
      d = 5
      GraphicsWindow.Width = gw
      GraphicsWindow.Height = gh
     
      'Load iamge and get its half width/height
      image = ImageList.LoadImage("https://i1.social.s-msft.com/profile/u/avatar.jpg?displayname=rizdek")
      imageW = ImageList.GetWidthOfImage(image)/2
      imageH = ImageList.GetHeightOfImage(image)/2
     
      For i = 1 To 10
        t[i] = Shapes.AddImage(image)
        x[i] = 50+Math.GetRandomNumber(gw-100)
        y[i] = 50+Math.GetRandomNumber(gh-100)
        Shapes.Move(t[i],x[i]-imageW,y[i]-imageH) 'Center image on x,y
      EndFor
     
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
    EndSub

    Sub MoveShapes
      dx = 0
      dy = 0
      If keys["Up"] Then
        dy = dy-d
      EndIf
      If keys["Down"] Then
        dy = dy+d
      EndIf
      If keys["Left"] Then
        dx = dx-d
      EndIf
      If keys["Right"] Then
        dx = dx+d
      EndIf
      For i = 1 To 10
        x[i] = x[i]+dx
        y[i] = y[i]+dy
        Shapes.Move(t[i],x[i]-imageW,y[i]-imageH)
      EndFor
    EndSub

    'EVENT SUBROUTINES

    Sub OnKeyDown
      key = GraphicsWindow.LastKey
      keys[key] = "True"
    EndSub

    Sub OnKeyUp
      key = GraphicsWindow.LastKey
      keys[key] = ""
    EndSub



    Friday, October 19, 2018 4:55 PM
    Moderator
  • Yes, again you solved my problem.  It seems like it had to do with WHERE in the program I assigned t as the Shape using

    t = Shapes.AddImage("C:\Small Basic\redman.png")

    I simply moved the statement to where the other t assignment statement was

    t = Shapes.AddRectangle(40,10) 

    commented that one out, and added

    t = Shapes.AddImage("C:\Small Basic\redman.png")

    and it worked.

    I am still not sure why "where" it was in the program made the difference, but...it worked to move it.
    Again thanks. 

    Friday, October 19, 2018 5:30 PM
  • It's like:

    u = 3
    'u = 5          '' overwrites/reassigns  new value
    TextWindow.WriteLine("'u' has value: "+ u)

    -------------

    Remove the comment mark in 2. line and run again.

    Friday, October 19, 2018 7:02 PM
    Answerer