Skip to content

Instantly share code, notes, and snippets.

@encela95dus
Created May 7, 2018 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encela95dus/168e93ca05f213124f1aec4c329d9c73 to your computer and use it in GitHub Desktop.
Save encela95dus/168e93ca05f213124f1aec4c329d9c73 to your computer and use it in GitHub Desktop.
cards.py
import scene
import random
import ui
import sound
class Block(scene.SpriteNode):
for effect in ['Click_1', 'Click_2', 'Coin_2', 'Coin_5']:
sound.load_effect(effect)
w, h = (96, 96) # (64, 64)
with ui.ImageContext(w, h) as ctx:
ui.set_color('silver')
ui.Path.rounded_rect(2, 2, w-4, h-4, 8).fill()
ui.set_color('white')
ui.Path.rounded_rect(2, 2, w-4, h-4, 8).fill()
tile_texture = scene.Texture(ctx.get_image())
def __init__(self, sprite=None, value=0, **kwargs):
super(Block, self).__init__(Block.tile_texture, **kwargs)
self.node = scene.SpriteNode(sprite, size=(Block.w, Block.h))
self.value = value
self.color = 'white'
self.add_child(self.node)
self.enable_touch = True
def complete_action(self):
self.parent.animation_on = False
def hide(self):
if self.node and self.node.parent:
self.node.remove_from_parent()
A = scene.Action
animate_action = A.sequence(
A.scale_x_to(0, .15),
A.scale_x_to(1, .15),
A.wait(.1),
A.call(self.complete_action)
)
self.parent.animation_on = True
self.run_action(animate_action)
sound.play_effect('Click_1')
def reveal(self):
if self.node and not self.node.parent:
self.add_child(self.node)
A = scene.Action
animate_action = A.sequence(
A.scale_x_to(0, .15),
A.scale_x_to(1, .15),
A.wait(.1),
A.call(self.complete_action)
)
self.parent.animation_on = True
self.run_action(animate_action)
sound.play_effect('Click_2')
def select(self):
sound.play_effect('Coin_5')
self.color = '#fdffce'
self.enable_touch = False
def deselect(self):
sound.play_effect('Coin_2')
self.color = 'white'
self.enable_touch = True
class GridOfBlocks(object):
def __init__(self, m, n,
w, h,
position=None,
margin=4,
parent=None,
**kwargs):
self.m = m
self.n = n
self.w = w
self.h = h
self.num_items = m*n//2
self.position = position
self.margin = margin
self.parent = parent
self.kwargs = kwargs
self.sprite_list = ['Rabbit_Face', 'Mouse_Face', 'Cat_Face',
'Dog_Face', 'Octopus', 'Bear_Face',
'Chicken', 'Cow_Face'] * 2
self.value_list = [i%(m*n//2) for i in range(m*n)]
self.setup()
def setup(self):
w1 = self.w + self.margin
h1 = self.h + self.margin
if self.position:
center = scene.Point(self.position)
else:
center = scene.Point(
self.parent.bounds.w/2,
self.parent.bounds.h/2)
self.start_position = scene.Point(
center.x - (self.n-1) * w1/2.0 + w1/2.0,
center.y - (self.m-1) * h1/2.0 + h1/2.0)
self.grid = {}
for i in range(self.m):
for j in range(self.n):
self.grid[i, j] = Block(
sprite=scene.Texture(self.sprite_list[i*self.n+j]),
parent=self.parent,
position=(
self.start_position.x + j*w1,
self.start_position.y + i*h1),
**self.kwargs)
def touch_began(self, touch):
for i, j in self.grid:
if (touch.location in (
self.grid[i, j].frame)) and self.grid[i, j].enable_touch:
self.block_action(i, j)
return
def initialize(self):
m, n = self.m, self.n
r = list(zip(self.sprite_list, self.value_list))
random.shuffle(r)
self.sprite_list, self.value_list = list(zip(*r))
for i in range(m):
for j in range(n):
block = self.grid[i,j]
block.value = self.value_list[i*n+j]
block.node.remove_from_parent()
block.node = scene.SpriteNode(scene.Texture(self.sprite_list[i*n+j]),
size=(self.w, self.h))
block.add_child(block.node)
block.deselect()
block.hide()
block.enable_touch = True
def block_action(self, i, j):
block = self.grid[i,j]
if self.parent.state.value == "play":
block.reveal()
self.parent.state.value = "one open"
self.parent.open_block = block
return
elif self.parent.state.value == "one open":
block.reveal()
if self.parent.open_block.value == block.value:
self.parent.state.value = "two open match success"
else:
self.parent.state.value = "two open match failure"
self.parent.open_block = (self.parent.open_block, block)
if self.parent.state.value == "two open match success":
self.parent.open_block[0].select()
self.parent.open_block[1].select()
self.parent.count += 1
if self.parent.count == self.num_items:
self.parent.state.set_win_state()
else:
self.parent.state.initialize()
def two_open_action_failure(self):
self.parent.open_block[0].hide()
self.parent.open_block[1].hide()
self.parent.state.initialize()
class State(object):
def __init__(self, position=(0, 0), font=('Helvetica', 40), parent=None):
self.value = 'play'
self.msg = 'Play'
self.parent = parent
self.label = scene.LabelNode(self.msg,
position=position,
font=font,
parent=parent)
def set_win_state(self):
self.value = 'win'
self.msg = 'You win'
self.label.text = self.msg
def set_lose_state(self):
self.value = 'lose'
self.msg = 'You lose'
self.label.text = self.msg
def initialize(self):
self.set_play_state()
def set_play_state(self):
self.value = 'play'
self.msg = 'Play '
self.label.text = self.msg
class MyScene (scene.Scene):
def initialize(self):
self.state.initialize()
self.gridofblocks.initialize()
self.count = 0
def setup(self):
self.count = 0
self.animation_on = False
self.open_block = None
self.state = State(position=(self.size.w/2, 100), parent=self)
self.gridofblocks = GridOfBlocks(4, 4,
w=Block.w, h=Block.h,
parent=self)
self.initialize()
def touch_began(self, touch):
if self.animation_on:
return
if self.state.value == 'win':
self.initialize()
return
if self.state.value == "two open match failure":
self.gridofblocks.two_open_action_failure()
return
self.gridofblocks.touch_began(touch)
scene.run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment