




已阅读5页,还剩43页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 i = 0; while (i 10)addObject (new Duke (“k”, “test.wav”),150, 100);Increment the Loop VariableThen, increment the loop 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 i = 0; while (i 10)addObject (new Duke (“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 same 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 of 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. 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 forever.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 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 arm 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 pressed 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 assigned 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 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.would 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 expression (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 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 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 Greenfoot 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 it 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 array, you can hold and access multiple variables, and assign different values to new instances each time they are created.An array is an object that holds multiple variables. An index can be used to access the variables.How Variables Hold ValuesA simple String variable named “keyname” is a container that holds a value: A single keyboard keys name.String keyname;Keyname containeraHow Arrays Hold VariablesAn array object can hold many variables. This array namedkeynames can hold many variables.String keynames0123“a”“s”“d”“f”Variable Declaration for an ArrayTo declare an array object, write the variable declaration as follows: Element type String for an array of Strings int for an array of integers Square brackets to indicate that this variable is an array Variable assignment Expression that creates the array object and fills it with an unlimited number of Strings or integersArray ExampleIn this array: The keynames string variable is created “a”, “s”, “d”, and “f”, Strings are the array object Array object is assigned to the variable keynamesString keynames;keynames = “a”, “s”, “d”, “f”;Accessing Elements in an ArrayUse an index to access the elements in the array object.Elements are accessed using square brackets ( ) and an index to specify which array element to access. An index is a position number in the array object.To use an index: Each String or integer has an index, starting at position zero 0. Each elements position increases by 1.String Index0123Element“a”“s”“d”“f”Access Elements in an ArrayTo access an element in the array, attach the index for that element in square brackets to the array name.The statement keynames3 accesses the array element at index 3the String “f”. This is the fourth element in the array.Play Different Sounds When Keys PressedTo make an instance able to play a variety of sounds based on which keyboard key is pressed: Create two arrays in the World class that include: Names of keyboard keys for Duke instancesNames of sound files for Duke instances Declare fields in the World class for those arrays Store the filled arrayswell as a number of counters to control the gameplay.Create Table VariablesCreate the following variables in the Table class. Save and compile the scenario.Setting Up the BlackJack TableThe BlackJack table (the Table class) requires the following set up each time the scenario is initialized: The deck of cards to draw from 5 spaces to hold the dealer cards 5 spaces to hold the players cards A button to end the round and begin the card checkingThis set up should take place automatically each time the scenario is initialized.Edit Table ClassOpen the code editor for the Table class and edit the Table constructor to increase the playing surface area:Super(600, 400, 1);within the public Table() method to Super(800, 600, 1);Then, enter the code shown on the following slide in the Table constructor to construct the objects on the table. When finished, save the scenario, but do not compile.Table ConstructorCoding Dealer GameplaySpecifications for the BlackJack dealer: Fully automated card dealing. Results in a hand of cards for the dealer which may be more than 21 but never less than 15. Two cards will be drawn and if they are less than 15, another will be drawn until the total is greater than 15. The number of cards drawn will be shown to the player but they will be face down.Create dealerHand MethodBeneath the Table constructor, create the dealerHand method shown on this slide and the next slide to code the dealer gameplay. Save and compile the scenario.Create dealerHand MethodScenario ExampleYour scenario should look similar to the following. Reset the scenario a few times to test that the dealer draws a different number of cards.Coding End of GameplayNext, enter the endGame method in the Table class to code what happens when the player clicks the Play Cards button. This method: Starts the endGame method Counts the players total Shows the dealers cards Checks to see who wins Updates the Play Game button image to display winner Stops the gameEnter the endGame method as shown on the following slide. Save and compile the code.Create endGame MethodCoding the Games ActionFinally, code the following specifications in the Table classs act method for how the BlackJack table will act when the player clicks on objects: React to clicks on the players hand to deal a card, but only up to 5 cards. A click anywhere in the hand will plac
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 夏天的沙滩作文
- 2025年教师招聘之《幼儿教师招聘》通关练习题库包及参考答案详解【夺分金卷】
- 2025内蒙古呼伦贝尔农垦集团有限公司招聘笔试及答案详解(新)
- 2025年教师招聘之《小学教师招聘》题库必背100题及完整答案详解【夺冠系列】
- 成都安全管理培训课件
- 卫浴投票活动策划方案范文
- 2025年公安辅警招聘知识考试题库附含答案
- 彩超在腹部的运用
- 工业母机关键核心部件国产化替代技术创新趋势报告
- 公安机关封控应急处置预案
- 初级招标采购从业人员《招标采购法律法规》近年考试真题试题库(含答案)
- 教学评一体化理念
- 人音版七年级音乐上册教案全册
- ECE-R90-欧盟第3版-中文版(R090r3e-01)
- 2023学年武汉市武昌区九年级语文上学期期中检测试卷附答案
- 渠道衬砌施工方案(渠道预制混凝土块)
- 不交社保劳动合同模板
- 2024年云南省中考数学试题(含答案)
- GB 14102.1-2024防火卷帘第1部分:通用技术条件
- 越野跑策划方案
- 《光学含沙量测量仪率定规范》
评论
0/150
提交评论