130 likes | 240 Views
Programming games using Visual Basic. Files; files vs. databases Reprise on state of program Lab/HW: work on projects, final project. Files. also known as ‘flat files’ or external files.
E N D
Programming games using Visual Basic Files; files vs. databases Reprise on state of program Lab/HW: work on projects, final project.
Files • also known as ‘flat files’ or external files. • Files exist independently of the VB program. Used to hold data that must be maintained ‘in-between’ executions of the program. • Files hold records • Records may hold individual fields • Only structure is sequencing of records. • You code everything: additions, modifications, deletions of records.
Databases • Databases are complex collections of data. • tables • records • fields • reports (queries) • relationships (cross-references) among fields • built-in checks • Databases are implemented with ‘stand-alone’ database management systems (for example, File Maker Pro, Access, MySQL, DBII, Oracle products)
File versus database • File implementation • generally cheaper (no use of DBMS) • less storage space • quicker execution • quicker implementation (assuming simple tasks) • Database implementation • quicker implementation (assuming that requirement is for all the standard tasks: sequencing to record, implementation of queries, adding, modifying, deleting)
Files versus databases • Move to databases (or initiate project with databases) if • Complex • and tables suit the complexity. Certain applications are not suited to tables. • more than one program using the information • potential for expansion • already have database software installed & programmers trained
Game applications • Saving set of best scores • Saving state of game in order to resume the game • ?
Side trip: strings • Visual Basic strings are • DIM strName as String • Basic data type • Flexible in length but you can specify a fixed length string: • DIM strCourseName as String * 10 • Other programming languages may handle strings differently: • Char as the primitive data type, with strings implemented as arrays of Char's.
VB types fixed length string Private Type bestdata strBname as String * 20 intBscore as Integer End Type … Dim newbest as bestdata … newbest.strBname = strPlayername intTop = newbest.intBscore
VB Files: Opening intLen = Len(newBest) Open “best.dat” for Random as intBest Len=intLen Open is used to allow VB to set up use of the file. best.dat is the filename & extension Random indicates how the file will be used. It is the most general: additions, deletions, changes, and in any order intBest sets up a file number. (Since only one file is used in the bestscores project, it could be replaced by the constant 1) intLen tells VB how big the records are for this file.
VB files: read & write Get #intBest, i, oldbest intBest indicates the file. The # is required. Use what you coded in the Open statement i indicates which record number oldbest is where the data will be put Put #intBest, i, newbest newbest holds the data (in this case, a string and an integer) to be written to the file.
File issues • Does the file need to exist before the application? • If you open a file for writing (output), will 'the system' create the file for you? • Visual Basic will create the file if it does not exist. • When reading from a file, how do you know when you have reached the end-of-file? • See chapter 10.
State of the game • What information is required to store and resume • mix & match cartoons • chance • memory • hangman • cannonball • quiz show • minesweeper • tic tac toe • variations • When would you want to store the history of a game?
Homework/lab • Manage your time • work on your final project • Be prepared to show projects next week • We do have Wednesday class • Final quiz 5/13 • Last day to show work: 5/8.