Skip to content

Commit 80ac5ac

Browse files
committed
Added a menu tutorial
1 parent 80731ff commit 80ac5ac

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed
33.8 MB
Binary file not shown.

intermediate/collision.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727

2828

2929
# This time we'll make an action that extends both the Action class and the new RectMapCollider
30-
# The RectMapCollider, you guessed it, lets us collide with rectmaps (A.K.A. tile maps)
30+
# The RectMapCollider, you guessed it, lets us collide with rectmaps (A.K.A. tilemaps)
3131
class GameAction(Action, RectMapCollider):
3232
# To begin, we'll add a function for when the Action starts
33+
# I use the start function instead of an __init__ function because of the way the Action parent class is structured
3334
def start(self):
3435
# We simply set the velocity of the target sprite to zero
3536
self.target.velocity = 0, 0

intermediate/intermediate.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Here is the order which the tutorial follows:
66
1. Tiled Maps Schema (mapmaking.tmx)
77
2. Applying Maps as Layers (maps.py)
88
3. Adding Layers and Scrolling on Maps (add.py)
9-
4. Adding Collision Detection to Maps (collision.py)
9+
4. Adding Collision Detection to Maps (collision.py)
10+
5. Making Menus with Cocos (menu.py)

intermediate/menu.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from cocos.audio.pygame import mixer
2+
from cocos.audio.pygame.mixer import Sound
3+
from cocos.director import director
4+
from cocos.menu import Menu, CENTER, MenuItem, shake, shake_back
5+
from cocos.scene import Scene
6+
from pyglet.app import exit
7+
8+
# Here we will make a simple menu that allows you to play/pause a song, and to increase or decrease its volume
9+
# As you'll see, Cocos makes writing menus EXTREMELY easy. Like I probably should have put this in beginner easy
10+
11+
12+
# To begin, we have to make a wrapper for the Sound class (remember this?)
13+
class Audio(Sound):
14+
def __init__(self, audio_file):
15+
super(Audio, self).__init__(audio_file)
16+
17+
18+
# Now here's the new part
19+
# We make a class that extends Cocos's Menu class
20+
class AudioMenu(Menu):
21+
def __init__(self):
22+
# When we initialize the super class, we can to pass in a title for our awesome menu, that will be displayed above it
23+
super(AudioMenu, self).__init__("AUDIO CONTROL")
24+
# if you want to add your own, personalized text, feel free to pass nothing in
25+
26+
# Then we simply set the vertical and horizontal alignment (in this case center for both)
27+
self.menu_valign = CENTER
28+
self.menu_halign = CENTER
29+
# The cocos.menu file comes with many more different positions you can mix and match to get what you like
30+
31+
# Next we need to make our menu items
32+
# This is the important part!
33+
# What we do is create a list filled with MenuItem objects
34+
menu_items = [
35+
# Each MenuItem has text to display, and a callback method
36+
# The callback method we pass in gets called when a user clicks on the MenuItem
37+
(MenuItem("Play/Pause", self.play_music)),
38+
(MenuItem("Increase Volume", self.volume_up)),
39+
(MenuItem("Decrease Volume", self.volume_down)),
40+
(MenuItem("Exit", self.on_quit))
41+
# Pretty easy I'd say
42+
]
43+
44+
# Now let's set the song we want them to hear (good god I love me some Latin Industries)
45+
self.song = Audio("assets/sound/LatinIndustries.wav")
46+
47+
# Then we simply create the menu using the create_menu method and passing in our MenuItem list
48+
self.create_menu(menu_items)
49+
50+
# And I set the is_playing status to False, which we will access for our Play/Pause functionality
51+
self.is_playing = False
52+
53+
# So, if you were paying attention, you'd notice that this is the first callback method we passed into the list
54+
def play_music(self):
55+
# If it is playing, I stop the song and set the boolean to false
56+
if self.is_playing:
57+
self.song.stop()
58+
self.is_playing = False
59+
# If not, I need to play the song (indefinitely I might add), and then set the boolean value to true
60+
else:
61+
self.song.play(-1)
62+
self.is_playing = True
63+
64+
# Now for the volume up functionality
65+
def volume_up(self):
66+
# First I grab the current volume of the song
67+
volume = self.song.get_volume()
68+
# Then I set the volume to that volume, but I add just a smidgen to it (remember that volume is from 0.0 - 1.0)
69+
self.song.set_volume(volume + .1)
70+
71+
# And the exact opposite for volume down
72+
def volume_down(self):
73+
volume = self.song.get_volume()
74+
self.song.set_volume(volume - .1)
75+
76+
# IMPORTANT!
77+
# All your menus must have this on_quit function or they will not exit even if you press escape
78+
# (That means some fun Control+Alt+Tabing for you Windows users)
79+
def on_quit(self):
80+
# It seems odd that I need a function that literally just calls the exit function but...
81+
exit()
82+
# You've gotta do what you've gotta do
83+
84+
85+
# Then we initialize the mixer (I bet you forgot about this!)
86+
mixer.init()
87+
# And initialize the director like usual
88+
director.init(resizable=True)
89+
# Finally, we run the scene
90+
director.run(Scene(AudioMenu()))
91+
92+
# ... Yep this definitely should have been in beginner

0 commit comments

Comments
 (0)