|
-
Jan 31st, 2013, 09:39 PM
#1
Thread Starter
New Member
PacMan Help!
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
Code:
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
That is the code jsut for the movement/collision detection
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!
-
Jan 31st, 2013, 10:10 PM
#2
Addicted Member
Re: PacMan Help!
I would start by labeling your pictureboxes on your form so you can possibly identify which picturebox collisions are messing you up when you move pac man around.
-
Jan 31st, 2013, 10:16 PM
#3
Thread Starter
New Member
Re: PacMan Help!
the problem is that that happens with every picturebox. it doesn't matter which picturebox the pacman collides with, anytime it collides with anything (other than the borders of the form) there is a 50% chance that the controls will get inverted.
-
Jan 31st, 2013, 11:05 PM
#4
Addicted Member
Re: PacMan Help!
 Originally Posted by Se_eZ
the problem is that that happens with every picturebox. it doesn't matter which picturebox the pacman collides with, anytime it collides with anything (other than the borders of the form) there is a 50% chance that the controls will get inverted.
well what i see happening is that if i'm moving up and i collide my position moves down.
The problem is that i'm still moving up so i collide again. so i'm colliding over and over and over again. The collision boolean is flipping back and forth between true and false pretty quick.
-
Feb 1st, 2013, 12:06 AM
#5
Fanatic Member
Re: PacMan Help!
All I can see is this part
Code:
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
has no Collision = False statement like the rest, this may or may not be an issue as it would think its colliding again when its not
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
|