Advertisement
steve-shambles-2109

Hi-Lo V1.52MP-windows and linux

Nov 20th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.71 KB | None | 0 0
  1. """
  2. Hi-Lo V1.52MP-windows and linux
  3. A GUI High-Low card game.
  4. By Steve Shambles Nov 2018, updated Nov 2019
  5. https://stevepython.wordpress.com/
  6. """
  7. from tkinter import Button, DISABLED, E, Label, LabelFrame, messagebox
  8. from tkinter import Menu, NORMAL, PhotoImage, Tk, W
  9. import random
  10. import sys
  11. import webbrowser
  12.  
  13. from pygame import mixer
  14.  
  15. class Glo:
  16.     """Global store, this makes these vars Global,e.g Glo.var."""
  17.     points_won = 0
  18.     high_score = 0
  19.     card1_val = 0
  20.     card2_val = 0
  21.     new_card = 0
  22.     random_card = 0
  23.     random_suit = 0
  24.     rnd_cc = 0
  25.  
  26. if sys.platform.startswith('win'):
  27.     platform = 'Windows'
  28. if sys.platform.startswith('linux'):
  29.     platform = 'Linux'
  30.  
  31. mixer.init()
  32.  
  33. root = Tk()
  34. root.title('Hi-Lo V1.52 MP')
  35.  
  36. if platform == 'Windows':
  37.     root.geometry('260x252')
  38.     root.resizable(False, False)
  39.  
  40. else:
  41.     root.geometry('282x251')
  42.  
  43.  
  44. # Frame for the buttons.
  45. btn_frame = LabelFrame(root)
  46. btn_frame.grid(row=0, column=1)
  47.  
  48. # Frame for the two card images.
  49. cards_frame = LabelFrame(root, pady=4)
  50. cards_frame.grid(row=0, column=0, padx=10, pady=5)
  51.  
  52. # Frame for the change button.
  53. chng_btn_frame = LabelFrame(root)
  54. chng_btn_frame.grid(row=1, column=0, sticky=W, padx=10, pady=10)
  55.  
  56. # Frame for printng the scores.
  57. score_frame = LabelFrame(root)
  58. score_frame.grid(row=2, column=0, padx=10, columnspan=2, sticky=W+E)
  59.  
  60. # Frame for printing messages.
  61. msg_frame = LabelFrame(root)
  62. msg_frame.grid(row=3, column=0, padx=10, columnspan=2, sticky=W+E)
  63.  
  64.  
  65. def about_menu():
  66.     """Display about program info if selected in drop-down menu."""
  67.     messagebox.showinfo('About', 'Hi-Lo V1.52 MP Freeware.\n\n'
  68.                         'By Steve Shambles 2018, updated Nov 2019.\n\n'
  69.                         'For Windows and Linux.')
  70.  
  71. def help_menu():
  72.     """How to use the program, menu help text."""
  73.     messagebox.showinfo('How To...', 'Play Hi-Lo.\n\n'
  74.                         'This game is extremly simple to play.\n\n'
  75.                         'All you have to do is guess if the blank\n'
  76.                         'card on the right will be higher or lower than\n'
  77.                         'the card shown on the left.\n\n'
  78.                         'At random times the CHANGE CARD button may light up.\n'
  79.                         'When lit, click the change card button if you want\n'
  80.                         'to change a bad card.\n\n'
  81.                         'A Joker is wild, i.e. counted as anything.\n'
  82.                         'If you turn over two jokers together you will\n'
  83.                         'be awarded a bonus of 3pts.\n\n'
  84.                         'Pairs also give you a win to the next round.\n\n'
  85.                         'Aces are counted as the highest card.\n\n'
  86.                         'Your high score will be saved to disc and reloaded\n'
  87.                         'the next time you play.\n\n'
  88.                         'Enjoy.')
  89.  
  90. def visit_blog():
  91.     """Visit my python blog if selected in drop-down menu."""
  92.     webbrowser.open('https://stevepython.wordpress.com/')
  93.  
  94. def visit_bensound():
  95.     """Visit bensound.com blog if selected in drop-down menu."""
  96.     webbrowser.open('https://bensound.com/')
  97.  
  98. def save_high_score():
  99.     """Save high score to file in current dir."""
  100.     with open('high_score.txt', 'w') as contents:
  101.         save_it = str(Glo.high_score)
  102.         contents.write(save_it)
  103.  
  104. def play_music(track):
  105.     """Play background music if a track is selected in drop-down menu"""
  106.     if track == "1":
  107.         mixer.music.load('cards/music/bensound-summer.mp3')
  108.     if track == "2":
  109.         mixer.music.load('cards/music/bensound-dance.mp3')
  110.     if track == "3":
  111.         mixer.music.load('cards/music/bensound-dreams.mp3')
  112.     if track == "4":
  113.          mixer.music.load('cards/music/bensound-motion.mp3')
  114.     if track == "5":
  115.         mixer.music.load('cards/music/bensound-scifi.mp3')
  116.  
  117.     mixer.music.play(-1)#play indefinetly -1. 0 or () = play once.
  118.  
  119. def stop_music():
  120.     """Stop music playing if selected in drop-down menu."""
  121.     mixer.music.stop()
  122.  
  123. def load_high_score():
  124.     """Load in text file containing players best high score."""
  125.     with open('high_score.txt', 'r') as contents:
  126.         saved_high_score = contents.read()
  127.         if saved_high_score > '':
  128.             Glo.high_score = int(saved_high_score)
  129.  
  130. def setup_card_one():
  131.     """Get card 1 randomly and display it."""
  132.     get_rnd_card()
  133.     card_one = Glo.new_card+'.png' #card image filename
  134.     Glo.card1_val = Glo.random_card #card number 0-14
  135.     card_btn_one = Button(cards_frame)
  136.     PHOTO = PhotoImage(file='cards/'+str(card_one))
  137.     card_btn_one.config(image=PHOTO)
  138.     card_btn_one.grid(row=0, column=0, padx=2, pady=2)
  139.     card_btn_one.photo = PHOTO
  140.  
  141. def setup_card_two():
  142.     """Get rnd card for card 2. Only display back of card 2 though."""
  143.     get_rnd_card()
  144.     Glo.card2_val = Glo.random_card
  145.     card_btn_two = Button(cards_frame)
  146.     PHOTO2 = PhotoImage(file='cards/blank.png')
  147.     card_btn_two.config(image=PHOTO2)
  148.     card_btn_two.grid(row=0, column=1, padx=2, pady=2)
  149.     card_btn_two.photo = PHOTO2
  150.  
  151. def show_card_two():
  152.     """Reveal card two."""
  153.     but_2a = Button(cards_frame)
  154.     photo_2a = PhotoImage(file='cards/'+str(Glo.new_card)+'.png')
  155.     but_2a.config(image=photo_2a)
  156.     but_2a.grid(row=0, column=1, padx=2, pady=2)
  157.     but_2a.photo = photo_2a
  158.  
  159. def get_rnd_card():
  160.     """Get a new random card."""
  161.     joker = False
  162.     # Note: Ace=14 (highest card), Jack=11, Queen=12, King=13
  163.     # Joker is wild when rnd is zero
  164.     Glo.random_card = random.randint(0, 13)
  165.     Glo.random_suit = random.randint(1, 4)
  166.  
  167.     # Make ace highest card
  168.     if Glo.random_card == 1:
  169.         Glo.random_card = 14
  170.  
  171.     # Mark joker if rnd = 0
  172.     if Glo.random_card == 0:
  173.         joker = True
  174.  
  175.     # Choose a random suit.
  176.     if Glo.random_suit == 1:
  177.         card_suit = 'clubs'
  178.     if Glo.random_suit == 2:
  179.         card_suit = 'hearts'
  180.     if Glo.random_suit == 3:
  181.         card_suit = 'spades'
  182.     if Glo.random_suit == 4:
  183.         card_suit = 'diamonds'
  184.  
  185.     # Override suit if joker
  186.     if joker:
  187.         card_suit = 'joker'
  188.  
  189.     # Create the new card.
  190.     Glo.new_card = str(Glo.random_card)+'-'+str(card_suit)
  191.  
  192. def clicked_higher():
  193.     """Clicked on 'Higher' button."""
  194.     chng_crd_btn.configure(state=DISABLED)
  195.     # Correct guess, card 2 was higher, same card value or a joker.
  196.     if Glo.card1_val < Glo.card2_val or Glo.card1_val == Glo.card2_val or Glo.card1_val == 0 or Glo.card2_val == 0:
  197.         Glo.points_won = Glo.points_won +1
  198.  
  199.         # Update highscore.
  200.         if Glo.points_won > Glo.high_score:
  201.             Glo.high_score = Glo.points_won
  202.  
  203.         update_score()
  204.         print_correct()
  205.  
  206.         # Print relevent game messages.
  207.         if Glo.card1_val == Glo.card2_val:
  208.             print_pairs()
  209.  
  210.         if Glo.card1_val == 0 or Glo.card2_val == 0:
  211.             print_joker()
  212.  
  213.         if Glo.card1_val == 0 and Glo.card2_val == 0:
  214.             joker_bonus()
  215.  
  216.         # Enable\disable relevent buttons.
  217.         nxt_crd_btn.configure(state=NORMAL)
  218.         hghr_btn.configure(state=DISABLED)
  219.         lwr_btn.configure(state=DISABLED)
  220.  
  221.         # Player guessed incorrectly.
  222.     else:
  223.         print_wrong()
  224.         show_card_two()
  225.         game_over_man()
  226.         return
  227.  
  228.     # Player was correct display card 2.
  229.     show_card_two()
  230.  
  231. def joker_bonus():
  232.     """When 2 jokers come up togther award bonus points."""
  233.     msg_label = Label(msg_frame, fg='blue', text='3 POINTS JOKER BONUS!          ')
  234.     msg_label.grid(row=2, column=0, pady=2, sticky=W)
  235.     Glo.points_won = Glo.points_won +3
  236.     update_score()
  237.  
  238. def clicked_lower():
  239.     """Clicked on 'Lower' button"""
  240.     chng_crd_btn.configure(state=DISABLED)
  241.  
  242.     if Glo.card1_val > Glo.card2_val or Glo.card1_val == Glo.card2_val  or Glo.card1_val ==0 or Glo.card2_val ==0:
  243.         Glo.points_won = Glo.points_won +1
  244.  
  245.         if Glo.points_won > Glo.high_score:
  246.             Glo.high_score = Glo.points_won
  247.  
  248.         update_score()
  249.         print_correct()
  250.  
  251.         if Glo.card1_val == Glo.card2_val:
  252.             print_pairs()
  253.  
  254.         if Glo.card1_val == 0 or Glo.card2_val == 0:
  255.             print_joker()
  256.  
  257.         if Glo.card1_val == 0 and Glo.card2_val == 0:
  258.             joker_bonus()
  259.  
  260.         nxt_crd_btn.configure(state=NORMAL)
  261.         hghr_btn.configure(state=DISABLED)
  262.         lwr_btn.configure(state=DISABLED)
  263.  
  264.     else:
  265.         print_wrong()
  266.         show_card_two()
  267.         game_over_man()
  268.         return
  269.  
  270.     show_card_two()
  271.  
  272. def update_score():
  273.     """Update score."""
  274.     # Note, the spacing is important to erase prev message.
  275.     msg_label = Label(score_frame,  \
  276.     text='Score :'+str(Glo.points_won)+'    Best :'+str(Glo.high_score)+'          ')
  277.     msg_label.grid(row=0, column=1, pady=4, sticky=W)
  278.  
  279. def print_aces_are_high():
  280.     """Print 'Aces are high' in-game message."""
  281.     msg_label = Label(msg_frame, fg='blue', text='ACES ARE HIGH!                 ')
  282.     msg_label.grid(row=2, column=0, pady=4, sticky=W)
  283.  
  284. def print_joker():
  285.     """Print 'Jokers are wild' in-game message."""
  286.     msg_label = Label(msg_frame, fg='blue', text='JOKERS ARE WILD!          ')
  287.     msg_label.grid(row=2, column=0, pady=2, sticky=W)
  288.  
  289. def print_correct():
  290.     """Print 'Well done' in-game message."""
  291.     msg_label = Label(msg_frame, fg='green', text='WELL DONE! CLICK NEXT')
  292.     msg_label.grid(row=2, column=0, pady=2, sticky=W)
  293.  
  294. def print_wrong():
  295.     """Print 'Unlucky' in-game message."""
  296.     msg_label = Label(msg_frame, fg='red', text='UNLUCKY!                    ')
  297.     msg_label.grid(row=2, column=0, pady=2, sticky=W)
  298.  
  299. def print_higher_or_lower():
  300.     '''Print 'Higher or Lower in-game message.'''
  301.     msg_label = Label(msg_frame, fg='blue', text='HIGHER OR LOWER?           ')
  302.     msg_label.grid(row=2, column=0, pady=2, sticky=W)
  303.  
  304. def print_pairs():
  305.     """Print 'pairs are good' in-game message."""
  306.     msg_label = Label(msg_frame, text='PAIRS ARE GOOD!                  ')
  307.     msg_label.grid(row=2, column=0, pady=2, sticky=W)
  308.  
  309. def clicked_next():
  310.     """Clicked on 'Next' button."""
  311.     setup_card_one()
  312.     setup_card_two()
  313.     print_higher_or_lower()
  314.  
  315.     if Glo.card1_val == 14:
  316.         print_aces_are_high()
  317.  
  318.     update_score()
  319.     nxt_crd_btn.configure(state=DISABLED)
  320.     hghr_btn.configure(state=NORMAL)
  321.     lwr_btn.configure(state=NORMAL)
  322.  
  323.     # Random chance to change card 1.
  324.     rnd_change_card_chance()
  325.     if Glo.rnd_cc == 3:
  326.         chng_crd_btn.configure(state=NORMAL)
  327.     else:
  328.         chng_crd_btn.configure(state=DISABLED)
  329.  
  330. def game_over_man():
  331.     """Game over, player guessed incorrectly."""
  332.     if Glo.points_won > Glo.high_score or Glo.points_won == Glo.high_score:
  333.         Glo.high_score = Glo.points_won
  334.  
  335.         ask_yn = messagebox.askyesno  \
  336.         ('OUCH!', '       G A M E  O V E R  M A N  \n\n'  \
  337.         '\n Congratulations, a new high score!\n\n          Play another game?')
  338.  
  339.         if ask_yn is False:
  340.             root.destroy
  341.             exit()
  342.  
  343.     else:
  344.         ask_yn = messagebox.askyesno  \
  345.         ('OUCH!', 'G A M E  O V E R  M A N\n\n   Play another game?')
  346.  
  347.     if ask_yn is False:
  348.         root.destroy
  349.         exit()
  350.  
  351.     save_high_score()
  352.     new_game()
  353.  
  354. def clicked_change():
  355.     """Clicked on the enabled 'change' button."""
  356.     setup_card_one()
  357.     setup_card_two()
  358.     chng_crd_btn.configure(state=DISABLED)
  359.  
  360. def rnd_change_card_chance():
  361.     """Randomly choose wether to allow a card change."""
  362.     Glo.rnd_cc = random.randint(1, 6)
  363.  
  364. def new_game():
  365.     """Start a new game, retaining high score"""
  366.     Glo.points_won = 0
  367.     update_score()
  368.     print_higher_or_lower()
  369.     clicked_next()
  370.  
  371. def exit_game():
  372.     ask_exit = messagebox.askyesno('Exit game', 'Are you sure you want to exit?')
  373.     if ask_exit:
  374.         stop_music()
  375.         root.destroy()
  376.     else:
  377.         return
  378.  
  379. # Higher-Lower buttons.
  380. hghr_btn = Button(btn_frame, bg='limegreen', text='HIGHER',
  381.                   command=clicked_higher)
  382. hghr_btn.grid(row=0, column=0, pady=3, padx=3, sticky=W+E)
  383. lwr_btn = Button(btn_frame, bg='indianred', text='LOWER ', command=clicked_lower)
  384. lwr_btn.grid(row=1, column=0, pady=3, padx=3, sticky=W+E)
  385. nxt_crd_btn = Button(btn_frame, bg='orange', text='NEXT', command=clicked_next)
  386. nxt_crd_btn.grid(row=2, column=0, pady=3, padx=3, sticky=W+E)
  387. nxt_crd_btn.configure(state=DISABLED)
  388.  
  389. # The change button.
  390. chng_crd_btn = Button(chng_btn_frame, bg='plum', text='   CHANGE CARD?    ',
  391.                     command=clicked_change)
  392. chng_crd_btn.grid(row=0, column=0, pady=0, padx=0)
  393. chng_crd_btn.configure(state=DISABLED)
  394.  
  395. # Drop-down menu.
  396. menu_bar = Menu(root)
  397. file_menu = Menu(menu_bar, tearoff=0)
  398. menu_bar.add_cascade(label='Menu', menu=file_menu)
  399. file_menu.add_command(label='About', command=about_menu)
  400. file_menu.add_command(label='Help', command=help_menu)
  401. file_menu.add_separator()
  402. file_menu.add_command(label='Visit Blog', command=visit_blog)
  403. file_menu.add_command(label='Exit', command=exit_game)
  404.  
  405. file_menu2 = Menu(menu_bar, tearoff=0)
  406. menu_bar.add_cascade(label='Music', menu=file_menu2)
  407. file_menu2.add_command(label='Play Summer', command=lambda: play_music("1"))
  408. file_menu2.add_command(label='Play Dance', command=lambda: play_music("2"))
  409. file_menu2.add_command(label='Play Dreams', command=lambda: play_music("3"))
  410. file_menu2.add_command(label='Play Motion', command=lambda: play_music("4"))
  411. file_menu2.add_command(label='Play SciF1', command=lambda: play_music("5"))
  412. file_menu2.add_separator()
  413. file_menu2.add_command(label='Stop music', command=stop_music)
  414. file_menu2.add_command(label='Free music from Bensound.com',
  415.                        command=visit_bensound)
  416. root.config(menu=menu_bar)
  417.  
  418. # Main.
  419. load_high_score()
  420. new_game()
  421.  
  422. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement