Almost all playstation games have one thing in common: they use the controller. And as you play one game after the other, you start to discover patterns in the way the controller is used; many games become like a cross-over between Simon Says and an advanced form of memory - you have to remember a certain string of repeated commands, often to a certain time-pattern, to achieve a certain goal. Sometimes this gets a little repetetive and you start wondering whether game-designers can't come up with something a little more original; but the things is that there are only a certain amount of combinations of button-presses that are feasible within any kind of arcade game, and the more games there are out there, the more often those combinations are bound to be similar. There is also an advantage of course - games can take a little less long to get into, as you know what you can do with the buttons as an experienced user. But games don't like to be too much like the next one, and so their programmers look for something different, usually of ever increasing complexity. Right now, in Yabasic, our options are even more limited than theirs - we cannot use the analog buttons and sticks to full force, nor can we program the vibration function (not yet, anyway). That's a shame, but don't think it leaves us empty-handed: there are still the cross, triangle, square and circle buttons, the R1, L1, R2, L2, R3 and L3 buttons, the left, right, up and down buttons, and the start and select buttons. Additionally, with the peek("port") function, we can read any combination of these buttons that human hands can meaningfully press, and add meaning to those combinations of presses. But that's not all - we can also read practically any combination of these buttons for additional permutations and assign a certain kind of meaning to that. (That's using the peek("port1") command - a little more can be read through the inkey$ command, but that makes reading combinations of commands much harder and slower). And finally, we can add a time factor: we can assign meaning to certain time-spans in which these button-presses follow each other - just like, for instance, single and double mouseclicks can mean different things on a PC. Alltogether that makes for a fairly rich palette of commands that we can read from the controller, and there are a whole bunch of games that use this system, a good example of which is the dance game "Bust-A-Groove", and of course our most beloved "Tekken" series. Both use timed button sequences and button combinations to control their characters. "Tekken" is perhaps the ultimate example, considering the vast array of characters and moves available in that game, and we'll use it as a reference throughout this section. If you look up the peek("port1") command in the reference, you'll see that it returns a number which represents the status of the controller. This number is not just a random number selected for each combination of numbers, but in fact is a translation of a number of on/off status bits, where each bit represents a button and the bits together represent a binary number that is translated to the decimal system. You may very well not know what a binary number is. Well, let me give you a speedy introduction to a world of 0 and 1's. Computers work with microswitches which have one of two states - on or off. By taking a group of switches, you can make different combinations of on or off, which together can produce a certain number of different states. For instance, a group of four bits can produce 16 different states: off - off - off - off (0000), off - off - off - on (0001), off - off - on - off (0010), off - off - on - on (0011), etc. If you take a careful look at this system and our own system of numbers, you'll see it works very similarly - the major difference is that you count to two (or from 0 to 1) before you begin with the next digit, instead of from 0 to 9. That means that each number represents a power of 2, rather than a power of 10. So, where 11 reprents 1 x 10 and 1 x 1 (=11) in the decimal system, 11 represents 1 x 2 and 1 x 1 (=3) in the binary system. Going back to the PS/2 controller, if we take one bit each for each button that we want to know the status of (either on or off, pressed or not pressed), then with a 16bit value we can represent the status of 16 different buttons. And that's pretty much how it works on the PS/2 controller:
Now, say that the triangle was pressed together with the left button. This means that the decimal value that represents the status of the controller now becomes 2^12 + 2^7 = 4096 + 128 = 4224. So, if you use the command print peek("port1") and the result is 4224, then you know that at the time of running the command, the triangle and the left button were both pressed. Again, see the reference under getting input from the controller for a more detailed table. But what if I'm only interested in whether the left button is pressed, no matter what other button is being pressed? This is tricky - after all, the value can be something else every time I check, because each different button pressed together with the left button adds a different value to the value of the left button. You saw it with the triangle button - without the triangle button, the value would have been 128, but it would still mean that the left button was pressed. We can solve this problem by remembering that each bit independently represents a button. So if I only wish to know the status of the left button, I must test the status of the 7th bit. This I can do through the binary and function - I take a number that represent the seventh bit (128), and binary and this number with another number which may or may not contain the seventh bit, like 4224. Internally, the program then looks at the numbers in their binary representatives (0000010000000 and 1000010000000), and compares these binary numbers digit for digit. If the '1's in the first number then coincide with '1's in the second number, the binary and function returns true, and you know that the 7th bit is being pressed, regardless of whatever other button is being pressed at the same time. The following bit of code demonstrates this:
First you have to press the triangle key to continue, then you have to press the left button together with the triangle key. It's almost like a game, isn't it? Well, maybe not quite, but it's a start. Next up is a general routine which displays any kind of button combination that is being pressed. Don't worry things will get a little more spectacular soon - but it's important that you know a little bit of this background material first, you'll better understand what you're doing later on when things get a little more complex ... . (c) 2001 Arwin van Arum "Sony" and "Playstation 2" are registered trademarks owned by Sony Inc. "Tekken" and "Bust-A-Groove" are registered trademarks and are owned by Namco. |