1
+ # Same inputs as keyboard except I also input the actions package from cocos
2
+ import cocos
3
+ from cocos .text import Label
4
+ from cocos import scene
5
+ from cocos .layer import Layer
6
+ from cocos .actions import *
7
+ from cocos .director import director
8
+ from cocos .sprite import Sprite
9
+ from pyglet .window .key import symbol_string
10
+
11
+ # Here we will be applying input to what we learned before by making our sprite perform these actions
12
+ # 1. Jump on a left click
13
+ # 2. Move left when the "LEFT" key is pressed
14
+ # 2. Move right when the "RIGHT" key is pressed
15
+
16
+
17
+ # Same starting code as usual
18
+ class InputExample (Layer ):
19
+ is_event_handler = True
20
+
21
+ def __init__ (self ):
22
+ super (InputExample , self ).__init__ ()
23
+
24
+ # Let's create a sprite this time instead of using Labels
25
+ self .sprite = Sprite ("assets/img/grossini_dance_08.png" )
26
+ self .sprite .position = 320 , 240
27
+
28
+ # While we're at it let's make it fancy by having our sprite fade in
29
+ self .sprite .opacity = 0
30
+ self .add (self .sprite )
31
+ self .sprite .do (FadeIn (2 ))
32
+
33
+ # Remember that our layer is an event handler
34
+ # This means that I don't need to add any calls to functions to execute the actions on those events
35
+
36
+ # Let's start with that jump action
37
+ # To start I need to overload the default click function
38
+ def on_mouse_press (self , x , y , buttons , modifiers ):
39
+ # Remember that we said we only wanted to jump on left clicks
40
+ # The number 1 represents left clicks in Cocos
41
+ # You can test this by adding a print statement for the buttons input
42
+ if buttons == 1 :
43
+ # The Jump action requires 4 inputs
44
+ # 1. How high on the Y axis the sprite should jump
45
+ # 2. How far on the X axis the sprite should jump to
46
+ # 3. How many times the sprite should jump
47
+ # 4. How many seconds it should take for the action to complete
48
+ self .sprite .do (Jump (50 , 0 , 1 , 1 ))
49
+
50
+ # Pretty easy, huh? Now let's do the movement
51
+
52
+ # Once again we overload a default event handler
53
+ def on_key_press (self , key , modifiers ):
54
+ # First I create a move actions because we programmers are lazy and hate having to retype code!
55
+ move_left = MoveBy ((- 50 , 0 ), .5 )
56
+
57
+ # Here's where that Pyglet symbol_string() function comes in handy
58
+ # Rather than having to interpret an inconsistent code, I can simply interpret the word LEFT and RIGHT
59
+ if symbol_string (key ) == "LEFT" :
60
+ self .sprite .do (move_left )
61
+
62
+ # Now I need to tell the layer what to do if the user inputs RIGHT
63
+ if symbol_string (key ) == "RIGHT" :
64
+ # This is a pretty awesome feature built into Cocos
65
+ # I only wrote code for moving left, but I can use the Reverse() function instead of rewriting code
66
+ # Reverse() simply tells Cocos to do the reverse action of whatever you pass into it.
67
+ self .sprite .do (Reverse (move_left ))
68
+
69
+
70
+ # And once again the same init code
71
+ director .init ()
72
+ director .run (scene .Scene (InputExample ()))
0 commit comments