Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import play
- from random import randint
- # game rules:
- hello = play.new_text(words='Think you can beat the computer?', x=0, y=220)
- hello2 = play.new_text(words='Closest to 21 wins!', x=0, y=180)
- # Make buttons:
- button_add = play.new_box(color='green', border_width=1, border_color='grey', x=-130, y=-160, width=170, height=40)
- add_txt = play.new_text(words='Feeling lucky...', font_size=30, x=-130, y=-160, color='white')
- button_stop = play.new_box(color='red', border_width=1, border_color='grey', x=130, y=-160, width=190, height=40)
- stop_txt = play.new_text(words='Hit me!', font_size=30, x=130, y=-160, color='white')
- # Set up card:
- card = play.new_image(image='card1.png', size=40, x=-150, y=20)
- card.hide()
- # Set up results:
- you_score = play.new_text(words='0', x = -100, y=-230)
- comp_score = play.new_text(words='0', x = 100, y=-230)
- result = play.new_text(words='', x=-200, y=-230)
- steps_txt1 = play.new_text(words='Cards drawn: ', x=-20, y=-280, font_size=30)
- steps_txt2 = play.new_text(words='0', x=60, y=-280, font_size=30) # number of user moves
- speed = 1.5 # pause between moves (sec)
- def lose():
- comp_score.color='green'
- you_score.color='red'
- result.color='red'
- result.words='You lose: ' # Defeat
- def win():
- comp_score.color='red'
- you_score.color='green'
- result.color='green'
- result.words='You win: ' # Winning
- def draw():
- result.words='Tie game: ' # Draw
- @button_add.when_clicked
- async def add_card():
- number=randint(1,11)
- # Pick a random number
- # Show a card with this number and add this score to the counter:
- card.image = 'card' + str(number) + '.png'
- card.show()
- you_score.words = int(you_score.words) + number
- steps_txt2.words = int(steps_txt2.words) + 1
- await play.timer(speed)
- card.hide()
- # Check the score:
- if int(you_score.words) > 21: # Bust! show defeat
- lose()
- @button_stop.when_clicked
- async def comp_play():
- card.x = 150 # computer cards show on the right
- steps = int(steps_txt2.words)
- # number of completed moves is displayed on the screen
- for i in range(steps): # do the same number of times
- number = randint(1, 11)
- card.image = 'card' + str(number) + '.png'
- card.show()
- comp_score.words = int(comp_score.words) + number
- await play.timer(speed)
- card.hide()
- # win and lose
- if int(comp_score.words) > 21 or int(comp_score.words) < int(you_score.words):
- win()
- elif int(comp_score.words) > int(you_score.words):
- lose()
- else:
- draw()
- play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement