|
| 1 | +# Same imports once again |
| 2 | + |
| 3 | +import cocos |
| 4 | +from cocos.text import Label |
| 5 | +from cocos import scene |
| 6 | +from cocos.layer import Layer |
| 7 | +from cocos.director import director |
| 8 | +from cocos.sprite import Sprite |
| 9 | + |
| 10 | +# Except for this import. This import, from Pyglet (one of the dependencies of cocos), is to recognize keyboard codes |
| 11 | +from pyglet.window.key import symbol_string |
| 12 | + |
| 13 | + |
| 14 | +# Similar starting code to before with one exception |
| 15 | +class KeyboardInput(Layer): |
| 16 | + # You need to tell cocos that your layer is for handling input! |
| 17 | + # This is key (no pun intended)! |
| 18 | + # If you don't include this you'll be scratching your head wondering why your game isn't accepting input |
| 19 | + is_event_handler = True |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + super(KeyboardInput, self).__init__() |
| 23 | + |
| 24 | + # Let's make a Label like we did in the HelloWorld sample to show the keys being pressed |
| 25 | + # We will write code to append the key being pressed further down in the file |
| 26 | + self.label = Label("Keys: ", |
| 27 | + font_name = "Helvetica", |
| 28 | + font_size = 32, |
| 29 | + anchor_x = "center", |
| 30 | + anchor_y = "center") |
| 31 | + self.label.position = 320, 240 |
| 32 | + |
| 33 | + # Here I make a variable to store the keys being pressed |
| 34 | + # set() creates a new set object. |
| 35 | + # A set object is an object built into Python that stores things in, you guessed it, sets |
| 36 | + # You can call the add() and remove() methods to modify the contents of a set |
| 37 | + self.keys_being_pressed = set() |
| 38 | + |
| 39 | + # Next I run my update_text function that I write further down in the file |
| 40 | + self.update_text() |
| 41 | + |
| 42 | + # And lastly I add my label to the layer! |
| 43 | + self.add(self.label) |
| 44 | + |
| 45 | + # This is the function that updates the label |
| 46 | + def update_text(self): |
| 47 | + # Here I get the name of the key |
| 48 | + # This line requires a bit of explanation |
| 49 | + # Essentially, I look at the key being pressed (which I set in a function further down) |
| 50 | + # And I match it up to the appropriate symbol for that string |
| 51 | + # So if the "T" key and the left "Shift" key were being pressed, this line would recognize their respective code, |
| 52 | + # And it would set the variable to "T", "LSHIFT" |
| 53 | + key_names = [symbol_string(k) for k in self.keys_being_pressed] |
| 54 | + |
| 55 | + # This line sets the text to the indicated string, joining in the key names until there are no more keys indicated |
| 56 | + text_for_label = "Keys: " + ", ".join(key_names) |
| 57 | + |
| 58 | + # This code is a bit lengthy, but essentially I'm just accessing the text element from the Label object |
| 59 | + self.label.element.text = text_for_label |
| 60 | + |
| 61 | + # This function is once of the default Cocos functions. I overload it and add my own code |
| 62 | + # By default this function just passes, but I make it actually do stuff! |
| 63 | + def on_key_press(self, key, modifiers): |
| 64 | + # By stuff I mean updating the keys_being_pressed variable with the key being passed in through Cocos |
| 65 | + self.keys_being_pressed.add(key) |
| 66 | + # Then I simply run my update text method to make sure the string gets updated |
| 67 | + self.update_text() |
| 68 | + |
| 69 | + # This function is also a default Cocos function! |
| 70 | + # I have to remember to update both the keys currently pressed and the string when they release a key |
| 71 | + # You can try commenting this out to see what happens when you hit a key and let go without this piece of code |
| 72 | + def on_key_release(self, key, modifiers): |
| 73 | + # Same code as before, except I remove instead of add the key |
| 74 | + self.keys_being_pressed.remove(key) |
| 75 | + self.update_text() |
| 76 | + |
| 77 | +# And then I use the same code you're used to seeing to run the layer |
| 78 | +director.init() |
| 79 | +director.run(scene.Scene(KeyboardInput())) |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments