├── README.md └── edition_1 ├── .goutputstream-M1ELIW ├── 03_01_dice.py ├── 03_02_double_dice.py ├── 03_03_double_dice_solution.py ├── 03_04_double_dice_while.py ├── 03_05_double_dice_while_break.py ├── 04_01_list_and_for.py ├── 04_02_polite_function.py ├── 04_03_hello_n.py ├── 04_04_hangman_words.py ├── 04_05_hangman_play.py ├── 04_06_hangman_get_guess.py ├── 04_07_hangman_print_word.py ├── 04_08_hangman_full.py ├── 04_09_hangman_full_solution.py ├── 04_10_stats.py ├── 04_11_except.py ├── 05_01_converter.py ├── 05_02_converter_offset_bad.py ├── 05_03_converters_final.py ├── 06_01_hangman_file.py ├── 06_02_hangman_file_try.py ├── 06_03_file_readline.py ├── 06_04_amazon_scraping.py ├── 07_01_hello.py ├── 07_02_temp_framework.py ├── 07_03_temp_ui.py ├── 07_04_temp_final.py ├── 07_05_kitchen_sink.py ├── 07_06_resizing.py ├── 07_07_scrolling.py ├── 07_08_dialogs.py ├── 07_09_color_chooser.py ├── 07_10_menus.py ├── 07_11_canvas.py ├── 08_01_hello_pygame.py ├── 08_02_rasp_game_mouse.py ├── 08_03_rasp_game_one.py ├── 08_04_rasp_game_scoring.py ├── 08_05_rasp_game_refactored.py ├── 08_06_rasp_game_final.py ├── 08_pygame.py ├── 08_raspberry_bounce_1.py ├── 08_raspberry_bounce_2.py ├── 08_raspberry_bounce_3.py ├── 08_raspberry_bounce_4.py ├── 08_raspberry_bounce_5.py ├── 10_01_clock.py ├── 10_02_fancy_clock.py ├── 11_01_rover_basic.py ├── 11_02_rover_plus.py ├── PiTest └── PiTest.ino ├── converters.py ├── hangman_words.txt ├── mylist.pickle ├── raspberry.jpg ├── raspirobot_basic.desktop ├── raspirobot_plus.desktop ├── spoon.jpg ├── test.txt └── test_dup.txt /README.md: -------------------------------------------------------------------------------- 1 | monk_raspberrypi 2 | ================ 3 | 4 | Raspberry Pi code by Simon Monk for the book Programming Raspberry Pi: Getting Started with Python -------------------------------------------------------------------------------- /edition_1/.goutputstream-M1ELIW: -------------------------------------------------------------------------------- 1 | #07_11_canvas.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | canvas = Canvas(master, width=400, height=200) 9 | canvas.pack() 10 | canvas.create_rectangle(20, 20, 300, 100, fill='blue') 11 | canvas.create_oval(30, 50, 290, 190, fill='#ff2277') 12 | canvas.create_line(0, 0, 400, 200, fill='black', width=5) 13 | root = Tk() 14 | app = App(root) 15 | root.mainloop() 16 | -------------------------------------------------------------------------------- /edition_1/03_01_dice.py: -------------------------------------------------------------------------------- 1 | #03_01_dice 2 | import random 3 | for x in range(1, 11): 4 | random_number = random.randint(1, 6) 5 | print(random_number) -------------------------------------------------------------------------------- /edition_1/03_02_double_dice.py: -------------------------------------------------------------------------------- 1 | #03_02_double_dice 2 | import random 3 | for x in range(1, 11): 4 | throw_1 = random.randint(1, 6) 5 | throw_2 = random.randint(1, 6) 6 | total = throw_1 + throw_2 7 | print(total) 8 | if total == 7: 9 | print('Seven Thrown!') 10 | if total == 11: 11 | print('Eleven Thrown!') 12 | if throw_1 == throw_2: 13 | print('Double Thrown!') -------------------------------------------------------------------------------- /edition_1/03_03_double_dice_solution.py: -------------------------------------------------------------------------------- 1 | #03_03_double_dice_solution 2 | import random 3 | for x in range(1, 11): 4 | throw_1 = random.randint(1, 6) 5 | throw_2 = random.randint(1, 6) 6 | total = throw_1 + throw_2 7 | print(total) 8 | if total == 7: 9 | print('Seven Thrown!') 10 | if total == 11: 11 | print('Eleven Thrown!') 12 | if throw_1 == throw_2: 13 | print('Double Thrown!') 14 | if total >= 5 and total <= 9: 15 | print('Not Bad!') 16 | if total > 10: 17 | print('Good Throw!') 18 | if total < 4: 19 | print('Unlucky!') 20 | -------------------------------------------------------------------------------- /edition_1/03_04_double_dice_while.py: -------------------------------------------------------------------------------- 1 | #03_04_double_dice_while 2 | import random 3 | throw_1 = random.randint(1, 6) 4 | throw_2 = random.randint(1, 6) 5 | while not (throw_1 == 6 and throw_2 == 6): 6 | total = throw_1 + throw_2 7 | print(total) 8 | throw_1 = random.randint(1, 6) 9 | throw_2 = random.randint(1, 6) 10 | print('Double Six thrown!') -------------------------------------------------------------------------------- /edition_1/03_05_double_dice_while_break.py: -------------------------------------------------------------------------------- 1 | #03_05_double_dice_while_break 2 | import random 3 | while True: 4 | throw_1 = random.randint(1, 6) 5 | throw_2 = random.randint(1, 6) 6 | total = throw_1 + throw_2 7 | print(total) 8 | if throw_1 == 6 and throw_2 == 6: 9 | break 10 | print('Double Six thrown!') -------------------------------------------------------------------------------- /edition_1/04_01_list_and_for.py: -------------------------------------------------------------------------------- 1 | #04_01_list_and_for 2 | list = [1, 'one', 2, True] 3 | for item in list: 4 | print(item) 5 | -------------------------------------------------------------------------------- /edition_1/04_02_polite_function.py: -------------------------------------------------------------------------------- 1 | #04_02_polite_function 2 | def make_polite(sentence): 3 | polite_sentence = sentence + ' please' 4 | return polite_sentence 5 | 6 | print(make_polite('Pass the salt')) 7 | -------------------------------------------------------------------------------- /edition_1/04_03_hello_n.py: -------------------------------------------------------------------------------- 1 | #04_03_hello_n 2 | def say_hello(n): 3 | for x in range(0, n): 4 | print('Hello') 5 | 6 | say_hello(5) -------------------------------------------------------------------------------- /edition_1/04_04_hangman_words.py: -------------------------------------------------------------------------------- 1 | #04_04_hangman_words 2 | import random 3 | 4 | words = ['chicken', 'dog', 'cat', 'mouse', 'frog'] 5 | 6 | def pick_a_word(): 7 | word_position = random.randint(0, len(words) - 1) 8 | return words[word_position] 9 | 10 | print(pick_a_word()) -------------------------------------------------------------------------------- /edition_1/04_05_hangman_play.py: -------------------------------------------------------------------------------- 1 | #04_05_hangman_play 2 | 3 | import random 4 | 5 | words = ['chicken', 'dog', 'cat', 'mouse', 'frog'] 6 | lives_remaining = 14 7 | 8 | def play(): 9 | word = pick_a_word() 10 | while True: 11 | guess = get_guess(word) 12 | if process_guess(guess, word): 13 | print('You win! Well Done!') 14 | break 15 | if lives_remaining == 0: 16 | print('You are Hung!') 17 | print('The word was: ' + word) 18 | break 19 | 20 | def pick_a_word(): 21 | word_position = random.randint(0, len(words) - 1) 22 | return words[word_position] 23 | 24 | def get_guess(word): 25 | return 'a' 26 | 27 | def process_guess(guess, word): 28 | global lives_remaining 29 | lives_remaining = lives_remaining -1 30 | return False 31 | 32 | play() -------------------------------------------------------------------------------- /edition_1/04_06_hangman_get_guess.py: -------------------------------------------------------------------------------- 1 | #04_06_hangman_get_guess 2 | 3 | import random 4 | 5 | words = ['chicken', 'dog', 'cat', 'mouse', 'frog'] 6 | lives_remaining = 14 7 | 8 | def play(): 9 | word = pick_a_word() 10 | while True: 11 | guess = get_guess(word) 12 | if process_guess(guess, word): 13 | print('You win! Well Done!') 14 | break 15 | if lives_remaining == 0: 16 | print('You are Hung!') 17 | print('The word was: ' + word) 18 | break 19 | 20 | def pick_a_word(): 21 | word_position = random.randint(0, len(words) - 1) 22 | return words[word_position] 23 | 24 | def get_guess(word): 25 | print_word_with_blanks(word) 26 | print('Lives Remaining: ' + str(lives_remaining)) 27 | guess = input(' Guess a letter or whole word?') 28 | return guess 29 | 30 | def process_guess(guess, word): 31 | global lives_remaining 32 | lives_remaining = lives_remaining -1 33 | return False 34 | 35 | def print_word_with_blanks(word): 36 | print('print_word_with_blanks: not done yet') 37 | 38 | play() -------------------------------------------------------------------------------- /edition_1/04_07_hangman_print_word.py: -------------------------------------------------------------------------------- 1 | #04_07_hangman_print_word 2 | 3 | import random 4 | 5 | words = ['chicken', 'dog', 'cat', 'mouse', 'frog'] 6 | lives_remaining = 14 7 | guessed_letters = '' 8 | 9 | def play(): 10 | word = pick_a_word() 11 | while True: 12 | guess = get_guess(word) 13 | if process_guess(guess, word): 14 | print('You win! Well Done!') 15 | break 16 | if lives_remaining == 0: 17 | print('You are Hung!') 18 | print('The word was: ' + word) 19 | break 20 | 21 | def pick_a_word(): 22 | word_position = random.randint(0, len(words) - 1) 23 | return words[word_position] 24 | 25 | def get_guess(word): 26 | print_word_with_blanks(word) 27 | print('Lives Remaining: ' + str(lives_remaining)) 28 | guess = input(' Guess a letter or whole word?') 29 | return guess 30 | 31 | def process_guess(guess, word): 32 | global lives_remaining 33 | global guessed_letters 34 | lives_remaining = lives_remaining - 1 35 | guessed_letters = guessed_letters + guess 36 | return False 37 | 38 | def print_word_with_blanks(word): 39 | display_word = '' 40 | for letter in word: 41 | if guessed_letters.find(letter) > -1: 42 | # letter found 43 | display_word = display_word + letter 44 | else: 45 | # letter not found 46 | display_word = display_word + '-' 47 | print(display_word) 48 | 49 | play() -------------------------------------------------------------------------------- /edition_1/04_08_hangman_full.py: -------------------------------------------------------------------------------- 1 | #04_08_hangman_full 2 | import random 3 | 4 | words = ['chicken', 'dog', 'cat', 'mouse', 'frog'] 5 | lives_remaining = 14 6 | guessed_letters = '' 7 | 8 | 9 | 10 | def play(): 11 | word = pick_a_word() 12 | while True: 13 | guess = get_guess(word) 14 | if process_guess(guess, word): 15 | print('You win! Well Done!') 16 | break 17 | if lives_remaining == 0: 18 | print('You are Hung!') 19 | print('The word was: ' + word) 20 | break 21 | 22 | def pick_a_word(): 23 | word_position = random.randint(0, len(words) - 1) 24 | return words[word_position] 25 | 26 | def get_guess(word): 27 | print_word_with_blanks(word) 28 | print('Lives Remaining: ' + str(lives_remaining)) 29 | guess = input(' Guess a letter or whole word?') 30 | return guess 31 | 32 | def print_word_with_blanks(word): 33 | display_word = '' 34 | for letter in word: 35 | if guessed_letters.find(letter) > -1: 36 | # letter found 37 | display_word = display_word + letter 38 | else: 39 | # letter not found 40 | display_word = display_word + '-' 41 | print(display_word) 42 | 43 | def process_guess(guess, word): 44 | if len(guess) > 1: 45 | return whole_word_guess(guess, word) 46 | else: 47 | return single_letter_guess(guess, word) 48 | 49 | 50 | def whole_word_guess(guess, word): 51 | global lives_remaining 52 | if guess == word: 53 | return True 54 | else: 55 | lives_remaining = lives_remaining - 1 56 | return False 57 | 58 | def single_letter_guess(guess, word): 59 | global guessed_letters 60 | global lives_remaining 61 | if word.find(guess) == -1: 62 | # letter guess was incorrect 63 | lives_remaining = lives_remaining - 1 64 | guessed_letters = guessed_letters + guess 65 | if all_letters_guessed(word): 66 | return True 67 | return False 68 | 69 | def all_letters_guessed(word): 70 | for letter in word: 71 | if guessed_letters.find(letter) == -1: 72 | return False 73 | return True 74 | 75 | play() -------------------------------------------------------------------------------- /edition_1/04_09_hangman_full_solution.py: -------------------------------------------------------------------------------- 1 | #04_09_hangman_full_solution 2 | import random 3 | 4 | words = ['chicken', 'dog', 'cat', 'mouse', 'frog'] 5 | lives_remaining = 14 6 | guessed_letters = '' 7 | 8 | 9 | 10 | def play(): 11 | word = pick_a_word() 12 | while True: 13 | guess = get_guess(word) 14 | if process_guess(guess, word): 15 | print('You win! Well Done!') 16 | break 17 | if lives_remaining == 0: 18 | print('You are Hung!') 19 | print('The word was: ' + word) 20 | break 21 | 22 | def pick_a_word(): 23 | word_position = random.randint(0, len(words) - 1) 24 | return words[word_position] 25 | 26 | def get_guess(word): 27 | print_word_with_blanks(word) 28 | print('Lives Remaining: ' + str(lives_remaining)) 29 | guess = input(' Guess a letter or whole word?') 30 | return guess 31 | 32 | def print_word_with_blanks(word): 33 | display_word = '' 34 | for letter in word: 35 | if guessed_letters.find(letter) > -1: 36 | # letter found 37 | display_word = display_word + letter 38 | else: 39 | # letter not found 40 | display_word = display_word + '-' 41 | print(display_word) 42 | 43 | def process_guess(guess, word): 44 | if len(guess) > 1 and len(guess) == len(word): 45 | return whole_word_guess(guess, word) 46 | else: 47 | return single_letter_guess(guess, word) 48 | 49 | 50 | def whole_word_guess(guess, word): 51 | global lives_remaining 52 | if guess.lower() == word.lower(): 53 | return True 54 | else: 55 | lives_remaining = lives_remaining - 1 56 | return False 57 | 58 | def single_letter_guess(guess, word): 59 | global guessed_letters 60 | global lives_remaining 61 | if word.find(guess) == -1: 62 | # letter guess was incorrect 63 | lives_remaining = lives_remaining - 1 64 | guessed_letters = guessed_letters + guess.lower() 65 | if all_letters_guessed(word): 66 | return True 67 | return False 68 | 69 | def all_letters_guessed(word): 70 | for letter in word: 71 | if guessed_letters.find(letter.lower()) == -1: 72 | return False 73 | return True 74 | 75 | play() -------------------------------------------------------------------------------- /edition_1/04_10_stats.py: -------------------------------------------------------------------------------- 1 | #04_09_stats 2 | def stats(numbers): 3 | numbers.sort() 4 | return (numbers[0], numbers[-1]) 5 | 6 | list = [5, 45, 12, 1, 78] 7 | min, max = stats(list) 8 | print(min) 9 | print(max) -------------------------------------------------------------------------------- /edition_1/04_11_except.py: -------------------------------------------------------------------------------- 1 | #04_11_except 2 | try: 3 | list = [1, 2, 3, 4] 4 | list[4] 5 | except IndexError, detail: 6 | print('Oops') 7 | -------------------------------------------------------------------------------- /edition_1/05_01_converter.py: -------------------------------------------------------------------------------- 1 | #05_01_converter 2 | class ScaleConverter: 3 | 4 | def __init__(self, units_from, units_to, factor): 5 | self.units_from = units_from 6 | self.units_to = units_to 7 | self.factor = factor 8 | 9 | def description(self): 10 | return 'Convert ' + self.units_from + ' to ' + self.units_to 11 | 12 | def convert(self, value): 13 | return value * self.factor 14 | 15 | 16 | 17 | c1 = ScaleConverter('inches', 'mm', 25) 18 | print(c1.description()) 19 | print('converting 2 inches') 20 | print(str(c1.convert(2)) + c1.units_to) 21 | -------------------------------------------------------------------------------- /edition_1/05_02_converter_offset_bad.py: -------------------------------------------------------------------------------- 1 | #05_02_converter_offset_bad 2 | class ScaleAndOffsetConverter: 3 | 4 | def __init__(self, units_from, units_to, factor, offset): 5 | self.units_from = units_from 6 | self.units_to = units_to 7 | self.factor = factor 8 | self.offset = offset 9 | 10 | def description(self): 11 | return 'Convert ' + self.units_from + ' to ' + self.units_to 12 | 13 | def convert(self, value): 14 | return value * self.factor + self.offset 15 | 16 | c2 = ScaleAndOffsetConverter('C', 'F', 1.8, 32) 17 | print(c2.description()) 18 | print('converting 20C') 19 | print(str(c2.convert(20)) + c2.units_to) 20 | -------------------------------------------------------------------------------- /edition_1/05_03_converters_final.py: -------------------------------------------------------------------------------- 1 | #05_03_converters_final 2 | class ScaleConverter: 3 | 4 | def __init__(self, units_from, units_to, factor): 5 | self.units_from = units_from 6 | self.units_to = units_to 7 | self.factor = factor 8 | 9 | def description(self): 10 | return 'Convert ' + self.units_from + ' to ' + self.units_to 11 | 12 | def convert(self, value): 13 | return value * self.factor 14 | 15 | class ScaleAndOffsetConverter(ScaleConverter): 16 | 17 | def __init__(self, units_from, units_to, factor, offset): 18 | ScaleConverter.__init__(self, units_from, units_to, factor) 19 | self.offset = offset 20 | 21 | def convert(self, value): 22 | return value * self.factor + self.offset 23 | 24 | c1 = ScaleConverter('inches', 'mm', 25) 25 | print(c1.description()) 26 | print('converting 2 inches') 27 | print(str(c1.convert(2)) + c1.units_to) 28 | 29 | c2 = ScaleAndOffsetConverter('C', 'F', 1.8, 32) 30 | print(c2.description()) 31 | print('converting 20C') 32 | print(str(c2.convert(20)) + c2.units_to) -------------------------------------------------------------------------------- /edition_1/06_01_hangman_file.py: -------------------------------------------------------------------------------- 1 | #06_01_hangman_file 2 | import random 3 | 4 | f = open('hangman_words.txt') 5 | words = f.read().splitlines() 6 | f.close() 7 | 8 | lives_remaining = 14 9 | guessed_letters = '' 10 | 11 | 12 | 13 | def play(): 14 | word = pick_a_word() 15 | while True: 16 | guess = get_guess(word) 17 | if process_guess(guess, word): 18 | print('You win! Well Done!') 19 | break 20 | if lives_remaining == 0: 21 | print('You are Hung!') 22 | print('The word was: ' + word) 23 | break 24 | 25 | def pick_a_word(): 26 | word_position = random.randint(0, len(words) - 1) 27 | return words[word_position] 28 | 29 | def get_guess(word): 30 | print_word(word) 31 | print('Lives Remaining: ' + str(lives_remaining)) 32 | guess = input(' Guess a letter or whole word?') 33 | return guess 34 | 35 | def print_word(word): 36 | display_word = '' 37 | for letter in word: 38 | if guessed_letters.find(letter) > -1: 39 | display_word = display_word + letter 40 | else: 41 | display_word = display_word + '-' 42 | print(display_word) 43 | 44 | def process_guess(guess, word): 45 | if len(guess) > 1: 46 | return whole_word_guess(guess, word) 47 | else: 48 | return single_letter_guess(guess, word) 49 | 50 | 51 | def whole_word_guess(guess, word): 52 | global lives_remaining 53 | if guess == word: 54 | return True 55 | else: 56 | lives_remaining = lives_remaining - 1 57 | 58 | def single_letter_guess(guess, word): 59 | global guessed_letters 60 | global lives_remaining 61 | if word.find(guess) == -1: 62 | lives_remaining = lives_remaining - 1 63 | guessed_letters = guessed_letters + guess 64 | if all_letters_guessed(word): 65 | return True 66 | 67 | def all_letters_guessed(word): 68 | for letter in word: 69 | if guessed_letters.find(letter) == -1: 70 | return False 71 | return True 72 | 73 | play() -------------------------------------------------------------------------------- /edition_1/06_02_hangman_file_try.py: -------------------------------------------------------------------------------- 1 | #06_02_hangman_file_try 2 | import random 3 | 4 | words_file = 'hangman_words.txt' 5 | try: 6 | f = open(words_file) 7 | words = f.read().splitlines() 8 | f.close() 9 | except IOError: 10 | print("Cannot find file: " + words_file) 11 | exit() 12 | 13 | lives_remaining = 14 14 | guessed_letters = '' 15 | 16 | 17 | 18 | def play(): 19 | word = pick_a_word() 20 | while True: 21 | guess = get_guess(word) 22 | if process_guess(guess, word): 23 | print('You win! Well Done!') 24 | break 25 | if lives_remaining == 0: 26 | print('You are Hung!') 27 | print('The word was: ' + word) 28 | break 29 | 30 | def pick_a_word(): 31 | word_position = random.randint(0, len(words) - 1) 32 | return words[word_position] 33 | 34 | def get_guess(word): 35 | print_word(word) 36 | print('Lives Remaining: ' + str(lives_remaining)) 37 | guess = input(' Guess a letter or whole word?') 38 | return guess 39 | 40 | def print_word(word): 41 | display_word = '' 42 | for letter in word: 43 | if guessed_letters.find(letter) > -1: 44 | display_word = display_word + letter 45 | else: 46 | display_word = display_word + '-' 47 | print(display_word) 48 | 49 | def process_guess(guess, word): 50 | if len(guess) > 1: 51 | return whole_word_guess(guess, word) 52 | else: 53 | return single_letter_guess(guess, word) 54 | 55 | 56 | def whole_word_guess(guess, word): 57 | global lives_remaining 58 | if guess == word: 59 | return True 60 | else: 61 | lives_remaining = lives_remaining - 1 62 | 63 | def single_letter_guess(guess, word): 64 | global guessed_letters 65 | global lives_remaining 66 | if word.find(guess) == -1: 67 | lives_remaining = lives_remaining - 1 68 | guessed_letters = guessed_letters + guess 69 | if all_letters_guessed(word): 70 | return True 71 | 72 | def all_letters_guessed(word): 73 | for letter in word: 74 | if guessed_letters.find(letter) == -1: 75 | return False 76 | return True 77 | 78 | play() -------------------------------------------------------------------------------- /edition_1/06_03_file_readline.py: -------------------------------------------------------------------------------- 1 | #06_03_file_readline 2 | words_file = 'hangman_words.txt' 3 | try: 4 | f = open(words_file) 5 | line = f.readline() 6 | while line != '': 7 | if line == 'elephant\n': 8 | print('There is an elephant in the file') 9 | break 10 | line = f.readline() 11 | f.close() 12 | except IOError: 13 | print("Cannot find file: " + words_file) 14 | -------------------------------------------------------------------------------- /edition_1/06_04_amazon_scraping.py: -------------------------------------------------------------------------------- 1 | #06_04_amazon_scraping 2 | import urllib.request 3 | 4 | u = 'http://www.amazon.com/s/ref=nb_sb_noss?field-keywords=raspberry+pi' 5 | f = urllib.request.urlopen(u) 6 | contents = str(f.read()) 7 | f.close() 8 | i = 0 9 | while True: 10 | i = contents.find('productTitle', i) 11 | if i == -1: 12 | break 13 | # Find the next two '>' after 'productTitle' 14 | i = contents.find('>', i+1) 15 | i = contents.find('>', i+1) 16 | # Find the first '<' after the two '>' 17 | j = contents.find('<', i+1) 18 | title = contents[i+2:j] 19 | print(title) -------------------------------------------------------------------------------- /edition_1/07_01_hello.py: -------------------------------------------------------------------------------- 1 | #07_01_hello.py 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | 7 | Label(root, text='Hello World').pack() 8 | 9 | root.mainloop() 10 | -------------------------------------------------------------------------------- /edition_1/07_02_temp_framework.py: -------------------------------------------------------------------------------- 1 | #07_02_temp_framework.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | frame = Frame(master) 9 | frame.pack() 10 | Label(frame, text='deg C').grid(row=0, column=0) 11 | button = Button(frame, text='Convert', command=self.convert) 12 | button.grid(row=1) 13 | 14 | def convert(self): 15 | print('Not implemented') 16 | 17 | root = Tk() 18 | root.wm_title('Temp Converter') 19 | app = App(root) 20 | root.mainloop() 21 | -------------------------------------------------------------------------------- /edition_1/07_03_temp_ui.py: -------------------------------------------------------------------------------- 1 | #07_03_temp_ui.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | frame = Frame(master) 9 | frame.pack() 10 | Label(frame, text='deg C').grid(row=0, column=0) 11 | self.c_var = DoubleVar() 12 | Entry(frame, textvariable=self.c_var).grid(row=0, column=1) 13 | Label(frame, text='deg F').grid(row=1, column=0) 14 | self.result_var = DoubleVar() 15 | Label(frame, textvariable=self.result_var).grid(row=1, column=1) 16 | button = Button(frame, text='Convert', command=self.convert) 17 | button.grid(row=2, columnspan=2) 18 | 19 | def convert(self): 20 | print('Not implemented') 21 | 22 | root = Tk() 23 | root.wm_title('Temp Converter') 24 | app = App(root) 25 | root.mainloop() 26 | -------------------------------------------------------------------------------- /edition_1/07_04_temp_final.py: -------------------------------------------------------------------------------- 1 | #07_04_temp_final.py 2 | 3 | from tkinter import * 4 | from converters import * 5 | 6 | class App: 7 | 8 | def __init__(self, master): 9 | self.t_conv = ScaleAndOffsetConverter('C', 'F', 1.8, 32) 10 | frame = Frame(master) 11 | frame.pack() 12 | Label(frame, text='deg C').grid(row=0, column=0) 13 | self.c_var = DoubleVar() 14 | Entry(frame, textvariable=self.c_var).grid(row=0, column=1) 15 | Label(frame, text='deg F').grid(row=1, column=0) 16 | self.result_var = DoubleVar() 17 | Label(frame, textvariable=self.result_var).grid(row=1, column=1) 18 | button = Button(frame, text='Convert', command=self.convert) 19 | button.grid(row=2, columnspan=2) 20 | 21 | def convert(self): 22 | c = self.c_var.get() 23 | self.result_var.set(self.t_conv.convert(c)) 24 | 25 | root = Tk() 26 | root.wm_title('Temp Converter') 27 | app = App(root) 28 | root.mainloop() 29 | -------------------------------------------------------------------------------- /edition_1/07_05_kitchen_sink.py: -------------------------------------------------------------------------------- 1 | #07_05_kitchen_sink.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | frame = Frame(master) 9 | frame.pack() 10 | Label(frame, text='Label').grid(row=0, column=0) 11 | Entry(frame, text='Entry').grid(row=0, column=1) 12 | Button(frame, text='Button').grid(row=0, column=2) 13 | check_var = StringVar() 14 | check = Checkbutton(frame, text='Checkbutton', variable=check_var, onvalue='Y', offvalue='N') 15 | check.grid(row=1, column=0) 16 | #Listbox 17 | listbox = Listbox(frame, height=3, selectmode=SINGLE) 18 | for item in ['red', 'green', 'blue', 'yellow', 'pink']: 19 | listbox.insert(END, item) 20 | listbox.grid(row=1, column=1) 21 | #Radiobutton set 22 | radio_frame = Frame(frame) 23 | radio_selection = StringVar() 24 | b1 = Radiobutton(radio_frame, text='portrait', 25 | variable=radio_selection, value='P') 26 | b1.pack(side=LEFT) 27 | b2 = Radiobutton(radio_frame, text='landscape', 28 | variable=radio_selection, value='L') 29 | b2.pack(side=LEFT) 30 | radio_frame.grid(row=1, column=2) 31 | #Scale 32 | scale_var = IntVar() 33 | Scale(frame, from_=1, to=10, orient=HORIZONTAL, 34 | variable=scale_var).grid(row=2, column=0) 35 | Label(frame, textvariable=scale_var, 36 | font=("Helvetica", 36)).grid(row=2, column=1) 37 | #Message 38 | message = Message(frame, 39 | text='Multiline Message Area') 40 | message.grid(row=2, column=2) 41 | #Spinbox 42 | Spinbox(frame, values=('a','b','c')).grid(row=3) 43 | root = Tk() 44 | root.wm_title('Kitchen Sink') 45 | app = App(root) 46 | root.mainloop() 47 | -------------------------------------------------------------------------------- /edition_1/07_06_resizing.py: -------------------------------------------------------------------------------- 1 | #07_06_resizing.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | frame = Frame(master) 9 | frame.pack(fill=BOTH, expand=1) 10 | #Listbox 11 | listbox = Listbox(frame) 12 | for item in ['red', 'green', 'blue', 'yellow', 'pink']: 13 | listbox.insert(END, item) 14 | listbox.grid(row=0, column=0, sticky=W+E+N+S) 15 | 16 | #Message 17 | text = Text(frame, relief=SUNKEN) 18 | text.grid(row=0, column=1, sticky=W+E+N+S) 19 | text.insert(END, 'word ' * 100) 20 | frame.columnconfigure(1, weight=1) 21 | frame.rowconfigure(0, weight=1) 22 | root = Tk() 23 | app = App(root) 24 | root.geometry("400x300+0+0") 25 | root.mainloop() 26 | -------------------------------------------------------------------------------- /edition_1/07_07_scrolling.py: -------------------------------------------------------------------------------- 1 | #07_07_scrolling.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | scrollbar = Scrollbar(master) 9 | scrollbar.pack(side=RIGHT, fill=Y) 10 | text = Text(master, yscrollcommand=scrollbar.set) 11 | text.pack(side=LEFT, fill=BOTH) 12 | text.insert(END, 'word ' * 1000) 13 | scrollbar.config(command=text.yview) 14 | 15 | root = Tk() 16 | root.wm_title('Scrolling') 17 | app = App(root) 18 | root.mainloop() 19 | -------------------------------------------------------------------------------- /edition_1/07_08_dialogs.py: -------------------------------------------------------------------------------- 1 | #07_08_gen_dialogs.py 2 | 3 | from tkinter import * 4 | import tkinter.messagebox as mb 5 | 6 | class App: 7 | 8 | def __init__(self, master): 9 | b=Button(master, text='Press Me', command=self.info).pack() 10 | 11 | def info(self): 12 | mb.showinfo('Information', "Please don't press that button again!") 13 | 14 | root = Tk() 15 | app = App(root) 16 | root.mainloop() 17 | -------------------------------------------------------------------------------- /edition_1/07_09_color_chooser.py: -------------------------------------------------------------------------------- 1 | #07_09_color_chooser.py 2 | 3 | from tkinter import * 4 | import tkinter.colorchooser as cc 5 | 6 | class App: 7 | 8 | def __init__(self, master): 9 | b=Button(master, text='Color..', command=self.ask_color).pack() 10 | 11 | def ask_color(self): 12 | (rgb, hx) = cc.askcolor() 13 | print("rgb=" + str(rgb) + " hx=" + hx) 14 | 15 | root = Tk() 16 | app = App(root) 17 | root.mainloop() 18 | -------------------------------------------------------------------------------- /edition_1/07_10_menus.py: -------------------------------------------------------------------------------- 1 | #07_10_menus.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | self.entry_text = StringVar() 9 | Entry(master, textvariable=self.entry_text).pack() 10 | 11 | menubar = Menu(root) 12 | 13 | filemenu = Menu(menubar, tearoff=0) 14 | filemenu.add_command(label='Quit', command=exit) 15 | menubar.add_cascade(label='File', menu=filemenu) 16 | 17 | editmenu = Menu(menubar, tearoff=0) 18 | editmenu.add_command(label='Fill', command=self.fill) 19 | menubar.add_cascade(label='Edit', menu=editmenu) 20 | 21 | master.config(menu=menubar) 22 | 23 | def fill(self): 24 | self.entry_text.set('abc') 25 | 26 | root = Tk() 27 | app = App(root) 28 | 29 | root.mainloop() 30 | 31 | -------------------------------------------------------------------------------- /edition_1/07_11_canvas.py: -------------------------------------------------------------------------------- 1 | #07_11_canvas.py 2 | 3 | from tkinter import * 4 | 5 | class App: 6 | 7 | def __init__(self, master): 8 | canvas = Canvas(master, width=400, height=200) 9 | canvas.pack() 10 | canvas.create_rectangle(20, 20, 300, 100, fill='blue') 11 | canvas.create_oval(30, 50, 290, 190, fill='#ff2277') 12 | canvas.create_line(0, 0, 400, 200, fill='black', width=5) 13 | root = Tk() 14 | app = App(root) 15 | root.mainloop() 16 | -------------------------------------------------------------------------------- /edition_1/08_01_hello_pygame.py: -------------------------------------------------------------------------------- 1 | #08_01_hello_pygame.py 2 | 3 | import pygame 4 | 5 | pygame.init() 6 | 7 | screen = pygame.display.set_mode((200, 200)) 8 | screen.fill((255, 255, 255)) 9 | pygame.display.set_caption('Hello Pygame') 10 | 11 | raspberry = pygame.image.load('raspberry.jpg').convert() 12 | screen.blit(raspberry, (100, 100)) 13 | 14 | pygame.display.update() 15 | 16 | -------------------------------------------------------------------------------- /edition_1/08_02_rasp_game_mouse.py: -------------------------------------------------------------------------------- 1 | #08_02_rasp_game_mouse 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | 7 | spoon_x = 300 8 | spoon_y = 300 9 | 10 | pygame.init() 11 | 12 | screen = pygame.display.set_mode((600, 400)) 13 | pygame.display.set_caption('Raspberry Catching') 14 | 15 | spoon = pygame.image.load('spoon.jpg').convert() 16 | 17 | while True: 18 | 19 | for event in pygame.event.get(): 20 | if event.type == QUIT: 21 | exit() 22 | 23 | screen.fill((255, 255, 255)) 24 | spoon_x, ignore = pygame.mouse.get_pos() 25 | screen.blit(spoon, (spoon_x, spoon_y)) 26 | 27 | pygame.display.update() 28 | 29 | -------------------------------------------------------------------------------- /edition_1/08_03_rasp_game_one.py: -------------------------------------------------------------------------------- 1 | #08_03_rasp_game_one 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | screen_width = 600 9 | screen_height = 400 10 | 11 | spoon_x = 300 12 | spoon_y = screen_height - 100 13 | 14 | raspberry_x = random.randint(10, screen_width) 15 | raspberry_y = 0 16 | 17 | pygame.init() 18 | 19 | screen = pygame.display.set_mode((screen_width, screen_height)) 20 | pygame.display.set_caption('Raspberry Catching') 21 | 22 | spoon = pygame.image.load('spoon.jpg').convert() 23 | raspberry = pygame.image.load('raspberry.jpg').convert() 24 | 25 | def update_spoon(): 26 | global spoon_x 27 | global spoon_y 28 | spoon_x, ignore = pygame.mouse.get_pos() 29 | screen.blit(spoon, (spoon_x, spoon_y)) 30 | 31 | def update_raspberry(): 32 | global raspberry_x 33 | global raspberry_y 34 | raspberry_y += 5 35 | if raspberry_y > spoon_y: 36 | raspberry_y = 0 37 | raspberry_x = random.randint(10, screen_width) 38 | raspberry_x += random.randint(-5, 5) 39 | if raspberry_x < 10: 40 | raspberry_x = 10 41 | if raspberry_x > screen_width - 20: 42 | raspberry_x = screen_width - 20 43 | screen.blit(raspberry, (raspberry_x, raspberry_y)) 44 | 45 | while True: 46 | for event in pygame.event.get(): 47 | if event.type == QUIT: 48 | exit() 49 | 50 | screen.fill((255, 255, 255)) 51 | update_raspberry() 52 | update_spoon() 53 | pygame.display.update() 54 | 55 | -------------------------------------------------------------------------------- /edition_1/08_04_rasp_game_scoring.py: -------------------------------------------------------------------------------- 1 | #08_04_rasp_game_scoring 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | score = 0 9 | 10 | screen_width = 600 11 | screen_height = 400 12 | 13 | spoon_x = 300 14 | spoon_y = screen_height - 100 15 | 16 | raspberry_x = random.randint(10, screen_width) 17 | raspberry_y = 0 18 | 19 | pygame.init() 20 | 21 | screen = pygame.display.set_mode((screen_width, screen_height)) 22 | pygame.display.set_caption('Raspberry Catching') 23 | 24 | spoon = pygame.image.load('spoon.jpg').convert() 25 | raspberry = pygame.image.load('raspberry.jpg').convert() 26 | 27 | def update_spoon(): 28 | global spoon_x 29 | global spoon_y 30 | spoon_x, ignore = pygame.mouse.get_pos() 31 | screen.blit(spoon, (spoon_x, spoon_y)) 32 | 33 | def update_raspberry(): 34 | global raspberry_x 35 | global raspberry_y 36 | raspberry_y += 5 37 | if raspberry_y > spoon_y: 38 | raspberry_y = 0 39 | raspberry_x = random.randint(10, screen_width) 40 | raspberry_x += random.randint(-5, 5) 41 | if raspberry_x < 10: 42 | raspberry_x = 10 43 | if raspberry_x > screen_width - 20: 44 | raspberry_x = screen_width - 20 45 | screen.blit(raspberry, (raspberry_x, raspberry_y)) 46 | 47 | def check_for_catch(): 48 | global score 49 | if raspberry_y >= spoon_y and raspberry_x >= spoon_x and \ 50 | raspberry_x < spoon_x + 50: 51 | score += 1 52 | display("Score: " + str(score)) 53 | 54 | def display(message): 55 | font = pygame.font.Font(None, 36) 56 | text = font.render(message, 1, (10, 10, 10)) 57 | screen.blit(text, (0, 0)) 58 | 59 | while True: 60 | 61 | for event in pygame.event.get(): 62 | if event.type == QUIT: 63 | exit() 64 | 65 | screen.fill((255, 255, 255)) 66 | update_raspberry() 67 | update_spoon() 68 | check_for_catch() 69 | pygame.display.update() 70 | 71 | 72 | -------------------------------------------------------------------------------- /edition_1/08_05_rasp_game_refactored.py: -------------------------------------------------------------------------------- 1 | #08_ 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | score = 0 9 | 10 | screen_width = 600 11 | screen_height = 400 12 | 13 | spoon_x = 300 14 | spoon_y = screen_height - 100 15 | 16 | class Raspberry: 17 | x = 0 18 | y = 0 19 | 20 | def __init__(self): 21 | self.x = random.randint(10, screen_width) 22 | self.y = 0 23 | 24 | def update(self): 25 | self.y += 5 26 | if self.y > spoon_y: 27 | self.y = 0 28 | self.x = random.randint(10, screen_width) 29 | self.x += random.randint(-5, 5) 30 | if self.x < 10: 31 | self.x = 10 32 | if self.x > screen_width - 20: 33 | self.x = screen_width - 20 34 | screen.blit(raspberry_image, (self.x, self.y)) 35 | 36 | def is_caught(self): 37 | return self.y >= spoon_y and self.x >= spoon_x and self.x < spoon_x + 50 38 | 39 | clock = pygame.time.Clock() 40 | r = Raspberry() 41 | 42 | pygame.init() 43 | 44 | screen = pygame.display.set_mode((screen_width, screen_height)) 45 | pygame.display.set_caption('Raspberry Catching') 46 | 47 | spoon = pygame.image.load('spoon.jpg').convert() 48 | raspberry_image = pygame.image.load('raspberry.jpg').convert() 49 | 50 | def update_spoon(): 51 | global spoon_x 52 | global spoon_y 53 | spoon_x, ignore = pygame.mouse.get_pos() 54 | screen.blit(spoon, (spoon_x, spoon_y)) 55 | 56 | def check_for_catch(): 57 | global score 58 | if r.is_caught(): 59 | score += 1 60 | 61 | def display(message): 62 | font = pygame.font.Font(None, 36) 63 | text = font.render(message, 1, (10, 10, 10)) 64 | screen.blit(text, (0, 0)) 65 | 66 | while True: 67 | 68 | for event in pygame.event.get(): 69 | if event.type == QUIT: 70 | exit() 71 | 72 | screen.fill((255, 255, 255)) 73 | r.update() 74 | update_spoon() 75 | check_for_catch() 76 | display("Score: " + str(score)) 77 | pygame.display.update() 78 | clock.tick(30) 79 | 80 | 81 | -------------------------------------------------------------------------------- /edition_1/08_06_rasp_game_final.py: -------------------------------------------------------------------------------- 1 | #08_06_rasp_game_final 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | score = 0 9 | 10 | screen_width = 600 11 | screen_height = 400 12 | 13 | spoon_x = 300 14 | spoon_y = screen_height - 100 15 | 16 | class Raspberry: 17 | x = 0 18 | y = 0 19 | dy = 0 20 | 21 | def __init__(self): 22 | self.x = random.randint(10, screen_width) 23 | self.y = 0 24 | self.dy = random.randint(3, 10) 25 | 26 | def update(self): 27 | self.y += self.dy 28 | if self.y > spoon_y: 29 | self.y = 0 30 | self.x = random.randint(10, screen_width) 31 | self.x += random.randint(-5, 5) 32 | if self.x < 10: 33 | self.x = 10 34 | if self.x > screen_width - 20: 35 | self.x = screen_width - 20 36 | screen.blit(raspberry_image, (self.x, self.y)) 37 | 38 | def is_caught(self): 39 | return self.y >= spoon_y and self.x >= spoon_x and self.x < spoon_x + 50 40 | 41 | clock = pygame.time.Clock() 42 | rasps = [Raspberry(), Raspberry(), Raspberry()] 43 | 44 | pygame.init() 45 | 46 | screen = pygame.display.set_mode((screen_width, screen_height)) 47 | pygame.display.set_caption('Raspberry Catching') 48 | 49 | spoon = pygame.image.load('spoon.jpg').convert() 50 | raspberry_image = pygame.image.load('raspberry.jpg').convert() 51 | 52 | def update_spoon(): 53 | global spoon_x 54 | global spoon_y 55 | spoon_x, ignore = pygame.mouse.get_pos() 56 | screen.blit(spoon, (spoon_x, spoon_y)) 57 | 58 | def check_for_catch(): 59 | global score 60 | for r in rasps: 61 | if r.is_caught(): 62 | score += 1 63 | 64 | def display(message): 65 | font = pygame.font.Font(None, 36) 66 | text = font.render(message, 1, (10, 10, 10)) 67 | screen.blit(text, (0, 0)) 68 | 69 | while True: 70 | for event in pygame.event.get(): 71 | if event.type == QUIT: 72 | exit() 73 | 74 | screen.fill((255, 255, 255)) 75 | for r in rasps: 76 | r.update() 77 | update_spoon() 78 | check_for_catch() 79 | display("Score: " + str(score)) 80 | pygame.display.update() 81 | clock.tick(30) 82 | 83 | 84 | -------------------------------------------------------------------------------- /edition_1/08_pygame.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | 4 | pygame.init() 5 | screen = pygame.display.set_mode((256, 256)) 6 | pygame.display.set_caption('Caption') 7 | 8 | ball_tile = pygame.image.load('ball.jpg').convert() 9 | 10 | graph_rect = ball_tile.get_rect() 11 | 12 | rows = int(256 / graph_rect.height) + 1 13 | columns = int(256 / graph_rect.width) + 1 14 | 15 | for y in xrange(rows): 16 | for x in xrange(columns): 17 | if x == 0 and y > 0: 18 | graph_rect = graph_rect.move([-(columns -1) * graph_rect.width, graph_rect.height]) 19 | if x > 0: 20 | graph_rect = graph_rect.move([graph_rect.width, 0]) 21 | screen.blit(ball_tile, graph_rect) 22 | 23 | pygame.display.update() 24 | 25 | while True: 26 | for event in pygame.event.get(): 27 | if event.type == pygame.QUIT: 28 | sys.exit(); 29 | -------------------------------------------------------------------------------- /edition_1/08_raspberry_bounce_1.py: -------------------------------------------------------------------------------- 1 | #08_02_rasp_game_mouse 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | 7 | spoon_x = 300 8 | spoon_y = 300 9 | 10 | pygame.init() 11 | 12 | screen = pygame.display.set_mode((600, 400)) 13 | pygame.display.set_caption('Raspberry Catching') 14 | 15 | spoon = pygame.image.load('spoon.jpg').convert() 16 | 17 | while True: 18 | 19 | for event in pygame.event.get(): 20 | if event.type == QUIT: 21 | exit() 22 | 23 | screen.fill((255, 255, 255)) 24 | spoon_x, ignore = pygame.mouse.get_pos() 25 | screen.blit(spoon, (spoon_x, spoon_y)) 26 | 27 | pygame.display.update() 28 | 29 | -------------------------------------------------------------------------------- /edition_1/08_raspberry_bounce_2.py: -------------------------------------------------------------------------------- 1 | #08_ 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | screen_width = 600 9 | screen_height = 400 10 | 11 | spoon_x = 300 12 | spoon_y = screen_height - 100 13 | 14 | raspberry_x = random.randint(10, screen_width) 15 | raspberry_y = 0 16 | 17 | pygame.init() 18 | 19 | screen = pygame.display.set_mode((screen_width, screen_height)) 20 | pygame.display.set_caption('Raspberry Catching') 21 | 22 | spoon = pygame.image.load('spoon.jpg').convert() 23 | raspberry = pygame.image.load('raspberry.jpg').convert() 24 | 25 | def update_spoon(): 26 | global spoon_x 27 | global spoon_y 28 | spoon_x, ignore = pygame.mouse.get_pos() 29 | screen.blit(spoon, (spoon_x, spoon_y)) 30 | 31 | def update_raspberry(): 32 | global raspberry_x 33 | global raspberry_y 34 | raspberry_y += 5 35 | if raspberry_y > spoon_y: 36 | raspberry_y = 0 37 | raspberry_x = random.randint(10, screen_width) 38 | raspberry_x += random.randint(-5, 5) 39 | if raspberry_x < 10: 40 | raspberry_x = 10 41 | if raspberry_x > screen_width - 20: 42 | raspberry_x = screen_width - 20 43 | screen.blit(raspberry, (raspberry_x, raspberry_y)) 44 | 45 | while True: 46 | 47 | for event in pygame.event.get(): 48 | if event.type == QUIT: 49 | exit() 50 | 51 | screen.fill((255, 255, 255)) 52 | update_raspberry() 53 | update_spoon() 54 | pygame.display.update() 55 | 56 | 57 | -------------------------------------------------------------------------------- /edition_1/08_raspberry_bounce_3.py: -------------------------------------------------------------------------------- 1 | #08_ 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | score = 0 9 | 10 | screen_width = 600 11 | screen_height = 400 12 | 13 | spoon_x = 300 14 | spoon_y = screen_height - 100 15 | 16 | raspberry_x = random.randint(10, screen_width) 17 | raspberry_y = 0 18 | 19 | clock = pygame.time.Clock() 20 | 21 | pygame.init() 22 | 23 | screen = pygame.display.set_mode((screen_width, screen_height)) 24 | pygame.display.set_caption('Raspberry Catching') 25 | 26 | spoon = pygame.image.load('spoon.jpg').convert() 27 | raspberry = pygame.image.load('raspberry.jpg').convert() 28 | 29 | def update_spoon(): 30 | global spoon_x 31 | global spoon_y 32 | spoon_x, ignore = pygame.mouse.get_pos() 33 | screen.blit(spoon, (spoon_x, spoon_y)) 34 | 35 | def update_raspberry(): 36 | global raspberry_x 37 | global raspberry_y 38 | raspberry_y += 5 39 | if raspberry_y > spoon_y: 40 | raspberry_y = 0 41 | raspberry_x = random.randint(10, screen_width) 42 | raspberry_x += random.randint(-5, 5) 43 | if raspberry_x < 10: 44 | raspberry_x = 10 45 | if raspberry_x > screen_width - 20: 46 | raspberry_x = screen_width - 20 47 | screen.blit(raspberry, (raspberry_x, raspberry_y)) 48 | 49 | def check_for_catch(): 50 | global score 51 | if raspberry_y >= spoon_y and raspberry_x >= spoon_x and raspberry_x < spoon_x + 50: 52 | score += 1 53 | display("Score: " + str(score)) 54 | 55 | def display(message): 56 | font = pygame.font.Font(None, 36) 57 | text = font.render(message, 1, (10, 10, 10)) 58 | screen.blit(text, (0, 0)) 59 | 60 | while True: 61 | 62 | for event in pygame.event.get(): 63 | if event.type == QUIT: 64 | exit() 65 | 66 | screen.fill((255, 255, 255)) 67 | update_raspberry() 68 | update_spoon() 69 | check_for_catch() 70 | pygame.display.update() 71 | clock.tick(30) 72 | 73 | 74 | -------------------------------------------------------------------------------- /edition_1/08_raspberry_bounce_4.py: -------------------------------------------------------------------------------- 1 | #08_ 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | score = 0 9 | 10 | screen_width = 600 11 | screen_height = 400 12 | 13 | spoon_x = 300 14 | spoon_y = screen_height - 100 15 | 16 | class Raspberry: 17 | x = 0 18 | y = 0 19 | 20 | def __init__(self): 21 | self.x = random.randint(10, screen_width) 22 | self.y = 0 23 | 24 | def update(self): 25 | self.y += 5 26 | if self.y > spoon_y: 27 | self.y = 0 28 | self.x = random.randint(10, screen_width) 29 | self.x += random.randint(-5, 5) 30 | if self.x < 10: 31 | self.x = 10 32 | if self.x > screen_width - 20: 33 | self.x = screen_width - 20 34 | screen.blit(raspberry_image, (self.x, self.y)) 35 | 36 | def is_caught(self): 37 | return self.y >= spoon_y and self.x >= spoon_x and self.x < spoon_x + 50 38 | 39 | clock = pygame.time.Clock() 40 | r = Raspberry() 41 | 42 | pygame.init() 43 | 44 | screen = pygame.display.set_mode((screen_width, screen_height)) 45 | pygame.display.set_caption('Raspberry Catching') 46 | 47 | spoon = pygame.image.load('spoon.jpg').convert() 48 | raspberry_image = pygame.image.load('raspberry.jpg').convert() 49 | 50 | def update_spoon(): 51 | global spoon_x 52 | global spoon_y 53 | spoon_x, ignore = pygame.mouse.get_pos() 54 | screen.blit(spoon, (spoon_x, spoon_y)) 55 | 56 | def check_for_catch(): 57 | global score 58 | if r.is_caught(): 59 | score += 1 60 | 61 | def display(message): 62 | font = pygame.font.Font(None, 36) 63 | text = font.render(message, 1, (10, 10, 10)) 64 | screen.blit(text, (0, 0)) 65 | 66 | while True: 67 | 68 | for event in pygame.event.get(): 69 | if event.type == QUIT: 70 | exit() 71 | 72 | screen.fill((255, 255, 255)) 73 | r.update() 74 | update_spoon() 75 | check_for_catch() 76 | display("Score: " + str(score)) 77 | pygame.display.update() 78 | clock.tick(30) 79 | 80 | 81 | -------------------------------------------------------------------------------- /edition_1/08_raspberry_bounce_5.py: -------------------------------------------------------------------------------- 1 | #08_ 2 | 3 | import pygame 4 | from pygame.locals import * 5 | from sys import exit 6 | import random 7 | 8 | score = 0 9 | 10 | screen_width = 600 11 | screen_height = 400 12 | 13 | spoon_x = 300 14 | spoon_y = screen_height - 100 15 | 16 | class Raspberry: 17 | x = 0 18 | y = 0 19 | dy = 0 20 | 21 | def __init__(self): 22 | self.x = random.randint(10, screen_width) 23 | self.y = 0 24 | self.dy = random.randint(3, 10) 25 | 26 | def update(self): 27 | self.y += self.dy 28 | if self.y > spoon_y: 29 | self.y = 0 30 | self.x = random.randint(10, screen_width) 31 | self.x += random.randint(-5, 5) 32 | if self.x < 10: 33 | self.x = 10 34 | if self.x > screen_width - 20: 35 | self.x = screen_width - 20 36 | screen.blit(raspberry_image, (self.x, self.y)) 37 | 38 | def is_caught(self): 39 | return self.y >= spoon_y and self.x >= spoon_x and self.x < spoon_x + 50 40 | 41 | clock = pygame.time.Clock() 42 | rasps = [Raspberry(), Raspberry(), Raspberry()] 43 | 44 | pygame.init() 45 | 46 | screen = pygame.display.set_mode((screen_width, screen_height)) 47 | pygame.display.set_caption('Raspberry Catching') 48 | 49 | spoon = pygame.image.load('spoon.jpg').convert() 50 | raspberry_image = pygame.image.load('raspberry.jpg').convert() 51 | 52 | def update_spoon(): 53 | global spoon_x 54 | global spoon_y 55 | spoon_x, ignore = pygame.mouse.get_pos() 56 | screen.blit(spoon, (spoon_x, spoon_y)) 57 | 58 | def check_for_catch(): 59 | global score 60 | for r in rasps: 61 | if r.is_caught(): 62 | score += 1 63 | 64 | def display(message): 65 | font = pygame.font.Font(None, 36) 66 | text = font.render(message, 1, (10, 10, 10)) 67 | screen.blit(text, (0, 0)) 68 | 69 | while True: 70 | 71 | for event in pygame.event.get(): 72 | if event.type == QUIT: 73 | exit() 74 | 75 | screen.fill((255, 255, 255)) 76 | for r in rasps: 77 | r.update() 78 | update_spoon() 79 | check_for_catch() 80 | display("Score: " + str(score)) 81 | pygame.display.update() 82 | clock.tick(30) 83 | 84 | 85 | -------------------------------------------------------------------------------- /edition_1/10_01_clock.py: -------------------------------------------------------------------------------- 1 | import i2c7segment as display 2 | import time 3 | 4 | disp = display.Adafruit7Segment() 5 | 6 | while True: 7 | h = time.localtime().tm_hour 8 | m = time.localtime().tm_min 9 | disp.print_int(h * 100 + m) 10 | disp.draw_colon(True) 11 | disp.write_display() 12 | time.sleep(0.5) 13 | disp.draw_colon(False) 14 | disp.write_display() 15 | time.sleep(0.5) 16 | -------------------------------------------------------------------------------- /edition_1/10_02_fancy_clock.py: -------------------------------------------------------------------------------- 1 | import i2c7segment as display 2 | import time 3 | import RPi.GPIO as io 4 | 5 | switch_pin = 17 6 | io.setmode(io.BCM) 7 | io.setup(switch_pin, io.IN, pull_up_down=io.PUD_UP) 8 | disp = display.Adafruit7Segment() 9 | 10 | time_mode, seconds_mode, date_mode = range(3) 11 | disp_mode = time_mode 12 | 13 | def display_time(): 14 | h = time.localtime().tm_hour 15 | m = time.localtime().tm_min 16 | disp.print_int(h * 100 + m) 17 | disp.draw_colon(True) 18 | disp.write_display() 19 | time.sleep(0.5) 20 | disp.draw_colon(False) 21 | disp.write_display() 22 | time.sleep(0.5) 23 | 24 | def disply_date(): 25 | d = time.localtime().tm_mday 26 | m = time.localtime().tm_mon 27 | #disp.print_int(d * 100 + m) # World format 28 | disp.print_int(m * 100 + d) # US format 29 | disp.draw_colon(True) 30 | disp.write_display() 31 | time.sleep(0.5) 32 | 33 | def display_seconds(): 34 | s = time.localtime().tm_sec 35 | disp.print_str('----') 36 | disp.print_int(s) 37 | disp.draw_colon(True) 38 | disp.write_display() 39 | time.sleep(0.5) 40 | 41 | while True: 42 | key_pressed = not io.input(switch_pin) 43 | if key_pressed: 44 | disp_mode = disp_mode + 1 45 | if disp_mode > date_mode: 46 | disp_mode = time_mode 47 | if disp_mode == time_mode: 48 | display_time() 49 | elif disp_mode == seconds_mode: 50 | display_seconds() 51 | elif disp_mode == date_mode: 52 | disply_date() 53 | 54 | -------------------------------------------------------------------------------- /edition_1/11_01_rover_basic.py: -------------------------------------------------------------------------------- 1 | from raspirobotboard import * 2 | import pygame 3 | import sys 4 | from pygame.locals import * 5 | 6 | rr = RaspiRobot() 7 | 8 | pygame.init() 9 | screen = pygame.display.set_mode((640, 480)) 10 | 11 | pygame.display.set_caption('RaspiRobot') 12 | pygame.mouse.set_visible(0) 13 | 14 | 15 | while True: 16 | for event in pygame.event.get(): 17 | if event.type == QUIT: 18 | sys.exit() 19 | if event.type == KEYDOWN: 20 | if event.key == K_UP: 21 | rr.forward() 22 | rr.set_led1(True) 23 | rr.set_led2(True) 24 | elif event.key == K_DOWN: 25 | rr.set_led1(True) 26 | rr.set_led2(True) 27 | rr.reverse() 28 | elif event.key == K_RIGHT: 29 | rr.set_led1(False) 30 | rr.set_led2(True) 31 | rr.right() 32 | elif event.key == K_LEFT: 33 | rr.set_led1(True) 34 | rr.set_led2(False) 35 | rr.left() 36 | elif event.key == K_SPACE: 37 | rr.stop() 38 | rr.set_led1(False) 39 | rr.set_led2(False) 40 | -------------------------------------------------------------------------------- /edition_1/11_02_rover_plus.py: -------------------------------------------------------------------------------- 1 | from raspirobotboard import * 2 | import pygame 3 | import sys 4 | from pygame.locals import * 5 | 6 | rr = RaspiRobot() 7 | 8 | pygame.init() 9 | screen = pygame.display.set_mode((640, 480)) 10 | font = pygame.font.SysFont("arial", 64) 11 | 12 | pygame.display.set_caption('RaspiRobot') 13 | pygame.mouse.set_visible(0) 14 | 15 | dir_stop, dir_forward, dir_back, dir_left, dir_right = range(5) 16 | bot_direction = dir_stop 17 | 18 | 19 | def update_distance(): 20 | dist = get_range() 21 | if dist == 0: 22 | return 23 | message = 'Distance: ' + str(dist) + ' in' 24 | text_surface = font.render(message, True, (127, 127, 127)) 25 | screen.fill((255, 255, 255)) 26 | screen.blit(text_surface, (100, 100)) 27 | 28 | w = screen.get_width() - 20 29 | proximity = ((100 - dist) / 100.0) * w 30 | if proximity < 0: 31 | proximity = 0 32 | pygame.draw.rect(screen, (0, 255, 0), Rect((10, 10),(w, 50))) 33 | pygame.draw.rect(screen, (255, 0, 0), Rect((10, 10),(proximity, 50))) 34 | pygame.display.update() 35 | 36 | def get_range(): 37 | try: 38 | dist = rr.get_range_inch() 39 | except: 40 | dist = 0 41 | return dist 42 | 43 | def collision_check(): 44 | dist = get_range() 45 | if dist > 0 and dist < 10 and bot_direction == dir_forward: 46 | go_stop() 47 | 48 | def go_stop(): 49 | global bot_direction 50 | bot_direction = dir_stop 51 | rr.stop() 52 | rr.set_led1(False) 53 | rr.set_led2(False) 54 | 55 | def go_forward(): 56 | global bot_direction 57 | bot_direction = dir_forward 58 | rr.forward() 59 | rr.set_led1(True) 60 | rr.set_led2(True) 61 | 62 | def go_back(): 63 | global bot_direction 64 | bot_direction = dir_back 65 | rr.set_led1(True) 66 | rr.set_led2(True) 67 | rr.reverse() 68 | 69 | def go_right(): 70 | global bot_direction 71 | bot_direction = dir_right 72 | rr.set_led1(False) 73 | rr.set_led2(True) 74 | rr.right() 75 | 76 | def go_left(): 77 | global bot_direction 78 | bot_direction = dir_left 79 | rr.set_led1(True) 80 | rr.set_led2(False) 81 | rr.left() 82 | 83 | while True: 84 | for event in pygame.event.get(): 85 | if event.type == QUIT: 86 | sys.exit() 87 | if event.type == KEYDOWN: 88 | if event.key == K_UP: 89 | go_forward() 90 | elif event.key == K_DOWN: 91 | go_back() 92 | elif event.key == K_RIGHT: 93 | go_right() 94 | elif event.key == K_LEFT: 95 | go_left() 96 | elif event.key == K_SPACE: 97 | go_stop() 98 | collision_check() 99 | update_distance() 100 | time.sleep(0.1) 101 | -------------------------------------------------------------------------------- /edition_1/PiTest/PiTest.ino: -------------------------------------------------------------------------------- 1 | // Pi and Arduino 2 | 3 | const int ledPin = 13; 4 | 5 | void setup() 6 | { 7 | pinMode(ledPin, OUTPUT); 8 | Serial.begin(9600); 9 | } 10 | 11 | void loop() 12 | { 13 | Serial.println("Hello Pi"); 14 | if (Serial.available()) 15 | { 16 | flash(Serial.read() - '0'); 17 | } 18 | delay(1000); 19 | } 20 | 21 | void flash(int n) 22 | { 23 | for (int i = 0; i < n; i++) 24 | { 25 | digitalWrite(ledPin, HIGH); 26 | delay(100); 27 | digitalWrite(ledPin, LOW); 28 | delay(100); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /edition_1/converters.py: -------------------------------------------------------------------------------- 1 | #05_03_converters_final 2 | class ScaleConverter: 3 | 4 | def __init__(self, units_from, units_to, factor): 5 | self.units_from = units_from 6 | self.units_to = units_to 7 | self.factor = factor 8 | 9 | def description(self): 10 | return 'Convert ' + self.units_from + ' to ' + self.units_to 11 | 12 | def convert(self, value): 13 | return value * self.factor 14 | 15 | class ScaleAndOffsetConverter(ScaleConverter): 16 | 17 | def __init__(self, units_from, units_to, factor, offset): 18 | ScaleConverter.__init__(self, units_from, units_to, factor) 19 | self.offset = offset 20 | 21 | def convert(self, value): 22 | return value * self.factor + self.offset 23 | -------------------------------------------------------------------------------- /edition_1/hangman_words.txt: -------------------------------------------------------------------------------- 1 | elephant 2 | cat 3 | tiger 4 | dog 5 | lion 6 | horse 7 | giraffe 8 | bird 9 | deer 10 | -------------------------------------------------------------------------------- /edition_1/mylist.pickle: -------------------------------------------------------------------------------- 1 | (lp0 2 | S'a' 3 | p1 4 | aI123 5 | a(lp2 6 | I4 7 | aI5 8 | aI01 9 | aa. -------------------------------------------------------------------------------- /edition_1/raspberry.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonmonk/monk_raspberrypi/a672026316fa86c2f37e99a9caa8b075287428ab/edition_1/raspberry.jpg -------------------------------------------------------------------------------- /edition_1/raspirobot_basic.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Encoding=UTF-8 3 | Type=Application 4 | Name=RaspiRobotBasic 5 | Comment= 6 | Exec=sudo python /home/pi/Python/code/11_01_rover_basic.py 7 | StartupNotify=false 8 | Terminal=false 9 | Hidden=false 10 | -------------------------------------------------------------------------------- /edition_1/raspirobot_plus.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Encoding=UTF-8 3 | Type=Application 4 | Name=RaspiRobotBasic 5 | Comment= 6 | Exec=sudo python /home/pi/Python/code/11_02_rover_plus.py 7 | StartupNotify=false 8 | Terminal=false 9 | Hidden=false 10 | -------------------------------------------------------------------------------- /edition_1/spoon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonmonk/monk_raspberrypi/a672026316fa86c2f37e99a9caa8b075287428ab/edition_1/spoon.jpg -------------------------------------------------------------------------------- /edition_1/test.txt: -------------------------------------------------------------------------------- 1 | This file is nologer empty -------------------------------------------------------------------------------- /edition_1/test_dup.txt: -------------------------------------------------------------------------------- 1 | This file is nologer empty --------------------------------------------------------------------------------