Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics...

18
Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Transcript of Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics...

Page 1: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Programming Games in Visual Basic: Data base

Catch up questions

Data base: generalities & specifics on Visual Basic links

Lab: work on projects.

Page 2: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Questions?

• Hangman– ‘word bank’ is an array of strings

– Dynamically loading alphabet (label control array)

– String operations

• Cannonball– moving ball (setup in Fire button Click, movement &

checking for hitting target or ground in timer)

– press button & drag

Page 3: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Suggestions

• Reflect [back] on a project. What was difficult? What was similar to another project?

• Similarities & differences between projects– Use of images in memory versus mixed & hangman– Use of Booleans (flags) for firstturn & follow-up in

chance, first and second turns in memory– Clicking on labels– Data in labels (memory) versus internal variables– Counting: matches in memory; matches in hangman;

turns in hangman– ?

Page 4: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Data base terminology

is a collection of files. A data base management system (DBMS) is a software application for creating & modifying databases and for producing report.

DatabaseTables

Records (‘rows’)Fields (attributes, ‘columns’)

Page 5: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Example (simpler than in quiz)

• Table = the questions• A question holds:

– Text of question– Answer– Value– Category

• So, in the simple case: 4 fields = 4 attributes. Datatype of 1st and 2nd and 4th field is text. Datatype of 3rd field is number.

Page 6: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Data base terminology, cont.

A query is a request for information from a data base.

SQL (structured query language) is the standard for queries to a data base.Select * from questions where category = ‘trivia’

Get the whole record (all attributes) from all the records from the questions table in which the category attribute equals ‘trivia’

Select question, answer from questions where used= ‘false’

Get the question and answer attributes from the questions table in which the used attribute is false

Select distinct category from archive

Get all the distinct (no duplicate) values of the category attribute

Page 7: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Advanced Warning

• Visual Basic statements involving Select statements have complex syntax:– A string containing the Select statement is sent to the

database.– The string may be made by concatenating strings and

the value of variables.– There may be single quotation marks and double

quotation marks.– Long statements may need to be continued on the next

line, requiring an underscore _

Page 8: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Quiz project

• VB to DB connection for– Controls on quiz form and DB edit form

• Set properties in the data control

– Generating list of categories• Generate a special recordset with just one field

– Generating list of non-used questions within a specific category

• Generate a recordset for the controls. Use Rnd to advance the cursor to a random choice within this set.

Page 9: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Setting up VB to database connections

• Connecting data control to the database and the table (there is only one) in the database

dbQuestions.DatabaseName = App.Path & "/quiz.mdb"dbQuestions.RecordSource = "archive"

• In the eventhandler for setting up the list of categoriesDim dbQuestions as DataBaseDim rst as Recordset

Set dbQuestions = OpenDatabase(App.Path & "/quiz.mdb")Set rst = _dbQuestions.OpenRecordset("Select distinct category from

archive")

Page 10: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Example of query

Note: your code has already set up dbQuestions as the link to the database.

dbQuestions.RecordSource = "Select * from " _ & "archive where category = '" & strCat _ & "' And " _ & "used = 0“

The words “Select * from archive where category = ‘”are taken literally. Then it is combined with the value of strCat and then the literal

“’ And used = 0”The single quotation marks will surround the actual value in the variable strCat

(namely the selected category).

Page 11: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Visual Basic / Data base

• Data control– at design time or run time, specify database

AND table

• Database ‘aware’ controls– link to the data control AND link to specific

field

Page 12: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Click & then select database

Choose table in database

Page 13: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

When the pulldown was clicked, the menu shows the only datacontrol.

Field in table in database

Page 14: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Visual Basic / database

• Recordset is set (sequence) of records!

• Implicit notion of the current record. This can be changed– movefirst– movenext– moveprev

Page 15: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Recordsets

• Can be associated with a data control and controls on the form

• OR

• can be independent of data control (and controls on the form)

Page 16: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Quiz project

Database is one table, named questionsA question is a record in the table.Question includes the following fields:

Question texttwo model answers (more on this later)Point value (may be set as currency)categoryused switch (yes/no)

Page 17: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Comparing strings to patterns

• Various ways of using special characters for patterns.

• The patterns suggested in the text use just the wild card character *

If strAns Like strModel then…

If strAns holds “Christopher Columbus” andstrModel holds “*Columbus” then the Like operator will return true

• Consult HELP for more possibilities

Page 18: Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.

Homework/Lab

• Continue with projects• Read text• Note: chapter 7 / Quiz show is one of the

'advanced' projects. • Your own project can be enhancement of

one of the 6 projects, one of the advanced projects or an existing game or something totally of your own design.