Skip to content

Commit c0d71e4

Browse files
committed
Extended the basics course
1 parent c9ccaba commit c0d71e4

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

basics/actions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# I chose these import statements just to make my code look cleaner
32
# For this to work you would just need to import cocos and then add the subdirectory after
43
# Ex: self.sprite = Sprite('directory') would be self.sprite = cocos.sprite.Sprite('directory')

basics/basics.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Here is the order which the tutorial follows:
1010
5. Mouse Input (mouse.py)
1111
6. Applying User Input (input.py)
1212
7. Audio Playback (audio.py)
13-
8. Review (review.py)
13+
8. Multiple Scene Games (scenes.py)
14+
9. Transitioning Between Scenes (transitions.py)
15+
10. Review (review.py)

basics/scenes.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# I won't explain the inputs this time - I think you guys are getting the jist of it
2+
import cocos
3+
from cocos.scene import Scene
4+
from cocos.layer import Layer, ColorLayer
5+
from cocos.director import director
6+
from cocos.sprite import Sprite
7+
from cocos.text import Label
8+
from cocos.actions import *
9+
from cocos.audio.pygame.mixer import Sound
10+
from cocos.audio.pygame import mixer
11+
12+
13+
# For this tutorial, we will be making two scenes and switching between them
14+
# Theoretically, we could have two scenes made of the same layer, but there would be no visual difference between them
15+
# For that reason we need to make two scenes with different text
16+
# As a bonus I make them different colours as well
17+
18+
19+
class Layer1(ColorLayer):
20+
# I set the layer to be an event handler because I want to be able to transition to another scene on a key press
21+
is_event_handler = True
22+
23+
def __init__(self):
24+
# Remember that I need to pass in an RGBA colour value because it's a ColorLayer
25+
super(Layer1, self).__init__(155, 89, 182, 1000)
26+
27+
# I make a simple label with no extra parameters such as font or anchors
28+
text = Label("This is the first scene")
29+
30+
# For the position, I do something we haven't done before
31+
# We've been making the position static, but we don't exactly know the width and height of a player's window
32+
# Therefore, getting the virtual height and width and then just dividing them by two is a safer bet
33+
text.position = director._window_virtual_width / 2, director._window_virtual_height / 2
34+
35+
# And lastly we add it
36+
self.add(text)
37+
38+
# Here is the overload for the on_key_press function
39+
def on_key_press(self, key, modifiers):
40+
# Here is entire purpose of this lesson!
41+
director.replace(Scene(Layer2()))
42+
# That line replaces whatever scene is currently running in the director with the scene indicated
43+
# In this case, we create a new scene object inline out of the second layer, which I have coded below
44+
45+
46+
# Here is the second Layer I make, for the second scene
47+
class Layer2(ColorLayer):
48+
# Same as before, we let it handle user input
49+
is_event_handler = True
50+
51+
# Initialize it and call the super class, but this time pass in a different colour
52+
def __init__(self):
53+
super(Layer2, self).__init__(231, 76, 60, 1000)
54+
55+
# Same Label code as before but this time with different text
56+
text = Label("This is the second scene")
57+
text.position = director._window_virtual_width / 2, director._window_virtual_height / 2
58+
self.add(text)
59+
60+
# And once again the same user input code
61+
def on_key_press(self, key, modifiers):
62+
director.replace(Scene(Layer1()))
63+
64+
65+
# Here we initialize the director and start running the first scene
66+
director.init()
67+
director.run(Scene(Layer1()))
68+
69+
# But if you remember, when you press a key that layer will tell the director to make a new scene out of the second layer
70+
# And that's about it. As you can see, it's pretty easy to switch between scenes

basics/transitions.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from cocos.director import director
2+
from cocos.layer import ColorLayer
3+
from cocos.scene import Scene
4+
from cocos.scenes import FadeTransition, SplitColsTransition
5+
from cocos.text import Label
6+
7+
8+
9+
# Here is all the same code we had before, but this time we will be adding transitions between the scenes
10+
# I won't be explaining what I already explained last tutorial
11+
class Layer1(ColorLayer):
12+
is_event_handler = True
13+
14+
def __init__(self):
15+
super(Layer1, self).__init__(155, 89, 182, 1000)
16+
17+
text = Label("This is the first scene")
18+
text.position = director._window_virtual_width / 2, director._window_virtual_height / 2
19+
self.add(text)
20+
21+
def on_key_press(self, key, modifiers):
22+
# Adding transitions into your Scenes is pretty easy
23+
# All you need to do is put a transition in the replace method
24+
director.replace(FadeTransition(Scene(Layer2())))
25+
# This specific transition is a simple Fade that makes the screen fade to black and then fade to the next scene
26+
# There are many different scenes available for you to use
27+
# You can check them out here: http://python.cocos2d.org/doc/api/cocos.scenes.transitions.html
28+
29+
30+
class Layer2(ColorLayer):
31+
is_event_handler = True
32+
33+
def __init__(self):
34+
super(Layer2, self).__init__(231, 76, 60, 1000)
35+
36+
text = Label("This is the second scene")
37+
text.position = director._window_virtual_width / 2, director._window_virtual_height / 2
38+
self.add(text)
39+
40+
def on_key_press(self, key, modifiers):
41+
# This is extremely similar to the one above
42+
director.replace(SplitColsTransition(Scene(Layer1())))
43+
# Except this time, we are using a Split Columns transition
44+
# It make three bars out of the screen, of which the middle one goes up and the others go down
45+
# Then the bars come back, this time containing the next scene
46+
# It sounds odd written out, but it's a neat transition when you view it
47+
48+
director.init()
49+
director.run(Scene(Layer1()))

0 commit comments

Comments
 (0)