icon move
Posted
#1
(In Topic #1231)
Trainee
Posted
Guru

Or
Change the PictureBox.Picture.X and Y
Or If you want more detailed less vague answers ask more detailed less vague questions. <EMOJI seq="1f60a" tseq="1f60a">😊</EMOJI>
Or post the code/project with details of what you need.
Welcome to the forum <EMOJI seq="1f601" tseq="1f601">😁</EMOJI>
Posted
Trainee
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.
Posted
Trainee
' 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
Posted
Guru

Ironically I wrote a snake game that uses a picture box last year. So you've come to the right place for help
Gambas One - Gambas ONE
I'll have a look at you code later when I finish work <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
Posted
Trainee
Appreciate your help 8-)
Posted
Guru

cliofused said
'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…
Code (gambas)
- ' Initialize sprite position
- SpriteX = 0
- SpriteY = 0
- ' Load your sprite image
- DrawPictureBox ' draw the sprite
- ' Subscribe to keyboard events
- PictureBox1.SetFocus()
- ' 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.
- SpriteY -= 10
- SpriteY += 10
- SpriteX += 10
- SpriteX -= 10
- ' Redraw the PictureBox picture to update sprite position
- DrawPictureBox
- ' Draw the sprite at its current position on a new picture using Paint.class
- Paint.Begin(p)
- Paint.DrawPicture(hSpritePicture, SpriteX, SpriteY, hSpritePicture.W, hSpritePicture.H)
- Paint.End
- PictureBox1.Picture = p
I hope that makes sense
Posted
Trainee
Thank you
So don't I need a Drawing Area over PictureBox then.
Hope im not vague again .me a beginner
Posted
Guru

cliofused said
Ok
Thank youBruceSteers
So don't I need a Drawing Area over PictureBox then.
Hope im not vague again .me a beginner
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.
Posted
Trainee
but can not find the path to it.
It says" icon:/32/bicycle" on PictureBox Picture column.
Confused@Cliofuse :geek:
Posted
Guru

cliofused said
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:
It's Stock. Stock class uses a mix of the system icons and some built in gambas ones.
the icon:/ path links internally to gb.form.stock icons and is not a real path.
bicycle is a gambas icon i think so it's built into gb.form.stock
if you do not have the source code you can get them from the gambas repository here..
comp/src/gb.form.stock/gambas · master · Gambas / gambas · GitLab
Posted
Trainee
Thanks :shock:
Posted
Trainee
I found this on Wikipedia and want to know from the experts when you move an icon around the screen is this what you basically doing.
A film – also called a movie, motion picture, moving picture, picture, photoplay or (slang) flick – is a work of visual art that simulates experiences and otherwise communicates ideas, stories, perceptions, feelings, beauty, or atmosphere -through the use of__[ moving images]____. These images are generally accompanied by sound and, more rarely, other sensory stimulations.[1] The word "cinema", short for cinematography, is often used to refer to filmmaking and the film industry, and the art form that is the result of it.
I put the part between brackets that interested me.
It is like you draw a ball across the card by using many cards with the ball in different position then it appears to move.
please correct me if i am wrong, still trying to understand Paint.move :shock:
I hope it will help other beginners to game programming with Gambas.
Posted
Guru

So no. I would not consider making/storing a ton of pictures with a ball in a different place to show as a movie.
If i wanted to animate a ball moving on a background i would do it like this…
Have a background image and a ball image.
Then use either a DrawingArea or a PictureBox picture and Paint the background image and then paint the ball on it at the required position.
The code to move the ball position and repaint the image would have to be in a timer event that called the image to refresh and not the Draw event as you have seen the refresh event can trigger without your instructions.
And there is no such command as Paint.Move() ??
Paint.MoveTo() is just a positioning command. it's like instructing the pen to come off the paper then move to another place to start writing.
After the Paint.MoveTo() instruction you will then use something like Paint.LineTo() to draw a line from that position.
Unless you are talking about the Control.Move() function a PictureBox or a DrawingArea has? In that case that is for moving the control (not its contents) the same as Button.Move() Form.Move() it moves the whole object and does not do anything to it's contents.
Maybe try reading the gambas wiki rather than wikipedia.
/comp/gb.qt4/paint - Gambas Documentation
And sorry to be a pain but please be more precise in your wording. You clearly did not mean Paint.Move() as that command does not exist so what exactly do you mean?
i can only guess if you are not completely accurate/precise.
It's going to be a learning process for you, i was coding with gambas for 5 years or so before i made my first graphical game.
If you are new to gambas and new to programming then maybe jumping straight into making a game is a bit ambitious. there is a hell of a lot of work and time involved in making a functional game.
I started making my Blockski+ game early January, it took about a month to get it ready to post here but it was very WIP and full of bugs as you can see if you read the topic. Gambas One - Gambas ONE after 1 month of coding it still had a long way to go.
it's still not completely finished and has a bug or 2.
You will get there though if you keep at it.
lots of experimenting, lots of debugging, lots of re-writing code as you find better ways to do things.
you may be better off posting code that you have that does not work for you. we can better understand what mistakes you are making by examining your code.
But i say again, read the gambas wiki / - Gambas Documentation then read it some more. then re-read it. especially Paint.class if you are going to use it. forget wikipedia, that knows nothing about gambas programming.
Posted
Guru

It has a DrawingArea
It selects a random desktop background image to use as the background
then it loads the gambas bug pic as the sprite.
When you click the image it starts the timer that each time it triggers it moves the bugs X position to the right and the Y towards the mouse.
The Timer event moves the bug position aPicPos[0] and aPicPos[1] then triggers a DrawingArea1.Refresh
The DrawingArea1_Draw() event paints the background and then the bug on top in the desired place.
It's not much code…
Code (gambas)
- ' Gambas class file
- MoveTimer.Delay = 10
- ' now select a random background picture
- hBG = Picture[sFile].Stretch(DrawingArea1.W, DrawingArea1.H) ' load the picture and stretch it to fit the drawing area
- Paint.DrawPicture(hBugPic, aPicPos[0], aPicPos[1], hBugPic.W, hBugPic.H) ' draw the bug
- aPicPos[0] += 2 ' move left 2 pixels
- DrawingArea1.Refresh
Have fun
Posted
Trainee
1 guest and 0 members have just viewed this.



