generated from mwc/project_game
added demo game in chris_demo
You had some really clear planning in your commit message-- In the "chris_demo" folder I implemented a bunch of what you planned. I suggest playing the game I built (go into chris_demo and run python game.py), and then carefully reading all the source code. Then, if you like the patterns I used, copy or move whatever you want to use into your main project folder. Also, if there are parts of my code you don't understand, please let me know (in a commit message or in person next time I'm in class) and I'll be happy to explain it.
This commit is contained in:
146
chris_demo/card.py
Normal file
146
chris_demo/card.py
Normal file
@@ -0,0 +1,146 @@
|
||||
|
||||
suits = ['♠', '♥', '♦', '♣']
|
||||
colors = ['black', 'red', 'red', 'black']
|
||||
ranks = "A23456789TJQK"
|
||||
|
||||
class Card:
|
||||
"""A card is not an agent itself, but it manages two agents
|
||||
which are always next to each other, representing
|
||||
the card as something like '♠A'.
|
||||
"""
|
||||
display = False
|
||||
|
||||
def __init__(self, position, index, hidden=False, selected=False):
|
||||
"""When creating a card, you pass an index, which should be a number between
|
||||
0 and 51. If you imagine a deck of playing cards in order (all the spades from A-K,
|
||||
then all the hearts from A-K, etc.), then the index is all we need to identify which
|
||||
card this is. See the CardSuit and CardRank classes below for the math we can use
|
||||
to convert an index value to a suit or a rank.
|
||||
"""
|
||||
self.index = index
|
||||
self.name = str(index)
|
||||
self.agents = [CardSuit(index, hidden, selected), CardRank(index, hidden, selected)]
|
||||
self.set_position(position)
|
||||
|
||||
def __str__(self):
|
||||
return ''.join(a.character for a in self.agents)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Card {self.index} {self}>"
|
||||
|
||||
def value(self):
|
||||
"""Returns how many points this card is worth. Currently treats aces as worth 1.
|
||||
Later we might need a way of considering that an ace could be worth 1 or 11 in
|
||||
blackjack.
|
||||
"""
|
||||
rank_index = self.index % 13
|
||||
return rank_index + 1
|
||||
|
||||
def set_position(self, position):
|
||||
"""When the Card's position gets set, it updates
|
||||
the position of its suit and rank. In this way, we have one
|
||||
agent (Card) which manages the positions of two agents on the
|
||||
screen.
|
||||
"""
|
||||
x, y = position
|
||||
suit, rank = self.agents
|
||||
self.position = position
|
||||
suit.position = position
|
||||
rank.position = (x+1, y)
|
||||
|
||||
def set_z(self, z):
|
||||
suit, rank = self.agents
|
||||
suit.z = z
|
||||
rank.z = z
|
||||
|
||||
def hide(self):
|
||||
for agent in self.agents:
|
||||
agent.hide()
|
||||
|
||||
def show(self):
|
||||
for agent in self.agents:
|
||||
agent.show()
|
||||
|
||||
def select(self):
|
||||
for agent in self.agents:
|
||||
agent.select()
|
||||
|
||||
def deselect(self):
|
||||
for agent in self.agents:
|
||||
agent.deselect()
|
||||
|
||||
class CardSuit:
|
||||
def __init__(self, index, hidden=False, selected=False):
|
||||
self.name = f"{index}_suit"
|
||||
self.index = index
|
||||
self.suit_color = colors[index // 13]
|
||||
self.hidden = hidden
|
||||
self.selected = selected
|
||||
self.update_display()
|
||||
|
||||
def select(self):
|
||||
self.selected = True
|
||||
self.update_display()
|
||||
|
||||
def deselect(self):
|
||||
self.selected = False
|
||||
self.update_display()
|
||||
|
||||
def show(self):
|
||||
self.hidden = False
|
||||
self.update_display()
|
||||
|
||||
def hide(self):
|
||||
self.hidden = True
|
||||
self.update_display()
|
||||
|
||||
def update_display(self):
|
||||
if self.hidden:
|
||||
self.character = "▒"
|
||||
fg = "blue"
|
||||
else:
|
||||
self.character = suits[self.index //13]
|
||||
fg = self.suit_color
|
||||
if self.selected:
|
||||
bg = "yellow"
|
||||
else:
|
||||
bg = "gray"
|
||||
self.color = fg + "_on_" + bg
|
||||
|
||||
class CardRank:
|
||||
def __init__(self, index, hidden=False, selected=False):
|
||||
self.name = f"{index}_rank"
|
||||
self.index = index
|
||||
self.suit_color = colors[index // 13]
|
||||
self.hidden = hidden
|
||||
self.selected = selected
|
||||
self.update_display()
|
||||
|
||||
def select(self):
|
||||
self.selected = True
|
||||
self.update_display()
|
||||
|
||||
def deselect(self):
|
||||
self.selected = False
|
||||
self.update_display()
|
||||
|
||||
def show(self):
|
||||
self.hidden = False
|
||||
self.update_display()
|
||||
|
||||
def hide(self):
|
||||
self.hidden = True
|
||||
self.update_display()
|
||||
|
||||
def update_display(self):
|
||||
if self.hidden:
|
||||
self.character = "▒"
|
||||
fg = "blue"
|
||||
else:
|
||||
self.character = ranks[self.index % 13]
|
||||
fg = self.suit_color
|
||||
if self.selected:
|
||||
bg = "yellow"
|
||||
else:
|
||||
bg = "gray"
|
||||
self.color = fg + "_on_" + bg
|
||||
Reference in New Issue
Block a user