icon move

Post

Posted
Rating:
#1 (In Topic #1231)
Trainee
hi how can I move an icon around the picturebox with the keyboard?
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
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. <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>
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
'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
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
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
Gambas One - Gambas ONE

I'll have a look at you code later when I finish work <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
Thank you

Appreciate your help  8-)
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘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)

  1.  
  2. Private hSpritePic As Picture
  3.  
  4. Public Sub Form_Open()
  5.     ' Initialize sprite position
  6.     SpriteX = 0
  7.     SpriteY = 0
  8.    
  9.     ' Load your sprite image
  10.    
  11.    hSpritePicture = Picture["/path/to/your/sprite/image.png"]
  12.    DrawPictureBox  ' draw the sprite
  13.  
  14.     ' Subscribe to keyboard events
  15.     PictureBox1.SetFocus()
  16.  
  17.  
  18.  
  19. Private Sub PictureBox1_KeyPress()
  20.    
  21.     ' Adjust sprite position based on arrow keys
  22.    ' 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.
  23.     Select Key.Code
  24.         Case Key.Up ' Up arrow key
  25.             SpriteY -= 10
  26.         Case Key.Down ' Down arrow key
  27.             SpriteY += 10
  28.         Case Key.Right ' Right arrow key
  29.             SpriteX += 10
  30.         Case Key.Left ' Left arrow key
  31.             SpriteX -= 10
  32.     End Select
  33.    
  34.        ' Redraw the PictureBox picture to update sprite position
  35.     DrawPictureBox
  36.  
  37.  
  38. Private Sub DrawPictureBox()
  39.  
  40.     ' Draw the sprite at its current position on a new picture using Paint.class
  41.  
  42.   Dim p As Picture = New Picture(PictureBox1.W, PictureBox1.H, True)    ' create a new picture the size of the PictureBox
  43.  
  44.   Paint.Begin(p)
  45.     Paint.DrawPicture(hSpritePicture, SpriteX, SpriteY, hSpritePicture.W, hSpritePicture.H)
  46.   Paint.End
  47.  
  48.   PictureBox1.Picture = p
  49.  
  50.  
  51.  

I hope that makes sense
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
Ok

Thank you BruceSteers

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

Hope im not vague again .me a beginner  :D
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

cliofused said

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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee
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:
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘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
Online now: No Back to the top

Post

Posted
Rating:
#12
Trainee
Feck ok

Thanks  :shock:
Online now: No Back to the top

Post

Posted
Rating:
#13
Trainee
So I am new to Gambas and PC programming still trying to understand what i am doing.

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.
Online now: No Back to the top

Post

Posted
Rating:
#14
Guru
BruceSteers is in the usergroup ‘Guru’
Well technically a movie is not so much "moving images" as it is a series of still images shown in rapid succession.

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.
Online now: No Back to the top

Post

Posted
Rating:
#15
Guru
BruceSteers is in the usergroup ‘Guru’
Maybe this will help get you started….

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)

  1.  
  2. ' Gambas class file
  3.  
  4. Private aPicPos As Integer[] = [100, 100]
  5.  
  6. Private MoveTimer As New Timer As "MoveTimer"
  7.  
  8. Public Sub Form_Open()
  9.  
  10.   MoveTimer.Delay = 10
  11.  
  12.   hBugPic = Picture["icon:/32/bug"]  ' bug picture
  13.  
  14.   ' now select a random background picture
  15.   Dim aFiles As String[] = RDir("/usr/share/backgrounds/", "*.jpg")
  16.   Dim sFile As String = "/usr/share/backgrounds" &/ aFiles[Rand(0, aFiles.Max)]
  17.   hBG = Picture[sFile].Stretch(DrawingArea1.W, DrawingArea1.H)  ' load the picture and stretch it to fit the drawing area
  18.  
  19.  
  20.  
  21. Public Sub DrawingArea1_Draw()
  22.  
  23.   Paint.DrawPicture(hBG, 0, 0, Last.W, Last.H) ' draw background
  24.   Paint.DrawPicture(hBugPic.Image.Colorize(Color.Black).Picture, aPicPos[0] + 3, aPicPos[1] + 3, hBugPic.W, hBugPic.H) ' draw a bug shadow
  25.   Paint.DrawPicture(hBugPic, aPicPos[0], aPicPos[1], hBugPic.W, hBugPic.H) ' draw the bug
  26.  
  27.  
  28. Public Sub DrawingArea1_MouseUp()
  29.  
  30.   MoveTimer.Enabled = Not MoveTimer.Enabled  ' toggle the timer on or off
  31.  
  32.  
  33. Public Sub MoveTimer_Timer()
  34.  
  35.   aPicPos[0] += 2  ' move left 2 pixels
  36.   If aPicPos[0] > DrawingArea1.W - 16 Then aPicPos[0] = 16  ' if we are far right move to the left
  37.  
  38.   If aPicPos[1] + 16 < Mouse.ScreenY - DrawingArea1.ScreenY Then Inc aPicPos[1] Else Dec aPicPos[1]  ' make it head towards the mouse
  39.  
  40.   DrawingArea1.Refresh
  41.  
  42.  
  43.  

Have fun :)

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#16
Trainee
 Yes that worked
Thank you BruceSteers
Online now: No Back to the top
1 guest and 0 members have just viewed this.