Avengers War (II)
What you have so far:
Now you will be able to :
- read from cards.txt
- get data for each hero
- use turtle to draw the picture of the hero
- write the data under the picture
Let’s enhance!
Randomly choose a hero
When player inputs “random”, pick a random card for him.
- Create a list of cards
- pick a random item from the list
if card == 'random':
card = random.choice(list(cards.keys()))
Organise your code
Organise you code to fulfil the following function:
- function name : choose_card
- input:
- card (string): the choosen card name (like ‘thor’)
- cards (dict): the cards dictionary
- pen (Pen): the turtle pen
- xcor (int): x place of the hero drawing
- ycor (int): y place of the hero drawing
- output:
- the “score” of the chosen card
- score = power + intelligence + speed
- what it does:
- given the input, draw the chosen hero at the chosen place
- return the score of the input card
def choose_card(card, cards, pen, xcor, ycor):
if card == 'random':
card = random.choice(list(cards.keys()))
if card in cards:
stats = cards[card]
pen.goto(xcor,ycor)
pen.shape(stats[-1])
pen.stamp()
pen.setheading(-90)
pen.forward(120)
pen.write("Name: " + card, font = style1, align = 'center' )
pen.forward(25)
pen.write("Power: " + stats[0], font = style1, align = 'center' )
pen.forward(25)
pen.write("Intelligence: " + stats[1], font = style1, align = 'center' )
pen.forward(25)
pen.write("Speed: " + stats[2], font = style1, align = 'center' )
score = int(stats[0]) + int(stats[1]) + int(stats[2])
return score
Call the function for a Duel !
Call the above function for two players, to make a duel, like this:
while True:
player_1 = input("Player 1 : ")
player_2 = input("Player 2 : ")
t.clear()
s1 = choose_card(player_1, cards, t, -100, 100)
t.goto(50,50)
t.write("VS.", font = style2, align = 'center')
s2 = choose_card(player_2, cards, t, 200, 100)
t.goto(50,150)
t.color('red')
if s1>s2:
t.write("Player 1 WIN !", font = style2, align = 'center')
elif s1<s2:
t.write("Player 2 WIN !", font = style2, align = 'center')
else:
t.write("DRAW !", font = style2, align = 'center')
Add more hero
You can add other heroes you like in this game, it’s quite easy and you don’t even have to change your code !
- change cards.txt to add a new line for a new hero
- put the hero’s picture (GIF format) in the same folder as your python code
here is an example when you added some “fun” heroes:
Homework
- Play against the Computer !
- The computer is now player2
- It choose card randomly
- Play against it!
- Card Game!
- Make card choosing really like a card game
- Each avengers can be chosen only once!
Reference code and files
Here is a link of the code and files so far, in case you need them: