Hello,
I'm still working on a pacman game. I need to get the ai and point system working. However, I first need to get collision detection working. I have got somewhat of a collision detection working. The catch is that it seems that everytime the pacman (picturebox) collides with something (also a picturebox) 50% of the time, everything would work, the other 50%, for some reason my controls would get inverted. ie. up key will move pacman down and right key will move pacman left.
Any help is appreciated
This is the entire code
That is the code jsut for the movement/collision detectionCode:Public Class Form1 Dim DirectionPac = New Label Dim timer As Stopwatch Dim backBuffer As Image Dim graphics As Graphics Dim clientWidth As Integer Dim clientHeight As Integer Dim interval As Long Dim startTick As Long Dim Collision As Boolean Dim Ghost As Rectangle Dim direction As Point Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Start() Me.DoubleBuffered = True Me.MaximizeBox = False Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle timer = New Stopwatch() clientWidth = 618 clientHeight = 529 interval = 16 Me.ClientSize = New Size(clientWidth, clientHeight) backBuffer = New Bitmap(clientWidth, clientHeight) graphics = graphics.FromImage(backBuffer) direction = New Point(0, 0) Ghost = New Rectangle(305, 245, 20, 20) End Sub Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown GameLoop() End Sub Private Sub GameLoop() timer.Start() Do While (Me.Created) startTick = timer.ElapsedMilliseconds RenderScene() 'this declares the sub that draws the scene Application.DoEvents() Do While timer.ElapsedMilliseconds - startTick < interval Loop Loop End Sub Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Right Then DirectionPac.Text = "Right" End If If e.KeyCode = Keys.Left Then DirectionPac.Text = "Left" End If If e.KeyCode = Keys.Down Then DirectionPac.Text = "Down" End If If e.KeyCode = Keys.Up Then DirectionPac.Text = "Up" End If End Sub Private Sub RenderScene() backBuffer = New Bitmap(clientWidth, clientHeight) graphics = graphics.FromImage(backBuffer) PictureBox1.Image = Nothing PictureBox1.Image = backBuffer graphics.FillRectangle(Brushes.Blue(), Ghost) End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If PACMAN.Bounds.IntersectsWith(PictureBox2.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox3.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox4.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox5.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox6.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox7.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox8.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox9.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox10.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox11.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox12.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox13.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox14.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox15.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox16.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox17.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox18.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox19.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox20.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox21.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox22.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox23.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox24.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox25.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox26.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox27.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox28.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox29.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox30.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox31.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox32.Bounds) Then Collision = True End If If DirectionPac.Text = "Right" And (PACMAN.Location.X + PACMAN.Width < Me.Width - 30) Then If Collision = True And (PACMAN.Location.X <> 0) Then PACMAN.Location = New Point(PACMAN.Location.X - 1, PACMAN.Location.Y) Else PACMAN.Location = New Point(PACMAN.Location.X + 1, PACMAN.Location.Y) End If End If If DirectionPac.Text = "Left" And PACMAN.Location.X <> 0 Then If Collision = True And (PACMAN.Location.X + PACMAN.Width < Me.Width - 30) Then PACMAN.Location = New Point(PACMAN.Location.X + 1, PACMAN.Location.Y) Collision = False Else PACMAN.Location = New Point(PACMAN.Location.X - 1, PACMAN.Location.Y) End If End If If DirectionPac.Text = "Up" And PACMAN.Location.Y <> 0 Then If Collision = True And (PACMAN.Location.Y <> 0) Then PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y + 1) Collision = False Else PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y - 1) End If End If If DirectionPac.Text = "Down" And (PACMAN.Location.Y + PACMAN.Height < Me.Height - 30) Then If Collision = True And (PACMAN.Location.Y <> 0) Then Collision = True PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y - 1) Collision = False Else PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y + 1) End If End If End Sub End Class
Code:Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If PACMAN.Bounds.IntersectsWith(PictureBox2.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox3.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox4.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox5.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox6.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox7.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox8.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox9.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox10.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox11.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox12.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox13.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox14.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox15.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox16.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox17.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox18.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox19.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox20.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox21.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox22.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox23.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox24.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox25.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox26.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox27.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox28.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox29.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox30.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox31.Bounds) Or PACMAN.Bounds.IntersectsWith(PictureBox32.Bounds) Then Collision = True End If If DirectionPac.Text = "Right" And (PACMAN.Location.X + PACMAN.Width < Me.Width - 30) Then If Collision = True And (PACMAN.Location.X <> 0) Then PACMAN.Location = New Point(PACMAN.Location.X - 1, PACMAN.Location.Y) Else PACMAN.Location = New Point(PACMAN.Location.X + 1, PACMAN.Location.Y) End If End If If DirectionPac.Text = "Left" And PACMAN.Location.X <> 0 Then If Collision = True And (PACMAN.Location.X + PACMAN.Width < Me.Width - 30) Then PACMAN.Location = New Point(PACMAN.Location.X + 1, PACMAN.Location.Y) Collision = False Else PACMAN.Location = New Point(PACMAN.Location.X - 1, PACMAN.Location.Y) End If End If If DirectionPac.Text = "Up" And PACMAN.Location.Y <> 0 Then If Collision = True And (PACMAN.Location.Y <> 0) Then PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y + 1) Collision = False Else PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y - 1) End If End If If DirectionPac.Text = "Down" And (PACMAN.Location.Y + PACMAN.Height < Me.Height - 30) Then If Collision = True And (PACMAN.Location.Y <> 0) Then Collision = True PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y - 1) Collision = False Else PACMAN.Location = New Point(PACMAN.Location.X, PACMAN.Location.Y + 1) End If End If End Sub
Another thing I need help with is the points and the ai. Regarding the dots. I really dont want to draw 100+ pictureboxes then coding each individual one. I still don't entirely understand how lists/arrays work so any explanation would do.
As for coding the AI, I am basically in the dark for coding ai in visual basic.
I am a begginer at VB so I apologize if I have tackled a task that may have been too difficult.
Again any help is appreciated.
Attached is my project.PacManTest.zip
Thanks!




Reply With Quote