Lab1.doc

advertisement
Lab 1
Getting Started With Blitz Basic -- Kong
In this laboratory we're going to get familiar with programming in Blitz Basic—how to install
the compiler, how to run the demonstration programs, and how to make changes in an existing
program. Programming is a challenging activity, and it can often seem difficult to get started.
That's why we're going to use the tried and true technique of stuck programmers everywhere:
"when in doubt, start with a working program and make some changes."
If this laboratory seems too advanced for you, don't worry, next week we will start with the
basics of the Blitz Basic language—and writing our own programs from scratch .
Pre-requisites
Read Chapter 1
Vocabulary
All key vocabulary words used in this lab are listed below, with similar terms grouped together:
Compiler
Executable
Program
Pseudocode
Variable
Initialization
Constant
Function
Post-lab Questions
Write your answers after completing the lab, but read them carefully now and keep them in mind
during the lab.
1) how many weird "buggy" behaviors can you determine in the original Kong.bb game?
Describe a couple here:
2) how many functions does the game Kong.bb have?__________ Pick three of them and
describe their names and jobs:
a)
b)
c)
3) What does HUD stand for (see index)?
1
4) (Advanced Students) Suppose you want to add a "spin" value to the ball? Conceptually, how
do you think it might work? What would you want to happen when the ball is hit or bounces off
the wall?
Discussion and Procedure
Part 1. Introduction to Blitz Basic
Before we dive into working with Blitz Basic, we’ll introduce you to the language and answer a
few common questions about it. This background information should help you make sense of
the steps required to write and run Blitz Basic programs.
What is Blitz Basic? Blitz Basic is a programming language, and like any programming
language, it’s used by humans to communicate instructions to computers. Blitz Basic's primary
application is for game programming. It draws on a very popular language from the 1970's
called BASIC, but it's been enhanced with special commands tailored to the needs of game
developers. Tasks such as deleting a destroyed space ship or rotating the image of an object,
which normally require several steps in other programming languages, can be performed with
one line of code in Blitz Basic.
How do I run a Blitz Basic program? In order to run programs in Blitz, we need to first install,
then run a special program provided on the text book CD. This is a demonstration version of the
Blitz compiler. A compiler is a program that translates instructions written in a high level
programming language, for example, Blitz Basic or C++, into the machine language (binary 1's
and 0's) recognized by the microprocessor (Intel, AMD, Motorala) on our computer. This is how
most software is created for the computer.
Normally a compiler produces a stand alone executable file of the high level program that can be
run independently from the compiler by clicking on it. So if we have a program file in Blitz
called myGame.bb (a text file containing Blitz Basic instructions) the compiler will produce a
machine language translation of the program and save it in a file called myGame.exe (an
executable file).
However, since we are only working with a demonstration version of the compiler, we will not
be able to produce our own executable files. Each Blitz program we write will need to be
compiled every time we want to run it. Fortunately, on today's computers, this will only take a
couple seconds. Our demo package also is restricted to 2D graphics only. We will demonstrate a
3D version later in the class. If you decide to go deeper into Blitz programming, you can buy a
regular copy of the 2D or 3D programs for around $100 at www.blitzbasic.com
Part 2. Installing the Blitz Basic compiler program
The first thing you need to do to start experimenting with Blitz Basic is to install the
demonstration program itself, which is on your textbook CD under Programs (along with some
other great programs to create 2D and 3D images and a sound editing program—since time is
limited in this class, you can explore those on your own!)
2
If you don't have a CD you can download BlitzBasicDemo.exe from the class website at
www.mpcfaculty.net/tom_rebold/CSIS196p1.htm and click on the compiler link under this
week's lecture materials.
1. Follow the steps outlined in the setup wizard to install blitz basic on your computer.
2. Once you have the compiler installed, you should be able to locate it by clicking on the
start menu and choosing all programs. Select blitz basic demo to open the program.
3. Once you get past the licensing and advertisements, you'll be in a table of contents where
you can choose a number of directions to explore. The first thing you should do is read
the user's guide. Which explains what all the menus do, and how to use the debugger.
Also pay attention to the toolbar section so you can just click on the tasks you wish to
perform. You should also check out some of the free artwork that is provided and the
Beginners Manual.
4. But we know why you're really here! Click on samples under blitz basic demos and
you'll find a series of 2D games (we'll get to 3D in the last week of class) that you can use
for inspiration (or relaxation). Try these games out and see how good you are. To run a
game, click on the
icon in the toolbar.
Part 3. Download and Play Kong
Before proceeding to step three, also download and expand the file Lab1.zip to get the programs
and files used for this laboratory.
5. The game we will study this week is Kong, a variation of the classic game Pong from the
1970s. To access this game, expand Lab1.zip. This file needs to be unzipped (expanded)
and stored in your MyDocuments folder (at home) or your network folder (at school).
Warning: Do not store any files on the local computer at school (MyDocuments or C:
drive) because the data on these computers can be erased. Also, be careful working with
floppy disks (or even USB Memory Devices) since they can become corrupted. They
should only be used to shuttle files between computers, not as primary storage.
6. Open the expanded Lab1 folder and click on Kong.bb. Play the game, and notice how
the ball responds randomly every time it hits a paddle. Sometimes it changes direction, or
speeds up. Sometimes it seems to get trapped by a paddle. We'll examine that in a later
section.
7. After you become familiar with the game, think about how the program must handle all
of the features you just demonstrated. Look through the program and notice some lines
begin with a semicolon (;) and these are written in what appears to be normal English,
while other lines are in a cryptic language. Lines that begin with ; are called comments.
Comments are optional in a program—the compiler simply ignores them—but they serve
an important role in explaining the purpose of the statements that follow them.
3
8. To see the value of comments, close Kong.bb and open KongPseudo.doc. This is the
same program with only the comments showing—the actual blitz basic code has been
removed. Read the program in this file now and see if it makes any more sense. When
you think about it, it sounds a little bit like an instruction manual or a recipe for making
something.
Part 4. “Breaking” the Kong program and other experiments
If all went well in the previous part of the lab, you successfully ran the Kong program. It’s
valuable to know how things work when you have a correct program, but it’s equally (if not
more) valuable to know how the program will react when you run an incorrect Blitz Basic
program. After all, even the best of us will frequently have to deal with mistakes (“bugs”) we
make writing our programs. For this reason, in this part of the lab, we will concentrate on
experimenting with Kong, changing the Blitz Basic code (sometimes intentionally putting bugs
in it) and seeing what happens when we run it.
We’ll start simple and make some legal changes to the Kong program (i.e., we won’t add any
bugs yet). This will help us get used to switching between editing and running Blitz Basic.
9. Save to a new file before making changes. What will happen if you make a change and it
damages the program and you can't remember what you did? A big headache!
Unfortunately, it seems that the Blitz editor does not have an undo feature. This means
you'll have to practice "defensive editing" and save new copies of your program
frequently. Before continuing to the next up, save the program as Kong1.bb. Make sure
you actually save the file in the same folder (Lab1).
10. Remove the randomness. The first change we will make is to remove the random
changes in velocity that occur when the ball is hit by a paddle or a wall. This will
eliminate some of the confusing bugs that appear during normal play.
Down near the bottom of the file, there is a function called testAI:
;TESTAI()
;Updates ball and score and enemy
Function TestAI()
Several lines below are the statements we will modify. The first group of statements we
will modify are the following:
;Lab1 STEP 10
;If ball hits human player, reflect it away from him and variate its velocity
and direction
If ImagesOverlap(ballimage,ball\x,ball\y,player1image,60,player1\y)
ball\xv = -ball\xv + Rand(-4,4)
ball\yv = ball\yv + Rand(-4,4)
The Rand(-4,4 ) command is what is making the speed of the ball change. We can
eliminate this randomness by turning those commands into comments. Modify the last
4
two lines by placing a ';' before the '+' sign. In other words,
ball\xv = -ball\xv ; + Rand(-4,4)
ball\yv = ball\yv ; + Rand(-4,4)
This is called "commenting out" portions of your code. By placing a ';' (comment) in the
middle of the line, the rest of the line following the comment is ignored by the compiler.
Notice also how the color turns blue indicating it is ignored. That means the random
portion of the statement is ignored and the ball will bounce perfectly predictably.
There are three other groups of statements using the Rand command immediately
following this group. Comment out their Rand commands similarly, then run the
program. How does it work now? Is it easier to play? Are there still strange behaviors?
11. Bring back some randomness . Maybe the game is too easy to play now. You can bring
back some of the randomness by restoring the Rand( ) command for the human player
(the first group of statements in the bunch). Strategically, this puts the human player at an
advantage because he/she has the most time to respond when the ball changes speed.
12. Another advantage for the human . Since we are able to modify the game as we see fit,
why don't we further handicap the computer player by reducing its maximum speed.
Const COMPUTERSPEED = 6 ;The computer's max speed
Simply change the number 6 to a smaller value, and the computer will move more
slowly as it tracks the ball. Why does this work? The word COMPUTERSPEED represents a
constant in the program, a named value that never changes while the program runs. The
code that actually changes the computers paddle is also in the TESTAI()function:
;If ball is above computer, move computer up
If ball\y > player2\y
player2\y = player2\y + COMPUTERSPEED
;if ball is lower than computer, move computer down
ElseIf ball\y < player2\y
player2\y = player2\y - COMPUTERSPEED
EndIf
What this says is that if the y position of the ball is greater than the y position of player2,
then the player2 y position will be increased by COMPUTERSPEED pixels. The reverse
happens if the ball is lower in value than the player2 paddle. In other words, the paddle
will closely track the vertical position of the ball. If you reduce COMPUTERSPEED, you
reduce the ability of the paddle to track the ball, making it easier for the human to win.
13. Break the program! It's time to make an intentional mistake and see what happens to the
program. One of the most important things to pay attention to when programming is
5
precision. In other words, spelling words properly, following the exact rules of the
BASIC language, and expressing exactly what you're trying to convey to the computer.
To see how important spelling is to a computer, try changing the spelling of
COMPUTERSPEED by deleting one letter:
Const COMPUTERSPED = 6 ;The computer's max speed
Notice, the program still runs, but now the paddle for the computer does not even move at
all. Can you explain why this happens?
Is the language case sensitive? What happens if you change the spelling to
ComputerSpeed ?
Before continuing, make any changes you need so the program runs properly again.
14. Break the program again. In this step, we will experiment with the structure of the
program.
Go back to the code snippet at the top of the TESTAI()function. Now remove a piece of
the programming branch structure (if, elseIf, endif), specifically EndIf.
;If ball is above computer, move computer up
If ball\y > player2\y
player2\y = player2\y + COMPUTERSPEED
;if ball is lower than computer, move computer down
ElseIf ball\y < player2\y
player2\y = player2\y - COMPUTERSPEED
EndIf
 Remove This
Run the program and study the cryptic error that results. This is typical in programming
when the grammatical structure of the language is not followed. The computer usually
cannot guess what part of the pattern is broken.
Before proceeding, fix the problem and run the program again to make sure it works.
15. One more break! This time we will change the meaning of the code segment we've been
working with. In the above example code, simply change the > to < and < to >. Run the
program again and observe the results. What happens?
OK, this has gotten way too easy for the human! But you should now have a
sense of how important it is to express your programming ideas precisely. Fix the
mistake and run the program again to make sure it works properly.
6
Part 5. Making COMPUTERSPEED a variable
Before moving on, we're going to make a make a final modification to the way the computer's
speed is handled, so we can make the computer "get tired" and slow down the longer it plays.
To do this we're going to convert COMPUTERSPEED from a constant to a variable. Constants
and variables are very similar. They both are named locations in memory where a number or
other data is stored. In other words, when we say
Const COMPUTERSPEED = 6 ;The computer's max speed
we are effectively making a little box in memory called COMPUTERSPEED and storing a 6 in
it. Because we said it was a constant (Const) we cannot change the value inside the box, It must
remain 6 as long as the program keeps running.
If we make the COMPUTERSPEED box a variable, however, we can change the number that is
stored inside it, and thereby change the computers maximum speed over time.
16. Save the program again! Before making any changes, make sure you save the program
as a new file, this time call it Kong2.bb
17. Make COMPUTERSPEED a variable. Converting COMPUTERSPEED to a Variable is
as easy as changing the above statement to the following:
Global computerspeed = 10 ;The computer's max speed (now set to 10)
The word Global means the variable can be viewed in any of the functions in the program
such as the TESTAI Function we've already worked with. Notice, we had to change the
name of the variable to lowercase—I think this must be a requirement of BlitzBasic.
18. Change all other ocurrences of COMPUTERSPEED to computerspeed. You can use the
Edit>Replace command, make sure to check "match case". Run the program again and it
should behave as it used to, because we haven't changed computerspeed yet.
19. Add a statement to lower the computer's speed
and add the line at the bottom to it.
Find the code in the following code box
;If ball hits computer, reflect it away from computer and variate …
ElseIf ImagesOverlap(ballimage,ball\x,ball\y,player2image,740,player2\y)
ball\xv = -ball\xv ;+ Rand(-4,4)
ball\yv = ball\yv ;+ Rand(-4,4)
computerspeed= computerspeed - 1
 Add this
We've simply added the statement computerspeed= computerspeed – 1 to the section
of code that's activated whenever the computer's paddle hits the ball. What this statement
does is take the existing value of computerspeed, subtract the number 1 from it, and
store the result back into the variable computerspeed. ( more on variables next week!)
7
This means every time the computers paddle hits the ball, and loses one point from its
maximum possible speed, which impacts its ability to track the ball. Eventually, you
might find the computer paddle actually coming to a stop. Or even running away from the
ball! (this happens when the speed drops below zero.)
TWO challenges for the adventurous:
1) put in a safeguard so that when the computer speed drops to something like 2 it no
longer diminishes in value (this will require constructing your own if statement)
2) put in a statement that resets the computer speed back to its initial value at the start of
every game.
Part 6. Further Explorations (Optional)
To finish off this lab, we’re going to fix one more bug that crops up now and then. You may
have already noticed the bug. Sometimes, the ball seems to get trapped by a paddle and then
shoots off at a high speed either backwards or forwards. Sometimes it seems to rob the player (or
computer) of a successful hit. after some investigation, it turns out this bug is a result of an error
in determining when two images overlap. If the ball is moving too slowly, then immediately
after a paddle hit, it could still be within the "overlap range" of the paddle and trigger another
velocity reversal. This traps the ball inside the image of the paddle until the velocity is randomly
changed to a high enough value for it to escape.
One solution to this problem would be to modify the code for the ImagesOverlap function so it
does not trigger false overlaps. A simpler solution is to modify the game itself so that this
problem will not cause the ball to get trapped. Programmers must often try to make faulty code
perform the way it's supposed to by creating their own "work arounds" and this is what we're
going to do here.
The problem can be restated as "sometimes, the ball interacts with a player's paddle much too
frequently causing it to become trapped." In this light, we can try to figure out a way to slow
down the interaction between the ball and the paddle. A fairly simple approach is to only allow
the ball to interact with a paddle when it's that player's turn. After an interaction, the ball has to
interact with the other player's paddle before it can interact again with the original paddle. This
will keep the ball from getting too interactive with a single paddle.
The implementation of this fix requires the use of a variable to keep track of who's turn it is.
(We will talk about variables and other programming basics next week.) We will create a
variable called turn that stores either the number 1 if it's the human player's turn, or the
number 2 if it's the computer's turn. Then, when a ball is close to a paddle we will check to make
sure it's the proper player's turn before changing the velocity of the ball. This may not make a lot
of sense that destroyed a fall long the best you can.
20. Save the program again! Before making any changes, make sure you save the program
as a new file, this time call it Kong3.bb
8
21. Create the turn variable. Near the top of the program is a block of code headed by the
comment TYPE INITIALIZATION . Find this block, which is shown in the code box
below, and add the two lines shown at the bottom of the code box.
;TYPE INITIALIZATION
;Create a ball
Global ball.ball = New ball
;Create the human
Global player1.player = New player
;Create the computer
Global player2.player = New player
; Keep track who's turn
Global turn = 2


Add this
Add this
The statement Global turn = 2
Creates a variable (a named memory
location that stores data, more on this next week) called turn, and stores a number 2
inside it. The word Global means the variable can be viewed in any of the functions in
the program such as the TESTAI Function we've already worked with.
Why do we put a 2 inside it? Because when the game starts, it's player 2's turn (the
computer).
22. Modify the TESTAI function. Now go back to the TESTAI Function we worked on in
previously, near the bottom of the file. Find the code to illustrated in the box below, and
add the lines indicated.
;If ball hits human player, reflect away from him and variate its velocity and direction
If ImagesOverlap(ballimage,ball\x,ball\y,player1image,60,player1\y) And turn = 1
ball\xv = -ball\xv + Rand(-4,4)
\
ball\yv = ball\yv + Rand(-4,4)
Add this
turn = 2
 Add this
;If ball hits computer, reflect away from computer and variate its velocity and
direction
ElseIf ImagesOverlap(ballimage,ball\x,ball\y,player2image,740,player2\y) And turn = 2
ball\xv = -ball\xv ;+ Rand(-4,4)
\
ball\yv = ball\yv ;+ Rand(-4,4)
Add this
turn = 1
 Add this
What's this all about? Well, the modifications you just made are checking not only that
the images overlap but also that it's the right players turn before modifying the velocity of
the ball (and changing turns). So if the ball overlaps the player1 paddle AND it's
player1's turn to hit the ball, then the ball changes direction and it's now player2's turn (so
there can't be any more interactions with player1 paddle until player2 hits it back).
Likewise if player2 hits the ball.
This simple change improves things so that the paddle can never trap the ball. Try
playing the game now. It should work OK, but you'll soon discover a new error that has
cropped up. This is also typical in programming: when you make a change, the change
9
creates an error somewhere else. In this case it's easy to fix. The problem is the player's
turn needs to be initialized after each game. In other words, we have to place turn=2 in
one more location—Inside the INITIALIZELEVEL Function.
23. Add turn=2 to the INITIALIZELEVEL Function. Find the code illustrated in the code box
below and make the change. This function sets up the starting values for the ball in the
player paddles. Now it establishes who's turn it is is at the beginning of the game as well.
;FUNCTIONS
;INITIALIZELEVEL()
;Sets up starting values
Function InitializeLevel()
;Put ball in center of the screen
ball\x = 400
ball\y = 300
;Make the ball move in a random direction
ball\xv = Rand(2,6)
ball\yv = Rand(-8,8)
;Place the players in their correct position
player2\y = 300
player1\y = 300
; make player 2's turn
turn=2
End Function


Add this
Add this
Further Reading
 Blitz Basic is a popular language for game development, so you may want to check out the
company web site for additional game examples. www.blitzbasic.com
10
Download