Skip to content

Commit d54a774

Browse files
committed
Added the keyboard tutorial
1 parent 3c89d8b commit d54a774

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Simply head to your terminal and type in `pip install cocos` or `sudo pip instal
5050

5151
----------------------------------------------------------------------------
5252

53-
From here you should be read to start reading through the samples and trying out the concepts yourself. I group the the code by difficulty level, so I recommend in the `basics` folder and working your way up.
53+
From here you should be read to start reading through the samples and trying out the concepts yourself. I group the the code by difficulty level, so I recommend starting in the `basics` folder and working your way up.
5454

5555
It's important to note that `basics` does not mean Python basics! You should at least have a basic grasp on writing object oriented code before trying to jump into Cocos2D Python!
5656

basics/actions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313

1414
# Here I make a class that extends cocos' ColorLayer class
15+
# This type of layer is different because it has a background color! (awesome right?)
1516
class Actions(ColorLayer):
1617
# When I initialize it I need to pass in an RRGB colour value so it can set the ColorLayer's background
18+
# I grabbed the Peter River colour (#3498db) from http://flatuicolors.com for this sample
1719
def __init__(self):
18-
super(Actions, self).__init__(52, 52, 73, 94)
20+
super(Actions, self).__init__(52, 52, 152, 219)
1921

2022
# I initialize a sprite using the cocos sprite.Sprite class
2123
# Change the path below to whatever your sprite image is

basics/basics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ Here is the order which the tutorial follows:
66
1. Getting Started (getting_started.py)
77
2. Adding Actors (actors.py)
88
3. Basic Actions (actions.py)
9+
4. Keyboard Input (keyboard.py)
10+
5. Mouse Input (mouse.py)
11+
6. Applying User Input (input.py)

basics/keyboard.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)