-
Apr 5th, 2012, 01:46 PM
#1
Thread Starter
Addicted Member
snake game
hello, I have a question about my snake game I have the code below, but its not working well at all.. the snake ´repeats´ himself every 256 pixels and I can´t seem to get the bounds right. Can someone help me? The code is in full below.
Code:
Public Class Form1
Dim m As Integer = 4
Dim p(m) As Point
Dim k As Keys = Keys.Right
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim b As New Bitmap(256, 256)
Dim g As Graphics = Graphics.FromImage(b)
If k = Keys.Left And p(m).X >= Me.Bounds.Left Then
p(m).X -= 16
End If
If k = Keys.Right And p(m).X <= Me.Bounds.Left Then
p(m).X += 16
End If
If k = Keys.Up And p(m).Y >= Me.Bounds.Left Then
p(m).Y -= 16
End If
If k = Keys.Down And p(m).Y <= Me.Bounds.Left Then
p(m).Y += 16
End If
For i As Integer = 0 To m - 1
p(i) = p(i + 1)
g.FillRectangle(Brushes.Black, New Rectangle(p(i), New Size(16, 16)))
Next
g.FillRectangle(Brushes.Black, New Rectangle(p(m), New Size(16, 16)))
g.Dispose()
Me.BackgroundImage = b
Me.Refresh()
Me.ClientSize = b.Size
Me.Refresh()
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
k = Keys.Left
End If
If e.KeyCode = Keys.Right Then
k = Keys.Right
End If
If e.KeyCode = Keys.Up Then
k = Keys.Up
End If
If e.KeyCode = Keys.Down Then
k = Keys.Down
End If
End Sub
-
Apr 5th, 2012, 04:14 PM
#2
Re: snake game
The problem caused due to this line Me.BackgroundImage = b, the default value of the property BackgroundImageLayout is Tile, so your bitmap is repeated.
I suggest to add PictureBox to your form and draw inside it instead the form, like this
-
Apr 5th, 2012, 05:02 PM
#3
Re: snake game
You can also look through my realization of the 'Snake game'.
-
Apr 5th, 2012, 05:45 PM
#4
Thread Starter
Addicted Member
Re: snake game
its working almost good now, just need to add collision with itself and the food. I am kind of stuck on the food. Is it possible to draw it with a brush and with something from yours?
Code:
Dim rng As New Random
Dim fx, fy As Integer
Dim foodpoint As Point
fx = rng.Next(0, PictureBox1.Width) * 16
fy = rng.Next(0, PictureBox1.Height) * 16
foodpoint.X = fx
foodpoint.Y = fy
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
|