Randomly display playing cards in Pythonista + scene

This time, I created a process to display playing cards in the scene of Pythonista.

The outline of the program is as follows. ・ Randomly draw 5 playing cards from 53 cards ・ Touch the smartphone screen to redraw the card

How to create a playing card process

First, create a playing card set of 53 cards (13 picture cards + joker). Create a character string in the form of mark + number and store it in the array of playing cards.

Mark uses'Clubs','Hearts','Spades', and'Diamonds' to match the Pythonista card image.

Implementation example

class TrumpGame:
	def make_card_list(self):	
 List of # marks
		symbol_list = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
 #Card list
		card_list = ['Joker']

 Combine #marks and numbers
		for symbol in symbol_list:
			for number in range(1, 14):
 # 11 and above and 1 are replaced
				if number == 1:
					card = symbol + 'A'
				elif number == 11:
					card = symbol + 'J'
				elif number == 12:
					card = symbol + 'Q'
				elif number == 13:
					card = symbol + 'K'
				else:
 # 10 or less as it is
					card = symbol + str(number)

 #Add card to list
				card_list.append(card)
			
		return card_list

Next, create the card as a function for shuffling.

You can easily create a shuffle using random.shuffle ().

◆ Implementation example

	def shuffle(self, card_list):
 #Shuffle cards
		random.shuffle(card_list)
		return card_list

Related articles >> [Python] Example of using random.shuffle ()

How to display playing cards on the screen

Call the card class to create a card list. Then pick up 5 pieces.

class MyScene (Scene):
	def setup(self):
 Create a card list by calling the #TrumpGame () class
		self.tg = TrumpGame()
		self.card_list = self.tg.make_card_list()
		self.card_list = self.tg.shuffle(self.card_list)
		self.draw_cards = []

 # 5 pickups
		for i in range(0, 5):
			self.draw_cards.append(
				self.card_list.pop(0)
			)

Next is the creation of the drawing process. Pass the card string with SpriteNode () and draw it on the screen with add_child ().

	
		self.items = []
		for i,card in enumerate(self.draw_cards):
			item = SpriteNode('card:' + card)
			item.anchor_point = (0.5, 0)
			item.position = (80 + i * 50, 350)
			self.add_child(item)
			self.items.append(item)

Below is the code that summarizes the card creation and scene processing.

from scene import *
import random

class MyScene (Scene):
	def setup(self):
 #Background settings
		self.background_color = '#004f82'
		ground = Node(parent=self)
		
		self.tg = TrumpGame()
		self.make_draw_cards()

		self.items = []
		for i,card in enumerate(self.draw_cards):
				item = SpriteNode('card:' + card)
				item.anchor_point = (0.5, 0)
				item.position = (80 + i * 50, 350)
				self.add_child(item)
				self.items.append(item)
	
	def touch_began(self, touch):
		self.change_draw_cards()
	
 # 5 sheets drawing process
	def make_draw_cards(self):
		self.card_list = self.tg.make_card_list()
		self.card_list = self.tg.shuffle(self.card_list)
		self.draw_cards = []

		for i in range(0, 5):
			self.draw_cards.append(
				self.card_list.pop(0)
			)	
			
 #The process of exchanging cards
	def change_draw_cards(self):
		self.make_draw_cards()
		for i,card in enumerate(self.draw_cards):
				self.items[i].texture = Texture('card:' + card)
			
class TrumpGame:
	def make_card_list(self):	
 List of # marks
		symbol_list = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
 #Card list
		card_list = ['Joker']

 Combine #marks and numbers
		for symbol in symbol_list:
			for number in range(1, 14):
 # 11 and above and 1 are replaced
				if number == 1:
					card = symbol + 'A'
				elif number == 11:
					card = symbol + 'J'
				elif number == 12:
					card = symbol + 'Q'
				elif number == 13:
					card = symbol + 'K'
				else:
 # 10 or less as it is
					card = symbol + str(number)

 #Add card to list
					card_list.append(card)
			
		return card_list
	def shuffle(self, card_list):
 #Shuffle cards
		random.shuffle(card_list)
		return card_list

if __name__ == '__main__':
	run(MyScene(), show_fps=False)

◆ Execution example

This is the process of creating playing cards with Pythonista + scene.

Pythonista is also summarized in the blog. Summary of how to use Pythonista

I made a poker making course with Pythonista on my blog. Pythonista + scene poker creation course

Recommended Posts

Randomly display playing cards in Pythonista + scene
Defeat ui.View.flex in Pythonista3
Display the timetable of the Morioka bus location system in Pythonista