游戏编程教学2.doc
《游戏编程教学2.doc》由会员分享,可在线阅读,更多相关《游戏编程教学2.doc(48页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Insert the Statements to ExecuteIn brackets, insert the statements to execute.ExampleAdd 10 new Duke objects with a specific keyboard key and sound file attached to each object.int i = 0; while (i 10)addObject (new Duke (“k”, “test.wav”),150, 100);Increment the Loop VariableThen, increment the loop
2、variable as follows: Insert a statement at the end of the loop body to increase the loop variable by 1 each time the loop is executed. Use closed brackets to end the statement.This will change the variable with each loop to ensure it does not loop indefinitely.int i = 0; while (i 10)addObject (new D
3、uke (“k”, “test.wav”),150, 150); i = i + 1;While Loop ExampleThis while loop was inserted into the World constructor and creates 10 Dukes when the world is initialized.Object Placement and While LoopsIn the previous example, when the constructor is executed, all of the instances are placed at the sa
4、me coordinates. This causes them to sit on top of each other.We can add an expression that calculates the size of an instance, and then places subsequent instances at different coordinates.Calculate the Placement of InstancesTo program instances so they land at different coordinates and not on top o
5、f each other, replace the fixed x- coordinate for the objects width with an expression that includes: Variable i Multiplication operator (*) Fixed offset integer so the first object does not land off-screenInfinite LoopsIf an end to the loop isnt established, the loop keeps executing and never stops
6、. Infinite loops are a common problem in programming.An infinite loop is when the loop keeps executing and does not stop because the end to the loop isnt established.With an infinite loop: The variable would never change. The condition would always remain true. The loop would continue looping foreve
7、r.Animating Objects with a Keyboard KeyAnother way to animate an object is to have the object change the image it displays when a keyboard key is pressed.Pseudocode for this action: Switch between two images when a key is pressed When key is pressed, show image1 When key is not pressed, show image2
8、Object needs to remember if the key is pressed or not (otherwise, it will rapidly switch the object it displays with no control by the keyboard key)Keyboard Key ExampleDukes arm should wave if a keyboard key is pressed. Two images are saved in the scenario: One with Dukes arm up, and one with his ar
9、m down.Pseudocode for this action:If Dukes arm is up, and the keyboard key is down thenchange the image to show the image with Dukes arm down.Remember that Dukes arm is currently down.Specify Image to DisplayFirst, write the code in the classs act method to specify the image to show if the key is pr
10、essed down, or not. Use the following methods: isKeyDown Greenfoot method (use dot notation) setImage methodpublic void act()if (Greenfoot.isKeyDown(“k”) setImage (“Duke.PNG”);else setImage (“DukeDown.PNG”);Declare isDown VariableNext, declare the isDown variablein the classs source code, and assign
11、ed it to Duke, to tell Duke to remember if the key is pressed down or not.True/false condition: True when keyboard key is pressed down False when keyboard key is not pressed downisDown Variable ExampleVariable isDown is declared and set to false, because the scenario starts with the keyboard key not
12、 down.Logic OperatorsTo test if Dukes arm is up or down when a key is pressed, this requires: Multiple boolean expressions to express if one or both are true or false. Logic operators to connect the boolean expressions. For example, the first statement:If Dukes arm is not down, and the “d” is down.w
13、ould be coded as:if (!isDown & Greenfoot.isKeyDown(“d”) )Types of Logic OperatorsLogic operators can be used to combine multiple boolean expressions into one boolean expression.Logic OperatorsLogic OperatorMeansDefinitionExclamation Mark (!)Double ampersand (&)NOTReverses the value of a boolean expr
14、ession (if b is true, !b is false. If b is false, !b is true).ANDCombines two boolean values, and returns a boolean value which is true if and only if both of its operands are true.Two lines (II)ORCombines two boolean variables orexpressions and returns a result that is true if either or both of its
15、 operands are true.Logic Operators ExampleLogic operators set the image that appears if the “d” key is up or down.Play SoundNow that the statement is programmed to animate Duke, the last step is to program the statement for Duke to make a sound when the “d” key is pressed, in addition to moving his
16、arm.Define the method to play the sound, so you can call it in the act method when the specific key is pressed down.Define Play Method in ClassFirst, define a method in the class called play. Write the method below the act method, as shown below.This method: Calls the playSound method from the Green
17、foot class using dot notation in the body of the if statement. Includes the sound file name to play.Enter Play Method in Act MethodEnter the play method in the act method: Enter it into one of the if statements to have it play when a keyboard key is pressed. Enter it below the if statement to have i
18、t play continuously during the game.ArraysWhen you create multiple instances using a while loop constructor, each receives the same sound file and keyboard key assignment.In most situations, this isnt ideal. Instances may need to react to different keyboard keys, or play different sounds.Using an ar
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 游戏 编程 教学
限制150内