16,549
社区成员
发帖
与我相关
我的任务
分享
Class Grid
Private width As Integer
Private height As Integer
Dim status(width, height) As Boolean
Public snake As Snake
Private food, initbody As Node
Private snakeDirection As Direction = Direction.LEFT
Public Sub Grid(width, height)
Me.width = width
Me.height = height
initSnake()
createFood()
End Sub
Private Function initSnake() As Snake
For i = width / 2 + 10 To width / 2 - 50 Step 10
initbody = New Node(i, height / 2 + 10)
snake.body.Add(initbody)
status(i, height / 2 + 10) = True
Next
Return snake
End Function
Public Function getSnake() As Snake
Return snake
End Function
Public Function getFood()
Return food
End Function
Public Function createFood() As Node
Dim x, y As Integer
Randomize()
x = (Fix(Rnd() * 21) + 1) * 10
y = (Fix(Rnd() * 21) + 1) * 10
food = New Node(x, y)
Return food
End Function
End Class
Class Gameview
Private grid As Grid
Private canvas As Panel
Private snake As Snake
Public Sub paintcomponent(graphics As Graphics)
drawGridBackground(graphics)
drawSnake(graphics, grid.getSnake())
drawFood(graphics, grid.getFood())
End Sub
Public Sub GameView(grid As Grid)
Me.grid = grid
End Sub
Public Sub draw(graphics As Graphics)
canvas.Refresh()
End Sub
Public Function getCanvas() As Panel
Return canvas
End Function
Public Sub drawSnake(graphics As Graphics, snake As Snake)
Dim rectanglehead As New Rectangle(snake.getHead.getX, snake.getHead.getY, 10, 10)
graphics.DrawPie(Pens.ForestGreen, rectanglehead, 0, 360)
For i = 2 To Len(snake.body)
Dim rectanglebody As New Rectangle(snake.body.Item(i).getX, snake.body.Item(i).getY, 10, 10)
graphics.DrawPie(Pens.LightGreen, rectanglebody, 0, 360)
Next
End Sub
Public Sub drawFood(graphics As Graphics, squareArea As Node)
Dim rectangle As New Rectangle(squareArea.getX, squareArea.getY, 10, 10)
graphics.DrawPie(Pens.Red, rectangle, 0, 360)
End Sub
Public Sub drawGridBackground(graphics As Graphics)
For i = 0 To 20
Dim point1 As New Point(10 + 10 * i, 10)
Dim point2 As New Point(10 + 10 * i, 210)
Dim point3 As New Point(10, 10 + 10 * i)
Dim point4 As New Point(210, 10 + 10 * i)
graphics.DrawLine(Pens.AliceBlue, point1, point2)
graphics.DrawLine(Pens.AliceBlue, point3, point4)
Next
End Sub
End Class
Class Node
Private x, y As Integer
Public Sub New(ByRef x, ByRef y)
MyClass.x = x
MyClass.y = y
End Sub
Public Function getX()
Return x
End Function
Public Function getY()
Return y
End Function
End Class
Class Snake
Public body As New Collection()
Private upnode, downnode, leftnode, rightnode As Node
Public Function eat(ByVal food As Node) As Node
If isNeighbor(body.Item(1), food) Then
body.Add(food)
End If
Return food
End Function
Private Function isNeighbor(ByVal a As Node, ByVal b As Node) As Boolean
Return Math.Abs(a.getX() - b.getX()) + Math.Abs(a.getY() - b.getY()) = 1
End Function
Public Function move(ByVal direction As Direction) As Node
Select Case direction
Case 0
upnode = New Node(getHead.getX, getHead.getY + 1)
body.Add(upnode)
Case 1
rightnode = New Node(getHead.getX + 1, getHead.getY)
body.Add(rightnode)
Case 2
downnode = New Node(getHead.getX, getHead.getY - 1)
body.Add(downnode)
Case 3
leftnode = New Node(getHead.getX - 1, getHead.getY)
body.Add(leftnode)
End Select
Return body.Item(-1) '尾部node未删去
End Function
Public Function getHead() As Node
Return Me.body.Item(1)
End Function
Public Function addTail(ByVal area As Node) As Node
Me.body.Add(area)
Return area
End Function
Public Function getBody() As Collection
Return Me.body
End Function
End Class