1 Introduction Creating a new scenario Using the API Creating a world Creating new actors Making things move Random behavious Dealing with images Detecting other objects (collisions) Keyboard input Mouse input Playing audio Controlling the scenario Using support classes Exporting a scenario Introduction Greenfoot is a software tool designed to let beginners get experience with object-oriented programming. You can play with the finished game (including a couple of simple AI players) over on the Greenfoot site.The Greenfoot Programmers' Manual Version 1.1 for Greenfoot 2.0 Michael Kölling, Martin Pain University of Kent Copyright © M Kölling, 2006, 2009, 2010 The Greenfoot Programmers' Manual is licensed under a Creative Commons Attribution-Non-Commercial 2.0 UK: England & Wales License. The 8000 in the last line is simply the volume of the sound (the height of the sine wave). The 0.25 and the 3200 on the middle line of the loop adjust the amount that the height of the projectile affects the frequency of the sine wave. There are three constants in this code that I fiddled with until they sounded right (literally!). Here’s some code that does just that, using the height calculation from our previous post:įor (int i = 0 i < soundWave.length i++)ĭouble t = (double)i / (timePerFrame * 44000.0) Ī += 0.25 + (t * vy - 0.5 * g * t * (t - 1)) / 3200.0 What this means is that instead of using a steadily-increasing number to feed to the sine wave function, you increase the number faster when the banana is higher. The gist is something like this - the sine wave gets more frequent (the peaks get closer together, horizontally), the higher the projectile is in the air: This creates a more familiar flight sound (such as you hear in cartoons). If we change the frequency in proportion to the height of the banana, we get a sound that starts low, increases in pitch as the banana reaches the apex of its flight, then decreases again as it comes back down. We can greatly improve the suitability of the sound if we vary the frequency of the sine wave during the flight. Just playing a simple sine wave for the sound of the flight is dull and incongruous. I subtract 5 frames off the total, to make sure that the sound is finished before we need to play the impact sound. The length of the sound in seconds will be the time it takes to execute one frame in Greenfoot, multiplied by the number of frames before impact (“impactTime”, above - which we saw how to calculate last post). The sound will play at 44000 samples per second, so we need to multiply the number of seconds by 44000 to get the number of samples. We can use this information to construct a sound that’s exactly as long as the time the banana will spend in the air:ĭouble lengthInSecs = timePerFrame * (impactTime - 5) We saw in our last post that for our monkeys-throwing-bananas game, we can work out how long the banana will be in flight for. Here’s an example of a sine wave on youtube. If you treat this as a sound wave, then you get a dull tone that we recognise as an electronic beep, since it’s used so often by electronic devices. If you take an ever increasing number and pass it to the sine function, you get back this graph: As well as being useful for working with triangles, the sine function can also be used for generating sounds.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |