Advertisement
shh_algo_PY

Twenty-One

Apr 10th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import play
  2. from random import randint
  3.  
  4. # game rules:
  5.  
  6. hello = play.new_text(words='Think you can beat the computer?', x=0, y=220)
  7. hello2 = play.new_text(words='Closest to 21 wins!', x=0, y=180)
  8.  
  9. # Make buttons:
  10.  
  11. button_add = play.new_box(color='green', border_width=1, border_color='grey', x=-130, y=-160, width=170, height=40)
  12.  
  13. add_txt = play.new_text(words='Feeling lucky...', font_size=30, x=-130, y=-160, color='white')
  14.  
  15. button_stop = play.new_box(color='red', border_width=1, border_color='grey', x=130, y=-160, width=190, height=40)
  16.  
  17. stop_txt = play.new_text(words='Hit me!', font_size=30, x=130, y=-160, color='white')
  18.  
  19. # Set up card:
  20.  
  21. card = play.new_image(image='card1.png', size=40, x=-150, y=20)
  22.  
  23. card.hide()
  24.  
  25. # Set up results:
  26.  
  27. you_score = play.new_text(words='0', x = -100, y=-230)
  28.  
  29. comp_score = play.new_text(words='0', x = 100, y=-230)
  30.  
  31. result = play.new_text(words='', x=-200, y=-230)
  32.  
  33. steps_txt1 = play.new_text(words='Cards drawn: ', x=-20, y=-280, font_size=30)
  34.  
  35. steps_txt2 = play.new_text(words='0', x=60, y=-280, font_size=30) # number of user moves
  36.  
  37. speed = 1.5 # pause between moves (sec)
  38.  
  39. def lose():
  40.     comp_score.color='green'
  41.     you_score.color='red'
  42.     result.color='red'
  43.     result.words='You lose: ' # Defeat
  44.  
  45. def win():
  46.     comp_score.color='red'
  47.     you_score.color='green'
  48.     result.color='green'
  49.     result.words='You win: ' # Winning
  50.  
  51. def draw():
  52.     result.words='Tie game: ' # Draw
  53.  
  54. @button_add.when_clicked
  55. async def add_card():
  56.     number=randint(1,11)
  57.     # Pick a random number
  58.     # Show a card with this number and add this score to the counter:
  59.     card.image = 'card' + str(number) + '.png'
  60.     card.show()
  61.     you_score.words = int(you_score.words) + number
  62.     steps_txt2.words = int(steps_txt2.words) + 1
  63.     await play.timer(speed)
  64.     card.hide()
  65.     # Check the score:
  66.     if int(you_score.words) > 21: # Bust! show defeat
  67.         lose()
  68.  
  69. @button_stop.when_clicked
  70. async def comp_play():
  71.     card.x = 150 # computer cards show on the right
  72.     steps = int(steps_txt2.words)
  73.     # number of completed moves is displayed on the screen
  74.     for i in range(steps): # do the same number of times
  75.         number = randint(1, 11)
  76.         card.image = 'card' + str(number) + '.png'
  77.         card.show()
  78.         comp_score.words = int(comp_score.words) + number
  79.         await play.timer(speed)
  80.         card.hide()
  81.  
  82. # win and lose
  83.     if int(comp_score.words) > 21 or int(comp_score.words) < int(you_score.words):
  84.         win()
  85.     elif int(comp_score.words) > int(you_score.words):
  86.         lose()
  87.     else:
  88.         draw()
  89.  
  90. play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement