icon move

New to Gambas? Post your questions here. No question is too silly or too simple.
cliofused
Posts: 9
Joined: Tuesday 23rd April 2024 11:09am

icon move

Post by cliofused »

hi how can I move an icon around the picturebox with the keyboard?
User avatar
BruceSteers
Posts: 1589
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: icon move

Post by BruceSteers »

Embed it in another picture object

Or
Change the PictureBox.Picture.X and Y

Or If you want more detailed less vague answers ask more detailed less vague questions. 😊

Or post the code/project with details of what you need.

Welcome to the forum 😁
If at first you don't succeed , try doing something differently.
BruceS
cliofused
Posts: 9
Joined: Tuesday 23rd April 2024 11:09am

Re: icon move

Post by cliofused »

Thanks @Bruce Steers.

I just have an icon on the top left corner of Picture box and wanted to know how to move it up,down,

left and right similar to snake game.

I tried chatgpt for 2 days now and it really takes me around the block giving me a different codeset

every time.

sorry if you find it vague but is that not a basic(simple) question but complicated for a beginner.
cliofused
Posts: 9
Joined: Tuesday 23rd April 2024 11:09am

Re: icon move

Post by cliofused »

'This is the code but not working :shock:
-----------------------------------------------



' Gambas class file


' This code assumes you have a form named "Form" with a PictureBox named "PictureBox1" representing the game board.
' Inside the PictureBox, you have a Label named "SpriteLabel" representing the sprite.

Public Sub Form_Open()
' Initialize sprite position
SpriteX = 0
SpriteY = 0

' Load your sprite image
PictureBox1.SpriteLabel.Picture = Image["/path/to/your/sprite/image.png"]

' Subscribe to keyboard events
PictureBox1.Focus()
End


Private Sub PictureBox1_KeyPress()
Dim Key As Integer
Key = Asc(PictureBox1.Key)

' Adjust sprite position based on arrow keys
Select Case Key
Case 273 ' Up arrow key
SpriteY = SpriteY - 10
Case 274 ' Down arrow key
SpriteY = SpriteY + 10
Case 275 ' Right arrow key
SpriteX = SpriteX + 10
Case 276 ' Left arrow key
SpriteX = SpriteX - 10
End Select

' Redraw the PictureBox to update sprite position
PictureBox1.Refresh()
End

Private Sub PictureBox1_Paint()
' Draw the sprite at its current position
PictureBox1.Paint.DrawImage(SpriteX, SpriteY, PictureBox1.SpriteLabel.Picture)
End

Private SpriteX As Integer
Private SpriteY As Integer
User avatar
BruceSteers
Posts: 1589
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: icon move

Post by BruceSteers »

Hmm you need to read the wiki on how to use Paint.class. that code is pretty far out.

Ironically I wrote a snake game that uses a picture box last year. So you've come to the right place for help :D
https://forum.gambas.one/viewtopic.php?t=1671

I'll have a look at you code later when I finish work 😎
If at first you don't succeed , try doing something differently.
BruceS
cliofused
Posts: 9
Joined: Tuesday 23rd April 2024 11:09am

Re: icon move

Post by cliofused »

Thank you

Appreciate your help 8-)
User avatar
BruceSteers
Posts: 1589
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: icon move

Post by BruceSteers »

cliofused wrote: Tuesday 23rd April 2024 9:04pm 'This is the code but not working :shock:
-----------------------------------------------



' Gambas class file


' This code assumes you have a form named "Form" with a PictureBox named "PictureBox1" representing the game board.
' Inside the PictureBox, you have a Label named "SpriteLabel" representing the sprite.
That's not how it works.
A picture box just shows a picture. you cannot add a Label to it.
you can create a new picture and embed the sprite in it like this...

Private SpriteX As Integer
Private SpriteY As Integer

Private hSpritePic As Picture

Public Sub Form_Open()
    ' Initialize sprite position
    SpriteX = 0
    SpriteY = 0
    
    ' Load your sprite image
    
   hSpritePicture = Picture["/path/to/your/sprite/image.png"]
   DrawPictureBox  ' draw the sprite

    ' Subscribe to keyboard events
    PictureBox1.SetFocus()

End


Private Sub PictureBox1_KeyPress()
    
    ' Adjust sprite position based on arrow keys
   ' Note , I changed this to use Key.class constants like Key.Up, Key.Down, etc as it is not wise to use numeric values because different toolkits give different values.
    Select Key.Code
        Case Key.Up ' Up arrow key
            SpriteY -= 10
        Case Key.Down ' Down arrow key
            SpriteY += 10
        Case Key.Right ' Right arrow key
            SpriteX += 10
        Case Key.Left ' Left arrow key
            SpriteX -= 10
    End Select
    
       ' Redraw the PictureBox picture to update sprite position
    DrawPictureBox

End

Private Sub DrawPictureBox()

    ' Draw the sprite at its current position on a new picture using Paint.class

  Dim p As Picture = New Picture(PictureBox1.W, PictureBox1.H, True)    ' create a new picture the size of the PictureBox

  Paint.Begin(p) 
    Paint.DrawPicture(hSpritePicture, SpriteX, SpriteY, hSpritePicture.W, hSpritePicture.H)
  Paint.End

  PictureBox1.Picture = p

End



I hope that makes sense
If at first you don't succeed , try doing something differently.
BruceS
cliofused
Posts: 9
Joined: Tuesday 23rd April 2024 11:09am

Re: icon move

Post by cliofused »

Ok

Thank you @BruceSteers

So don't I need a Drawing Area over PictureBox then.

Hope im not vague again .me a beginner :D
User avatar
BruceSteers
Posts: 1589
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: icon move

Post by BruceSteers »

cliofused wrote: Wednesday 24th April 2024 12:32pm Ok

Thank you @BruceSteers

So don't I need a Drawing Area over PictureBox then.

Hope im not vague again .me a beginner :D
No a PictureBox shows a picture that you create using paint methods.
(you can also use Paint.Begin(PictureBox1.Picture to edit the picturebox's picture directly)

Or you could use a drawing area instead and directly paint to the drawing area not a Picture.
If at first you don't succeed , try doing something differently.
BruceS
cliofused
Posts: 9
Joined: Tuesday 23rd April 2024 11:09am

Re: icon move

Post by cliofused »

I am using the Bicycle icon from stock

but can not find the path to it.

It says" icon:/32/bicycle" on PictureBox Picture column.

Confused@Cliofuse :geek:
Post Reply