1
+ # Alright!
2
+ # It's time for us to string everything we learned in the basics tutorial together
3
+
4
+ # Let's start with all of the inputs we need
5
+ import cocos
6
+ from cocos import scene
7
+ from cocos .layer import Layer
8
+ from cocos .director import director
9
+ from cocos .sprite import Sprite
10
+ from cocos .actions import *
11
+ from cocos .audio .pygame .mixer import Sound
12
+ from cocos .audio .pygame import mixer
13
+ from pyglet .window .key import symbol_string
14
+
15
+
16
+ # Here is how we will structure our code
17
+ # First, we will write an Audio class that is the child of SDL's Sound class
18
+ # Lastly we will write an Input layer that controls both the sprite and the audio
19
+
20
+ # We start with the audio class, same as before
21
+ class Audio (Sound ):
22
+ def __init__ (self , filename ):
23
+ super (Audio , self ).__init__ (filename )
24
+ # Pretty easy, I'd say
25
+
26
+
27
+ # Let's be fancy and make this a color layer AND an event handler
28
+ class InputLayer (cocos .layer .ColorLayer ):
29
+ is_event_handler = True
30
+
31
+ def __init__ (self ):
32
+ super (InputLayer , self ).__init__ (46 , 204 , 113 , 1000 )
33
+
34
+ # Now we need a little guy to manipulate
35
+ self .sprite = Sprite ('assets/img/grossini_dance_08.png' )
36
+ self .sprite .position = 320 , 240
37
+ self .sprite .opacity = 0
38
+ self .add (self .sprite )
39
+ self .sprite .do (FadeIn (2 ))
40
+
41
+ # You should be bored seeing this same code over and over again
42
+ # Here's something different though
43
+ # Now I create a couple audio objects and store them within self
44
+ self .jump_sound = Audio ("assets/sound/bounce.wav" )
45
+ self .bg_music = Audio ("assets/sound/LatinIndustries.wav" )
46
+ # We lower the volume of the background music and have it play the whole time
47
+ self .bg_music .set_volume (.2 )
48
+ self .bg_music .play (- 1 )
49
+
50
+ # We don't need anything else here, let's just let our sprite be moved in the event handlers
51
+
52
+ # So now we can overload some default event handlers
53
+ # We'll only be doing keyboard input for this program
54
+ def on_key_press (self , key , modifiers ):
55
+ # If you don't know what these next couple lines do, go check the previous tutorials
56
+ move_left = MoveBy ((- 50 , 0 ), .5 )
57
+ move_up = MoveBy ((0 , 50 ), .5 )
58
+
59
+ # Check if they want to go left, and then actually make the sprite go left
60
+ if symbol_string (key ) == "LEFT" :
61
+ self .sprite .do (move_left )
62
+
63
+ # Or maybe if they want to move right?
64
+ if symbol_string (key ) == "RIGHT" :
65
+ self .sprite .do (Reverse (move_left ))
66
+
67
+ # And lastly we need that jump game to be strong
68
+ if symbol_string (key ) == "UP" :
69
+ self .sprite .do (move_up )
70
+
71
+ if symbol_string (key ) == "DOWN" :
72
+ self .sprite .do (Reverse (move_up ))
73
+
74
+
75
+ # And finally we do our usual initialization and run the scene
76
+ mixer .init ()
77
+ director .init ()
78
+ director .run (scene .Scene (InputLayer ()))
0 commit comments