Skip to content

Commit 10f3c58

Browse files
committed
FInalized the beginner course. Enjoy
1 parent c0d71e4 commit 10f3c58

File tree

8 files changed

+80
-17
lines changed

8 files changed

+80
-17
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ AVbin is a package that provides some audio and the video support for Cocos2D Py
4141
Head to https://avbin.github.io/AVbin/Download.html to download the correct installer for your operating system.
4242

4343
###SDL
44-
SDL is the framework that PyGame (another Python game library) relies on for audio support. Cocos uses it for it's audio as well.
44+
SDL is the framework that PyGame (another Python game library) relies on for audio support. Cocos uses it for its audio as well.
4545

4646
Head over to https://www.libsdl.org/download-1.2.php to download the base SDL library and then to https://www.libsdl.org/projects/SDL_mixer/release-1.2.html to download the Mixer library from SDL (which was what we will be using for audio support in our games)
4747

@@ -73,9 +73,11 @@ From here you should be ready to start reading through the samples and trying ou
7373

7474
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!
7575

76+
*_I highly recommend that you browse through the code and READMEs on GitHub, and only using the cloned repository to run the games_*
77+
7678
-------------------------------------------------------------
7779

78-
Cocos has also provided some pretty good basic documentation and video samples, but unforunately it stops there. You can check those out here http://python.cocos2d.org/doc/programming_guide/index.html
80+
Cocos has also provided some pretty good basic documentation and video samples, but unfortunately it stops there. You can check those out here http://python.cocos2d.org/doc/programming_guide/index.html
7981

8082

8183
I created this repository because I found the Cocos2D Python documentation to be extremely limited, especially in code samples, beyond the basic level. It is also relatively complex and hard to understand. I often found myself needing to look at the source code for explanations on how to use certain classes and features.
67.5 MB
Binary file not shown.

basics/basics.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Here is the order which the tutorial follows:
55

6-
1. Getting Started (getting_started.py)
6+
1. Getting Started (start.py)
77
2. Adding Actors (actors.py)
88
3. Basic Actions (actions.py)
99
4. Keyboard Input (keyboard.py)
@@ -12,4 +12,5 @@ Here is the order which the tutorial follows:
1212
7. Audio Playback (audio.py)
1313
8. Multiple Scene Games (scenes.py)
1414
9. Transitioning Between Scenes (transitions.py)
15-
10. Review (review.py)
15+
10. Effects (effects.py)
16+
11. Review (review.py)

basics/effects.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from cocos.director import director
2+
from cocos.layer import ColorLayer
3+
from cocos.scene import Scene
4+
from cocos.sprite import Sprite
5+
from pyglet.window.key import symbol_string
6+
from cocos.actions import *
7+
8+
9+
class EffectLayer(ColorLayer):
10+
is_event_handler = True
11+
12+
def __init__(self):
13+
super(EffectLayer, self).__init__(231, 76, 60, 1000)
14+
15+
sprite = Sprite("assets/img/grossini_dance_08.png")
16+
sprite.position = 320, 240
17+
self.add(sprite)
18+
19+
def on_key_press(self, key, modifiers):
20+
if symbol_string(key) == "T":
21+
self.do(Twirl(amplitude=5, duration=2))
22+
23+
elif symbol_string(key) == "W":
24+
self.do(Reverse(Waves(duration=2)))
25+
26+
elif symbol_string(key) == "F":
27+
self.do(FlipX3D(duration=1) + FlipY3D(duration=1) + Reverse(FlipY3D(duration=1)))
28+
29+
30+
director.init()
31+
director.run(Scene(EffectLayer()))

basics/mouse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# I make the layer, set it to an event handler, and initialize the super class to begin
1212
class MouseInput(Layer):
1313
is_event_handler = True
14+
1415
def __init__(self):
1516
super(MouseInput, self).__init__()
1617

basics/review.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
# Let's start with all of the inputs we need
55
import cocos
66
from cocos import scene
7-
from cocos.layer import Layer
7+
from cocos.layer import Layer, ColorLayer
88
from cocos.director import director
9+
from cocos.scenes import *
910
from cocos.sprite import Sprite
1011
from cocos.actions import *
1112
from cocos.audio.pygame.mixer import Sound
@@ -15,7 +16,8 @@
1516

1617
# Here is how we will structure our code
1718
# 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+
# Second we will write an Input layer that controls both the sprite and the audio
20+
# And to spice things up, we'll add two states to the first layer: a normal and a trippy mode
1921

2022
# We start with the audio class, same as before
2123
class Audio(Sound):
@@ -28,35 +30,46 @@ def __init__(self, filename):
2830
class InputLayer(cocos.layer.ColorLayer):
2931
is_event_handler = True
3032

31-
def __init__(self):
33+
def __init__(self, x=320, y=240, is_trippy=False):
3234
super(InputLayer, self).__init__(46, 204, 113, 1000)
3335

36+
# We set the trippy boolean based on the value passed in (by default it's not trippy)
37+
self.is_trippy = is_trippy
38+
3439
# Now we need a little guy to manipulate
3540
self.sprite = Sprite('assets/img/grossini_dance_08.png')
36-
self.sprite.position = 320, 240
41+
self.sprite.position = x, y
3742
self.sprite.opacity = 0
3843
self.add(self.sprite)
3944
self.sprite.do(FadeIn(2))
4045

4146
# You should be bored seeing this same code over and over again
4247
# 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")
48+
# Now I create an audio object and store it within self, based on whether or not it's trippy
49+
if self.is_trippy:
50+
# When it is trippy, I have a slowed down and distorted version of the song I made in Audacity
51+
self.bg_music = Audio("assets/sound/LatinIndustriesSlow.wav")
52+
# I also start running a couple effects that make it seem very trippy
53+
# It's important to note that you can do math on Cocos2D effects and actions
54+
self.do((Waves(duration=4) * 100) + Liquid(duration=15))
55+
# In this case I added two actions together and multiplied the waves by 100 for run
56+
else:
57+
# If it's not trippy then I just make it the same boring old song we've been using before
58+
self.bg_music = Audio("assets/sound/LatinIndustries.wav")
59+
4660
# We lower the volume of the background music and have it play the whole time
4761
self.bg_music.set_volume(.2)
4862
self.bg_music.play(-1)
4963

5064
# We don't need anything else here, let's just let our sprite be moved in the event handlers
5165

5266
# So now we can overload some default event handlers
53-
# We'll let the user move in any direction on the screen with the arrow keys, and jump around with the spacebar
67+
# We'll let the user move in any direction on the screen with the arrow keys
5468
# We'll only be doing keyboard input for this program
5569
def on_key_press(self, key, modifiers):
5670
# If you don't know what these next couple lines do, go check the previous tutorials
5771
move_left = MoveBy((-50, 0), .5)
5872
move_up = MoveBy((0, 50), .5)
59-
jump = Jump(50, 0, 1, .25)
6073

6174
# Check if they want to go left, and then actually make the sprite go left
6275
if symbol_string(key) == "LEFT":
@@ -66,15 +79,32 @@ def on_key_press(self, key, modifiers):
6679
elif symbol_string(key) == "RIGHT":
6780
self.sprite.do(Reverse(move_left))
6881

69-
# And lastly we need that jump game to be strong
82+
# Lastly we need to make it move up
7083
elif symbol_string(key) == "UP":
7184
self.sprite.do(move_up)
7285

86+
# Oh yeah don't forget about down
7387
elif symbol_string(key) == "DOWN":
7488
self.sprite.do(Reverse(move_up))
7589

90+
# That's it for movements!
91+
# Now let's look at transitioning to a new scene
92+
# Let's make the game all trippy when they hit space
7693
elif symbol_string(key) == "SPACE":
77-
self.sprite.do(jump)
94+
# I need to stop the music before we transition to the next scene so that two songs aren't playing at once
95+
self.bg_music.stop()
96+
97+
# If you were paying attention, you would've noticed I take three parameters in the init function
98+
# I get the X and Y coordinates of the sprite to figure out where to place it when the scenes transition
99+
coordinates = self.sprite.position
100+
# You should try printing the X and Y coordinates yourself to see the type of object that it returns
101+
if self.is_trippy:
102+
# This line only runs when the layer is already trippy, and by default the layer makes itself not trippy
103+
# This line only needs to give the X and Y coordinates in that case
104+
director.replace(scene.Scene(InputLayer(coordinates[0], coordinates[1])))
105+
else:
106+
# This huge line makes a new scene, with a transition, and inputs the coordinates and makes it trippy!
107+
director.replace(scene.Scene(InputLayer(coordinates[0], coordinates[1], True)))
78108

79109

80110
# And finally we do our usual initialization and run the scene
File renamed without changes.

basics/transitions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from cocos.scenes import FadeTransition, SplitColsTransition
55
from cocos.text import Label
66

7-
8-
97
# Here is all the same code we had before, but this time we will be adding transitions between the scenes
108
# I won't be explaining what I already explained last tutorial
119
class Layer1(ColorLayer):

0 commit comments

Comments
 (0)