Thread: how do u...
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Frank Kabel Frank Kabel is offline
external usenet poster
 
Posts: 3,885
Default how do u...

Hi
not tested, just to give you some ideas:
- Define some global variables for storing the current position and
direction
- Use the OnTime procedure to move the snake every x econds (check the
global variables for direction / position)
- Use the OnKey method to change the global direction variables

Saying this you may have a look at the following code:
- uses the sheet 'Snake_Test'
- moves the selection within the range A1:J10
- uses the arrow keys for changing the direction
You may adapt this as you like. copy the code in a module of your
workbook

Option Explicit
Dim Nexttime
Dim PosX As Integer
Dim PosY As Integer
Dim direction As Byte
Dim start As Boolean

Sub Snake()
Dim wks As Worksheet
Set wks = ActiveWorkbook.Worksheets("Snake_test")
Nexttime = Now + TimeValue("00:00:01")
If Not start Then
PosX = Int(9 * Rnd + 2)
PosY = Int(9 * Rnd + 2)
direction = Int(4 * Rnd + 1)
start = True
wks.Cells(PosY, PosX).Select
Application.OnKey "{UP}", "move_up"
Application.OnKey "{DOWN}", "move_down"
Application.OnKey "{LEFT}", "move_left"
Application.OnKey "{RIGHT}", "move_right"
Application.OnTime Nexttime, "Snake"
Else
Select Case direction
Case 1
PosX = PosX + 1
Case 2
PosX = PosX - 1
Case 3
PosY = PosY + 1
Case 4
PosY = PosY - 1
End Select
If PosY < 1 Or PosY 10 Or PosX < 1 Or PosX 10 Then
MsgBox "Game over"
start = False
Application.OnKey "{UP}"
Application.OnKey "{DOWN}"
Application.OnKey "{LEFT}"
Application.OnKey "{RIGHT}"
Else
wks.Cells(PosY, PosX).Select
Application.OnTime Nexttime, "Snake"
End If
End If
End Sub


Sub move_left()
direction = 2
End Sub

Sub move_right()
direction = 1
End Sub

Sub move_up()
direction = 4
End Sub

Sub move_down()
direction = 3
End Sub





--
Regards
Frank Kabel
Frankfurt, Germany

thx for posting back

i know about the onkey method and how to make the snake 'move' (using
the arrow keys if i want to), but is it possible to make the snake
keep moving until i make it change direction?


---
Message posted from http://www.ExcelForum.com/