Advertisement
barsunduk

renpy memoria game

Aug 16th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. ##### The game screen
  2. screen memo_scr:    
  3.    
  4.     ##### Timer
  5.     timer 1.0 action If (memo_timer > 1, SetVariable("memo_timer", memo_timer - 1), Jump("memo_game_lose") ) repeat True
  6.    
  7.     text str(memo_timer) xalign 0.5 yalign 0.05
  8.    
  9.    
  10.     ##### Cards
  11.     #
  12.     # To use images, just comment out lines that show text and uncomment lines that show images
  13.     grid 3 4:
  14.         for card in cards_list:
  15.             button:
  16.                 background None
  17.  
  18.                 if card["c_chosen"]:        # shows the face of the card
  19.                     text card["c_value"]    # will show text
  20.                     #add card["c_value"]    # will show image
  21.  
  22.                 else:                       # shows the back of the card
  23.                     text "X"                # will show text
  24.                     #add "C"                # will show image
  25.  
  26.                 action If ( (card["c_chosen"] or not can_click), None, [SetDict(cards_list[card["c_number"]], "c_chosen", True), Return(card["c_number"]) ] )
  27.  
  28.  
  29.    
  30.  
  31. init:
  32.     python:
  33.         def cards_shuffle(x):
  34.             renpy.random.shuffle(x)
  35.             return x
  36.  
  37.     ##### Images
  38.     image A = "img_1.png"         # different card images
  39.     image B = "img_2.png"
  40.     image C = "back.png"          # back of the card
  41.  
  42.    
  43.  
  44.    
  45. label memoria_game:
  46.    
  47.     #####
  48.     #
  49.     # At first, let's set the cards to play (the amount should match the grid size - in this example 12)
  50.     $ values_list = ["A", "A", "A", "A", "A", "A", "B",  "B", "B", "B", "B", "B"]
  51.    
  52.     # Then - shuffle them
  53.     $ values_list = cards_shuffle(values_list)
  54.    
  55.     # And make the cards_list that describes all the cards
  56.     $ cards_list = []
  57.     python:
  58.         for i in range (0, len(values_list) ):
  59.             cards_list.append ( {"c_number":i, "c_value": values_list[i], "c_chosen":False} )  
  60.  
  61.     # Before start the game, let's set the timer
  62.     $ memo_timer = 50.0
  63.    
  64.     # Shows the game screen
  65.     show screen memo_scr
  66.    
  67.     # The game loop
  68.     label memo_game_loop:
  69.         $ can_click = True
  70.         $ turned_cards_numbers = []
  71.         $ turned_cards_values = []
  72.        
  73.         # Let's set the amount of cards that should be opened each turn (all of them should match to win)
  74.         $ turns_left = 3
  75.        
  76.         label turns_loop:
  77.             if turns_left > 0:
  78.                 $ result = ui.interact()
  79.                 $ memo_timer = memo_timer
  80.                 $ turned_cards_numbers.append (cards_list[result]["c_number"])
  81.                 $ turned_cards_values.append (cards_list[result]["c_value"])
  82.                 $ turns_left -= 1
  83.                 jump turns_loop
  84.        
  85.         # To prevent further clicking befor chosen cards will be processed
  86.         $ can_click = False
  87.         # If not all the opened cards are matched, will turn them face down after pause
  88.         if turned_cards_values.count(turned_cards_values[0]) != len(turned_cards_values):
  89.             $ renpy.pause (1.0, hard = True)
  90.             python:
  91.                 for i in range (0, len(turned_cards_numbers) ):
  92.                     cards_list[turned_cards_numbers[i]]["c_chosen"] = False
  93.        
  94.         # If cards are matched, will check if player has opened all the cards
  95.         else:
  96.             $ renpy.pause (1.0, hard = True)
  97.             python:
  98.                
  99.                 # Let's remove opened cards from game field
  100.                 # But if you prefere to let them stay - just comment out next 2 lines
  101.                 for i in range (0, len(turned_cards_numbers) ):
  102.                     cards_list[turned_cards_numbers[i]]["c_value"] = Null()
  103.                    
  104.                    
  105.                 for j in cards_list:
  106.                     if j["c_chosen"] == False:
  107.                         renpy.jump ("memo_game_loop")
  108.                 renpy.jump ("memo_game_win")
  109.                
  110.                
  111.  
  112.         jump memo_game_loop
  113.  
  114. label memo_game_lose:
  115.     hide screen memo_scr
  116.     $ renpy.pause (0.1, hard = True)
  117.     $ renpy.pause (0.1, hard = True)
  118.     "You lose! Try again."
  119.     jump memoria_game
  120.  
  121. label memo_game_win:
  122.     hide screen memo_scr
  123.     $ renpy.pause (0.1, hard = True)
  124.     $ renpy.pause (0.1, hard = True)
  125.     "You win!"
  126.     return
  127.  
  128. label start:
  129.     call memoria_game
  130.     return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement