Skip to content

Commit d9569a2

Browse files
committed
Added some more tutorials
1 parent 25badf1 commit d9569a2

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

basics/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def fade_in(self):
4141
# You should try removing this to see what happens
4242
self.sprite.opacity = 0
4343

44-
# I add the sprite ot the screen (remember it can't be seen yet)
44+
# I add the sprite to the screen (remember it can't be seen yet)
4545
self.add(self.sprite, z=1)
4646

4747
# I then start the fading in (which takes 2 seconds)

basics/input.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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()))

basics/mouse.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Same imports once again, no pyglet keyboard input this time
2+
3+
import cocos
4+
from cocos.text import Label
5+
from cocos import scene
6+
from cocos.layer import Layer
7+
from cocos.director import director
8+
from cocos.sprite import Sprite
9+
10+
11+
# I make the layer, set it to an event handler, and initialize the super class to begin
12+
class MouseInput(Layer):
13+
is_event_handler = True
14+
def __init__(self):
15+
super(MouseInput, self).__init__()
16+
17+
# This time I set variables for the position rather than hardcoding it
18+
# I do this because we will want to alter these values later
19+
self.position_x = 100
20+
self.position_y = 240
21+
22+
# Once again I make a label
23+
self.text = Label("No mouse interaction yet",
24+
font_name = "Helvetica",
25+
font_size = 24,
26+
x = self.position_x,
27+
y = self.position_y)
28+
29+
# Then I just add the text!
30+
self.add(self.text)
31+
32+
# Like last time we need to make a function to update that self.text label to display the mouse data
33+
def update_text(self, mouse_x_pos, mouse_y_pos):
34+
# I make a text variable and store a string containing the x and y positions passed into the function
35+
text = 'Mouse is at %d,%d' % (mouse_x_pos, mouse_y_pos)
36+
37+
# Next I simply do what I did for the keyboard and update the self.text label to contain our new string
38+
self.text.element.text = text
39+
40+
# I also update its now dynamic position by moving the text to wherever the user clicks
41+
self.text.element.x, self.text.element.y = self.position_x, self.position_y
42+
43+
# Also similarly to the keyboard, I overload a few default functions that do nothing
44+
# I make all of them update the text, and I make clicks update both the text and the label's position
45+
def on_mouse_motion(self, x, y, dx, dy):
46+
self.update_text(x, y)
47+
48+
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
49+
self.update_text(x, y)
50+
51+
def on_mouse_press(self, x, y, buttons, modifiers):
52+
# This next line seems a bit odd, and that's because it is!
53+
self.position_x, self.position_y = director.get_virtual_coordinates(x, y)
54+
# It introduces a new topic, virtual coordinates
55+
# If I had used default coordinates, the position might be updated in the OS's coordinates rather than the scene
56+
# The director provides us with the appropriate coordinates within our "virtual" window
57+
58+
self.update_text(x, y)
59+
60+
# Just your standard stuff down here
61+
director.init()
62+
director.run(scene.Scene(MouseInput()))

0 commit comments

Comments
 (0)