Results 1 to 9 of 9

Thread: Tutorial [Snake using Picture boxes] NMs Game tutorial 0.4

Threaded View

  1. #6

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Eat food:

    Start with making a Sub called FindFood. The firs thing we need to figure out is if the head of the snake is the same place as the food. And finally our hard coding work is finally paying off. Because we made sure that the food is placed exactly at a place where the snake can get, this is really easy to do. So to check if the snake found food, we only need to do a small if test. Place this code in the FindFood Sub.

    VB Code:
    1. Dim i As Long
    2.          
    3.     'If the head finds food
    4.  
    5.     If Snake(0).X = imgFood.Left And Snake(0).Y = imgFood.Top Then
    6.          
    7.     End if


    This if test are testing if the snakes head X and Y coordinates is at the exact same place as the foods X, Y coordinates. We now need some lines of code to make the snake bigger, and then place the new body part of the snake in a new place. To make a new image control of our array of image controls add this line of code in the If statement.

    VB Code:
    1. 'Loads a new instance of the snake body
    2.  
    3. Load imgSnake(Length)


    It is making one new image control. Then we need to place the image control at the end of the snake. Add this code in the same if statement.

    VB Code:
    1. 'Placing the new body on its place
    2.  
    3. If Snake(Length - 1).Facing = 1 Then        'Looking left
    4.  
    5.      Snake(Length).X = Snake(Length - 1).X - imgSnake(0).Width
    6.      Snake(Length).Y = Snake(Length - 1).Y
    7.      imgSnake(Length).Visible = True
    8. ElseIf Snake(Length - 1).Facing = 2 Then    'Looking up
    9.  
    10.      Snake(Length).X = Snake(Length - 1).X
    11.      Snake(Length).Y = Snake(Length - 1).Y + imgSnake(0).Height
    12.      imgSnake(Length).Visible = True
    13. ElseIf Snake(Length - 1).Facing = 3 Then    'Looking right
    14.  
    15.      Snake(Length).X = Snake(Length - 1).X - imgSnake(0).Width
    16.      Snake(Length).Y = Snake(Length - 1).Y
    17.      imgSnake(Length).Visible = True
    18. ElseIf Snake(Length - 1).Facing = 4 Then    'Looking down
    19.  
    20.      Snake(Length).X = Snake(Length - 1).X
    21.      Snake(Length).Y = Snake(Length - 1).Y - imgSnake(0).Height
    22.      imgSnake(Length).Visible = True
    23. End If


    This code is checking to see what way the back of the snake is heading. If it is heading left the new part is added to the right of the snake. If the rear is heading up, the new bodypart is added to the bottom of the snake. It is then setting the visible variable to true of that image control.
    We have now added a new body part. And now we need to increase the length of the snake variable. So add this line in the If statement as well.

    VB Code:
    1. Length = Length + 1


    And then we have to place the food in a new place on the form. But because we did this in another sub early in the game, we only need to write one line of code to make it work. This is a really powerful programming practice. So just add this line to the If statement as well.

    VB Code:
    1. 'Placing the food an other place
    2.  
    3. Call PlaceFood


    You have now finished the "game", and can try it. It is still tons of things that you can add to it to make it a game. But I will leave it here for now. And later when I have more time, I will add more to it. But I can make a list of things that you can try to add to the game.


    - Make it impossible to walk through your own body
    - A picture of an actual head of the snake.
    - A picture of an actual tail of the snake.
    - Make the game stop if the snake hits the outer bounds of the form.
    - Make walls in the game.
    - Add levels to it, with different speed and walls.
    - Add a two player game.


    If you have more questions, please ask in the forum, where there is probably more than just me that can answer to the questions.

    Here is the "full game" zipped down in the attachment.
    Attached Files Attached Files
    Last edited by si_the_geek; Feb 27th, 2004 at 12:52 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width