├── AdventureGame.py ├── Art.py ├── Chatbot.py ├── Dicegame.py ├── GUIGame.py ├── README.md └── images ├── ImageInformation.txt ├── greenChar.gif ├── spider.gif └── stickfigure.gif /AdventureGame.py: -------------------------------------------------------------------------------- 1 | # adventure game 2 | print('Welcome to the Santa Cruz Mountain Adventure Game!') 3 | print('*************************************************') 4 | print('You are visiting Santa Cruz, California.') 5 | print('You go on an evening hike alone in the mountains.') 6 | print('You can pick one item to take with you - ') 7 | print('map (m), flashlight(f), chocolate (c), rope(r), or stick (s): ') 8 | item = input('What do you choose?: ') 9 | print('You hear a humming sound.') 10 | choice1 = input('Do you follow the sound? Enter y or n: ') 11 | if choice1 == 'y': 12 | print('You keep moving closer to the sound.') 13 | print('The sound suddenly stops.') 14 | print('You are now LOST! ... ') 15 | print('You try to call on your phone, but there is no signal!') 16 | direction = input('Which direction do you go? north, south, east, or west: ') 17 | if direction == 'north': 18 | print('You reach an abandoned cabin.') 19 | if item == 'm': 20 | print('You use the map and find your way home.') 21 | print('CONGRATULATIONS! You won the game. ') 22 | else: 23 | print('If you had a map, you could find your way from here.') 24 | print('---You are still lost. You lost the game.---') 25 | elif direction == 'south': 26 | print('You reach a river with a broken bridge.') 27 | if item == 'r' or item == 's': 28 | print('You chose an item that can fix the bridge.') 29 | print('You fix the bridge, cross over, and find your way home') 30 | print('CONGRATULATIONS! You won the game.') 31 | else: 32 | print('If you had a rope or a stick, you could fix the bridge.') 33 | print('---You are still lost. You lost the game.---') 34 | elif direction == 'west': 35 | print('You are walking and trip over a fallen log.') 36 | print('You have hurt your foot. You sit down and wait for help.') 37 | print('This could be a long time. You are still lost.') 38 | print('---You lost the game.---') 39 | else: 40 | print('You reach the side of the highway. It is dark.') 41 | if item == 'f': 42 | print('You use the flashlight to signal.') 43 | print('A car stops and gives you a ride home.') 44 | print('CONGRATULATIONS! You got out safely. You won the game.') 45 | else: 46 | print('If you had a flashlight, you could signal for help.') 47 | print('---You are still lost. You lost the game.--') 48 | else: 49 | print('Good idea. You are not taking risks. ') 50 | print('You start walking back to the starting point.') 51 | print('You realize you are LOST! ') 52 | print('The sound is behind you and is getting louder. You panic! ') 53 | action = input('Do you start running (r), stop to make a call (c)?: ') 54 | while action == 'c': 55 | print('The call does not go through') 56 | action = input('Do you want to run (r), or try calling again (c)?: ') 57 | print('You are running fast. The sound gets really loud') 58 | 59 | print('A woman on an electric scooter comes up behind you.') 60 | answer = input('She says, "Name my favorite computer programming language.": ') 61 | if answer.lower() == 'python': 62 | print('She says, "Yes, Python is my favorite programming language."') 63 | print('"If you have some chocolate, I can help you."') 64 | if item == 'c': 65 | print('Luckily you did choose correctly!') 66 | print('You give her the chocolate.') 67 | print('She helps you get home.') 68 | print('CONGRATULATIONS! You got out safely. You won the game.') 69 | else: 70 | print('You should have chosen that chocolate!') 71 | print('She rides away, leaving you alone and lost.') 72 | print('You lost the game.') 73 | else: 74 | print('She did not like your answer.') 75 | print('She rides away, leaving you lost!') 76 | print('You lost the game.') 77 | -------------------------------------------------------------------------------- /Art.py: -------------------------------------------------------------------------------- 1 | # make a geometric rainbow pattern 2 | import turtle 3 | # pick order of colors for the hexagon 4 | colors = ['red', 'yellow', 'blue', 'orange', \ 5 | 'green', 'red'] 6 | shelly = turtle.Turtle() 7 | turtle.bgcolor('black') # turn background black 8 | # make 36 hexagons, each 10 degrees apart 9 | for n in range(36): 10 | # make hexagon by repeating 6 times 11 | for i in range(6): 12 | shelly.color(colors[i]) # pick color at position i 13 | shelly.forward(100) 14 | shelly.left(60) 15 | # add a turn before the next hexagon 16 | shelly.right(10) 17 | 18 | # get ready to draw 36 circles 19 | shelly.penup() 20 | shelly.color('white') 21 | # repeat 36 times to match the 36 hexagons 22 | for i in range(36): 23 | shelly.forward(220) 24 | shelly.pendown() 25 | shelly.circle(5) 26 | shelly.penup() 27 | shelly.backward(220) 28 | shelly.right(10) 29 | # hide turtle to finish the drawing 30 | shelly.hideturtle() 31 | -------------------------------------------------------------------------------- /Chatbot.py: -------------------------------------------------------------------------------- 1 | # chatbot introduction 2 | print('Hello. I am Zyxo 64. I am a chatbot') 3 | print('I like animals and I love to talk about food') 4 | name = input('What is your name?: ') 5 | print('Hello', name, ', Nice to meet you') 6 | 7 | # get year information 8 | year = input('I am not very good at dates. What \ 9 | is the year?: ') 10 | print('Yes, I think that is correct. Thanks! ') 11 | # ask user to guess age 12 | myage = input('Can you guess my age? - enter a \ 13 | number: ') 14 | print('Yes you are right. I am ', myage) 15 | # do math to calculate when chatbot will be 100 16 | myage = int(myage) 17 | nyears = 100 - myage 18 | print('I will be 100 in', nyears, 'years') 19 | print('That will be the year', int(year) + \ 20 | nyears) 21 | 22 | # food conversation 23 | print('I love chocolate and I also like trying out new kinds of food') 24 | food = input('How about you? What is your favorite food?: ') 25 | print('I like', food, 'too.') 26 | question = 'How often do you eat ' + food + '?: ' 27 | howoften = input(question) 28 | print('Interesting. I wonder if that is good for your health') 29 | # animal conversation 30 | animal = input('My favorite animal is a giraffe. What is yours?: ') 31 | print(animal,'! I do not like them.') 32 | print('I wonder if a', animal, 'likes to eat', food, '?') 33 | 34 | # conversation about feelings 35 | feeling = input('How are you feeling today?: ') 36 | print('Why are you feeling', feeling, 'now?') 37 | reason = input('Please tell me: ') 38 | print('I understand. Thanks for sharing') 39 | 40 | 41 | # goodbye 42 | print('It has been a long day') 43 | print('I am too tired to talk. We can chat again later.') 44 | print('Goodbye', name, 'I liked chatting with you') 45 | -------------------------------------------------------------------------------- /Dicegame.py: -------------------------------------------------------------------------------- 1 | # dice game 2 | 3 | 4 | 5 | import random 6 | 7 | import time 8 | 9 | def roll_dice(n): 10 | dice = [] # start with empty list of dice 11 | # add random numbers between 1 to 6 to the list 12 | for i in range(n): 13 | dice.append(random.randint(1,6)) 14 | return dice 15 | 16 | def find_winner(cdice_list, udice_list): 17 | computer_total = sum(cdice_list) 18 | user_total = sum(udice_list) 19 | print('Computer total', computer_total) 20 | print('User total',user_total ) 21 | if user_total > computer_total: 22 | print('User wins') 23 | elif user_total < computer_total: 24 | print('Computer wins') 25 | else: 26 | print('It is a tie!') 27 | 28 | def roll_again(choices, dice_list): 29 | print('Rolling again ...') 30 | time.sleep(3) 31 | for i in range(len(choices)): 32 | if choices[i] == 'r': 33 | dice_list[i] = random.randint(1,6) 34 | time.sleep(3) 35 | 36 | def computer_strategy1(n): 37 | # create computer choices : roll everything again 38 | print('Computer is thinking ...') 39 | time.sleep(3) 40 | choices = '' # start with an empty list of choices 41 | for i in range(n): 42 | choices = choices + 'r' 43 | return choices 44 | 45 | def computer_strategy2(n): 46 | # create computer choices: roll if < 5 47 | print('Computer is thinking ...') 48 | time.sleep(3) 49 | choices = '' # start with an empty list of choices 50 | for i in range(n): 51 | if computer_rolls[i] < 5: 52 | choices = choices + 'r' 53 | else: 54 | choices = choices + '-' 55 | return choices 56 | 57 | 58 | # step1 in main program area - start game 59 | number_dice = input('Enter number of dice:') 60 | number_dice = int(number_dice) 61 | ready = input('Ready to start? Hit any key to continue') 62 | 63 | 64 | # step 2 in main program area - roll dice 65 | # User turn to roll 66 | user_rolls = roll_dice(number_dice) 67 | print('User first roll: ', user_rolls) 68 | 69 | # step 4 - get user choices 70 | user_choices = input("Enter - to hold or r to \ 71 | roll again :") 72 | # check length of user input 73 | while len(user_choices) != number_dice: 74 | print('You must enter', number_dice, \ 75 | 'choices') 76 | user_choices = input("Enter - to hold or r \ 77 | to roll again :") 78 | 79 | # step 5 - roll again based on user choices 80 | roll_again(user_choices, user_rolls) 81 | print('Player new Roll: ', user_rolls) 82 | 83 | # Computer's turn to roll 84 | print('Computers turn ') 85 | computer_rolls = roll_dice(number_dice) 86 | print('Computer first roll: ', computer_rolls) 87 | 88 | # step 6 89 | # decide on what choice - using one of the strategy functions 90 | computer_choices = computer_strategy2(number_dice) 91 | print('Computer Choice: ', computer_choices) 92 | # Computer rolls again using the choices it made 93 | roll_again(computer_choices, computer_rolls) 94 | print('Computer new Roll: ', computer_rolls) 95 | 96 | 97 | 98 | # final line in code - deciding who wins 99 | find_winner(computer_rolls,user_rolls) 100 | -------------------------------------------------------------------------------- /GUIGame.py: -------------------------------------------------------------------------------- 1 | # The Candy Monster game program 2 | from tkinter import * 3 | import random 4 | 5 | # make window 6 | window = Tk() 7 | window.title('The Candy Monster Game') 8 | 9 | # create a canvas to put objects on the screen 10 | canvas = Canvas(window, width=400, height=400, bg = 'black') 11 | canvas.pack() 12 | 13 | # set up welcome screen with title and directions 14 | title = canvas.create_text(200, 200, text= 'The Candy Monster', \ 15 | fill='white', font = ('Helvetica', 30)) 16 | directions = canvas.create_text(200, 300, text= 'Collect candy \ 17 | but avoid the red ones', fill='white', font = ('Helvetica', 20)) 18 | 19 | # set up score display using label widget 20 | score = 0 21 | score_display = Label(window, text="Score :" + str(score)) 22 | score_display.pack() 23 | 24 | # set up level display using label widget 25 | level = 1 26 | level_display = Label(window, text="Level :" + str(level)) 27 | level_display.pack() 28 | 29 | # create an image object using the gif file 30 | player_image = PhotoImage(file="greenChar.gif") 31 | # use image object to create a character at position 200, 360 32 | mychar = canvas.create_image(200, 360, image = player_image) 33 | 34 | # variables and lists needed for managing candy 35 | candy_list = [] # list containing all candy created, empty at start 36 | bad_candy_list = [] # list containing all bad candy created, empty at start 37 | candy_speed = 2 # initial speed of falling candy 38 | candy_color_list = ['red', 'yellow', 'blue', 'green', 'purple', 'pink', \ 39 | 'white'] 40 | # function to make candy at random places 41 | def make_candy(): 42 | # pick a random x position 43 | xposition = random.randint(1, 400) 44 | # pick a random color 45 | candy_color = random.choice(candy_color_list) 46 | # create a candy of size 30 at random position and color 47 | candy = canvas.create_oval(xposition, 0, xposition+30, 30, fill = \ 48 | candy_color) 49 | # add candy to list 50 | candy_list.append(candy) 51 | # if color of candy is red - add it to bad_candy_list 52 | if candy_color == 'red' : 53 | bad_candy_list.append(candy) 54 | # schedule this function to make candy again 55 | window.after(1000, make_candy) 56 | 57 | # function moves candy downwards, and schedules call to move_candy 58 | def move_candy(): 59 | # loop through list of candy and change y position 60 | for candy in candy_list: 61 | canvas.move(candy, 0, candy_speed) 62 | # check if end of screen - restart at random position 63 | if canvas.coords(candy)[1] > 400: 64 | xposition = random.randint(1,400) 65 | canvas.coords(candy, xposition, 0, xposition+30,30) 66 | # schedule this function to move candy again 67 | window.after(50, move_candy) 68 | 69 | 70 | # function updates score, level and candy_speed 71 | def update_score_level(): 72 | # use of global since variables are changed 73 | global score, level, candy_speed 74 | score = score + 1 75 | score_display.config(text="Score :" + \ 76 | str(score)) 77 | # determine if level needs to change 78 | # update level and candy speed 79 | if score > 5 and score <= 10: 80 | candy_speed = candy_speed + 1 81 | level = 2 82 | level_display.config(text="Level :" + \ 83 | str(level)) 84 | elif score > 10: 85 | candy_speed = candy_speed + 1 86 | level = 3 87 | level_display.config(text="Level :" + \ 88 | str(level)) 89 | 90 | # function called to end game - destroys window 91 | def end_game_over(): 92 | window.destroy() 93 | 94 | # this destroys the instructions on the screen 95 | def end_title(): 96 | canvas.delete(title) # remove title 97 | canvas.delete(directions) # remove directions 98 | 99 | 100 | # check distance between 2 objects - return true if they 'touch' 101 | def collision(item1, item2, distance): 102 | xdistance = abs(canvas.coords(item1)[0] - canvas.coords(item2)[0]) 103 | ydistance = abs(canvas.coords(item1)[1] - canvas.coords(item2)[1]) 104 | overlap = xdistance < distance and ydistance < distance 105 | return overlap 106 | 107 | # checks if character hit bad candy, schedule end_game_over 108 | # if character hits candy, remove from screen, list, update score 109 | def check_hits(): 110 | # check if it hit a bad candy - need to end game 111 | for candy in bad_candy_list: 112 | if collision(mychar, candy, 30): 113 | game_over = canvas.create_text(200, 200, text= 'Game \ 114 | Over', fill='red', font = ('Helvetica', 30)) 115 | # end game but after user can see score 116 | window.after(2000, end_game_over) 117 | # do not check any other candy, window to be destroyed 118 | return 119 | # check if it hit any good candy 120 | for candy in candy_list: 121 | if collision(mychar, candy, 30): 122 | canvas.delete(candy) # remove from canvas 123 | # find where in list and remove and update score 124 | candy_list.remove(candy) 125 | update_score_level() 126 | # schedule check Hits again 127 | window.after(100, check_hits) 128 | 129 | move_direction = 0 # track which direction player is moving 130 | # Function handles when user first presses arrow keys 131 | def check_input(event): 132 | global move_direction 133 | key = event.keysym 134 | if key == "Right": 135 | move_direction = "Right" 136 | elif key == "Left": 137 | move_direction = "Left" 138 | 139 | # Function handles when user stop pressing arrow keys 140 | def end_input(event): 141 | global move_direction 142 | move_direction = "None" 143 | 144 | # Function checks if not on edge and updates x coordinates based on right/left 145 | def move_character(): 146 | if move_direction == "Right" and canvas.coords(mychar)[0] < 400: 147 | canvas.move(mychar, 10,0) 148 | if move_direction == "Left" and canvas.coords(mychar)[0] > 0 : 149 | canvas.move(mychar, -10,0) 150 | window.after(16, move_character) # Move character at 60 frames per second 151 | 152 | # bind the keys to the character 153 | canvas.bind_all('', check_input) # bind key press 154 | canvas.bind_all('', end_input) # bind all keys to circle 155 | 156 | # Start game loop by scheduling all the functions 157 | window.after(1000, end_title) # destroy title and instructions 158 | window.after(1000, make_candy) # start making candy 159 | window.after(1000, move_candy) # start moving candy 160 | window.after(1000, check_hits) # check if character hit a candy 161 | window.after(1000, move_character) # handle keyboard controls 162 | 163 | window.mainloop() # last line is the GUI main event loop 164 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # creativecodinginpython 2 | Creative Coding In Python 3 | Contains complete source code of the projects in the book, as well as images for projects. 4 | -------------------------------------------------------------------------------- /images/ImageInformation.txt: -------------------------------------------------------------------------------- 1 | Images for the projects in Creative Coding In Python 2 | -------------------------------------------------------------------------------- /images/greenChar.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sheena1010/creativecodinginpython/090bd0173d9d05c8d688457b9657b078972cac9f/images/greenChar.gif -------------------------------------------------------------------------------- /images/spider.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sheena1010/creativecodinginpython/090bd0173d9d05c8d688457b9657b078972cac9f/images/spider.gif -------------------------------------------------------------------------------- /images/stickfigure.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sheena1010/creativecodinginpython/090bd0173d9d05c8d688457b9657b078972cac9f/images/stickfigure.gif --------------------------------------------------------------------------------