none
Need Help with some Code in Small Basic. (display random image) RRS feed

  • Question

  • I was messing around in small basic and decided to make a program that randomly displays images in random places in the graphics window with a decreasing time delay. When I try to run it it will not display any of the pictures apart from the test images. I have tried different formats and file paths. I would appreciate any help. Thanks.

    This is the code I have so far:

    'position the Graphics Window

    GraphicsWindow.Show() GraphicsWindow.Top = 75 GraphicsWindow.Left = 175 GraphicsWindow.Width = 1080 GraphicsWindow.Height = 720

    'Set the image locations num_1 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image1.PNG" num_2 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image2.PNG" num_3 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image3.PNG" num_4 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image4.PNG" num_5 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image5.PNG" num_6 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image6.PNG" num_7 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image7.PNG" num_8 = "H:\Documents and Settings\God\My Documents\SMALLBASIC\KeyboardCat.GIF"

    'Place some test images and set the background colour. GraphicsWindow.BackgroundColor = "cyan" GraphicsWindow.DrawResizedImage(num_1, 50, 50, 200, 200) GraphicsWindow.DrawResizedImage(num_2, 150, 150, 200, 200) GraphicsWindow.DrawResizedImage(num_3, 250, 250, 200, 200) GraphicsWindow.DrawResizedImage(num_4, 350, 350, 200, 200)

    'set the initial delay time (1000 milliseconds) delay_num = 1000

    'Display the random image For i = 1 To 100 If delay_num >= 200 then Program.Delay(delay_num) delay_num = delay_num - 50 random_x = Math.GetRandomNumber(GraphicsWindow.Width) random_y = Math.GetRandomNumber(GraphicsWindow.Height) GraphicsWindow.DrawImage(Text.Append("num_", Math.GetRandomNumber(8)), random_x, random_y) Else delay_num = 200 random_x = Math.GetRandomNumber(GraphicsWindow.Width) random_y = Math.GetRandomNumber(GraphicsWindow.Height) GraphicsWindow.DrawImage(Text.Append("num_", Math.GetRandomNumber(8)), random_x, random_y) EndIf EndFor

    Sunday, June 30, 2013 2:35 PM

Answers

  • The problem is the variable names.

    For example:

    num_1 is a variable it contains the string

    "H:\Documents and Settings\God\My Documents\SMALLBASIC\image1.PNG"

    This is then used to draw the test images - no problem.

    But when you come the do the random bits, you are using:

    Text.Append("num_", Math.GetRandomNumber(8))

    This results in a string, for example "num_1" and it is this string that is used for the file name, which doesn't exist.  "num_1" is not the same as the variable num_1 which is actually a path to a file.

    The solution is to use arrays, for example:

    num[1] = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image1.PNG"

    and

    GraphicsWindow.DrawImage(num[Math.GetRandomNumber(8)], random_x, random_y)

    Sunday, June 30, 2013 2:47 PM
    Moderator

All replies

  • The problem is the variable names.

    For example:

    num_1 is a variable it contains the string

    "H:\Documents and Settings\God\My Documents\SMALLBASIC\image1.PNG"

    This is then used to draw the test images - no problem.

    But when you come the do the random bits, you are using:

    Text.Append("num_", Math.GetRandomNumber(8))

    This results in a string, for example "num_1" and it is this string that is used for the file name, which doesn't exist.  "num_1" is not the same as the variable num_1 which is actually a path to a file.

    The solution is to use arrays, for example:

    num[1] = "H:\Documents and Settings\God\My Documents\SMALLBASIC\image1.PNG"

    and

    GraphicsWindow.DrawImage(num[Math.GetRandomNumber(8)], random_x, random_y)

    Sunday, June 30, 2013 2:47 PM
    Moderator
  • Thanks a lot. It works perfectly now.

    Sunday, June 30, 2013 3:40 PM