-
Nov 6th, 2013, 01:54 PM
#1
Thread Starter
Member
Creating a loop for bitmap movements
Hi,
I would like to loop my bitmap that I have hitting another bitmap.
Code:
If (ship2.Top + ship2.Height >= PictureBox3.Top) And (ship2.Top <= PictureBox3.Top + PictureBox3.Height) And (ship2.Left <= PictureBox3.Left + PictureBox3.Width) Then
ship2.Visible = False
Timer1.Enabled = True
Thank you.
-
Nov 6th, 2013, 01:56 PM
#2
Re: Creating a loop for bitmap movements
It looks like you're using pictureboxes, use the IntersectsWith method to determine if a collision occurred. The best rectangle to use would be the Bounds property.
-
Nov 6th, 2013, 02:14 PM
#3
Thread Starter
Member
Re: Creating a loop for bitmap movements
My code already makes collision of two bitmaps, one in motion and the other stationary.
I need example code of how to loop it so that it repeats the same process all over again.
Private Sub InstanceRectangleIntersection( _
ByVal e As PaintEventArgs)
Dim rectangle1 As New Rectangle(50, 50, 200, 100)
Dim rectangle2 As New Rectangle(70, 20, 100, 200)
e.Graphics.DrawRectangle(Pens.Black, rectangle1)
e.Graphics.DrawRectangle(Pens.Red, rectangle2)
If (rectangle1.IntersectsWith(rectangle2)) Then
rectangle1.Intersect(rectangle2)
If Not (rectangle1.IsEmpty) Then
e.Graphics.FillRectangle(Brushes.Green, rectangle1)
End If
End If
End Sub
How does this above actually help me with the bitmaps?
-
Nov 6th, 2013, 02:41 PM
#4
Re: Creating a loop for bitmap movements
So you want to loop the process of moving and colliding the bitmaps? Try something like this:
Pseudo Code:
'Loop 10 times
For i As Integer = 0 to 9
Do Until rect1.IntersectsWith(rect2)
'Move
Loop
Next
Basically what I use there is a nested loop.
-
Nov 6th, 2013, 02:49 PM
#5
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by dday9
So you want to loop the process of moving and colliding the bitmaps? Try something like this:
Pseudo Code:
'Loop 10 times For i As Integer = 0 to 9 Do Until rect1.IntersectsWith(rect2) 'Move Loop Next
Basically what I use there is a nested loop.
Well ship2 collides with picturebox3, once done it is removed from the screen. So I need the whole process to start again. I'm doing this for two bitmaps that collide with picturebox3. I still haven't gotten around to having two images load exactly where the ship1, 2 are removed as debris.
Lastly, I'm puzzled as to where to put the loop, in my private sub checkhit() or the actual Timer1?
'IntersectsWith' is not a member of 'System.Windows.Forms.PictureBox'.
^This is what loads eitherway.
-
Nov 6th, 2013, 02:59 PM
#6
Re: Creating a loop for bitmap movements
IntersectsWith is a member of a rectangle. The PictureBox has a couple properties that expose rectangles, though I can't quite remember the names at the moment. Bounds might be one of them. The idea is to get the area of the PictureBox as a rectangle so that you can use the methods for checking the interaction of two rectangles. Take a look at the properties of the PictureBox to see which return type Rectangle, then you can compare them to see which seems most suitable (I think there are two).
That loop will run until the intersection happens, and it will prevent any user interaction while it occurs. As such, it's an example, but quite likely not what you want to have happen. If you have a timer ticking away, and the pictureboxes move each time the timer ticks, then the timer is in lieu of the loop, so the actual loop wouldn't exist anywhere.
Other than that, I can't understand what process you want to have start again, so I'm not sure what it is you want to be looping.
My usual boring signature: Nothing
-
Nov 6th, 2013, 03:02 PM
#7
Thread Starter
Member
Re: Creating a loop for bitmap movements
Well once the two bitmaps hit the other bitmap which is picturebox3, they both are removed, while two new bitmaps take the location of the former images, and then the whole process begins again. Essentially a loop.
-
Nov 6th, 2013, 03:11 PM
#8
Re: Creating a loop for bitmap movements
There are a lot of details about your project we do not know and that may affect the algorithms designed to make it work. So, I suggest you describe (briefly please) what it is that your program does or you want it to do. Of course, it is about two or three ships collinding, but, are they moving randomly? do then start always at the same place? do they colide with the picturebox at the same time? are you keeping score? does the user have any input? and so on.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Nov 6th, 2013, 03:16 PM
#9
Re: Creating a loop for bitmap movements
Originally Posted by pocket
Well once the two bitmaps hit the other bitmap which is picturebox3, they both are removed, while two new bitmaps take the location of the former images, and then the whole process begins again. Essentially a loop.
I'd point you back to post #4.
-
Nov 6th, 2013, 03:36 PM
#10
Thread Starter
Member
Re: Creating a loop for bitmap movements
No ship 1, and 2 move towards picturebox3 as a timer. There is no random movement. How ever for the computer controlled ship, I'll want to try and get something like that going for the missiles.
But just for now, its as I've explained. The form loads, the hypothetical loop occurs, and begins again once an event takes place, such as collision with picturebox3. Thats it.
-
Nov 6th, 2013, 03:40 PM
#11
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by dday9
I'd point you back to post #4.
But it states an error, does the loop code line go in private sub or the timer1? Eitherway it doesn't change. I did edit the names, obviously. 'ship2.IntersectsWith(PictureBox3)'
That is correct, if not, what am I doing wrong here?
-
Nov 6th, 2013, 04:05 PM
#12
Re: Creating a loop for bitmap movements
Right now you're checking if ship2 intersects with the picturebox, when you need to check if it intersects with the picturebox's bounds. This would be a corrected version:
Code:
ship2.IntersectsWith(PictureBox3.Bounds)
Of course you can use any other property that returns a rectangle, it's just that the bounds property works best(I've found).
-
Nov 6th, 2013, 04:39 PM
#13
Thread Starter
Member
Re: Creating a loop for bitmap movements
It still displays an error that the intersectsWith is not a member of the system windows forms picturebox.
Regardless of where I place the Loop code.
-
Nov 6th, 2013, 04:44 PM
#14
Re: Creating a loop for bitmap movements
If Ship2 is a picturebox, then the code should be:
Ship2.Bounds.IntersectsWith(PictureBox3.Bounds)
As I mentioned earlier, .IntersectsWith is a member of Rectangle, not Picturebox, but Pictureboxes have properties that return rectangles.
My usual boring signature: Nothing
-
Nov 6th, 2013, 04:44 PM
#15
Re: Creating a loop for bitmap movements
Ah, I'm sorry I thought that ship2 was a rectangle. In that case you'd do the same thing to ship2:
Code:
ship2.Bounds.IntersectsWith(PictureBox3.Bounds)
Edit - Shaggy is a bit quicker on the trigger today.
-
Nov 6th, 2013, 05:00 PM
#16
Thread Starter
Member
Re: Creating a loop for bitmap movements
Right I pasted this, the error is gone. How ever none of my bitmaps are moving. Whether I place it in either Timer1 or the private sub checkhit() And in form, the program doesn't even load.
-
Nov 6th, 2013, 05:06 PM
#17
Re: Creating a loop for bitmap movements
Depending on what you pasted, you won't see the bitmaps move with this code anyways. Running a loop like that will block the UI thread from pumping messages. Therefore, it will prevent any invalidation messages from getting through, so the screen will never be painted. The program should appear to freeze, depending on what you put in place of the 'Move comment.
I still say that loop was just an example. Since you want to deal with user input, and other things, you wouldn't really use a loop at all. Every time the timer ticks, you'd move the bitmaps, deal with user input, check for collisions, then be done with it. The timer would be the loop.
My usual boring signature: Nothing
-
Nov 6th, 2013, 05:10 PM
#18
Re: Creating a loop for bitmap movements
Yeah, now that I'm thinking of it you really need a managed game loop. I have an example of a managed game loop in 2 different locations. The first can be found in my signature under "My Snippet Website" and under articles. The second can be found in my signature under Game Programming Codebank Contributions.
-
Nov 6th, 2013, 05:11 PM
#19
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by Shaggy Hiker
Depending on what you pasted, you won't see the bitmaps move with this code anyways. Running a loop like that will block the UI thread from pumping messages. Therefore, it will prevent any invalidation messages from getting through, so the screen will never be painted. The program should appear to freeze, depending on what you put in place of the 'Move comment.
I still say that loop was just an example. Since you want to deal with user input, and other things, you wouldn't really use a loop at all. Every time the timer ticks, you'd move the bitmaps, deal with user input, check for collisions, then be done with it. The timer would be the loop.
So I should code in the timer1 section instead of using a loop to get the overall objective done. That is what you are getting at..
Well I'm notsure how else to get the repeating pattern if not using a loop. If this loop code isn't the answer, how else should I get this done?
Both image hit picbox 3, done, but then I want two new images which will obviously remain invisible, but load once those two are removed. But on top of that, the whole process repeats again.
If PictureBox3.Bounds.IntersectsWith(PictureBox10.Bounds) Then
Timer3.Stop()
PictureBox3.Visible = False
PictureBox10.Visible = False
End If
Would that in theory achieve my current goal?
Last edited by pocket; Nov 6th, 2013 at 05:16 PM.
Reason: Added code found via Search on VBforums
-
Nov 6th, 2013, 05:19 PM
#20
Re: Creating a loop for bitmap movements
Yes, that is what I am getting at, though dday is right about the game loop. The problem with a timer is the time itself. The basic idea is that you want to do this:
1) Get user input.
2) Move the display items.
3) Check for intersections and deal with any that occur.
4) Goto 1
If you have a timer, then each time it ticks it would do 1-3. Item 4 would be covered by the timer itself. However, if the timer ticks too slowly, the motion will appear very jerky. If the timer ticks too fast, there won't be sufficient time to get through items 1-3 before the timer ticks again. This is the reason for a game loop. The idea with that is to do items 1-3 and measure how long it took to do them. Step 4 becomes "Draw the screen", then step five is goto 1. By measuring how long it took to get through steps 1-4, you would know how much time has elapsed since the last time you went through the loop, and you'd move the objects accordingly in step 1. It's a more complex way of doing things, but vastly more efficient. After all, in the best circumstance with a timer, the computer will be sitting idle most of the time, waiting for the timer to tick again. If you tune that timer too tightly, though, your performance is likely to collapse as timer ticks begin to overlap one another. The game loop maximizes performance.
My usual boring signature: Nothing
-
Nov 6th, 2013, 05:27 PM
#21
Thread Starter
Member
Re: Creating a loop for bitmap movements
I'm not using any XNA, dday. I'm developing for a WM Pocket CE device.
-
Nov 6th, 2013, 05:29 PM
#22
Re: Creating a loop for bitmap movements
Originally Posted by pocket
I'm not using any XNA, dday. I'm developing for a WM Pocket CE device.
Sorry, but I never said that you were, you're misunderstanding. The managed game loop isn't specifically for XNA, it's relevant to GDI+ too.
Edit - Here is even an example of using the managed game loop with no GDI+ or XNA, just using a few pictureboxes:
http://www.vbforums.com/showthread.p...=1#post4510137
-
Nov 6th, 2013, 05:33 PM
#23
Thread Starter
Member
Re: Creating a loop for bitmap movements
Ah right, Hmm. When I read the article is referred to XNA that is obviously desktop related. I thought GDI+ was actual type up code. I haven't use any of that. That is much more complex for little ship designs. I had to use pictureboxes as the only alternative.
-
Nov 6th, 2013, 05:34 PM
#24
Re: Creating a loop for bitmap movements
Originally Posted by pocket
Ah right, Hmm. When I read the article is referred to XNA that is obviously desktop related. I thought GDI+ was actual type up code. I haven't use any of that. That is much more complex for little ship designs. I had to use pictureboxes as the only alternative.
I think that at this point I'm going to back out of this thread. Still take a look at the link I posted in #22. Good luck to you.
-
Nov 6th, 2013, 05:38 PM
#25
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by dday9
I think that at this point I'm going to back out of this thread. Still take a look at the link I posted in #22. Good luck to you.
dday, well I've learnt that pictureboxes cannot be rotated. I had to make a few compromises for my little project, for educational purposes.
Well I've read the code, still puzzeling.
-
Nov 6th, 2013, 05:49 PM
#26
Re: Creating a loop for bitmap movements
Originally Posted by pocket
dday, well I've learnt that pictureboxes cannot be rotated. I had to make a few compromises for my little project, for educational purposes.
Well I've read the code, still puzzeling.
The pictureboxes themselves can't be rotated, but the image inside of them can be. For example:
Code:
Dim b As Bitmap = DirectCast(PictureBox1.Image, Bitmap)
b.RotateFlip(RotateFlipType.Rotate180FlipNone)
PictureBox1.Image = b
That code gives the appearance that the picturebox rotated 180 degrees.
-
Nov 6th, 2013, 06:00 PM
#27
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by dday9
The pictureboxes themselves can't be rotated, but the image inside of them can be. For example:
Code:
Dim b As Bitmap = DirectCast(PictureBox1.Image, Bitmap)
b.RotateFlip(RotateFlipType.Rotate180FlipNone)
PictureBox1.Image = b
That code gives the appearance that the picturebox rotated 180 degrees.
The rotating of the image at any angle? But the picture box requires an updated image, right..?
I can't open the bouncy ball file using VS 2003.
-
Nov 6th, 2013, 06:18 PM
#28
Re: Creating a loop for bitmap movements
Originally Posted by pocket
The rotating of the image at any angle? But the picture box requires an updated image, right..?
If you want to rotate by any angle, rotate it by it's graphics:
Code:
Dim g As Graphics = Graphics.FromImage(Picturebox1.Image)
g.RotateTransform(<your angle here>)
g.Save()
Originally Posted by pocket
I can't open the bouncy ball file using VS2003
Wow, 2003?! I guess to use the WM Pocket CE device correct? I don't think that it would compile 2003 as I think that I use LINQ.
-
Nov 6th, 2013, 06:54 PM
#29
Thread Starter
Member
Re: Creating a loop for bitmap movements
Yes, I ordered a VB.net programming book for CE on Amazon UK for £2.90, for reference on GDI+, may be for animation effect if possible. And button usage.
But my concern was doing as I stated, but eventually after then creating a random missile sequence for the enemy ship. So straight right away once the program is loaded the missiles fire and hit the player controlled ship, even though it may not rotate, even if it did, not going anywhere. But never mind about that.
The missile will have to be designed to hit the controlled ship on different variations, I guess in theory?
Sort of like the Pong game sequence. Ofcourse different events and directions are taking place.
-
Nov 7th, 2013, 11:46 AM
#30
Thread Starter
Member
Re: Creating a loop for bitmap movements
Private quit As Boolean = False
Private Sub gameloop()
'update
Call checkhit()
If (ship2.Top + ship2.Height >= PictureBox3.Top) And (ship2.Top <= PictureBox3.Top + PictureBox3.Height) And (ship2.Left <= PictureBox3.Left + PictureBox3.Width) Then
ship2.Visible = False
Timer1.Enabled = True
End If
'loop
If quit = False Then
End If
End Sub
In bold that is one piece of the code for the program. Nothing happens.
I also used just another If and End If for the debris. I only thought about that as I woke up not long after.
-
Nov 7th, 2013, 12:24 PM
#31
Re: Creating a loop for bitmap movements
That looks like code for intersecting two rectangles. What else is it supposed to do? Did you put a breakpoint on that If statement and take a look at how it will evaluate? If nothing happens, the most likely reason is because the If evaluates to False.
By the way, always use AndAlso and OrElse in place of And and Or unless you have good reason to do otherwise.
My usual boring signature: Nothing
-
Nov 7th, 2013, 06:50 PM
#32
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by Shaggy Hiker
That looks like code for intersecting two rectangles. What else is it supposed to do? Did you put a breakpoint on that If statement and take a look at how it will evaluate? If nothing happens, the most likely reason is because the If evaluates to False.
By the way, always use AndAlso and OrElse in place of And and Or unless you have good reason to do otherwise.
As I've explained in my previous posts, my aim is getting two bitmaps hitting another, and then two other images loads, while the first two are removed. Which I have done.
I now want to loop all this, so it is reoccuring infinately. Unless I close the program. Changing true or false on the game look doesn't change anything. It doesn't start again.
I learned the If and End if statements from some old VB.net video tutorials. I have tried for Loops, but nothing helpful.
-
Nov 7th, 2013, 07:01 PM
#33
Re: Creating a loop for bitmap movements
Use a while infinite loop, or a managed game loop. I've already shown you the managed game loop, if you don't want to take the time to study that and adjust it to your needs, take a stab a the while infinite loop. Here is an example of that:
Pseudo Code:
Private quit As Boolean = False Private Sub StartLoop() 'This would be in a seperate thread so that it doesn't tie up the GUI While quit = False 'Move your bitmaps End While End Sub Private Sub FormClosing(...) Handles Me.FormClosing quit = True End Sub
Everything in the While loop will execute until the variable quit equals True, which happens in the form_closing event.
-
Nov 7th, 2013, 07:20 PM
#34
Thread Starter
Member
Re: Creating a loop for bitmap movements
Originally Posted by dday9
Use a while infinite loop, or a managed game loop. I've already shown you the managed game loop, if you don't want to take the time to study that and adjust it to your needs, take a stab a the while infinite loop. Here is an example of that:
Pseudo Code:
Private quit As Boolean = False
Private Sub StartLoop()
'This would be in a seperate thread so that it doesn't tie up the GUI
While quit = False
'Move your bitmaps
End While
End Sub
Private Sub FormClosing(...) Handles Me.FormClosing
quit = True
End Sub
Everything in the While loop will execute until the variable quit equals True, which happens in the form_closing event.
I tried moving my code snippets around, with the managed game loop. The private boolean is declared at the top of my code form.
And at the very bottom is the gameloop
Code:
Private Sub gameloop()
'update
Call checkhit()
'loop
If (ship2.Top + ship2.Height >= PictureBox3.Top) And (ship2.Top <= PictureBox3.Top + PictureBox3.Height) And (ship2.Left <= PictureBox3.Left + PictureBox3.Width) Then
ship2.Visible = False
Timer1.Enabled = True
End If
If (ship1.Top + ship1.Height >= PictureBox3.Top) And (ship1.Top <= PictureBox3.Top + PictureBox3.Height) And (ship1.Left <= PictureBox3.Left + PictureBox3.Width) Then
ship1.Visible = False
Timer1.Enabled = True
End If
If quit = True Then
End If
End Sub
Additionally, can the alternative loop be helpful for missile firing by the CPU controlled bitmap.
-
Nov 8th, 2013, 01:49 PM
#35
Thread Starter
Member
Re: Creating a loop for bitmap movements
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|