Visual Basic .NET BASICS - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Visual Basic .NET BASICS

Description:

The Quit button differs from the Stop Game button. The Quit button ends the program. ... The Stop Game and Quit buttons also end the game. ... – PowerPoint PPT presentation

Number of Views:319
Avg rating:3.0/5.0
Slides: 21
Provided by: stephenc9
Category:
Tags: basics | net | basic | game | stop | visual

less

Transcript and Presenter's Notes

Title: Visual Basic .NET BASICS


1
Visual Basic .NET BASICS
  • Lesson 16
  • Case Study Snake Game

2
Objectives
  • Run the Snake Game.
  • Draw with individual pixels and use the Paint
    event.
  • Describe how the Snake Game draws and controls
    the direction of the snake.
  • Describe how the code of the Snake Game functions.

3
Running the Snake Game
  • The Snake Game that you created in Lesson 1 is an
    example of how a simple game can be created in
    Visual Basic .NET with very little code.
  • Writing a program like the Snake Game is based on
    a very simple principle.

4
Drawing with Pixels and Using the Paint Event
Handler
  • There are only four Visual Basic .NET features
    that the Snake Game uses that you have not
    learned
  • The feature that allows you to create an image
    and draw it on your form
  • The method that allows you to draw on an image
    one pixel at a time
  • A feature that allows you to retrieve the color
    of a specific pixel
  • A paint event that prevents the graphics you draw
    from being erased if another window covers the
    graphic you have already drawn

5
Creating a Graphic Image
  • A Bitmap object is an object used to work with
    images defined by pixel data.
  • A Bitmap object and a Graphics object are created
    using Dim statements.
  • The DrawImage method can be called directly after
    any change has been made to the image.
  • The Paint method is called whenever the form
    needs to be drawn.

6
Using the SetPixel Method
  • The SetPixel method of the Bitmap object is an
    easy-to-use feature that allows you to change the
    color of individual pixels on a bitmap.
  • To use SetPixel, you identify the X,Y coordinates
    and specify a color for the pixel.

7
Using the GetPixel Method
  • The GetPixel method returns the Color structure
    of a pixel at the specified pixel.
  • Use SetPixel to change the color of a pixel and
    use GetPixel to determine the current color of a
    pixel.
  • The ToArgb method is used to convert this
    structure into a more easily compared integer
    value.

8
Handling the Paint Event
  • The Paint event is called whenever an object is
    resized, covered, uncovered, minimized, and then
    restored or when the Refresh event is called.
  • This event can make sure that the board is
    redrawn or repainted to prevent from losing data
    from the form.

9
How the Snake Game Draws the Snake
  • The snake that appears on the Snake Game is drawn
    one pixel at a time using SetPixel.
  • There are four variables that are key in
    controlling the snake.

10
Analyzing the Snake Game Code
  • The primary work of the game is done in the Start
    Game buttons Click event procedure.
  • The Application.DoEvents function allows other
    events to be processed as the Start Game buttons
    Click event procedure is being executed.
  • The Do Events function also allows the user to
    click the direction buttons.

11
Form-Level Variables
  • There are three form-level variables necessary.
  • First, we need a way to stop the program if the
    user clicks the Stop Game button.
  • The intXFactor and intYFactor variables must be
    form-level because the factors will be changed by
    the direction buttons and used by the Start Game
    button.

12
The Start Game Button
  • The Start Game Click event procedure is the
    primary code for the game.
  • At the heart of the event procedure is a loop.
  • First six local variables are declared to be used
    in this procedure.
  • Now that all the initial work is done, the
    DrawBoard method is called to draw a box with a
    border around it.

13
The Start Game Buttons Loop
  • The loop adds one pixel to the snake with each
    interaction.
  • The intXFactor and intYFactor determine where the
    next pixel is added.
  • The GetPixel method looks at the color of the
    next pixel that the program would write to and
    makes sure that it is not already clrSnakeColor.
  • Clicking the End Game button would change the
    value of blnStop, which would end the game.

14
Changing the Coordinate Factors
  • The intXFactor and intYFactor variables are
    adjusted through the Click event procedures of
    the four direction buttons.
  • Because intXFactor and intYFactor are form-level
    variables, the new values for these factors will
    remain set until one of the other direction
    buttons changes the values.

15
The Stop Game Button
  • The code for the Stop Game button simply changes
    the blnStop to True.
  • If the Stop Game button is pressed while the game
    is in play and blnStop is changed to True, the
    loop will stop.

16
The Quit button
  • The Quit button differs from the Stop Game
    button.
  • The Quit button ends the program.

17
Summary
  • The Snake Game is an example of a simple game
    created in Visual Basic. NET.
  • The SetPixel method allows you to change the
    color of individual pixels on a bitmap.
  • The GetPixel method retrieves the color of the
    specified pixel on a bitmap.
  • The Refresh method causes the Paint event to
    execute.

18
Summary (continued)
  • The Paint event handler can be used to redraw
    objects that are erased when a form is covered or
    minimized.
  • The Snake Game keeps track of a point on the form
    where the next pixel will appear as the snake
    grows.
  • The snake is controlled by two factors. One
    factor specifies the change in the X coordi-nate
    and the other factor specifies the change in the
    Y coordinate.

19
Summary (continued)
  • Once in play, the Snake Game continues until the
    user runs the snake into a wall or the snakes
    tail. The Stop Game and Quit buttons also end the
    game.
  • The direction buttons change the factors that
    control the direction of the snake. The variables
    that store the factors are form-level so that
    multiple buttons can change the value of the
    factors.

20
Summary (continued)
  • The color of the wall and snake are con-trolled
    by changing the value of clrSnakeColor.
  • The speed of the snake is controlled by adjusting
    the value in the Sleep method call.
Write a Comment
User Comments (0)
About PowerShow.com