├── .replit ├── Day-1 ├── RATIONAL.md ├── README.md ├── finished │ └── multiplication game.py ├── multiplication game.py └── number guess game.py ├── Day-2 ├── Archived-README.md ├── RATIONAL.md ├── README.md ├── finished │ ├── challenge problem.py │ ├── madlibs.py │ └── silly sentence generator.py ├── madlibs.py └── silly sentence generator.py ├── Day-3 ├── RATIONAL.md ├── README.md ├── coin flip.py ├── dice rolls.py ├── finished │ ├── coin flip.py │ └── dice rolls.py └── tax calculator.py ├── Day-4 ├── RATIONAL.md ├── README.md ├── coordinates.png ├── finished │ ├── house drawing.py │ └── random drawing.py ├── pygame house.png └── random drawing.py ├── Day-5 ├── RATIONAL.md ├── README.md ├── animation.py ├── beach_ball.png ├── finished │ ├── animation.py │ └── double-speed-and-bounce-off-all-walls.py └── minecraft.gif ├── Day-6 ├── RATIONAL.md ├── README.md ├── finished │ ├── bounce game with speed boost.py │ ├── bounce game.py │ └── pong finished.py └── paddle starter.py ├── Day-7 ├── README.md ├── blank pygame project.py ├── finished │ ├── snake finished.py │ ├── snake part 1.py │ ├── snake part 2.py │ ├── snake part 3.py │ ├── snake part 4.py │ ├── snake part 5.py │ ├── snake part 6.py │ ├── snake part 7.py │ └── zoop.wav └── snake almost done.py ├── Extras ├── Finished │ ├── breakout.py │ ├── brick.png │ ├── catch the good ones.py │ ├── green_ball.png │ ├── hailstone.py │ ├── pong finished with function.py │ ├── red_ball.png │ └── snake.py └── README.md ├── RATIONAL.md ├── README.md └── bell_number.py /.replit: -------------------------------------------------------------------------------- 1 | language = "python3" 2 | run = "python bell_number.py" 3 | -------------------------------------------------------------------------------- /Day-1/RATIONAL.md: -------------------------------------------------------------------------------- 1 | The first goal is to get them writing code right away. 2 | 3 | We will talk about variable types by using input and raw_input. 4 | 5 | We will introduce most programming concepts by reading the guessing game code. These include loops, conditionals, and importing modules 6 | 7 | The goal of the two projects is to give them a lot of code to look at for examples how to solve problems. Any time they have a question and we can ask "where have you done something similar before, go look at that code", they are able to do critical thinking and solve things themselves. 8 | 9 | -------------------------------------------------------------------------------- /Day-1/README.md: -------------------------------------------------------------------------------- 1 | ##Installation 2 | 3 | Go to http://helloworldbookblog.com/tools/ and download the installer for your operating system. This will include everything you need. We will be using Python version 2.X. 4 | 5 | If there are installation issues, http://repl.it/ can be used for the first few sessions (before graphics are required). 6 | 7 | 8 | ##First Intro to Python 9 | 10 | Open Python IDLE. This opens the output window - we will write our code in a new file. Click File -> New Window. This will open up a new window to type Python code. 11 | 12 | In the new window, type this to print something: 13 | 14 | ```python 15 | print "hello world" 16 | ``` 17 | 18 | Save the project as a .py extension (you can hit CTRL/CMD + S to save). Hit F5 to run your program. 19 | 20 | Create your first variable and print it. In computer language, a variable is what lets the computer remember something. In this case, we want the computer to remember our name. 21 | 22 | Below that is a `print` statement that shows you how to print a variable. 23 | 24 | ```python 25 | print "hello world" 26 | name = "Brian" 27 | print "my name is", name 28 | ``` 29 | 30 | The next task is to ask the user for something. That's what `raw_input` does. 31 | 32 | We can have the user type in something and store it in a variable. 33 | 34 | ```python 35 | print "hello world" 36 | name = "Brian" 37 | print "my name is", name 38 | 39 | your_name = raw_input("What is your name?") 40 | ``` 41 | 42 | If you run this, you'll see that there's no space between the `?` and the user's input. We can fix this by adding a space in. 43 | 44 | When you use `raw_input`, it stores what the user typed in in a variable. We can print the info the user typed in: 45 | 46 | ```python 47 | print "hello world" 48 | name = "Brian" 49 | print "my name is", name 50 | 51 | your_name = raw_input("What is your name? ") 52 | print "your name is", your_name 53 | ``` 54 | 55 | You can use `raw_input` to ask the user for strings - things like names and sentences. 56 | If you use `input`, Python will try to change what the user typed into a number. 57 | 58 | ```python 59 | print "hello world" 60 | name = "Brian" 61 | print "my name is", name 62 | 63 | your_name = raw_input("What is your name? ") 64 | print "your name is", your_name 65 | your_age = input("How old are you? ") 66 | print "in a year you will be", your_age+1, "years old" 67 | ``` 68 | 69 | If you try to type "nine" when it asks for the age, it won't work. Python doesn't know what "nine" is, it only know the digit "9". 70 | 71 | There are two types of variables we'll be using today: strings (things like words, sentences), and integers (whole numbers). Python thinks "nine" is a string, and can't change it to a number. 72 | 73 | **Bonus challenge**: In the example above, we added 1 to your age and told you how old you will be in a year. Can you set a new variable years_to_100 that will calculate how many years until you turn 100? Then print out the variable with a message. 74 | 75 | ##Reading and Altering Code - Number Guess Game 76 | 77 | Grab the file [number guess game.py](number guess game.py) and copy it into a new Python window. This is a complete game that came out of a book. Save it, and play the game! 78 | 79 | Then, we'll start looking through the code, and start making changes to it. 80 | 81 | Let's read through the code to see how it does it! 82 | 83 | It uses three variables: 84 | * `secret`, which stores the number you have to guess 85 | * `guess`, which is what the player guesses 86 | * `tries`, which is how many times the player has guessed. 87 | 88 | Look at those comments, with the `#` before them. This doesn't mean anything to the computer, but they can help when you come back to your code and need to understand it again. 89 | 90 | Look at this while: 91 | 92 | ```python 93 | while guess != secret and tries < 6: 94 | ``` 95 | 96 | This is saying, "While the player has guessed wrong, and they have tries, do this over and over again." **After the `while`, several lines of code are indented (with the Tab key), which indicate which code gets repeated. Indents matter in Python!** 97 | 98 | Some other things to point out: 99 | * Random is a module that was imported so you will be able to pick a random number 100 | * **if/else branches the code so that it only happens if the condition is met. The code that gets run if the condition is met is under it indented one space.** 101 | * != (check if not equal), == (check if equal) 102 | 103 | Let's make some changes! Can you: 104 | * Give the pirate your name 105 | * Change the game to be guessing between 1-200 106 | * Give 8 guesses 107 | 108 | 109 | ## Multiplication Game 110 | 111 | We are going to take what we learned in the Number Guess Game and create a new game - a multiplication quiz. The finished project will ask you a series of 10 random multiplication questions, you have to get each one right to go on to the next one. 112 | 113 | ###Getting started 114 | * Grab the starter file [multiplication game.py](multiplication game.py) and copy it into a new window. 115 | * Run it to see what it does 116 | * Read the code to see how it was done 117 | 118 | We'll be making a lot more changes to this program to get it to do what we want. 119 | 120 | ###Finish the project 121 | * Print a welcome message introducing the game 122 | * Use random so it doesn't ask 1 x 1 every time (look at the number guessing game for an example) 123 | * How can you repeat the code so it asks 10 questions in a row? 124 | 125 | You could use a for loop that repeats code a specific number of times. This line will repeat the indented code 10 times: 126 | 127 | ```python 128 | for num in range(0,10): 129 | ``` 130 | 131 | You could also use a while loop and count yourself. See how the number guess game counted the number of tries. 132 | 133 | What code would you want to repeat? Highlight all of it and hit tab to indent it one space. 134 | 135 | * Print a message at the end of the game 136 | 137 | * Finished example (no peeking early): [multiplication game.py](finished/multiplication game.py) 138 | 139 | ### You Finished, Now What? 140 | Congrats on finishing your very first game in Python! I'm going to guess that it's not very fun to play though. Computer programming is not just about finishing the basic requirements, it's about being creative and making the game your own! Always ask yourself, what would make this game more fun? What would make it cool? 141 | 142 | Here are some ideas to get you started: 143 | 144 | * Can you count how many times they got questions wrong and tell them at the end of the game? 145 | * Can you add another level with a harder num range? 146 | * Can you add division problems as well? 147 | * Can you ask to multiply 3 numbers instead of 2? 148 | * Can you time the player? 149 | * What other games can you do with user input? 150 | -------------------------------------------------------------------------------- /Day-1/finished/multiplication game.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | #print your welcome message here 4 | print "Welcome to the multiplication game." 5 | print "How well do you know your 2-12 multiplication tables?" 6 | 7 | for num in range(0,10): 8 | #pick the numbers to multiply 9 | number1 = random.randint(2,12) 10 | number2 = random.randint(2,12) 11 | answer = number1 * number2 12 | 13 | guess = 0 14 | print "What is", number1, "x", number2, "?" 15 | 16 | while guess != answer: 17 | guess = input("Answer: ") 18 | if guess != answer: 19 | print "No, try again" 20 | 21 | print "You got it!" 22 | 23 | print "That's it, good work!" 24 | -------------------------------------------------------------------------------- /Day-1/multiplication game.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | #print your welcome message here 4 | 5 | 6 | #pick the numbers to multiply 7 | number1 = 1 8 | number2 = 1 9 | answer = number1 * number2 10 | 11 | guess = 0 12 | print "What is", number1, "x", number2, "?" 13 | 14 | while guess != answer: 15 | guess = input("Answer: ") 16 | if guess != answer: 17 | print "No, try again" 18 | 19 | print "You got it!" 20 | -------------------------------------------------------------------------------- /Day-1/number guess game.py: -------------------------------------------------------------------------------- 1 | # Listing_1-2.py 2 | # Copyright Warren Sande, 2009 3 | # Released under MIT license http://www.opensource.org/licenses/mit-license.php 4 | # Version 61 ---------------------------- 5 | 6 | #Minor changes by Brian Skinner, Sept-2013 7 | 8 | # Number Guess game 9 | 10 | import random 11 | 12 | secret = random.randint(1, 99) # pick a secret number 13 | guess = 0 14 | tries = 0 15 | 16 | print "AHOY! I'm the Dread Pirate Roberts, and I have a secret!" 17 | print "It is a number from 1 to 99. I'll give you 6 tries. " 18 | 19 | # try until they guess it or run out of turns 20 | while guess != secret and tries < 6: 21 | guess = input("What's yer guess? ") # get the player's guess 22 | if guess < secret: 23 | print "Too low, ye scurvy dog!" 24 | if guess > secret: 25 | print "Too high, landlubber!" 26 | tries = tries + 1 # used up one try 27 | 28 | # print message at end of game 29 | if guess == secret: 30 | print "Avast! Ye got it! Found my secret, ye did!" 31 | else: 32 | print "No more guesses! Better luck next time, matey!" 33 | print "The secret number was", secret 34 | -------------------------------------------------------------------------------- /Day-2/Archived-README.md: -------------------------------------------------------------------------------- 1 | This is an archive of the README file for Day 2 of the Fall 2013 tracks. 2 | 3 | ##MadLibs 4 | 5 | Open the starter file: [madlibs.py](madlibs.py). Copy all the code into a new Python window. 6 | 7 | If you try to run it, what happens? 8 | 9 | Look at the code - you have set all the variables for the program to run. We can use `raw_input` (from the last session) to ask the user for words: 10 | 11 | ``` 12 | adjective2 = raw_input("Enter another adjective: ") 13 | adjective3 = raw_input("Enter yet another adjective: ") 14 | adjective4 = raw_input("Enter a fourth adjective: ") 15 | ``` 16 | 17 | Notice that you can use commas to string things together in a `print` statement. Python automatically adds a space when you do something like `"Hello,", name`. 18 | 19 | You can also notice that IDLE automatically adds colors to code. Built-in functions like `raw_input` are purple, keywords are orange, and strings are green. Comments (that Python ignores) are red, and most other things are black. The colors don't matter to Python, but they're useful to help you understand your code. 20 | 21 | If you run the program, Python takes all our variables and sticks them into our sentences. Try running the program multiple times and see what sentences you can create! 22 | 23 | ##Silly Sentence Generator 24 | 25 | Our next project is kind of like MadLibs, but instead of asking the user for words, the computer is going to pick words itself. 26 | 27 | Open the starter file: [silly sentence generator.py](silly sentence generator.py). Copy all the code into a new Python window. 28 | 29 | ###Let's look at the code: 30 | 31 | ####Lists 32 | 33 | Up until now, we've stored things in one variable at a time. A list is a way to collect multiple values in one variable. Let's make a list: 34 | 35 | ``` 36 | >>> grocery_list = ["apples","bananas","a pet chicken"] 37 | ``` 38 | 39 | We can look at specific items in the list with an *index*, and indexes start from zero. 40 | 41 | ``` 42 | >>> print grocery_list[0] 43 | apples 44 | ``` 45 | 46 | ####Random 47 | 48 | We can use the random module to create random numbers: 49 | 50 | ``` 51 | import random 52 | random.randint(0,5) 53 | ``` 54 | 55 | A module is something extra in Python that lets you go beyond what Python lets you do by default. 56 | 57 | ####The `random_word` Function 58 | The `random_word` function picks a random index from 0 to the last item in the list. Then, it finds the word with that index, and hands it back: 59 | 60 | ``` 61 | #this function is given a list of words and selects one at random 62 | def random_word(list_of_words): 63 | number_of_words = len(list_of_words) #get the length of the words list 64 | random_word_number = random.randint(0,number_of_words - 1) #pick a random number up to the end of the list 65 | selected_word = list_of_words[random_word_number] #select the word at the number spot 66 | return selected_word #hand it back 67 | ``` 68 | 69 | *Functions* in Python let you take a piece of code and use it over and over again. 70 | 71 | #####Functions 72 | We can use the `def` keyword to define a function. Functions let you send something to the function, and the function sends you something back. You can re-use functions by sending them different data, and they'll send you different data back. 73 | 74 | In the statement `random_word(verb_list)`, `verb_list` is what we're sending to the function. The function doesn't care that it's the verb list, so we can give it any list of words. 75 | 76 | ###Add our own code! 77 | 78 | ####Create your own noun and adjective lists 79 | 80 | Look at how the verb list was done. We can make our own lists of adjectives and nouns, and maybe add some more verbs for variety. 81 | 82 | ####Use the function to select random words from each list 83 | 84 | You will need 5 variables: adjective1, noun1, verb, adjective2, and noun2. Remember, we can re-use functions by passing them different information, and they'll send different information back. 85 | 86 | ####Print the result in a sentence. 87 | 88 | Be sure to insert the word "the" in the right places. What sentences can you make? 89 | 90 | ####`random.choice` 91 | We just wrote a function to pick a random word from a list of words. However, the `random` module already gives us a way to do this. We can use `random.choice` to write a new version of `random_word`: 92 | 93 | ``` 94 | def random_word2(list_of_words): 95 | return random.choice(list_of_words) 96 | ``` 97 | 98 | We've just shortened a 4-line function to a 1-line function. When you have a 1-line function, you have to think, "Do I really need that function?" In this case, it's kind of OK, because we arrived at that 1-line function by a process of iteration. 99 | 100 | ####Making more silly sentences 101 | Can you change the program to print 10 silly sentences instead of 1? Look back at [the notes from last week](../day-1) if you need to remember how loops work. 102 | 103 | ####Possible extensions 104 | 105 | * Improve your word list. If you like Minecraft, for example, add some Minecraft terms! 106 | * Improve the sentences' grammar 107 | * Make the sentences longer, with more word types 108 | * Just see how creative you can get with your sentences! 109 | 110 | 111 | ##Challenge Problem 112 | 113 | Here is a bonus [MadLib](http://www.teach-nology.com/worksheets/language_arts/madlibs/6/) that you can turn into a program: 114 | 115 | ``` 116 | print "Make Me A Video Game MadLib!" 117 | print "I, the", adj1, "and", adj2, name1 + ", has", past_tense_verb, name2 + "'s sister and plans" 118 | print "to steal her", adj3, noun + "! What are a", animal1, "and backpacking", animal2 119 | print "to do? Before you can help", name3, "you'll have to collect the", adj4 120 | print plural_noun1,"and",adj5, plural_noun2, "that open up the", number1, "worlds connected to" 121 | print name4 + "'s Lair. There are", number2, plural_noun3, "and", number3, plural_noun4, "in the game," 122 | print "along with hundreds of other goodies for you to find." 123 | ``` 124 | -------------------------------------------------------------------------------- /Day-2/RATIONAL.md: -------------------------------------------------------------------------------- 1 | The goal for this class is to introduce lists and functions. 2 | -------------------------------------------------------------------------------- /Day-2/README.md: -------------------------------------------------------------------------------- 1 | ## Silly Sentence Generator 2 | 3 | Our next project is a silly sentence generator that will randomly pick words from lists to generate a funny sentence. 4 | 5 | Open the starter file: [silly sentence generator.py](silly%20sentence%20generator.py). Copy all the code into a new Python window. 6 | 7 | ### Let's look at the code: 8 | 9 | #### Lists 10 | 11 | Up until now, we've stored things in one variable at a time. A list is a way to collect multiple values in one variable. Let's make a list: 12 | 13 | ```python 14 | >>> grocery_list = ["apples","bananas","a pet chicken"] 15 | ``` 16 | 17 | We can look at specific items in the list with an *index*, and indexes start from zero. 18 | 19 | ```python 20 | >>> print grocery_list[0] 21 | apples 22 | ``` 23 | 24 | #### Random 25 | 26 | We can use the random module to create random numbers: 27 | 28 | ```python 29 | import random 30 | random.randint(0,5) 31 | ``` 32 | 33 | A module is something extra in Python that lets you go beyond what Python lets you do by default. 34 | 35 | #### The `random_word` Function 36 | The `random_word` function picks a random index from 0 to the last item in the list. Then, it finds the word with that index, and hands it back: 37 | 38 | ```python 39 | #this function is given a list of words and selects one at random 40 | def random_word(list_of_words): 41 | number_of_words = len(list_of_words) #get the length of the words list 42 | random_word_number = random.randint(0,number_of_words - 1) #pick a random number up to the end of the list 43 | selected_word = list_of_words[random_word_number] #select the word at the number spot 44 | return selected_word #hand it back 45 | ``` 46 | 47 | *Functions* in Python let you take a piece of code and use it over and over again. 48 | 49 | ##### Functions 50 | We can use the `def` keyword to define a function. Functions let you send something to the function, and the function sends you something back. You can re-use functions by sending them different data, and they'll send you different data back. 51 | 52 | In the statement `random_word(verb_list)`, `verb_list` is what we're sending to the function. The function doesn't care that it's the verb list, so we can give it any list of words. 53 | 54 | ### Add our own code! 55 | 56 | #### Create your own noun and adjective lists 57 | 58 | Look at how the verb list was done. We can make our own lists of adjectives and nouns, and maybe add some more verbs for variety. 59 | 60 | #### Use the function to select random words from each list 61 | 62 | You will need 5 variables: `adjective1`, `noun1`, `verb`, `adjective2`, and `noun2`. Remember, we can re-use functions by passing them different information, and they'll send different information back. 63 | 64 | #### Print the result in a sentence. 65 | 66 | Be sure to insert the word "the" in the right places. What sentences can you make? 67 | 68 | #### `random.choice` 69 | We just wrote a function to pick a random word from a list of words. However, the `random` module already gives us a way to do this. We can use `random.choice` to write a new version of `random_word`: 70 | 71 | ```python 72 | def random_word2(list_of_words): 73 | return random.choice(list_of_words) 74 | ``` 75 | 76 | We've just shortened a 4-line function to a 1-line function. When you have a 1-line function, you have to think, "Do I really need that function?" In this case, it's kind of OK, because we arrived at that 1-line function by a process of iteration. 77 | 78 | #### Making more silly sentences 79 | Can you change the program to print 10 silly sentences instead of 1? Look back at [the notes from last week](../day-1) if you need to remember how loops work. 80 | 81 | #### Possible extensions 82 | 83 | * Improve your word list. If you like Minecraft, for example, add some Minecraft terms! 84 | * Improve the sentences' grammar 85 | * Make the sentences longer, with more word types 86 | * Just see how creative you can get with your sentences! 87 | 88 | 89 | -------------------------------------------------------------------------------- /Day-2/finished/challenge problem.py: -------------------------------------------------------------------------------- 1 | 2 | adj1 = raw_input("Enter an adjective: ") 3 | adj2 = raw_input("Enter an adjective: ") 4 | adj3 = raw_input("Enter an adjective: ") 5 | adj4 = raw_input("Enter an adjective: ") 6 | adj5 = raw_input("Enter an adjective: ") 7 | name1 = raw_input("Enter a name: ") 8 | name2 = raw_input("Enter a name: ") 9 | name3 = raw_input("Enter a name: ") 10 | name4 = raw_input("Enter a name: ") 11 | number1 = raw_input("Enter a number: ") 12 | number2 = raw_input("Enter a number: ") 13 | number3 = raw_input("Enter a number: ") 14 | plural_noun1 = raw_input("Enter a plural noun: ") 15 | plural_noun2 = raw_input("Enter a plural noun: ") 16 | plural_noun3 = raw_input("Enter a plural noun: ") 17 | plural_noun4 = raw_input("Enter a plural noun: ") 18 | past_tense_verb = raw_input("Enter a past tense verb: ") 19 | noun = raw_input("Enter a noun: ") 20 | animal1 = raw_input("Enter an animal: ") 21 | animal2 = raw_input("Enter an animal: ") 22 | 23 | 24 | print "" 25 | print "Make Me A Video Game MadLib!" 26 | print "I, the", adj1, "and", adj2, name1 + ", has", past_tense_verb, name2 + "'s sister and plans" 27 | print "to steal her", adj3, noun + "! What are a", animal1, "and backpacking", animal2 28 | print "to do? Before you can help", name3, "you'll have to collect the", adj4 29 | print plural_noun1,"and",adj5, plural_noun2, "that open up the", number1, "worlds connected to" 30 | print name4 + "'s Lair. There are", number2, plural_noun3, "and", number3, plural_noun4, "in the game," 31 | print "along with hundreds of other goodies for you to find." 32 | -------------------------------------------------------------------------------- /Day-2/finished/madlibs.py: -------------------------------------------------------------------------------- 1 | #Madlibs credit: http://www.teach-nology.com/worksheets/language_arts/madlibs/1/ 2 | 3 | #Ask the user for all the variables 4 | #Have to set: adjective1 through 5, animal1, animal2, food1, food2, 5 | # verb, ptverb (a past tense verb) 6 | adjective1 = raw_input("Enter an adjective: ") 7 | adjective2 = raw_input("Enter another adjective: ") 8 | adjective3 = raw_input("Enter yet another adjective: ") 9 | adjective4 = raw_input("Enter a fourth adjective: ") 10 | adjective5 = raw_input("Enter a fifth adjective (last one, I promise): ") 11 | animal1 = raw_input("Enter an animal: ") 12 | animal2 = raw_input("Enter another animal: ") 13 | food1 = raw_input("Enter a food item: ") 14 | food2 = raw_input("Enter another food item: ") 15 | verb = raw_input("Enter a verb: ") 16 | ptverb = raw_input("Enter an -ed verb: ") 17 | 18 | 19 | #print out the result 20 | print "" #blank line 21 | print "Today I went to the zoo. I saw a", adjective1, animal1, "jumping" 22 | print "up and down in its tree. He",ptverb, "through the large" 23 | print "tunnel that led to its", adjective2, "cage. I got a", food1 24 | print "and passed it through the cage to a gigantic", adjective3, animal2 25 | print "towering over my head. Feeding that animal made me hungry. " 26 | print "I went to get a", adjective4,"scoop of", food2 +". Afterwards I had to" 27 | print verb, "to catch our bus. It was a", adjective5, "day at the zoo!" 28 | -------------------------------------------------------------------------------- /Day-2/finished/silly sentence generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | verb_list = ["ran","jumped","climbed","licked","programmed"] #create a list of all your verbs 4 | noun_list = ["pet duck","tree","toaster","computer"] 5 | adjective_list = ["tasty","large","smelly","green"] 6 | 7 | #this function is given a list of words and selects one at random 8 | def random_word(list_of_words): 9 | number_of_words = len(list_of_words) #get the length of the words list 10 | random_word_number = random.randint(0,number_of_words - 1) #pick a random number up to the end of the list 11 | selected_word = list_of_words[random_word_number] #select the word at the number spot 12 | return selected_word #hand it back 13 | 14 | def random_word2(list_of_words): 15 | return random.choice(list_of_words) 16 | 17 | #a simple sentence structure: 18 | #The the . 19 | 20 | for i in range(10): 21 | adjective1 = random_word2(adjective_list) 22 | adjective2 = random_word2(adjective_list) 23 | noun1 = random_word2(noun_list) 24 | noun2 = random_word2(noun_list) 25 | verb = random_word2(verb_list) #call the function and remember the result 26 | print "The",adjective1,noun1,verb,"the",adjective2,noun2 27 | -------------------------------------------------------------------------------- /Day-2/madlibs.py: -------------------------------------------------------------------------------- 1 | #Madlibs credit: http://www.teach-nology.com/worksheets/language_arts/madlibs/1/ 2 | 3 | #Ask the user for all the variables 4 | #Have to set: adjective1 through 5, animal1, animal2, food1, food2, 5 | # verb, ptverb (a past tense verb) 6 | adjective1 = raw_input("Enter an adjective: ") 7 | 8 | #print out the result 9 | print "" #blank line 10 | print "Today I went to the zoo. I saw a", adjective1, animal1, "jumping" 11 | print "up and down in its tree. He",ptverb, "through the large" 12 | print "tunnel that led to its", adjective2, "cage. I got a", food1 13 | print "and passed it through the cage to a gigantic", adjective3, animal2 14 | print "towering over my head. Feeding that animal made me hungry. " 15 | print "I went to get a", adjective4,"scoop of", food2 +". Afterwards I had to" 16 | print verb, "to catch our bus. It was a", adjective5, "day at the zoo!" 17 | 18 | -------------------------------------------------------------------------------- /Day-2/silly sentence generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | verb_list = ["run","jump","climb"] #create a list of all your verbs 4 | 5 | #this function is given a list of words and selects one at random 6 | def random_word(list_of_words): 7 | number_of_words = len(list_of_words) #get the length of the words list 8 | random_word_number = random.randint(0,number_of_words - 1) #pick a random number up to the end of the list 9 | selected_word = list_of_words[random_word_number] #select the word at the number spot 10 | return selected_word #hand it back 11 | 12 | #a simple sentence structure: 13 | #The the . 14 | 15 | verb = random_word(verb_list) #call the function and remember the result 16 | print verb 17 | -------------------------------------------------------------------------------- /Day-3/RATIONAL.md: -------------------------------------------------------------------------------- 1 | The goal for this class is to introduce loops and gain more experience with variables and conditionals. 2 | -------------------------------------------------------------------------------- /Day-3/README.md: -------------------------------------------------------------------------------- 1 | ##Simulating Coin Flips 2 | 3 | Grab the starter file [coin flip.py](coin flip.py) and copy it into a new Python window. 4 | 5 | The goal of the program is to simulate flipping a coin 1000 times. It uses a function flip_coin() that is incomplete. It's your job to finish it. 6 | 7 | Here is one approach to building your function: 8 | * Pick a random number between 1 and 2 (where have you done this before?) 9 | * If the random number is 1 return heads. Else return tails. (where have you done an if/else?) 10 | 11 | This line of code is how you can return heads: 12 | 13 | ```python 14 | return "heads" 15 | ``` 16 | 17 | That's it! Test it out and see how many times can your program flip a coin a second. 18 | 19 | Extra: See if you can count how many coin flips it takes before you get 10 heads in a row. 20 | 21 | 22 | ##Simulating Dice Rolls 23 | 24 | Grab the starter file [dice rolls.py](dice rolls.py) and copy it into a new Python window. 25 | 26 | This program uses an array named totals. An array is a variable that remembers a list of info in specific slots (the slots start counting at 0). 27 | 28 | This is how you can set a specific slot in the array. In this example we are setting spot 5 (remember it counts starting at zero) to be 99. 29 | 30 | ```python 31 | totals[5] = 99 32 | ``` 33 | 34 | This is how we can look up what the number is in a spot and print it out. This would print 99 35 | 36 | ```python 37 | print totals[5] 38 | ``` 39 | 40 | Your job is to complete the dice_roll() function. It should roll two 6-sided dice and add the rolls together. It should return their total. 41 | 42 | That's it! Test it and see if it is faster or slower than the coin flip program. 43 | 44 | Take home challenge: can you change the program to roll 3 dice? How many slots would you have to add to the array? How about 5 dice? 45 | 46 | ##Bonus Text-Based Programs 47 | 48 | In Day 4 we will begin doing some graphics. There are a couple more fun text-based programs that you can try before then. Go to https://github.com/CoderDojoSV/beginner-python/tree/master/Extras and check them out! 49 | -------------------------------------------------------------------------------- /Day-3/coin flip.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | total_heads = 0 4 | total_tails = 0 5 | 6 | #create a function that half the time returns "heads" and half the time returns "tails" 7 | def flip_coin(): 8 | return "" 9 | 10 | #flip 1000 times 11 | for n in range(0,1000): 12 | flip = flip_coin() 13 | if flip == "heads": 14 | total_heads = total_heads + 1 15 | else: 16 | total_tails = total_tails + 1 17 | 18 | print "Heads flipped: ", total_heads 19 | print "Tails flipped: ", total_tails 20 | 21 | -------------------------------------------------------------------------------- /Day-3/dice rolls.py: -------------------------------------------------------------------------------- 1 | # Listing_23-2.py 2 | # Copyright Warren Sande, 2009 3 | # Released under MIT license http://www.opensource.org/licenses/mit-license.php 4 | # Version 61 ---------------------------- 5 | # Changes by Brian Skinner - Sept, 2013 6 | 7 | # Rolling two six-sided dice 1000 times. 8 | 9 | import random 10 | 11 | totals = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 12 | 13 | #function that rolls 2 dice and returns the sum of their rolls 14 | def roll_dice(): 15 | return 2 16 | 17 | for i in range(1000): 18 | dice_total = roll_dice() 19 | totals[dice_total] = totals[dice_total] + 1 20 | 21 | #why did we skip spots 0 and 1? 22 | print "2 was rolled", totals[2], "times" 23 | print "3 was rolled", totals[3], "times" 24 | print "4 was rolled", totals[4], "times" 25 | print "5 was rolled", totals[5], "times" 26 | print "6 was rolled", totals[6], "times" 27 | print "7 was rolled", totals[7], "times" 28 | print "8 was rolled", totals[8], "times" 29 | print "9 was rolled", totals[9], "times" 30 | print "10 was rolled", totals[10], "times" 31 | print "11 was rolled", totals[11], "times" 32 | print "12 was rolled", totals[12], "times" 33 | -------------------------------------------------------------------------------- /Day-3/finished/coin flip.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | total_heads = 0 4 | total_tails = 0 5 | 6 | #create a function that half the time returns "heads" and half the time returns "tails" 7 | def flip_coin(): 8 | random_number = random.randint(1,2) 9 | if random_number == 1: 10 | return "heads" 11 | else: 12 | return "tails" 13 | 14 | #flip 1000 times 15 | for n in range(0,1000): 16 | flip = flip_coin() 17 | if flip == "heads": 18 | total_heads = total_heads + 1 19 | else: 20 | total_tails = total_tails + 1 21 | 22 | print "Heads flipped: ", total_heads 23 | print "Tails flipped: ", total_tails 24 | 25 | -------------------------------------------------------------------------------- /Day-3/finished/dice rolls.py: -------------------------------------------------------------------------------- 1 | # Listing_23-2.py 2 | # Copyright Warren Sande, 2009 3 | # Released under MIT license http://www.opensource.org/licenses/mit-license.php 4 | # Version 61 ---------------------------- 5 | # Changes by Brian Skinner - Sept, 2013 6 | 7 | # Rolling two six-sided dice 1000 times. 8 | 9 | import random 10 | 11 | totals = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 12 | 13 | #function that rolls 2 dice and returns the sum of their rolls 14 | def roll_dice(): 15 | roll1 = random.randint(1,6) 16 | roll2 = random.randint(1,6) 17 | total = roll1 + roll2 18 | return total 19 | 20 | for i in range(1000): 21 | dice_total = roll_dice() 22 | totals[dice_total] = totals[dice_total]+ 1 23 | 24 | #why did we skip spots 0 and 1? 25 | print "2 was rolled", totals[2], "times" 26 | print "3 was rolled", totals[3], "times" 27 | print "4 was rolled", totals[4], "times" 28 | print "5 was rolled", totals[5], "times" 29 | print "6 was rolled", totals[6], "times" 30 | print "7 was rolled", totals[7], "times" 31 | print "8 was rolled", totals[8], "times" 32 | print "9 was rolled", totals[9], "times" 33 | print "10 was rolled", totals[10], "times" 34 | print "11 was rolled", totals[11], "times" 35 | print "12 was rolled", totals[12], "times" 36 | -------------------------------------------------------------------------------- /Day-3/tax calculator.py: -------------------------------------------------------------------------------- 1 | # Listing_13-4 2 | # Copyright Warren Sande, 2009 3 | # Released under MIT license http://www.opensource.org/licenses/mit-license.php 4 | # Version 61 ---------------------------- 5 | #Significant changes by Brian Skinner - Sept, 2013 6 | 7 | #Explain the program 8 | print "This is a tax calculator. Enter in the price of your item" 9 | print "and the program will calculate how much it costs with tax." 10 | 11 | #Create tax function that takes price and tax_rate as imputs and returns the total price 12 | def calculate_tax(price, tax_rate): 13 | total = price + (price * tax_rate) 14 | total = round(total, 2) #round it to 2 decimal places 15 | return total 16 | 17 | #float is a variable type for decimals 18 | my_price = float(raw_input("Price $")) 19 | my_price_with_tax = calculate_tax(my_price,.0875) 20 | print "The price with tax: $", my_price_with_tax 21 | 22 | -------------------------------------------------------------------------------- /Day-4/RATIONAL.md: -------------------------------------------------------------------------------- 1 | We will introduce PyGame by first drawing random shapes and colors on the screen. 2 | -------------------------------------------------------------------------------- /Day-4/README.md: -------------------------------------------------------------------------------- 1 | Note: You cannot use [repl.it](http://repl.it/) for this session. Make sure you've [installed everything](https://github.com/CoderDojoSV/beginner-python/blob/master/Day-1/README.md#installation) before you come to the session. 2 | 3 | You can check if you have pygame installed by typing `import pygame` in the shell and hitting enter. If you have it setup, it will not do anything. If you don't, you will get an error. 4 | 5 | ##Starting out with Pygame 6 | 7 | **Do not name your file pygame.py!** IDLE will think your file is the `pygame` module we are trying to use, which won't work! 8 | 9 | We can start out by telling Pygame to open a window. This will give us a place to draw things on the screen. 10 | 11 | ```python 12 | import pygame 13 | pygame.init() 14 | screen = pygame.display.set_mode((640,480)) 15 | ``` 16 | 17 | If you tried running that code, you might notice that it doesn't work too well. Depending on your computer, it might have frozen, crashed, or opened a window and closed it really fast. This is because the program doesn't have an *event loop* - a way to tell the program how to keep the window open and respond to things the user does. 18 | 19 | Here's what our code looks like with an event loop added: 20 | 21 | ```python 22 | import pygame 23 | pygame.init() 24 | screen = pygame.display.set_mode((640,480)) 25 | 26 | running = True 27 | while running: 28 | for event in pygame.event.get(): 29 | if event.type == pygame.QUIT: 30 | running = False 31 | pygame.quit() 32 | ``` 33 | 34 | ##The coordinate grid 35 | 36 | Pygame uses a coordinate system to describe where things are on the screen. (0,0) is the top-left and (640,480) is the bottom right, and anything in between is somewhere inside the window. 37 | 38 | ![The coordinate system](coordinates.png) 39 | 40 | ##Drawing Shapes 41 | 42 | We can use Pygame to draw lines, rectangles, and circles on the screen. 43 | 44 | Start with this code: 45 | 46 | ```python 47 | #setup 48 | import pygame 49 | pygame.init() 50 | screen = pygame.display.set_mode((640,480)) 51 | 52 | 53 | screen.fill(pygame.color.THECOLORS['black']) 54 | 55 | pygame.draw.line(screen,pygame.color.THECOLORS['white'],(500,450),(400,300)) 56 | pygame.draw.circle(screen,pygame.color.THECOLORS['white'],(300,300),50) 57 | pygame.draw.rect(screen,pygame.color.THECOLORS['white'],(200,100,50,100)) 58 | 59 | pygame.display.flip() 60 | 61 | 62 | #closing the window 63 | running = True 64 | while running: 65 | for event in pygame.event.get(): 66 | if event.type == pygame.QUIT: 67 | running = False 68 | pygame.quit() 69 | ``` 70 | 71 | ####Challenge 1 72 | Can you change the color of all the shapes? 73 | 74 | ####Challenge 2 75 | Can you make the circle twice as big? 76 | 77 | ####Challenge 3 78 | Can you make the line go from the bottom left corner of the screen all the way to the top right? 79 | 80 | ####Challenge 4 81 | Can you make another rectangle that is short and fat? 82 | 83 | ####Challenge 5 84 | Can you make the line be on top of the other shapes? 85 | 86 | 87 | We can also have shapes that aren't filled in: 88 | 89 | ```python 90 | #setup 91 | import pygame 92 | pygame.init() 93 | screen = pygame.display.set_mode((640,480)) 94 | 95 | 96 | screen.fill(pygame.color.THECOLORS['black']) 97 | 98 | pygame.draw.line(screen,pygame.color.THECOLORS['red'],(500,450),(400,300),6) 99 | pygame.draw.circle(screen,pygame.color.THECOLORS['blue'],(300,300),50,4) 100 | pygame.draw.rect(screen,pygame.color.THECOLORS['green'],(200,100,50,100),8) 101 | 102 | pygame.display.flip() 103 | 104 | 105 | #closing the window 106 | running = True 107 | while running: 108 | for event in pygame.event.get(): 109 | if event.type == pygame.QUIT: 110 | running = False 111 | pygame.quit() 112 | ``` 113 | 114 | ####Challenge 5 115 | Can you make another rectangle inside the existing one that has a thinner border? 116 | 117 | ##Drawing Random Shapes 118 | 119 | We can combine random and pygame to create art! Copy the code from the starter file [random drawing.py](random drawing.py). 120 | 121 | How can we change the art the program creates? 122 | 123 | ####Challenge 1 124 | Can you make it draw lines with a thickness of 2? 125 | 126 | ####Challenge 2 127 | Can you make it draw 100 lines? 128 | 129 | ####Challenge 3 130 | Can you add to the program so it also draws circles at random locations with a random color? 131 | 132 | ####Challenge 4 133 | Can you make those circles have a random radius between 2 and 10? 134 | 135 | ####Challenge 5 136 | Can you add rectangles too? 137 | 138 | ####Challenge 6 139 | Can you change the program so it adds shapes to the screen one at a time? You can use `pygame.time.delay(20)` to pause for a bit. 140 | 141 | ##Take Home Challenge 142 | 143 | Draw your own picture! Go to http://colorpicker.com/ to determine the RGB values for different colors to use in your image. [Here is the finished code](finished/house drawing.py) for drawing this image: 144 | 145 | ![The coordinate system](pygame house.png) 146 | -------------------------------------------------------------------------------- /Day-4/coordinates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Day-4/coordinates.png -------------------------------------------------------------------------------- /Day-4/finished/house drawing.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | pygame.init() 3 | screen = pygame.display.set_mode((640,480)) 4 | 5 | #used http://colorpicker.com/ to find RGB colors 6 | 7 | def draw_tree(x,y): 8 | #tree trunk (50 wide and 100 tall) 9 | pygame.draw.rect(screen,(117,90,0),(x,y-100,50,100)) 10 | #leaves are a circle 11 | pygame.draw.circle(screen,(27,117,0),(x+25,y-120),50) 12 | 13 | def draw_house(x,y): 14 | #pink house 15 | pygame.draw.rect(screen,(255,171,244),(x,y-180,200,180)) 16 | #brown door 17 | pygame.draw.rect(screen,(89,71,0),(x+80,y-60,40,60)) 18 | #yellow door knob 19 | pygame.draw.circle(screen,(255,204,0),(x+112,y-30),4) 20 | #triangle roof 21 | pygame.draw.polygon(screen, (125,125,125), ( (x,y-180),(x+100,y-250),(x+200,y-180) ) ) 22 | draw_window(x+20,y-90) 23 | draw_window(x+130,y-90) 24 | 25 | def draw_window(x,y): 26 | #glass 27 | pygame.draw.rect(screen,(207,229,255),(x,y-50,50,50)) 28 | #frame 29 | pygame.draw.rect(screen,(0,0,0),(x,y-50,50,50),5) 30 | pygame.draw.rect(screen,(0,0,0),(x+23,y-50,5,50)) 31 | pygame.draw.rect(screen,(0,0,0),(x,y-27,50,5)) 32 | 33 | #this function is able to draw clouds of different sizes 34 | def draw_cloud(x,y,size): 35 | #put int() around any multiplications by decimals to get rid of this warning: 36 | #DeprecationWarning: integer argument expected, got float 37 | pygame.draw.circle(screen,(255,255,255),(x,y),int(size*.5)) 38 | pygame.draw.circle(screen,(255,255,255),(int(x+size*.5),y),int(size*.6)) 39 | pygame.draw.circle(screen,(255,255,255),(x+size,int(y-size*.1)),int(size*.4)) 40 | 41 | 42 | #green ground 43 | pygame.draw.rect(screen,(0,160,3),(0,400,640,80)) 44 | #light blue sky 45 | pygame.draw.rect(screen,(135,255,255),(0,0,640,400)) 46 | 47 | draw_tree(60,400) #x and y location are the bottom left of tree trunk 48 | draw_tree(550,400) 49 | 50 | draw_house(225,400) 51 | 52 | draw_cloud(60,120,80) 53 | draw_cloud(200,50,40) 54 | draw_cloud(450,100,120) 55 | 56 | 57 | pygame.display.flip() 58 | 59 | running = True 60 | while running: 61 | for event in pygame.event.get(): 62 | if event.type == pygame.QUIT: 63 | running = False 64 | pygame.quit() 65 | -------------------------------------------------------------------------------- /Day-4/finished/random drawing.py: -------------------------------------------------------------------------------- 1 | import random 2 | import pygame 3 | pygame.init() 4 | screen = pygame.display.set_mode((640,480)) 5 | 6 | def pick_random_color(): 7 | colorname = random.choice(pygame.color.THECOLORS.keys()) 8 | return pygame.color.THECOLORS[colorname] 9 | def random_coordinates(): 10 | x = random.randint(0,640) 11 | y = random.randint(0,480) 12 | return (x,y) 13 | 14 | screen.fill((0,0,0)) 15 | 16 | for i in range(20): 17 | pygame.draw.line(screen,pick_random_color(),random_coordinates(),random_coordinates(),random.randint(1,5)) 18 | pygame.draw.circle(screen,pick_random_color(),random_coordinates(),random.randint(10,500),random.randint(1,5)) 19 | 20 | 21 | pygame.display.flip() 22 | 23 | running = True 24 | while running: 25 | for event in pygame.event.get(): 26 | if event.type == pygame.QUIT: 27 | running = False 28 | pygame.quit() 29 | -------------------------------------------------------------------------------- /Day-4/pygame house.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Day-4/pygame house.png -------------------------------------------------------------------------------- /Day-4/random drawing.py: -------------------------------------------------------------------------------- 1 | #setup 2 | import random 3 | import pygame 4 | pygame.init() 5 | screen = pygame.display.set_mode((640,480)) 6 | 7 | #functions 8 | def random_color(): 9 | red = random.randint(0,255) 10 | green = random.randint(0,255) 11 | blue = random.randint(0,255) 12 | return (red, green, blue) 13 | 14 | def random_coordinates(): 15 | x = random.randint(0,640) 16 | y = random.randint(0,480) 17 | return (x,y) 18 | 19 | screen.fill(pygame.color.THECOLORS['black']) 20 | 21 | for i in range(50): 22 | pygame.draw.line(screen,random_color(),random_coordinates(),random_coordinates()) 23 | 24 | pygame.display.flip() #updates the display 25 | 26 | 27 | #closing the window 28 | running = True 29 | while running: 30 | for event in pygame.event.get(): 31 | if event.type == pygame.QUIT: 32 | running = False 33 | pygame.quit() 34 | -------------------------------------------------------------------------------- /Day-5/RATIONAL.md: -------------------------------------------------------------------------------- 1 | 2 | Animation is important to games, so we will start here. Get an image to move across the screen and do something when it gets to the other side (like start over or head back in the same direction). 3 | -------------------------------------------------------------------------------- /Day-5/README.md: -------------------------------------------------------------------------------- 1 | ##PyGame Animation 2 | 3 | Grab the starter file [animation.py](animation.py) and copy it into a new Python window. 4 | 5 | Download the image [beach_ball.png](beach_ball.png) and save it in the same location as your Python file (you can right click the image and select "save image as"). 6 | 7 | Try running it! 8 | 9 | If you receive the error: 10 | 11 | ```python 12 | Traceback (most recent call last): 13 | File "C:/Python25/pygame tester.py", line 16, in 14 | my_ball = pygame.image.load('beach_ball.png') 15 | error: Couldn't open beach_ball.png 16 | ``` 17 | 18 | Then your image file was not saved in the correct location! 19 | 20 | Read through the file to see what it is doing. 21 | 22 | ####Challenge #1 23 | Can you make the beach ball appear lower on the screen? 24 | 25 | ####Challenge #2 26 | Can you make the beach ball go twice as fast? 27 | 28 | ####Challenge #3 29 | Can you switch the beach ball's direction? 30 | 31 | ####Challenge #4 32 | Can you add a second image to the animation? Download a small image from the internet ([look, here is one](minecraft.gif) or do a Google image search) and save it in the same location as your Python file. What new variables do you need to create? 33 | 34 | ####Challenge #5 35 | Can you make one of the images travel in both the x and y direction and bounce off all four walls? 36 | 37 | ####Challenge #6 38 | Can you make the other image travel left and right, bouncing off the walls and doubling in speed each bounce? Increasing the speed by 10% might work better. You also might need to give it a max speed! 39 | -------------------------------------------------------------------------------- /Day-5/animation.py: -------------------------------------------------------------------------------- 1 | # Listing_16-17.py 2 | # Copyright Warren Sande, 2009 3 | # Released under MIT license http://www.opensource.org/licenses/mit-license.php 4 | # Version 61 ---------------------------- 5 | 6 | #Changes by Brian Skinner, Sept-2013 7 | 8 | # move a beach ball image in a pygame window with wrapping 9 | 10 | import pygame, sys 11 | pygame.init() 12 | screen = pygame.display.set_mode([640,480]) 13 | white = [255, 255, 255] 14 | 15 | #load the beach ball image (must be saved in the same place as this file) 16 | my_ball = pygame.image.load('beach_ball.png') 17 | #set variables to use in our program 18 | x = 50 19 | y = 50 20 | x_speed = 5 21 | 22 | running = True 23 | while running: 24 | for event in pygame.event.get(): 25 | if event.type == pygame.QUIT: 26 | running = False 27 | 28 | pygame.time.delay(20) #pause for 20 milliseconds 29 | 30 | screen.fill(white) #make the screen completely white 31 | 32 | #the animation logic 33 | x = x + x_speed 34 | if x > screen.get_width(): 35 | x = 0 36 | 37 | #draw the ball on the screen at the x and y location 38 | screen.blit(my_ball, [x, y]) 39 | #update the entire display 40 | pygame.display.update() 41 | 42 | pygame.quit() 43 | -------------------------------------------------------------------------------- /Day-5/beach_ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Day-5/beach_ball.png -------------------------------------------------------------------------------- /Day-5/finished/animation.py: -------------------------------------------------------------------------------- 1 | # Listing_16-17.py 2 | # Copyright Warren Sande, 2009 3 | # Released under MIT license http://www.opensource.org/licenses/mit-license.php 4 | # Version 61 ---------------------------- 5 | 6 | #Some changes by Brian Skinner, Sept-2013 7 | 8 | # move a beach ball image in a pygame window with wrapping 9 | 10 | import pygame, sys 11 | pygame.init() 12 | screen = pygame.display.set_mode([640,480]) 13 | white = [255, 255, 255] 14 | 15 | #loads the beach ball image (must be saved in the same place as this file) 16 | my_ball = pygame.image.load('beach_ball.png') 17 | my_minecraft = pygame.image.load('minecraft.gif') 18 | #set variables to use in our program 19 | x = 50 20 | y = 250 21 | x_speed = 10 22 | minecraft_x = 100 23 | minecraft_y = 100 24 | y_speed = 5 25 | 26 | while True: 27 | #this checks if you've exited the game 28 | for event in pygame.event.get(): 29 | if event.type == pygame.QUIT: 30 | pygame.quit() 31 | #sys.exit() 32 | 33 | #pause for 20 milliseconds 34 | pygame.time.delay(20) 35 | #make the screen completely white 36 | screen.fill(white) 37 | 38 | #the animation logic 39 | x = x - x_speed 40 | if x < 0: 41 | x = screen.get_width() 42 | minecraft_y = minecraft_y + y_speed 43 | if minecraft_y > screen.get_width(): 44 | minecraft_y = 0 45 | 46 | #draw the ball on the screen at the x and y location 47 | screen.blit(my_ball, [x, y]) 48 | screen.blit(my_minecraft, [minecraft_x, minecraft_y]) 49 | #update the entire display 50 | pygame.display.update() 51 | -------------------------------------------------------------------------------- /Day-5/finished/double-speed-and-bounce-off-all-walls.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | pygame.init() 3 | screen = pygame.display.set_mode([640,480]) 4 | white = [255, 255, 255] 5 | 6 | #load the beach ball image (must be saved in the same place as this file 7 | my_ball = pygame.image.load('beach_ball.png') 8 | minecraft = pygame.image.load('minecraft.gif') 9 | #set variables to use in our program 10 | x = 50 11 | y = 200 12 | x_speed = -1 13 | x_speed_max = 50 14 | x2 = 300 15 | y2 = 300 16 | x2_speed = 5 17 | y2_speed = 5 18 | 19 | running = True; 20 | while running: 21 | for event in pygame.event.get(): 22 | if event.type == pygame.QUIT: 23 | running = False 24 | 25 | #pause for 20 milliseconds 26 | pygame.time.delay(20) 27 | #make the screen completely white 28 | screen.fill(white) 29 | 30 | #the animation logic 31 | x = x + x_speed 32 | #if the ball is on the right side of the screen 33 | if x > screen.get_width() - my_ball.get_width(): 34 | x_speed = -x_speed*2 35 | 36 | #if the ball is on the left side of the screen 37 | if x < 0: 38 | x_speed = -x_speed*2 39 | 40 | #if the ball is going too fast, restart it 41 | if x_speed > x_speed_max: 42 | x_speed = -1 43 | x = 500 44 | 45 | 46 | 47 | x2 = x2 + x2_speed 48 | y2 = y2 + y2_speed 49 | #if minecraft is on the left side of the screen 50 | if x2 < 0: 51 | x2_speed = -x2_speed 52 | #if minecraft is on the right side of the screen 53 | if x2 > screen.get_width() - minecraft.get_width(): 54 | x2_speed = -x2_speed 55 | #if minecraft is on the top of the screen 56 | if y2 < 0: 57 | y2_speed = -y2_speed 58 | #if minecraft is on the bottom of the screen 59 | if y2 > screen.get_height() - minecraft.get_height(): 60 | y2_speed = -y2_speed 61 | 62 | 63 | #draw the ball on the screen at the x and y location 64 | screen.blit(my_ball, [x, y]) 65 | #draw minecraft on the screen at the x2 and y2 location 66 | screen.blit(minecraft, [x2, y2]) 67 | #update the entire display 68 | pygame.display.update() 69 | 70 | pygame.quit() 71 | -------------------------------------------------------------------------------- /Day-5/minecraft.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Day-5/minecraft.gif -------------------------------------------------------------------------------- /Day-6/RATIONAL.md: -------------------------------------------------------------------------------- 1 | We will provide starter code that does a lot of a simple paddle game. It's a one player game with two paddles (like pong) and you have to control both - one using the arrow keys and the mouse. This will show them how to use both type of inputs. They will have to add things like score, speeding up the game every 5 kids, etc. 2 | -------------------------------------------------------------------------------- /Day-6/README.md: -------------------------------------------------------------------------------- 1 | ##Paddle Game 2 | 3 | Let's create our first game - grab the starter file [paddle starter.py](paddle starter.py) and copy it into a new Python window. Run it to see what it does! 4 | 5 | Pretty simple so far, but we'll make improvements and turn it into a game. First, let's read the code to see what it is doing. 6 | 7 | There are two functions provided called doRectsOverlap and isPointInsideRect. These are going to be used to detect if two objects have collided in the game. 8 | 9 | ```python 10 | def doRectsOverlap(rect1, rect2): 11 | for a, b in [(rect1, rect2), (rect2, rect1)]: 12 | # Check if a's corners are inside b 13 | if ((isPointInsideRect(a.left, a.top, b)) or 14 | (isPointInsideRect(a.left, a.bottom, b)) or 15 | (isPointInsideRect(a.right, a.top, b)) or 16 | (isPointInsideRect(a.right, a.bottom, b))): 17 | return True 18 | 19 | return False 20 | 21 | def isPointInsideRect(x, y, rect): 22 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 23 | return True 24 | else: 25 | return False 26 | ``` 27 | 28 | There is also a whole section of code that creates all the variables used by the game. If we want to create more variables or change their starter values, this would be the place. 29 | 30 | ```python 31 | #the game's variables 32 | ball_x = 50 33 | ball_y = 50 34 | ball_radius = 10 35 | ball_color = [222,50,50] 36 | ball_speed = 5 37 | 38 | paddle_x = 200 39 | paddle_y = 400 40 | paddle_width = 60 41 | paddle_height = 20 42 | paddle_color = [20,180,180] 43 | paddle_speed = 20 44 | ``` 45 | 46 | Inside the game loop there are a few things worth pointing out. The first is that the event loop checks for if you have pressed the left or right key. If you did, it will change some game variables. 47 | 48 | ```python 49 | for event in pygame.event.get(): 50 | #check if you've exited the game 51 | if event.type == pygame.QUIT: 52 | running = False 53 | 54 | #check if you pressed a key 55 | if event.type == pygame.KEYDOWN: 56 | if event.key == pygame.K_LEFT: 57 | paddle_x = paddle_x - paddle_speed 58 | if event.key == pygame.K_RIGHT: 59 | paddle_x = paddle_x + paddle_speed 60 | ``` 61 | 62 | The other new piece of code is for detecting collisions between the paddle and ball. First it creates a rectangle (that isn't drawn on the screen) around the ball and paddle, then uses the doRectsOverlap function to find out if they are touching. If they are it will print "YES" to the shell window. 63 | 64 | ```python 65 | #create imaginary rectangles around ball and paddle 66 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 67 | paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) 68 | #see if the rectangles overlap 69 | if doRectsOverlap(ball_rect, paddle_rect): 70 | print "YES" 71 | ``` 72 | 73 | ##Steps to turn it into a game (can be done in any order) 74 | 75 | ####Challenge 1: Make the ball move in both the x and y direction and bounce off the walls 76 | 77 | Refer to your code for the Day 5 animation projects for how to do it. 78 | 79 | ####Challenge 2: Make the ball bounce up when it hits the paddle 80 | 81 | ####Challenge 3: Create a score that increases every time you hit the ball 82 | 83 | Here is some example code. The first line creates a font (this only needs to be done once, so not in the game loop). Then it renders it on the screen and blits it (these should happen in the game loop). 84 | 85 | ```python 86 | myfont = pygame.font.SysFont("Arial", 15) 87 | label = myfont.render("Some text!", 1, pygame.color.THECOLORS['red']) 88 | screen.blit(label, (100, 100)) 89 | ``` 90 | 91 | ####Challenge 4: Make the paddle be controlled by the mouse 92 | 93 | KEYDOWN isn't the only event supported by Pygame. The following code is an example of how to use the MOUSEMOTION event to move your paddle: 94 | 95 | ```python 96 | if event.type == pygame.MOUSEMOTION: 97 | coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates 98 | paddle_x = coordinates[0] #sets the paddle_x variable to the first item in coordinates 99 | ``` 100 | 101 | Check out http://www.pygame.org/docs/ref/event.html for the complete list of Pygame events. Also see the [Key documentation](http://www.pygame.org/docs/ref/key.html) for the complete list of key codes. 102 | 103 | ####Challenge 5: Don't allow the paddle to go off the screen 104 | 105 | How can you add an if so that you don't ever move your paddle off the screen? Or put another way, if you paddle is off the screen, how can you move it back onto the screen? 106 | 107 | ####Challenge 6: Add a speed boost 108 | 109 | How can you increase your ball speed 10% every time you hit it? 110 | 111 | ####Challenge 7: What should happen when the ball hits the bottom of the screen? 112 | 113 | ##Pong 114 | 115 | Pong is a 2 player paddle game where you try to hit the ball past the other player's paddle. Can you turn your game into a 2 player game? You get a point every time you hit it past the other paddle. It will probably work best if you put the paddles moving up and down on the left and right sides of the screen. 116 | 117 | Do 'Save as' so that you can save your pong game as a different file than your first paddle game (otherwise it will write over it). 118 | 119 | To allow for the holding down of keys, add the following line of code before your game loop: 120 | 121 | ```python 122 | pygame.key.set_repeat(20, 20) 123 | ``` 124 | 125 | This will make sure the pygame.KEYDOWN event keeps firing every 20 ms while you are holding the key. 126 | 127 | See [pong finished.py](finished/pong finished.py) for a complete example. 128 | 129 | 130 | ##Bonus Graphics Programs 131 | 132 | There are more ideas fun graphics programs that you can make. Go to https://github.com/CoderDojoSV/beginner-python/tree/master/Extras#more-graphics-programs-to-try and check them out! 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /Day-6/finished/bounce game with speed boost.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | ball_x = 50 30 | ball_y = 50 31 | ball_radius = 10 32 | ball_color = [222,50,50] 33 | ball_speed_x = 3 34 | ball_speed_y = 5 35 | 36 | paddle_x = 200 37 | paddle_y = 440 38 | paddle_width = 60 39 | paddle_height = 20 40 | paddle_color = [20,180,180] 41 | paddle_speed = 20 42 | 43 | myfont = pygame.font.SysFont("Arial", 15) 44 | score = 0 45 | speed_boost = 1.1 46 | 47 | running = True 48 | #game loop 49 | while running: 50 | for event in pygame.event.get(): 51 | #check if you've exited the game 52 | if event.type == pygame.QUIT: 53 | running = False 54 | 55 | if event.type == pygame.MOUSEMOTION: 56 | coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates 57 | paddle_x = coordinates[0] - paddle_width/2 #sets the paddle_x variable to the first item in coordinates 58 | #if the paddle is off the left side bring it back 59 | if paddle_x < 0: 60 | paddle_x = 0 61 | #if the paddle is off the right side bring it back 62 | if paddle_x > screen.get_width() - paddle_width: 63 | paddle_x = screen.get_width() - paddle_width 64 | 65 | #pause for 20 milliseconds 66 | pygame.time.delay(20) 67 | #make the screen completely white 68 | screen.fill(black) 69 | 70 | #move the ball 71 | ball_y = ball_y + ball_speed_y 72 | ball_x = ball_x + ball_speed_x 73 | #check if the ball is off the bottom of the screen 74 | if ball_y > screen.get_height(): 75 | ball_y = 20 76 | score = 0 77 | ball_speed_y = 5 78 | ball_speed_x = 3 79 | #check if the ball hit the top of the screen 80 | if ball_y < ball_radius: 81 | ball_speed_y = -ball_speed_y 82 | #check if the ball hit the left side of the screen 83 | if ball_x < ball_radius: 84 | ball_speed_x = -ball_speed_x 85 | #check if the ball hit the right side of the screen 86 | if ball_x > screen.get_width() - ball_radius: 87 | ball_speed_x = -ball_speed_x 88 | 89 | #create imaginary rectangles around ball and paddle 90 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 91 | paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) 92 | #see if the rectangles overlap 93 | if doRectsOverlap(ball_rect, paddle_rect): 94 | #only bounce when the ball is moving down 95 | if ball_speed_y > 0: 96 | ball_speed_y = -ball_speed_y * speed_boost 97 | ball_speed_x = ball_speed_x * speed_boost 98 | score = score + 1 99 | 100 | 101 | #draw everything on the screen 102 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) 103 | screen.blit(score_label, (10, 10)) 104 | pygame.draw.circle(screen, ball_color, [int(ball_x), int(ball_y)], ball_radius, 0) 105 | pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) 106 | #update the entire display 107 | pygame.display.update() 108 | 109 | 110 | pygame.quit() 111 | -------------------------------------------------------------------------------- /Day-6/finished/bounce game.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | ball_x = 50 30 | ball_y = 50 31 | ball_radius = 10 32 | ball_color = [222,50,50] 33 | ball_speed_x = 3 34 | ball_speed_y = 5 35 | 36 | paddle_x = 200 37 | paddle_y = 440 38 | paddle_width = 60 39 | paddle_height = 20 40 | paddle_color = [20,180,180] 41 | paddle_speed = 20 42 | 43 | myfont = pygame.font.SysFont("Arial", 15) 44 | score = 0 45 | 46 | running = True 47 | #game loop 48 | while running: 49 | for event in pygame.event.get(): 50 | #check if you've exited the game 51 | if event.type == pygame.QUIT: 52 | running = False 53 | 54 | if event.type == pygame.MOUSEMOTION: 55 | coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates 56 | paddle_x = coordinates[0] - paddle_width/2 #sets the paddle_x variable to the first item in coordinates 57 | #if the paddle is off the left side bring it back 58 | if paddle_x < 0: 59 | paddle_x = 0 60 | #if the paddle is off the right side bring it back 61 | if paddle_x > screen.get_width() - paddle_width: 62 | paddle_x = screen.get_width() - paddle_width 63 | 64 | #pause for 20 milliseconds 65 | pygame.time.delay(20) 66 | #make the screen completely white 67 | screen.fill(black) 68 | 69 | #move the ball 70 | ball_y = ball_y + ball_speed_y 71 | ball_x = ball_x + ball_speed_x 72 | #check if the ball is off the bottom of the screen 73 | if ball_y > screen.get_height(): 74 | ball_y = 20 75 | score = 0 76 | #check if the ball hit the top of the screen 77 | if ball_y < ball_radius: 78 | ball_speed_y = -ball_speed_y 79 | #check if the ball hit the left side of the screen 80 | if ball_x < ball_radius: 81 | ball_speed_x = -ball_speed_x 82 | #check if the ball hit the right side of the screen 83 | if ball_x > screen.get_width() - ball_radius: 84 | ball_speed_x = -ball_speed_x 85 | 86 | #create imaginary rectangles around ball and paddle 87 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 88 | paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) 89 | #see if the rectangles overlap 90 | if doRectsOverlap(ball_rect, paddle_rect): 91 | #only bounce when the ball is moving down 92 | if ball_speed_y > 0: 93 | ball_speed_y = -ball_speed_y 94 | score = score + 1 95 | 96 | 97 | #draw everything on the screen 98 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) 99 | screen.blit(score_label, (10, 10)) 100 | pygame.draw.circle(screen, ball_color, [ball_x, ball_y], ball_radius, 0) 101 | pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) 102 | #update the entire display 103 | pygame.display.update() 104 | 105 | 106 | pygame.quit() 107 | -------------------------------------------------------------------------------- /Day-6/finished/pong finished.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | ball_x = 50 30 | ball_y = 50 31 | ball_radius = 10 32 | ball_color = [222,50,50] 33 | ball_speed_x = 3 34 | ball_speed_y = 5 35 | 36 | paddle1_x = 20 37 | paddle1_y = 200 38 | paddle2_x = 600 39 | paddle2_y = 200 40 | paddle_width = 20 41 | paddle_height = 60 42 | paddle_color = [20,180,180] 43 | paddle_speed = 10 44 | 45 | myfont = pygame.font.SysFont("Arial", 15) 46 | score1 = 0 47 | score2 = 0 48 | speed_boost = 1.03 49 | 50 | #allows for holding of key 51 | pygame.key.set_repeat(20, 20) 52 | 53 | running = True 54 | #game loop 55 | while running: 56 | for event in pygame.event.get(): 57 | #check if you've exited the game 58 | if event.type == pygame.QUIT: 59 | running = False 60 | 61 | #check if you pressed a key 62 | if event.type == pygame.KEYDOWN: 63 | if event.key == pygame.K_w: 64 | paddle1_y = paddle1_y - paddle_speed 65 | if paddle1_y < 0: 66 | paddle1_y = 0 67 | if event.key == pygame.K_s: 68 | paddle1_y = paddle1_y + paddle_speed 69 | if paddle1_y > screen.get_height() - paddle_height: 70 | paddle1_y = screen.get_height() - paddle_height 71 | if event.key == pygame.K_o: 72 | paddle2_y = paddle2_y - paddle_speed 73 | if paddle2_y < 0: 74 | paddle2_y = 0 75 | if event.key == pygame.K_l: 76 | paddle2_y = paddle2_y + paddle_speed 77 | if paddle2_y > screen.get_height() - paddle_height: 78 | paddle2_y = screen.get_height() - paddle_height 79 | 80 | 81 | 82 | 83 | #pause for 20 milliseconds 84 | pygame.time.delay(20) 85 | #make the screen completely white 86 | screen.fill(black) 87 | 88 | #move the ball 89 | ball_y = ball_y + ball_speed_y 90 | ball_x = ball_x + ball_speed_x 91 | #check if the ball is off the bottom of the screen 92 | if ball_y > screen.get_height() - ball_radius: 93 | ball_speed_y = -ball_speed_y 94 | #check if the ball hit the top of the screen 95 | if ball_y < ball_radius: 96 | ball_speed_y = -ball_speed_y 97 | #check if the ball hit the left side of the screen 98 | if ball_x < ball_radius: 99 | ball_speed_x = -3 100 | ball_speed_y = 5 101 | score2 = score2 + 1 102 | ball_x = 600 103 | #check if the ball hit the right side of the screen 104 | if ball_x > screen.get_width() - ball_radius: 105 | ball_speed_x = 3 106 | ball_speed_y = 5 107 | score1 = score1 + 1 108 | ball_x = 40 109 | 110 | #create imaginary rectangles around ball and paddle 111 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 112 | paddle1_rect = pygame.Rect(paddle1_x, paddle1_y, paddle_width, paddle_height) 113 | paddle2_rect = pygame.Rect(paddle2_x, paddle2_y, paddle_width, paddle_height) 114 | #see if the rectangles overlap 115 | if doRectsOverlap(ball_rect, paddle1_rect) or doRectsOverlap(ball_rect, paddle2_rect): 116 | ball_speed_x = -ball_speed_x * speed_boost 117 | 118 | 119 | #draw everything on the screen 120 | score1_label = myfont.render(str(score1), 1, pygame.color.THECOLORS['white']) 121 | screen.blit(score1_label, (5, 10)) 122 | score2_label = myfont.render(str(score2), 1, pygame.color.THECOLORS['white']) 123 | screen.blit(score2_label, (620, 10)) 124 | pygame.draw.circle(screen, ball_color, [int(ball_x), int(ball_y)], ball_radius, 0) 125 | pygame.draw.rect(screen, paddle_color, [paddle1_x, paddle1_y, paddle_width, paddle_height], 0) 126 | pygame.draw.rect(screen, paddle_color, [paddle2_x, paddle2_y, paddle_width, paddle_height], 0) 127 | #update the entire display 128 | pygame.display.update() 129 | 130 | 131 | pygame.quit() 132 | -------------------------------------------------------------------------------- /Day-6/paddle starter.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | ball_x = 50 30 | ball_y = 50 31 | ball_radius = 10 32 | ball_color = [222,50,50] 33 | ball_speed_y = 5 34 | 35 | paddle_x = 200 36 | paddle_y = 400 37 | paddle_width = 60 38 | paddle_height = 20 39 | paddle_color = [20,180,180] 40 | paddle_speed = 20 41 | 42 | running = True 43 | #game loop 44 | while running: 45 | for event in pygame.event.get(): 46 | #check if you've exited the game 47 | if event.type == pygame.QUIT: 48 | running = False 49 | 50 | #check if you pressed a key 51 | if event.type == pygame.KEYDOWN: 52 | if event.key == pygame.K_LEFT: 53 | paddle_x = paddle_x - paddle_speed 54 | if event.key == pygame.K_RIGHT: 55 | paddle_x = paddle_x + paddle_speed 56 | 57 | #pause for 20 milliseconds 58 | pygame.time.delay(20) 59 | #make the screen completely white 60 | screen.fill(black) 61 | 62 | #move the ball 63 | ball_y = ball_y + ball_speed_y 64 | #check if the ball is off the bottom of the screen 65 | if ball_y > screen.get_height(): 66 | ball_y = 0 67 | 68 | #create imaginary rectangles around ball and paddle 69 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 70 | paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) 71 | #see if the rectangles overlap 72 | if doRectsOverlap(ball_rect, paddle_rect): 73 | print "YES" 74 | 75 | 76 | #draw everything on the screen 77 | pygame.draw.circle(screen, ball_color, [ball_x, ball_y], ball_radius, 0) 78 | pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) 79 | #update the entire display 80 | pygame.display.update() 81 | 82 | 83 | pygame.quit() 84 | -------------------------------------------------------------------------------- /Day-7/README.md: -------------------------------------------------------------------------------- 1 | ##Snake! 2 | 3 | Snake is a game where you control an always moving character using the keys. The goal is to hit the target without hitting a wall. If you get the target, you get a point and the target appears somewhere else. The game ends when you hit the wall. 4 | 5 | There are two different ways you can make this game. The first the snake speeds up every time you get a target. The second the snake grows every time you get a target. 6 | 7 | - Here is an example of the speeding up version created in Scratch: http://scratch.mit.edu/projects/2627038/ 8 | 9 | - Here is an example of the growing version (please turn off your sound): http://playsnake.org/ 10 | 11 | After creating the paddle game, you should know everything you need to know to create the version that speeds up. So let's make this game today! 12 | 13 | ##Making the Game 14 | 15 | We are going to start with a [blank Pygame project](blank pygame project.py). You will notice that it contains the structure of your past Pygame projects, but no variables or logic to control the game. In fact, nothing is even draw to the screen other than the background! 16 | 17 | The reason we are starting from a blank project is so you will be able to make your own games at home after this final week. After creating Snake, you will have the complete picture and can create other simple games independently. The blank pygame project file is a good play to start for any game you create. 18 | 19 | It is very important to refer back to your previous projects for examples of how to do things. Learn from the code you have already written! 20 | 21 | ###Part 1 - The Snake 22 | 23 | What variables do we need to create for the snake? How can the computer remember its size, position (both x and y), and color? What would be good variable names for these? (Hint: this will go in SECTION 1 of the code.) 24 | 25 | How can you draw the snake on the screen? Look back at your previous projects for an example of how to draw a circle. (Hint: this will go in SECTION 5 of the code.) 26 | 27 | Here is an example line of code that draws a circle using variables: 28 | 29 | ```python 30 | pygame.draw.circle(screen, circl_color, [circle_x, circle_y], circle_size) 31 | ``` 32 | 33 | [Solution](finished/snake part 1.py) 34 | 35 | ###Part 2 - Snake Movement 36 | 37 | The snake is like an animation that either moves up, down, left, or right. How can the computer remember which way the snake is moving and its speed? (Hint: this will go in SECTION 1 of the code.) 38 | 39 | How can you set the direction variable when the arrow keys are hit? (Hint: this will go in SECTION 3 of the code.) Here is an example line of code from the paddle game: 40 | 41 | ```python 42 | if event.key == pygame.K_LEFT: 43 | paddle_x = paddle_x - paddle_speed 44 | ``` 45 | 46 | How can you change the snake's position variables based on your direction variable? (Hint: this will go in SECTION 4 of the code.) You will probably need 4 ifs like this: 47 | 48 | ```python 49 | if direction == "right": 50 | snake_x = snake_x + speed 51 | ``` 52 | 53 | You can also delete SECTION 2 from your code - there are not any mouse controls in this game. 54 | 55 | [Solution](finished/snake part 2.py) 56 | 57 | ###Part 3 - The Target 58 | 59 | What variables do we need to create for the target? How can the computer remember its size, position (both x and y), and color? What would be good variable names for these? (Hint: this will go in SECTION 1 of the code.) 60 | 61 | How can you draw it to the screen? (Hint: this will go in SECTION 5 of the code.) 62 | 63 | [Solution](finished/snake part 3.py) 64 | 65 | ###Part 4 - New Target Location Function 66 | 67 | Let's create a function for setting a new random target location. Creating a function for this will allow us to keep our game loop code nice and neat. 68 | 69 | In SECTION 1 put the following code: 70 | 71 | ```python 72 | def new_target_location(): 73 | global target_x, target_y 74 | target_x = random.randint(30, 610) 75 | target_y = random.randint(30, 450) 76 | ``` 77 | 78 | Any time we want to change one of the game's variables inside of a function, the very first line must tell it which variables will be changed by using the "global" keyword. Otherwise this function would have created a new variable called target_x and target_y that would only exist within the function (not modifying the variable in the game loop). 79 | 80 | [Solution](finished/snake part 4.py) 81 | 82 | ###Part 5 - Detecting Target Collisions 83 | 84 | How can you tell when the snake has hit the target? Refer back to your paddle game to see an example of creating imaginary rectangles around a circle (your ball in the paddle game) and using the provided doRectsOverlap function. How can you call your new target location function when this happens? (Hint: this will go in SECTION 4 of the code.) 85 | 86 | Here is some example code that creates imaginary rectangles around two circles and checks if they are overlapping: 87 | 88 | ```python 89 | circle1_rect = pygame.Rect(circle1_x-circle1_size, circle1_y-circle1_size, circle1_size*2,circle1_size*2) 90 | circle2_rect = pygame.Rect(circle2_x-circle2_size, circle2_y-circle2_size, circle2_size*2,circle2_size*2) 91 | #see if the rectangles overlap 92 | if doRectsOverlap(circle1_rect, circle2_rect): 93 | print "YES" 94 | ``` 95 | 96 | [Solution](finished/snake part 5.py) 97 | 98 | ###Part 6 - Detecting Wall Collisions 99 | 100 | How do you know when the snake has hit the wall? You probably will want to do 4 different ifs to check whether the snake has hit each wall. Even better, how can you create a function called snake_hit_wall that returns True if one of those 4 conditions are met and False if not? (Hint: this will go in SECTION 1 of the code.) 101 | 102 | Here is an example function that only checks for two walls (this number might have to be adjusted): 103 | 104 | ```python 105 | def snake_hit_wall(): 106 | if snake_y < 20: 107 | return True 108 | if snake_x < 20: 109 | return True 110 | 111 | #if the function hasn't already returned True, it should return False 112 | return False 113 | ``` 114 | 115 | How can you use this function to quit the game when the snake hits the wall? (Hint: this will go in SECTION 4 of the code.) 116 | 117 | [Solution](finished/snake part 6.py) 118 | 119 | ###Part 7 - Speeding Up and Score 120 | 121 | How can you change the snake's speed every time it hits the target? (Hint: this will go in SECTION 4 of the code.) 122 | 123 | Can you also create a score variable and display it as a label? (Hint: this will go in SECTIONs 1, 4, and 5 of the code.) Here is some example code for creating a label that displays a variable called score: 124 | 125 | ```python 126 | myfont = pygame.font.SysFont("Arial", 22) 127 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) 128 | screen.blit(score_label, (5, 10)) 129 | ``` 130 | 131 | [Solution](finished/snake part 7.py) 132 | 133 | ###Finishing Touches 134 | 135 | It is a fun game as it is, but there are always ways that you can make it more fun. Do you have any ideas? Here are a few: 136 | 137 | Can you make it so the snake only speeds up every 2 or 3 points instead of every time? This way you can play much longer before hitting the wall. 138 | 139 | Can you clean up your code by creating a function that moves the snake? 140 | 141 | Can you pause for 1 second after you lose so that you can see your score? 142 | 143 | One way to make the game more difficult is to not allow switching to the opposite direction the snake is currently moving. So if the snake is moving right, and you hit the left key, nothing would happen. You would have to hit up or down, then left. Can you add this to your game? 144 | 145 | Can you make the screen flash a different color when you hit the target? 146 | 147 | Can you make a bigger screen size? 148 | 149 | [Solution](finished/snake finished.py) 150 | -------------------------------------------------------------------------------- /Day-7/blank pygame project.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | 31 | 32 | running = True 33 | #game loop 34 | while running: 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | running = False 38 | 39 | if event.type == pygame.MOUSEMOTION: 40 | print "mouse moved" 41 | #SECTION 2 - YOUR CODE HERE FOR WHEN THE MOUSE IS MOVED 42 | 43 | if event.type == pygame.KEYDOWN: 44 | print "key pressed" 45 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 46 | 47 | #pause for 20 milliseconds 48 | pygame.time.delay(20) 49 | #make the screen completely black 50 | screen.fill(black) 51 | 52 | #logic for moving everything in the game and checking collisions 53 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 54 | 55 | #draw everything on the screen 56 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 57 | 58 | #update the entire display 59 | pygame.display.update() 60 | 61 | 62 | pygame.quit() 63 | -------------------------------------------------------------------------------- /Day-7/finished/snake finished.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND VARIABLES 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | target_x = 400 37 | target_y = 400 38 | target_color = (255,0,0) 39 | target_radius = 10 40 | score = 0 41 | myfont = pygame.font.SysFont("Arial", 22) 42 | my_sound = pygame.mixer.Sound('zoop.wav') 43 | 44 | 45 | def new_target_location(): 46 | global target_x, target_y 47 | target_x = random.randint(30, 610) 48 | target_y = random.randint(30, 450) 49 | 50 | def snake_hit_wall(): 51 | if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: 52 | return True 53 | else: 54 | return False 55 | 56 | def move_snake(): 57 | global snake_x, snake_y 58 | if direction == "right": 59 | snake_x = snake_x + speed 60 | if direction == "left": 61 | snake_x = snake_x - speed 62 | if direction == "up": 63 | snake_y = snake_y - speed 64 | if direction == "down": 65 | snake_y = snake_y + speed 66 | 67 | running = True 68 | #game loop 69 | while running: 70 | for event in pygame.event.get(): 71 | if event.type == pygame.QUIT: 72 | running = False 73 | 74 | if event.type == pygame.KEYDOWN: 75 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 76 | if event.key == pygame.K_RIGHT and direction != "left": 77 | direction = "right" 78 | if event.key == pygame.K_LEFT and direction != "right": 79 | direction = "left" 80 | if event.key == pygame.K_UP and direction != "down": 81 | direction = "up" 82 | if event.key == pygame.K_DOWN and direction != "up": 83 | direction = "down" 84 | 85 | #pause for 20 milliseconds 86 | pygame.time.delay(20) 87 | #make the screen completely black 88 | screen.fill(black) 89 | 90 | #logic for moving everything in the game and checking collisions 91 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 92 | move_snake() 93 | 94 | snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) 95 | target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) 96 | if doRectsOverlap(snake_rect, target_rect): 97 | new_target_location() 98 | my_sound.play() 99 | score = score + 1 100 | screen.fill(pygame.color.THECOLORS['white']) 101 | if score % 2 == 0: 102 | speed = speed + 1 103 | 104 | if snake_hit_wall() == True: 105 | running = False 106 | 107 | #draw everything on the screen 108 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 109 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 110 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 111 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) 112 | screen.blit(score_label, (5, 10)) 113 | #update the entire display 114 | pygame.display.update() 115 | 116 | pygame.time.delay(1000) 117 | pygame.quit() 118 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 1.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | 35 | running = True 36 | #game loop 37 | while running: 38 | for event in pygame.event.get(): 39 | if event.type == pygame.QUIT: 40 | running = False 41 | 42 | if event.type == pygame.MOUSEMOTION: 43 | print "mouse moved" 44 | #SECTION 2- YOUR CODE HERE FOR WHEN THE MOUSE IS MOVED 45 | 46 | if event.type == pygame.KEYDOWN: 47 | print "key pressed" 48 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 49 | 50 | #pause for 20 milliseconds 51 | pygame.time.delay(20) 52 | #make the screen completely black 53 | screen.fill(black) 54 | 55 | #logic for moving everything in the game and checking collisions 56 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 57 | 58 | #draw everything on the screen 59 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 60 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 61 | 62 | #update the entire display 63 | pygame.display.update() 64 | 65 | 66 | pygame.quit() 67 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 2.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | 37 | running = True 38 | #game loop 39 | while running: 40 | for event in pygame.event.get(): 41 | if event.type == pygame.QUIT: 42 | running = False 43 | 44 | if event.type == pygame.KEYDOWN: 45 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 46 | if event.key == pygame.K_RIGHT: 47 | direction = "right" 48 | if event.key == pygame.K_LEFT: 49 | direction = "left" 50 | if event.key == pygame.K_UP: 51 | direction = "up" 52 | if event.key == pygame.K_DOWN: 53 | direction = "down" 54 | 55 | #pause for 20 milliseconds 56 | pygame.time.delay(20) 57 | #make the screen completely black 58 | screen.fill(black) 59 | 60 | #logic for moving everything in the game and checking collisions 61 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 62 | if direction == "right": 63 | snake_x = snake_x + speed 64 | if direction == "left": 65 | snake_x = snake_x - speed 66 | if direction == "up": 67 | snake_y = snake_y - speed 68 | if direction == "down": 69 | snake_y = snake_y + speed 70 | 71 | #draw everything on the screen 72 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 73 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 74 | 75 | #update the entire display 76 | pygame.display.update() 77 | 78 | 79 | pygame.quit() 80 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 3.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | target_x = 400 37 | target_y = 400 38 | target_color = (255,0,0) 39 | target_radius = 10 40 | 41 | 42 | running = True 43 | #game loop 44 | while running: 45 | for event in pygame.event.get(): 46 | if event.type == pygame.QUIT: 47 | running = False 48 | 49 | if event.type == pygame.KEYDOWN: 50 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 51 | if event.key == pygame.K_RIGHT: 52 | direction = "right" 53 | if event.key == pygame.K_LEFT: 54 | direction = "left" 55 | if event.key == pygame.K_UP: 56 | direction = "up" 57 | if event.key == pygame.K_DOWN: 58 | direction = "down" 59 | 60 | #pause for 20 milliseconds 61 | pygame.time.delay(20) 62 | #make the screen completely black 63 | screen.fill(black) 64 | 65 | #logic for moving everything in the game and checking collisions 66 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 67 | if direction == "right": 68 | snake_x = snake_x + speed 69 | if direction == "left": 70 | snake_x = snake_x - speed 71 | if direction == "up": 72 | snake_y = snake_y - speed 73 | if direction == "down": 74 | snake_y = snake_y + speed 75 | 76 | #draw everything on the screen 77 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 78 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 79 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 80 | 81 | #update the entire display 82 | pygame.display.update() 83 | 84 | 85 | pygame.quit() 86 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 4.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | target_x = 400 37 | target_y = 400 38 | target_color = (255,0,0) 39 | target_radius = 10 40 | 41 | def new_target_location(): 42 | global target_x, target_y 43 | target_x = random.randint(30, 610) 44 | target_y = random.randint(30, 450) 45 | 46 | running = True 47 | #game loop 48 | while running: 49 | for event in pygame.event.get(): 50 | if event.type == pygame.QUIT: 51 | running = False 52 | 53 | if event.type == pygame.KEYDOWN: 54 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 55 | if event.key == pygame.K_RIGHT: 56 | direction = "right" 57 | if event.key == pygame.K_LEFT: 58 | direction = "left" 59 | if event.key == pygame.K_UP: 60 | direction = "up" 61 | if event.key == pygame.K_DOWN: 62 | direction = "down" 63 | 64 | #pause for 20 milliseconds 65 | pygame.time.delay(20) 66 | #make the screen completely black 67 | screen.fill(black) 68 | 69 | #logic for moving everything in the game and checking collisions 70 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 71 | if direction == "right": 72 | snake_x = snake_x + speed 73 | if direction == "left": 74 | snake_x = snake_x - speed 75 | if direction == "up": 76 | snake_y = snake_y - speed 77 | if direction == "down": 78 | snake_y = snake_y + speed 79 | 80 | #draw everything on the screen 81 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 82 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 83 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 84 | 85 | #update the entire display 86 | pygame.display.update() 87 | 88 | 89 | pygame.quit() 90 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 5.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | target_x = 400 37 | target_y = 400 38 | target_color = (255,0,0) 39 | target_radius = 10 40 | 41 | def new_target_location(): 42 | global target_x, target_y 43 | target_x = random.randint(30, 610) 44 | target_y = random.randint(30, 450) 45 | 46 | 47 | running = True 48 | #game loop 49 | while running: 50 | for event in pygame.event.get(): 51 | if event.type == pygame.QUIT: 52 | running = False 53 | 54 | if event.type == pygame.KEYDOWN: 55 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 56 | if event.key == pygame.K_RIGHT: 57 | direction = "right" 58 | if event.key == pygame.K_LEFT: 59 | direction = "left" 60 | if event.key == pygame.K_UP: 61 | direction = "up" 62 | if event.key == pygame.K_DOWN: 63 | direction = "down" 64 | 65 | #pause for 20 milliseconds 66 | pygame.time.delay(20) 67 | #make the screen completely black 68 | screen.fill(black) 69 | 70 | #logic for moving everything in the game and checking collisions 71 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 72 | if direction == "right": 73 | snake_x = snake_x + speed 74 | if direction == "left": 75 | snake_x = snake_x - speed 76 | if direction == "up": 77 | snake_y = snake_y - speed 78 | if direction == "down": 79 | snake_y = snake_y + speed 80 | 81 | snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) 82 | target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) 83 | if doRectsOverlap(snake_rect, target_rect): 84 | new_target_location() 85 | 86 | #draw everything on the screen 87 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 88 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 89 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 90 | 91 | #update the entire display 92 | pygame.display.update() 93 | 94 | 95 | pygame.quit() 96 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 6.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | target_x = 400 37 | target_y = 400 38 | target_color = (255,0,0) 39 | target_radius = 10 40 | 41 | def new_target_location(): 42 | global target_x, target_y 43 | target_x = random.randint(30, 610) 44 | target_y = random.randint(30, 450) 45 | 46 | def snake_hit_wall(): 47 | if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: 48 | return True 49 | else: 50 | return False 51 | 52 | running = True 53 | #game loop 54 | while running: 55 | for event in pygame.event.get(): 56 | if event.type == pygame.QUIT: 57 | running = False 58 | 59 | if event.type == pygame.KEYDOWN: 60 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 61 | if event.key == pygame.K_RIGHT: 62 | direction = "right" 63 | if event.key == pygame.K_LEFT: 64 | direction = "left" 65 | if event.key == pygame.K_UP: 66 | direction = "up" 67 | if event.key == pygame.K_DOWN: 68 | direction = "down" 69 | 70 | #pause for 20 milliseconds 71 | pygame.time.delay(20) 72 | #make the screen completely black 73 | screen.fill(black) 74 | 75 | #logic for moving everything in the game and checking collisions 76 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 77 | if direction == "right": 78 | snake_x = snake_x + speed 79 | if direction == "left": 80 | snake_x = snake_x - speed 81 | if direction == "up": 82 | snake_y = snake_y - speed 83 | if direction == "down": 84 | snake_y = snake_y + speed 85 | 86 | snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) 87 | target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) 88 | if doRectsOverlap(snake_rect, target_rect): 89 | new_target_location() 90 | 91 | if snake_hit_wall() == True: 92 | running = False 93 | 94 | #draw everything on the screen 95 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 96 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 97 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 98 | 99 | #update the entire display 100 | pygame.display.update() 101 | 102 | 103 | pygame.quit() 104 | -------------------------------------------------------------------------------- /Day-7/finished/snake part 7.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | #SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS 30 | snake_x = 100 31 | snake_y = 100 32 | snake_color = (0,233,0) 33 | snake_radius = 25 34 | direction = "right" 35 | speed = 2 36 | target_x = 400 37 | target_y = 400 38 | target_color = (255,0,0) 39 | target_radius = 10 40 | score = 0 41 | myfont = pygame.font.SysFont("Arial", 22) 42 | 43 | def new_target_location(): 44 | global target_x, target_y 45 | target_x = random.randint(30, 610) 46 | target_y = random.randint(30, 450) 47 | 48 | def snake_hit_wall(): 49 | if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: 50 | return True 51 | else: 52 | return False 53 | 54 | running = True 55 | #game loop 56 | while running: 57 | for event in pygame.event.get(): 58 | if event.type == pygame.QUIT: 59 | running = False 60 | 61 | if event.type == pygame.KEYDOWN: 62 | #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED 63 | if event.key == pygame.K_RIGHT: 64 | direction = "right" 65 | if event.key == pygame.K_LEFT: 66 | direction = "left" 67 | if event.key == pygame.K_UP: 68 | direction = "up" 69 | if event.key == pygame.K_DOWN: 70 | direction = "down" 71 | 72 | #pause for 20 milliseconds 73 | pygame.time.delay(20) 74 | #make the screen completely black 75 | screen.fill(black) 76 | 77 | #logic for moving everything in the game and checking collisions 78 | #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS 79 | if direction == "right": 80 | snake_x = snake_x + speed 81 | if direction == "left": 82 | snake_x = snake_x - speed 83 | if direction == "up": 84 | snake_y = snake_y - speed 85 | if direction == "down": 86 | snake_y = snake_y + speed 87 | 88 | snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) 89 | target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) 90 | if doRectsOverlap(snake_rect, target_rect): 91 | new_target_location() 92 | speed = speed + 1 93 | score = score + 1 94 | 95 | if snake_hit_wall() == True: 96 | running = False 97 | 98 | #draw everything on the screen 99 | #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING 100 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 101 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 102 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) 103 | screen.blit(score_label, (5, 10)) 104 | #update the entire display 105 | pygame.display.update() 106 | 107 | 108 | pygame.quit() 109 | -------------------------------------------------------------------------------- /Day-7/finished/zoop.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Day-7/finished/zoop.wav -------------------------------------------------------------------------------- /Day-7/snake almost done.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | def new_target_location(): 24 | global target_x, target_y 25 | target_x = random.randint(30, 610) 26 | target_y = random.randint(30, 450) 27 | 28 | def snake_hit_wall(): 29 | if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: 30 | return True 31 | else: 32 | return False 33 | 34 | 35 | import pygame, sys, random 36 | pygame.init() 37 | screen = pygame.display.set_mode([640,480]) 38 | black = [0, 0, 0] 39 | 40 | 41 | #the game's variables 42 | snake_x = 100 43 | snake_y = 100 44 | snake_color = (0,233,0) 45 | snake_radius = 25 46 | direction = "right" 47 | speed = 2 48 | target_x = 400 49 | target_y = 400 50 | target_color = (255,0,0) 51 | target_radius = 10 52 | 53 | 54 | 55 | running = True 56 | #game loop 57 | while running: 58 | for event in pygame.event.get(): 59 | if event.type == pygame.QUIT: 60 | running = False 61 | 62 | if event.type == pygame.KEYDOWN: 63 | if event.key == pygame.K_RIGHT: 64 | direction = "right" 65 | if event.key == pygame.K_LEFT: 66 | direction = "left" 67 | if event.key == pygame.K_UP: 68 | direction = "up" 69 | #YOUR CODE HERE FOR WHEN THE DOWN KEY IS PRESSED 70 | 71 | 72 | #pause for 20 milliseconds 73 | pygame.time.delay(20) 74 | #make the screen completely black 75 | screen.fill(black) 76 | 77 | #logic for moving everything in the game and checking collisions 78 | if direction == "right": 79 | snake_x = snake_x + speed 80 | if direction == "left": 81 | snake_x = snake_x - speed 82 | if direction == "up": 83 | snake_y = snake_y - speed 84 | #YOUR CODE HERE FOR WHEN THE DIRECTION IS DOWN 85 | 86 | 87 | #check for collisions 88 | snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) 89 | target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) 90 | if doRectsOverlap(snake_rect, target_rect): 91 | new_target_location() 92 | 93 | if snake_hit_wall() == True: 94 | running = False 95 | 96 | #draw everything on the screen 97 | pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) 98 | pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) 99 | 100 | #update the entire display 101 | pygame.display.update() 102 | 103 | 104 | pygame.quit() 105 | -------------------------------------------------------------------------------- /Extras/Finished/breakout.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | pygame.init() 3 | screen = pygame.display.set_mode([640,480]) 4 | black = [0, 0, 0] 5 | 6 | class Brick(pygame.sprite.Sprite): 7 | image = None 8 | 9 | def __init__(self, x, y): 10 | pygame.sprite.Sprite.__init__(self) 11 | 12 | if Brick.image is None: 13 | # This is the first time this class has been 14 | # instantiated. So, load the image 15 | Brick.image = pygame.image.load("brick.png") 16 | self.image = Brick.image 17 | 18 | # Make our top-left corner the passed-in location. 19 | self.rect = self.image.get_rect() 20 | self.x = x 21 | self.y = y 22 | self.rect.topleft = (self.x, self.y) 23 | 24 | 25 | #the game's variables 26 | ball_x = 50 27 | ball_y = 100 28 | ball_radius = 10 29 | ball_color = [222,50,50] 30 | ball_speed_x = 3 31 | ball_speed_y = 5 32 | 33 | paddle_x = 20 34 | paddle_y = 450 35 | paddle_width = 60 36 | paddle_height = 20 37 | paddle_color = [20,180,180] 38 | paddle_speed = 10 39 | 40 | myfont = pygame.font.SysFont("Arial", 22) 41 | score = 0 42 | 43 | brick_array = [] 44 | for i in range(1,8): 45 | brick1 = Brick(75*i,50) 46 | brick_array.append(brick1) 47 | 48 | 49 | #allows for holding of key 50 | pygame.key.set_repeat(20, 20) 51 | 52 | running = True 53 | #game loop 54 | while running: 55 | for event in pygame.event.get(): 56 | #check if you've exited the game 57 | if event.type == pygame.QUIT: 58 | running = False 59 | 60 | if event.type == pygame.MOUSEMOTION: 61 | coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates 62 | paddle_x = coordinates[0] - paddle_width/2 #sets the paddle_x variable to the first item in coordinates 63 | if paddle_x < 0: 64 | paddle_x = 0 65 | if paddle_x > screen.get_width() - paddle_width: 66 | paddle_x = screen.get_width() - paddle_width 67 | 68 | 69 | 70 | #pause for 20 milliseconds 71 | pygame.time.delay(20) 72 | #make the screen completely white 73 | screen.fill(black) 74 | 75 | #move the ball 76 | ball_y = ball_y + ball_speed_y 77 | ball_x = ball_x + ball_speed_x 78 | #check if the ball is off the bottom of the screen 79 | if ball_y > screen.get_height() - ball_radius: 80 | ball_speed_y = -ball_speed_y 81 | #do something different 82 | #check if the ball hit the top of the screen 83 | if ball_y < ball_radius: 84 | ball_speed_y = -ball_speed_y 85 | #check if the ball hit the left side of the screen 86 | if ball_x < ball_radius: 87 | ball_speed_x = -ball_speed_x 88 | #check if the ball hit the right side of the screen 89 | if ball_x > screen.get_width() - ball_radius: 90 | ball_speed_x = -ball_speed_x 91 | 92 | #create imaginary rectangles around ball and paddle 93 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 94 | paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) 95 | #see if the rectangles overlap 96 | if ball_rect.colliderect(paddle_rect): 97 | ball_speed_y = -ball_speed_y 98 | 99 | for brick in brick_array: 100 | if brick.rect.colliderect(ball_rect): 101 | score = score + 1 102 | brick_array.remove(brick) 103 | ball_speed_y = - ball_speed_y 104 | 105 | 106 | #draw everything on the screen 107 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) 108 | screen.blit(score_label, (5, 10)) 109 | for brick in brick_array: 110 | screen.blit(brick.image, brick.rect) 111 | pygame.draw.circle(screen, ball_color, [int(ball_x), int(ball_y)], ball_radius, 0) 112 | pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) 113 | #update the entire display 114 | pygame.display.update() 115 | 116 | 117 | pygame.quit() 118 | -------------------------------------------------------------------------------- /Extras/Finished/brick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Extras/Finished/brick.png -------------------------------------------------------------------------------- /Extras/Finished/catch the good ones.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | pygame.init() 3 | screen = pygame.display.set_mode([640,480]) 4 | white = [255, 255, 255] 5 | 6 | paddle_x = 20 7 | paddle_y = 440 8 | paddle_width = 60 9 | paddle_height = 20 10 | paddle_color = [20,180,180] 11 | 12 | myfont = pygame.font.SysFont("Arial", 15) 13 | score = 0 14 | 15 | class BallSprite(pygame.sprite.Sprite): 16 | image_red = None 17 | image_green = None 18 | 19 | def __init__(self, color_input): 20 | pygame.sprite.Sprite.__init__(self) 21 | 22 | if color_input == "red": 23 | if BallSprite.image_red is None: 24 | # This is the first time this class has been 25 | # instantiated. So, load the image for this and 26 | # all subsequence red instances. 27 | BallSprite.image_red = pygame.image.load("red_ball.png") 28 | self.image = BallSprite.image_red 29 | else: 30 | if BallSprite.image_green is None: 31 | # This is the first time this class has been 32 | # instantiated. So, load the image for this and 33 | # all subsequence green instances. 34 | BallSprite.image_green = pygame.image.load("green_ball.png") 35 | self.image = BallSprite.image_green 36 | self.color = color_input 37 | 38 | # Make our top-left corner the passed-in location. 39 | self.rect = self.image.get_rect() 40 | self.x = random.randint(0,592) 41 | self.y = 0 42 | self.rect.topleft = (self.x, self.y) 43 | 44 | def move_ball(self): 45 | self.y = self.y + 8 46 | self.rect.topleft = (self.x, self.y) 47 | 48 | 49 | red_ball = BallSprite("red") 50 | green_ball = BallSprite("green") 51 | ball_list = [red_ball, green_ball] 52 | 53 | 54 | running = True 55 | #game loop 56 | while running: 57 | for event in pygame.event.get(): 58 | #check if you've exited the game 59 | if event.type == pygame.QUIT: 60 | running = False 61 | 62 | if event.type == pygame.MOUSEMOTION: 63 | coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates 64 | paddle_x = coordinates[0] - paddle_width/2 #sets the paddle_x variable to the first item in coordinates 65 | if paddle_x < 0: 66 | paddle_x = 0 67 | if paddle_x > screen.get_width() - paddle_width: 68 | paddle_x = screen.get_width() - paddle_width 69 | 70 | #pause for 20 milliseconds 71 | pygame.time.delay(20) 72 | #make the screen completely white 73 | screen.fill(white) 74 | 75 | #create a new ball at random 76 | if random.randint(0,25) == 1: 77 | if random.randint(0,1) == 0: 78 | new_ball = BallSprite("red") 79 | else: 80 | new_ball = BallSprite("green") 81 | ball_list.append(new_ball) 82 | 83 | #create imaginary rect around the paddle 84 | paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) 85 | 86 | #loop over all the balls in the list 87 | for ball in ball_list: 88 | ball.move_ball() 89 | #remove the ball if it is off the screen 90 | if ball.y > screen.get_height(): 91 | if ball.color == "green": 92 | score = score - 1 93 | ball_list.remove(ball) 94 | #check if the ball hit the paddle 95 | if ball.rect.colliderect(paddle_rect): 96 | if ball.color == "red": 97 | score = score -10 98 | else: 99 | score = score + 1 100 | ball_list.remove(ball) 101 | 102 | 103 | #draw everything on the screen 104 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['black']) 105 | screen.blit(score_label, (5, 10)) 106 | pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) 107 | for ball in ball_list: 108 | screen.blit(ball.image, ball.rect) 109 | #update the entire display 110 | pygame.display.update() 111 | 112 | 113 | pygame.quit() 114 | -------------------------------------------------------------------------------- /Extras/Finished/green_ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Extras/Finished/green_ball.png -------------------------------------------------------------------------------- /Extras/Finished/hailstone.py: -------------------------------------------------------------------------------- 1 | print "This is a cool number pattern!" 2 | print "For any positive whole number, it will always end in one." 3 | print "If the number is even, it will divide by two." 4 | print "If the number is odd, it will multiply by three and add one." 5 | print "It repeats these steps until the number is one!" 6 | 7 | number = input("Please enter a number: ") 8 | steps = 0 9 | 10 | while number != 1: 11 | if number % 2 == 0: 12 | number = number / 2 13 | else: 14 | number = number*3 + 1 15 | steps = steps + 1 16 | print number 17 | 18 | print "It took", steps, "steps to reach one!" 19 | -------------------------------------------------------------------------------- /Extras/Finished/pong finished with function.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,480]) 26 | black = [0, 0, 0] 27 | 28 | #the game's variables 29 | ball_x = 50 30 | ball_y = 50 31 | ball_radius = 10 32 | ball_color = [222,50,50] 33 | ball_speed_x = 3 34 | ball_speed_y = 5 35 | 36 | paddle1_x = 20 37 | paddle1_y = 200 38 | paddle2_x = 600 39 | paddle2_y = 200 40 | paddle_width = 20 41 | paddle_height = 60 42 | paddle_color = [20,180,180] 43 | paddle_speed = 10 44 | 45 | myfont = pygame.font.SysFont("Arial", 15) 46 | score1 = 0 47 | score2 = 0 48 | speed_boost = 1.03 49 | 50 | #allows for holding of key 51 | pygame.key.set_repeat(20, 20) 52 | 53 | def reset_ball(side): 54 | global ball_x 55 | global ball_speed_x 56 | global ball_speed_y 57 | global score1 58 | global score2 59 | 60 | ball_speed_y = 5 61 | if side == "left": 62 | ball_speed_x = -3 63 | score2 = score2 + 1 64 | ball_x = 600 65 | else: 66 | ball_speed_x = 3 67 | score1 = score1 + 1 68 | ball_x = 40 69 | 70 | running = True 71 | #game loop 72 | while running: 73 | for event in pygame.event.get(): 74 | #check if you've exited the game 75 | if event.type == pygame.QUIT: 76 | running = False 77 | 78 | #check if you pressed a key 79 | if event.type == pygame.KEYDOWN: 80 | if event.key == pygame.K_w: 81 | paddle1_y = paddle1_y - paddle_speed 82 | if paddle1_y < 0: 83 | paddle1_y = 0 84 | if event.key == pygame.K_s: 85 | paddle1_y = paddle1_y + paddle_speed 86 | if paddle1_y > screen.get_height() - paddle_height: 87 | paddle1_y = screen.get_height() - paddle_height 88 | if event.key == pygame.K_o: 89 | paddle2_y = paddle2_y - paddle_speed 90 | if paddle2_y < 0: 91 | paddle2_y = 0 92 | if event.key == pygame.K_l: 93 | paddle2_y = paddle2_y + paddle_speed 94 | if paddle2_y > screen.get_height() - paddle_height: 95 | paddle2_y = screen.get_height() - paddle_height 96 | 97 | 98 | #pause for 20 milliseconds 99 | pygame.time.delay(20) 100 | #make the screen completely white 101 | screen.fill(black) 102 | 103 | #move the ball 104 | ball_y = ball_y + ball_speed_y 105 | ball_x = ball_x + ball_speed_x 106 | #check if the ball is off the bottom of the screen 107 | if ball_y > screen.get_height() - ball_radius: 108 | ball_speed_y = -ball_speed_y 109 | #check if the ball hit the top of the screen 110 | if ball_y < ball_radius: 111 | ball_speed_y = -ball_speed_y 112 | #check if the ball hit the left side of the screen 113 | if ball_x < ball_radius: 114 | reset_ball("left") 115 | #check if the ball hit the right side of the screen 116 | if ball_x > screen.get_width() - ball_radius: 117 | reset_ball("right") 118 | 119 | #create imaginary rectangles around ball and paddle 120 | ball_rect = pygame.Rect(ball_x-ball_radius, ball_y-ball_radius, ball_radius*2,ball_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 121 | paddle1_rect = pygame.Rect(paddle1_x, paddle1_y, paddle_width, paddle_height) 122 | paddle2_rect = pygame.Rect(paddle2_x, paddle2_y, paddle_width, paddle_height) 123 | #see if the rectangles overlap 124 | if doRectsOverlap(ball_rect, paddle1_rect) or doRectsOverlap(ball_rect, paddle2_rect): 125 | ball_speed_x = -ball_speed_x * speed_boost 126 | 127 | 128 | #draw everything on the screen 129 | score1_label = myfont.render(str(score1), 1, pygame.color.THECOLORS['white']) 130 | screen.blit(score1_label, (5, 10)) 131 | score2_label = myfont.render(str(score2), 1, pygame.color.THECOLORS['white']) 132 | screen.blit(score2_label, (620, 10)) 133 | pygame.draw.circle(screen, ball_color, [int(ball_x), int(ball_y)], ball_radius, 0) 134 | pygame.draw.rect(screen, paddle_color, [paddle1_x, paddle1_y, paddle_width, paddle_height], 0) 135 | pygame.draw.rect(screen, paddle_color, [paddle2_x, paddle2_y, paddle_width, paddle_height], 0) 136 | #update the entire display 137 | pygame.display.update() 138 | 139 | 140 | pygame.quit() 141 | -------------------------------------------------------------------------------- /Extras/Finished/red_ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderDojoSV/beginner-python/5542c126df18a36b68beae9a4438d660e9812185/Extras/Finished/red_ball.png -------------------------------------------------------------------------------- /Extras/Finished/snake.py: -------------------------------------------------------------------------------- 1 | #Credit the Invent With Python book (http://inventwithpython.com) 2 | #for doRectsOverlap and isPointInsideRect functions 3 | 4 | #used to detect collisions in our game 5 | def doRectsOverlap(rect1, rect2): 6 | for a, b in [(rect1, rect2), (rect2, rect1)]: 7 | # Check if a's corners are inside b 8 | if ((isPointInsideRect(a.left, a.top, b)) or 9 | (isPointInsideRect(a.left, a.bottom, b)) or 10 | (isPointInsideRect(a.right, a.top, b)) or 11 | (isPointInsideRect(a.right, a.bottom, b))): 12 | return True 13 | 14 | return False 15 | 16 | #used the by the doRectsOverlap function (won't be called directly from game code) 17 | def isPointInsideRect(x, y, rect): 18 | if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): 19 | return True 20 | else: 21 | return False 22 | 23 | import pygame, sys, random 24 | pygame.init() 25 | screen = pygame.display.set_mode([640,640]) 26 | white = [255, 255, 255] 27 | 28 | #the game's variables 29 | target_x = 50 30 | target_y = 50 31 | target_radius = 10 32 | target_color = [222,50,50] 33 | 34 | snake_x = 50 35 | snake_y = 300 36 | snake_radius = 25 37 | snake_color = [20,180,180] 38 | snake_speed = 5 39 | direction = "right" 40 | 41 | myfont = pygame.font.SysFont("Arial", 15) 42 | score = 0 43 | speed_boost = 1.03 44 | 45 | 46 | 47 | running = True 48 | #game loop 49 | while running: 50 | for event in pygame.event.get(): 51 | #check if you've exited the game 52 | if event.type == pygame.QUIT: 53 | running = False 54 | 55 | #check if you pressed a key 56 | if event.type == pygame.KEYDOWN: 57 | if event.key == pygame.K_UP: 58 | direction = "up" 59 | if event.key == pygame.K_DOWN: 60 | direction = "down" 61 | if event.key == pygame.K_RIGHT: 62 | direction = "right" 63 | if event.key == pygame.K_LEFT: 64 | direction = "left" 65 | 66 | 67 | #pause for 20 milliseconds 68 | pygame.time.delay(20) 69 | #make the screen completely white 70 | screen.fill(white) 71 | 72 | #move the snake 73 | if direction == "up": 74 | snake_y = snake_y - snake_speed 75 | if direction == "down": 76 | snake_y = snake_y + snake_speed 77 | if direction == "left": 78 | snake_x = snake_x - snake_speed 79 | if direction == "right": 80 | snake_x = snake_x + snake_speed 81 | 82 | #check if the snake is off the bottom of the screen 83 | if snake_y > screen.get_height() - snake_radius: 84 | running = False 85 | #check if the snake hit the top of the screen 86 | if snake_y < snake_radius: 87 | running = False 88 | #check if the snake hit the left side of the screen 89 | if snake_x < snake_radius: 90 | running = False 91 | #check if the snake hit the right side of the screen 92 | if snake_x > screen.get_width() - snake_radius: 93 | running = False 94 | 95 | #create imaginary rectangles around ball and paddle 96 | snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 97 | target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) #circles are measured from the center, so have to subtract 1 radius from the x and y 98 | #see if the rectangles overlap 99 | if doRectsOverlap(snake_rect, target_rect): 100 | snake_speed = snake_speed * speed_boost 101 | target_x = random.randint(0,620) 102 | target_y = random.randint(0,620) 103 | score = score + 1 104 | 105 | #draw everything on the screen 106 | score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['black']) 107 | screen.blit(score_label, (5, 10)) 108 | pygame.draw.circle(screen, target_color, [target_x, target_y], target_radius, 0) 109 | pygame.draw.circle(screen, snake_color, [int(snake_x), int(snake_y)], snake_radius, 0) 110 | #update the entire display 111 | pygame.display.update() 112 | 113 | 114 | pygame.quit() 115 | -------------------------------------------------------------------------------- /Extras/README.md: -------------------------------------------------------------------------------- 1 | # More Text-Based Programs To Try 2 | 3 | Days 1, 2, and 3 have focus on text-based games and programs. Here are some more fun text-based programs to try. 4 | 5 | ### Hailstone Pattern 6 | 7 | The hailstone pattern is a cool number pattern that for any positive whole number, it will always end in one. It repeats the following steps until it gets to one: if the number is even, it will divide by two. If the number is odd, it will multiply by three and add one. 8 | 9 | Here is an example that starts with the number 7: 10 | 11 | ``` 12 | 7 13 | 22 14 | 11 15 | 34 16 | 17 17 | 52 18 | 26 19 | 13 20 | 40 21 | 20 22 | 10 23 | 5 24 | 16 25 | 8 26 | 4 27 | 2 28 | 1 29 | ``` 30 | 31 | It took 16 steps! 32 | 33 | How can you make this into a program? It should ask the user to enter a number, output the number at every step, and count how many steps it took to finish. 34 | 35 | This will require one thing that you likely haven't seen before: mod (%). Mod can be used to determine if a number is even or odd. Mod is similar to division, but just worries about the remainder. So if you do 7 mod 2, it is asking what the remainder is when you divide 7 by 2. The answer is 1. This is how you can check if a variable called 'number' is even: 36 | 37 | ```python 38 | if number % 2 == 0: 39 | ``` 40 | 41 | What happens when you enter a really big number? How many steps does it take? 42 | 43 | Refer to the finished folder for a complete example. 44 | 45 | ### Tic-Tac-Toe 46 | 47 | The San Francisco CoderDojo has done a really fun Tic-Tac-Toe Python project that you can go through. Head to https://github.com/CoderDojoSF/tic-tac-toe and follow the links for Lesson 1 and Lesson 2! 48 | 49 | ### Dragon Realm - Invent With Python 50 | 51 | [Invent With Python](http://inventwithpython.com/) is a free online Python book with some great projects. 52 | 53 | Chapter 6 (page 84) in the book shows how to create a text-adventure game called Dragon Realm. [Here is the online pdf version of the book](http://inventwithpython.com/IYOCGwP_book1.pdf). 54 | 55 | ### Invent With Python - Hangman 56 | 57 | [Invent With Python](http://inventwithpython.com/) is a free online Python book with some great projects. 58 | 59 | Chapter 9 (page 132) in the book shows how to create the letter and word guessing game Hangman. [Here is the online pdf version of the book](http://inventwithpython.com/IYOCGwP_book1.pdf). 60 | 61 | 62 | ### Bagels - Invent With Python 63 | 64 | [Invent With Python](http://inventwithpython.com/) is a free online Python book with some great projects. 65 | 66 | Chapter 11 (page 217) in the book shows how to create a number guessing game called Bagels. Here is the description of the game from the book: 67 | 68 | >Bagels is a simple game you can play with a friend. Your friend thinks up a random 3-digit number with no repeating digits,and you try to guess what the number is. After each guess, your friend gives you clues on how close your guess was. If the friend tells you “bagels”, that means that none of the three digits you guessed is in the secret number. If your friend tells you “pico”, then one of the digits is in the secret number, but your guess has the digit in the wrong place. If your friend tells you “fermi”, then your guess has a correct digit in the correct place. Of course, even if you get a pico or fermi clue, you still don't know which digit in your guess is the correct one. 69 | 70 | >You can also get multiple clues after each guess. Say the secret number is 456, and your guess is 546. The clue you get from your friend would be “fermi pico pico” because one digit is correct and in the correct place (the digit 6), and two digits are in the secret number but in the wrong place (the digits 4 and 5). 71 | 72 | [Here is the online pdf version of the book](http://inventwithpython.com/IYOCGwP_book1.pdf). 73 | 74 | 75 | 76 | ## More Graphics Programs to Try 77 | 78 | ### Snake 79 | 80 | Snake is a game where you control an always moving character using the keys. The goal is to hit the target without hitting a wall. If you get the target, your character starts moving faster and the target appears somewhere else. The game ends when you hit the wall. 81 | 82 | Here is an example of the game created in Scratch: http://scratch.mit.edu/projects/2627038/ 83 | 84 | Refer to the finished folder for a complete example. 85 | 86 | ### Adding Complexity - Functions that Set Variables 87 | 88 | If you want to make more complex games, there are some additional concepts that you can put to use. The first is using functions (which you have created before) to change variables. 89 | 90 | In order for a function to change a variable, you must use the 'global' keyword to let Python know you are using the variable already created outside of the function rather than creating a new one. The [pong finished with function.py](Finished/pong finished with function.py) example uses the following function to reset the ball: 91 | 92 | ```python 93 | def reset_ball(side): 94 | global ball_x 95 | global ball_speed_x 96 | global ball_speed_y 97 | global score1 98 | global score2 99 | 100 | ball_speed_y = 5 101 | if side == "left": 102 | ball_speed_x = -3 103 | score2 = score2 + 1 104 | ball_x = 600 105 | else: 106 | ball_speed_x = 3 107 | score1 = score1 + 1 108 | ball_x = 40 109 | ``` 110 | 111 | Functions are a powerful way to start creating more impressive games. 112 | 113 | ### Adding Complexity - Classes and Sprites in Pygame 114 | 115 | For the 6 day track we didn't cover using sprites in Pygame because it involves a new concept: classes. A class can be thought of as a blue-print that you can use to create many copies of something. Each copy can have their own set of variables (called properties). In pong there were already a lot of variables to keep track of, what if the game had many more objects in it! 116 | 117 | Take a look at [catch the good ones.py](Finished/catch the good ones.py) for an example of a simple game that uses a class. In this game your goal is to catch all the falling green balls and avoid the red ones. (You must also download the red_ball.png and green_ball.png files in the [Finished](Finished/) folder.) 118 | 119 | There are many sources to learn more about Pygame sprites and creating Python classes. If you are learning on your own, check out a book like [Hello World! Computer Programming for Kids and Other Beginners](http://www.amazon.com/Hello-World-Computer-Programming-Beginners/dp/1933988495). 120 | 121 | ### Breakout 122 | 123 | Take a look at [breakout.py](Finished/breakout.py) for another game that uses a class. (You must also download the brick.png file in the [Finished](Finished/) folder.) 124 | 125 | How can you improve this starter game? 126 | -------------------------------------------------------------------------------- /RATIONAL.md: -------------------------------------------------------------------------------- 1 | This course is designed to support kids new to Python and typed programming. The typical student will be 10 with limited typing skills (generally knows the layout of the keys, but can't do much typing without looking at the keys). 2 | 3 | The goal is to limit the time spent lecturing and the typing required. This will be done by providing completed example programs that will be edited. 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Run on Repl.it](https://repl.it/badge/github/CoderDojoSV/beginner-python)](https://repl.it/github/CoderDojoSV/beginner-python) 2 | 3 | #Introduction to Python 4 | 5 | *Learn Python to make text and 2-D games with a typed programming language!* 6 | 7 | ##Is this course right for you? 8 | 9 | If you are curious about typed programming and have very little to no previous experience, this is a great fit for you. Scratch or other drag-and-drop experience helps, but is not necessary. Typing is required and limited as much as possible - if you are intimidated by typing a few sentences, this course might not be a good fit. Suggested age: 10 to 13. 10 | 11 | The only real requirements are curiousity and enthusiasm! 12 | 13 | ##Description 14 | 15 | This course is inspired by the material from the book [Hello World! Computer Programming for Kids and Other Beginners](http://www.amazon.com/Hello-World-Computer-Programming-Beginners/dp/1933988495). The authors of the book will be some of your mentors! 16 | 17 | All materials necessary for the course will be provided. If you are interested in more projects after the 6 weeks are over, grab a copy of the book. 18 | 19 | [:rocket: Day 1][0]: Intro, Number Guessing Game, Multiplication Game 20 | 21 | [:rocket: Day 2][1]: Silly Sentence Generator and MadLibs 22 | 23 | [:rocket: Day 3][2]: Simulating Coin Flips and Dice Rolls 24 | 25 | [:rocket: Day 4][3]: PyGame Random Art 26 | 27 | [:rocket: Day 5][4]: PyGame Animation 28 | 29 | [:rocket: Day 6][5]: Paddle Game 30 | 31 | [:rocket: Day 7][6]: Snake Game 32 | 33 | [:rocket: Extras][7]: Extra Projects 34 | 35 | [0]: Day-1/ 36 | [1]: Day-2/ 37 | [2]: Day-3/ 38 | [3]: Day-4/ 39 | [4]: Day-5/ 40 | [5]: Day-6/ 41 | [6]: Day-7/ 42 | [7]: Extras/ 43 | -------------------------------------------------------------------------------- /bell_number.py: -------------------------------------------------------------------------------- 1 | #Python program to print bell number 2 | #Bell Number:-Let S(n, k) be total number of partitions of n elements into k sets. The value of n’th Bell Number is sum of S(n, k) for k = 1 to n. Value of S(n, k) can be defined recursively as, S(n+1, k) = k*S(n, k) + S(n, k-1) 3 | A sample Bell triangle is as follows: 4 | 1 5 | 1 3 6 | 3 8 13 7 | 13 23 33 43 8 | #The code to print the bell triangle is as follows- 9 | #--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 10 | n=int(input("enter the number of bell")) #taking value from the user 11 | bell=0 #initialising bell to 'zero' 12 | k=0 #initialising k to 'zero' 13 | for i in range(0,n): #loop for changing rows from 0 to n 14 | for j in range(0,i+1): #printing columns 15 | if j==0 and i>0: #repeating the last number of previous row in new row 16 | print(bell,'',end='') #printing first number of each line 17 | else: 18 | k=(i**2)+1+bell #to generate other numbers of line 19 | print(k,'',end='') #printing other number in lines 20 | bell=k #updating value of bell 21 | print('\n') #for moving into next lines 22 | print("last number of bell is",bell) 23 | --------------------------------------------------------------------------------