diff --git a/Day-1/README.md b/Day-1/README.md index de53bde..1b14a5c 100644 --- a/Day-1/README.md +++ b/Day-1/README.md @@ -72,40 +72,6 @@ There are two types of variables we'll be using today: strings (things like word **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. -##Reading and Altering Code - Number Guess Game - -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! - -Then, we'll start looking through the code, and start making changes to it. - -Let's read through the code to see how it does it! - -It uses three variables: - * `secret`, which stores the number you have to guess - * `guess`, which is what the player guesses - * `tries`, which is how many times the player has guessed. - -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. - -Look at this while: - -```python -while guess != secret and tries < 6: -``` - -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!** - -Some other things to point out: -* Random is a module that was imported so you will be able to pick a random number -* **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.** -* != (check if not equal), == (check if equal) - -Let's make some changes! Can you: -* Give the pirate your name -* Change the game to be guessing between 1-200 -* Give 8 guesses - - ## Multiplication Game 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. diff --git a/Day-1/finished/multiplication game.py b/Day-1/finished/multiplication game.py deleted file mode 100644 index 0440b86..0000000 --- a/Day-1/finished/multiplication game.py +++ /dev/null @@ -1,23 +0,0 @@ -import random - -#print your welcome message here -print "Welcome to the multiplication game." -print "How well do you know your 2-12 multiplication tables?" - -for num in range(0,10): - #pick the numbers to multiply - number1 = random.randint(2,12) - number2 = random.randint(2,12) - answer = number1 * number2 - - guess = 0 - print "What is", number1, "x", number2, "?" - - while guess != answer: - guess = input("Answer: ") - if guess != answer: - print "No, try again" - - print "You got it!" - -print "That's it, good work!" diff --git a/Day-1/multiplication game.py b/Day-1/multiplication_game.py old mode 100644 new mode 100755 similarity index 100% rename from Day-1/multiplication game.py rename to Day-1/multiplication_game.py diff --git a/Day-2/finished/challenge problem.py b/Day-2/finished/challenge problem.py deleted file mode 100644 index c6b0973..0000000 --- a/Day-2/finished/challenge problem.py +++ /dev/null @@ -1,31 +0,0 @@ - -adj1 = raw_input("Enter an adjective: ") -adj2 = raw_input("Enter an adjective: ") -adj3 = raw_input("Enter an adjective: ") -adj4 = raw_input("Enter an adjective: ") -adj5 = raw_input("Enter an adjective: ") -name1 = raw_input("Enter a name: ") -name2 = raw_input("Enter a name: ") -name3 = raw_input("Enter a name: ") -name4 = raw_input("Enter a name: ") -number1 = raw_input("Enter a number: ") -number2 = raw_input("Enter a number: ") -number3 = raw_input("Enter a number: ") -plural_noun1 = raw_input("Enter a plural noun: ") -plural_noun2 = raw_input("Enter a plural noun: ") -plural_noun3 = raw_input("Enter a plural noun: ") -plural_noun4 = raw_input("Enter a plural noun: ") -past_tense_verb = raw_input("Enter a past tense verb: ") -noun = raw_input("Enter a noun: ") -animal1 = raw_input("Enter an animal: ") -animal2 = raw_input("Enter an animal: ") - - -print "" -print "Make Me A Video Game MadLib!" -print "I, the", adj1, "and", adj2, name1 + ", has", past_tense_verb, name2 + "'s sister and plans" -print "to steal her", adj3, noun + "! What are a", animal1, "and backpacking", animal2 -print "to do? Before you can help", name3, "you'll have to collect the", adj4 -print plural_noun1,"and",adj5, plural_noun2, "that open up the", number1, "worlds connected to" -print name4 + "'s Lair. There are", number2, plural_noun3, "and", number3, plural_noun4, "in the game," -print "along with hundreds of other goodies for you to find." diff --git a/Day-2/finished/madlibs.py b/Day-2/finished/madlibs.py deleted file mode 100644 index 046fb7f..0000000 --- a/Day-2/finished/madlibs.py +++ /dev/null @@ -1,27 +0,0 @@ -#Madlibs credit: http://www.teach-nology.com/worksheets/language_arts/madlibs/1/ - -#Ask the user for all the variables -#Have to set: adjective1 through 5, animal1, animal2, food1, food2, -# verb, ptverb (a past tense verb) -adjective1 = raw_input("Enter an adjective: ") -adjective2 = raw_input("Enter another adjective: ") -adjective3 = raw_input("Enter yet another adjective: ") -adjective4 = raw_input("Enter a fourth adjective: ") -adjective5 = raw_input("Enter a fifth adjective (last one, I promise): ") -animal1 = raw_input("Enter an animal: ") -animal2 = raw_input("Enter another animal: ") -food1 = raw_input("Enter a food item: ") -food2 = raw_input("Enter another food item: ") -verb = raw_input("Enter a verb: ") -ptverb = raw_input("Enter an -ed verb: ") - - -#print out the result -print "" #blank line -print "Today I went to the zoo. I saw a", adjective1, animal1, "jumping" -print "up and down in its tree. He",ptverb, "through the large" -print "tunnel that led to its", adjective2, "cage. I got a", food1 -print "and passed it through the cage to a gigantic", adjective3, animal2 -print "towering over my head. Feeding that animal made me hungry. " -print "I went to get a", adjective4,"scoop of", food2 +". Afterwards I had to" -print verb, "to catch our bus. It was a", adjective5, "day at the zoo!" diff --git a/Day-2/finished/silly sentence generator.py b/Day-2/finished/silly sentence generator.py deleted file mode 100644 index 9ea3f95..0000000 --- a/Day-2/finished/silly sentence generator.py +++ /dev/null @@ -1,26 +0,0 @@ -import random - -verb_list = ["ran","jumped","climbed","licked","programmed"] #create a list of all your verbs -noun_list = ["pet duck","tree","toaster","computer"] -adjective_list = ["tasty","large","smelly","green"] - -#this function is given a list of words and selects one at random -def random_word(list_of_words): - number_of_words = len(list_of_words) #get the length of the words list - random_word_number = random.randint(0,number_of_words - 1) #pick a random number up to the end of the list - selected_word = list_of_words[random_word_number] #select the word at the number spot - return selected_word #hand it back - -def random_word2(list_of_words): - return random.choice(list_of_words) - -#a simple sentence structure: -#The the . - -for i in range(10): - adjective1 = random_word2(adjective_list) - adjective2 = random_word2(adjective_list) - noun1 = random_word2(noun_list) - noun2 = random_word2(noun_list) - verb = random_word2(verb_list) #call the function and remember the result - print "The",adjective1,noun1,verb,"the",adjective2,noun2 diff --git a/Day-3/finished/coin flip.py b/Day-3/finished/coin flip.py deleted file mode 100644 index c55f579..0000000 --- a/Day-3/finished/coin flip.py +++ /dev/null @@ -1,24 +0,0 @@ -import random - -total_heads = 0 -total_tails = 0 - -#create a function that half the time returns "heads" and half the time returns "tails" -def flip_coin(): - random_number = random.randint(1,2) - if random_number == 1: - return "heads" - else: - return "tails" - -#flip 1000 times -for n in range(0,1000): - flip = flip_coin() - if flip == "heads": - total_heads = total_heads + 1 - else: - total_tails = total_tails + 1 - -print "Heads flipped: ", total_heads -print "Tails flipped: ", total_tails - diff --git a/Day-3/finished/dice rolls.py b/Day-3/finished/dice rolls.py deleted file mode 100644 index 523dd9e..0000000 --- a/Day-3/finished/dice rolls.py +++ /dev/null @@ -1,35 +0,0 @@ -# Listing_23-2.py -# Copyright Warren Sande, 2009 -# Released under MIT license http://www.opensource.org/licenses/mit-license.php -# Version 61 ---------------------------- -# Changes by Brian Skinner - Sept, 2013 - -# Rolling two six-sided dice 1000 times. - -import random - -totals = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - -#function that rolls 2 dice and returns the sum of their rolls -def roll_dice(): - roll1 = random.randint(1,6) - roll2 = random.randint(1,6) - total = roll1 + roll2 - return total - -for i in range(1000): - dice_total = roll_dice() - totals[dice_total] = totals[dice_total]+ 1 - -#why did we skip spots 0 and 1? -print "2 was rolled", totals[2], "times" -print "3 was rolled", totals[3], "times" -print "4 was rolled", totals[4], "times" -print "5 was rolled", totals[5], "times" -print "6 was rolled", totals[6], "times" -print "7 was rolled", totals[7], "times" -print "8 was rolled", totals[8], "times" -print "9 was rolled", totals[9], "times" -print "10 was rolled", totals[10], "times" -print "11 was rolled", totals[11], "times" -print "12 was rolled", totals[12], "times" diff --git a/Day-4/finished/house drawing.py b/Day-4/finished/house drawing.py deleted file mode 100644 index 368379f..0000000 --- a/Day-4/finished/house drawing.py +++ /dev/null @@ -1,64 +0,0 @@ -import pygame -pygame.init() -screen = pygame.display.set_mode((640,480)) - -#used http://colorpicker.com/ to find RGB colors - -def draw_tree(x,y): - #tree trunk (50 wide and 100 tall) - pygame.draw.rect(screen,(117,90,0),(x,y-100,50,100)) - #leaves are a circle - pygame.draw.circle(screen,(27,117,0),(x+25,y-120),50) - -def draw_house(x,y): - #pink house - pygame.draw.rect(screen,(255,171,244),(x,y-180,200,180)) - #brown door - pygame.draw.rect(screen,(89,71,0),(x+80,y-60,40,60)) - #yellow door knob - pygame.draw.circle(screen,(255,204,0),(x+112,y-30),4) - #triangle roof - pygame.draw.polygon(screen, (125,125,125), ( (x,y-180),(x+100,y-250),(x+200,y-180) ) ) - draw_window(x+20,y-90) - draw_window(x+130,y-90) - -def draw_window(x,y): - #glass - pygame.draw.rect(screen,(207,229,255),(x,y-50,50,50)) - #frame - pygame.draw.rect(screen,(0,0,0),(x,y-50,50,50),5) - pygame.draw.rect(screen,(0,0,0),(x+23,y-50,5,50)) - pygame.draw.rect(screen,(0,0,0),(x,y-27,50,5)) - -#this function is able to draw clouds of different sizes -def draw_cloud(x,y,size): - #put int() around any multiplications by decimals to get rid of this warning: - #DeprecationWarning: integer argument expected, got float - pygame.draw.circle(screen,(255,255,255),(x,y),int(size*.5)) - pygame.draw.circle(screen,(255,255,255),(int(x+size*.5),y),int(size*.6)) - pygame.draw.circle(screen,(255,255,255),(x+size,int(y-size*.1)),int(size*.4)) - - -#green ground -pygame.draw.rect(screen,(0,160,3),(0,400,640,80)) -#light blue sky -pygame.draw.rect(screen,(135,255,255),(0,0,640,400)) - -draw_tree(60,400) #x and y location are the bottom left of tree trunk -draw_tree(550,400) - -draw_house(225,400) - -draw_cloud(60,120,80) -draw_cloud(200,50,40) -draw_cloud(450,100,120) - - -pygame.display.flip() - -running = True -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False -pygame.quit() diff --git a/Day-4/finished/random drawing.py b/Day-4/finished/random drawing.py deleted file mode 100644 index 665d28f..0000000 --- a/Day-4/finished/random drawing.py +++ /dev/null @@ -1,28 +0,0 @@ -import random -import pygame -pygame.init() -screen = pygame.display.set_mode((640,480)) - -def pick_random_color(): - colorname = random.choice(pygame.color.THECOLORS.keys()) - return pygame.color.THECOLORS[colorname] -def random_coordinates(): - x = random.randint(0,640) - y = random.randint(0,480) - return (x,y) - -screen.fill((0,0,0)) - -for i in range(20): - pygame.draw.line(screen,pick_random_color(),random_coordinates(),random_coordinates(),random.randint(1,5)) - pygame.draw.circle(screen,pick_random_color(),random_coordinates(),random.randint(10,500),random.randint(1,5)) - - -pygame.display.flip() - -running = True -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False -pygame.quit() diff --git a/Day-5/finished/animation.py b/Day-5/finished/animation.py deleted file mode 100644 index 09b77c6..0000000 --- a/Day-5/finished/animation.py +++ /dev/null @@ -1,50 +0,0 @@ -# Listing_16-17.py -# Copyright Warren Sande, 2009 -# Released under MIT license http://www.opensource.org/licenses/mit-license.php -# Version 61 ---------------------------- - -#Some changes by Brian Skinner, Sept-2013 - -# move a beach ball image in a pygame window with wrapping - -import pygame, sys -pygame.init() -screen = pygame.display.set_mode([640,480]) -white = [255, 255, 255] - -#loads the beach ball image (must be saved in the same place as this file) -my_ball = pygame.image.load('beach_ball.png') -my_minecraft = pygame.image.load('minecraft.gif') -#set variables to use in our program -x = 50 -y = 250 -x_speed = 10 -minecraft_x = 100 -minecraft_y = 100 -y_speed = 5 - -while True: - #this checks if you've exited the game - for event in pygame.event.get(): - if event.type == pygame.QUIT: - pygame.quit() - #sys.exit() - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely white - screen.fill(white) - - #the animation logic - x = x - x_speed - if x < 0: - x = screen.get_width() - minecraft_y = minecraft_y + y_speed - if minecraft_y > screen.get_width(): - minecraft_y = 0 - - #draw the ball on the screen at the x and y location - screen.blit(my_ball, [x, y]) - screen.blit(my_minecraft, [minecraft_x, minecraft_y]) - #update the entire display - pygame.display.update() diff --git a/Day-5/finished/double-speed-and-bounce-off-all-walls.py b/Day-5/finished/double-speed-and-bounce-off-all-walls.py deleted file mode 100644 index fe3e9e6..0000000 --- a/Day-5/finished/double-speed-and-bounce-off-all-walls.py +++ /dev/null @@ -1,70 +0,0 @@ -import pygame, sys -pygame.init() -screen = pygame.display.set_mode([640,480]) -white = [255, 255, 255] - -#load the beach ball image (must be saved in the same place as this file -my_ball = pygame.image.load('beach_ball.png') -minecraft = pygame.image.load('minecraft.gif') -#set variables to use in our program -x = 50 -y = 200 -x_speed = -1 -x_speed_max = 50 -x2 = 300 -y2 = 300 -x2_speed = 5 -y2_speed = 5 - -running = True; -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely white - screen.fill(white) - - #the animation logic - x = x + x_speed - #if the ball is on the right side of the screen - if x > screen.get_width() - my_ball.get_width(): - x_speed = -x_speed*2 - - #if the ball is on the left side of the screen - if x < 0: - x_speed = -x_speed*2 - - #if the ball is going too fast, restart it - if x_speed > x_speed_max: - x_speed = -1 - x = 500 - - - - x2 = x2 + x2_speed - y2 = y2 + y2_speed - #if minecraft is on the left side of the screen - if x2 < 0: - x2_speed = -x2_speed - #if minecraft is on the right side of the screen - if x2 > screen.get_width() - minecraft.get_width(): - x2_speed = -x2_speed - #if minecraft is on the top of the screen - if y2 < 0: - y2_speed = -y2_speed - #if minecraft is on the bottom of the screen - if y2 > screen.get_height() - minecraft.get_height(): - y2_speed = -y2_speed - - - #draw the ball on the screen at the x and y location - screen.blit(my_ball, [x, y]) - #draw minecraft on the screen at the x2 and y2 location - screen.blit(minecraft, [x2, y2]) - #update the entire display - pygame.display.update() - -pygame.quit() diff --git a/Day-6/finished/bounce game with speed boost.py b/Day-6/finished/bounce game with speed boost.py deleted file mode 100644 index 8bf654f..0000000 --- a/Day-6/finished/bounce game with speed boost.py +++ /dev/null @@ -1,110 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -ball_x = 50 -ball_y = 50 -ball_radius = 10 -ball_color = [222,50,50] -ball_speed_x = 3 -ball_speed_y = 5 - -paddle_x = 200 -paddle_y = 440 -paddle_width = 60 -paddle_height = 20 -paddle_color = [20,180,180] -paddle_speed = 20 - -myfont = pygame.font.SysFont("Arial", 15) -score = 0 -speed_boost = 1.1 - -running = True -#game loop -while running: - for event in pygame.event.get(): - #check if you've exited the game - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.MOUSEMOTION: - coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates - paddle_x = coordinates[0] - paddle_width/2 #sets the paddle_x variable to the first item in coordinates - #if the paddle is off the left side bring it back - if paddle_x < 0: - paddle_x = 0 - #if the paddle is off the right side bring it back - if paddle_x > screen.get_width() - paddle_width: - paddle_x = screen.get_width() - paddle_width - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely white - screen.fill(black) - - #move the ball - ball_y = ball_y + ball_speed_y - ball_x = ball_x + ball_speed_x - #check if the ball is off the bottom of the screen - if ball_y > screen.get_height(): - ball_y = 20 - score = 0 - ball_speed_y = 5 - ball_speed_x = 3 - #check if the ball hit the top of the screen - if ball_y < ball_radius: - ball_speed_y = -ball_speed_y - #check if the ball hit the left side of the screen - if ball_x < ball_radius: - ball_speed_x = -ball_speed_x - #check if the ball hit the right side of the screen - if ball_x > screen.get_width() - ball_radius: - ball_speed_x = -ball_speed_x - - #create imaginary rectangles around ball and paddle - 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 - paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) - #see if the rectangles overlap - if doRectsOverlap(ball_rect, paddle_rect): - #only bounce when the ball is moving down - if ball_speed_y > 0: - ball_speed_y = -ball_speed_y * speed_boost - ball_speed_x = ball_speed_x * speed_boost - score = score + 1 - - - #draw everything on the screen - score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) - screen.blit(score_label, (10, 10)) - pygame.draw.circle(screen, ball_color, [int(ball_x), int(ball_y)], ball_radius, 0) - pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-6/finished/bounce game.py b/Day-6/finished/bounce game.py deleted file mode 100644 index b8aea34..0000000 --- a/Day-6/finished/bounce game.py +++ /dev/null @@ -1,106 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -ball_x = 50 -ball_y = 50 -ball_radius = 10 -ball_color = [222,50,50] -ball_speed_x = 3 -ball_speed_y = 5 - -paddle_x = 200 -paddle_y = 440 -paddle_width = 60 -paddle_height = 20 -paddle_color = [20,180,180] -paddle_speed = 20 - -myfont = pygame.font.SysFont("Arial", 15) -score = 0 - -running = True -#game loop -while running: - for event in pygame.event.get(): - #check if you've exited the game - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.MOUSEMOTION: - coordinates = pygame.mouse.get_pos() #gives (x,y) coordinates - paddle_x = coordinates[0] - paddle_width/2 #sets the paddle_x variable to the first item in coordinates - #if the paddle is off the left side bring it back - if paddle_x < 0: - paddle_x = 0 - #if the paddle is off the right side bring it back - if paddle_x > screen.get_width() - paddle_width: - paddle_x = screen.get_width() - paddle_width - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely white - screen.fill(black) - - #move the ball - ball_y = ball_y + ball_speed_y - ball_x = ball_x + ball_speed_x - #check if the ball is off the bottom of the screen - if ball_y > screen.get_height(): - ball_y = 20 - score = 0 - #check if the ball hit the top of the screen - if ball_y < ball_radius: - ball_speed_y = -ball_speed_y - #check if the ball hit the left side of the screen - if ball_x < ball_radius: - ball_speed_x = -ball_speed_x - #check if the ball hit the right side of the screen - if ball_x > screen.get_width() - ball_radius: - ball_speed_x = -ball_speed_x - - #create imaginary rectangles around ball and paddle - 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 - paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height) - #see if the rectangles overlap - if doRectsOverlap(ball_rect, paddle_rect): - #only bounce when the ball is moving down - if ball_speed_y > 0: - ball_speed_y = -ball_speed_y - score = score + 1 - - - #draw everything on the screen - score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) - screen.blit(score_label, (10, 10)) - pygame.draw.circle(screen, ball_color, [ball_x, ball_y], ball_radius, 0) - pygame.draw.rect(screen, paddle_color, [paddle_x, paddle_y, paddle_width, paddle_height], 0) - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-6/finished/pong finished.py b/Day-6/finished/pong finished.py deleted file mode 100644 index f7bd1fb..0000000 --- a/Day-6/finished/pong finished.py +++ /dev/null @@ -1,131 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -ball_x = 50 -ball_y = 50 -ball_radius = 10 -ball_color = [222,50,50] -ball_speed_x = 3 -ball_speed_y = 5 - -paddle1_x = 20 -paddle1_y = 200 -paddle2_x = 600 -paddle2_y = 200 -paddle_width = 20 -paddle_height = 60 -paddle_color = [20,180,180] -paddle_speed = 10 - -myfont = pygame.font.SysFont("Arial", 15) -score1 = 0 -score2 = 0 -speed_boost = 1.03 - -#allows for holding of key -pygame.key.set_repeat(20, 20) - -running = True -#game loop -while running: - for event in pygame.event.get(): - #check if you've exited the game - if event.type == pygame.QUIT: - running = False - - #check if you pressed a key - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_w: - paddle1_y = paddle1_y - paddle_speed - if paddle1_y < 0: - paddle1_y = 0 - if event.key == pygame.K_s: - paddle1_y = paddle1_y + paddle_speed - if paddle1_y > screen.get_height() - paddle_height: - paddle1_y = screen.get_height() - paddle_height - if event.key == pygame.K_o: - paddle2_y = paddle2_y - paddle_speed - if paddle2_y < 0: - paddle2_y = 0 - if event.key == pygame.K_l: - paddle2_y = paddle2_y + paddle_speed - if paddle2_y > screen.get_height() - paddle_height: - paddle2_y = screen.get_height() - paddle_height - - - - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely white - screen.fill(black) - - #move the ball - ball_y = ball_y + ball_speed_y - ball_x = ball_x + ball_speed_x - #check if the ball is off the bottom of the screen - if ball_y > screen.get_height() - ball_radius: - ball_speed_y = -ball_speed_y - #check if the ball hit the top of the screen - if ball_y < ball_radius: - ball_speed_y = -ball_speed_y - #check if the ball hit the left side of the screen - if ball_x < ball_radius: - ball_speed_x = -3 - ball_speed_y = 5 - score2 = score2 + 1 - ball_x = 600 - #check if the ball hit the right side of the screen - if ball_x > screen.get_width() - ball_radius: - ball_speed_x = 3 - ball_speed_y = 5 - score1 = score1 + 1 - ball_x = 40 - - #create imaginary rectangles around ball and paddle - 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 - paddle1_rect = pygame.Rect(paddle1_x, paddle1_y, paddle_width, paddle_height) - paddle2_rect = pygame.Rect(paddle2_x, paddle2_y, paddle_width, paddle_height) - #see if the rectangles overlap - if doRectsOverlap(ball_rect, paddle1_rect) or doRectsOverlap(ball_rect, paddle2_rect): - ball_speed_x = -ball_speed_x * speed_boost - - - #draw everything on the screen - score1_label = myfont.render(str(score1), 1, pygame.color.THECOLORS['white']) - screen.blit(score1_label, (5, 10)) - score2_label = myfont.render(str(score2), 1, pygame.color.THECOLORS['white']) - screen.blit(score2_label, (620, 10)) - pygame.draw.circle(screen, ball_color, [int(ball_x), int(ball_y)], ball_radius, 0) - pygame.draw.rect(screen, paddle_color, [paddle1_x, paddle1_y, paddle_width, paddle_height], 0) - pygame.draw.rect(screen, paddle_color, [paddle2_x, paddle2_y, paddle_width, paddle_height], 0) - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake finished.py b/Day-7/finished/snake finished.py deleted file mode 100644 index 5911bd3..0000000 --- a/Day-7/finished/snake finished.py +++ /dev/null @@ -1,117 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND VARIABLES -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 -target_x = 400 -target_y = 400 -target_color = (255,0,0) -target_radius = 10 -score = 0 -myfont = pygame.font.SysFont("Arial", 22) -my_sound = pygame.mixer.Sound('zoop.wav') - - -def new_target_location(): - global target_x, target_y - target_x = random.randint(30, 610) - target_y = random.randint(30, 450) - -def snake_hit_wall(): - if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: - return True - else: - return False - -def move_snake(): - global snake_x, snake_y - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT and direction != "left": - direction = "right" - if event.key == pygame.K_LEFT and direction != "right": - direction = "left" - if event.key == pygame.K_UP and direction != "down": - direction = "up" - if event.key == pygame.K_DOWN and direction != "up": - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - move_snake() - - snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) - target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) - if doRectsOverlap(snake_rect, target_rect): - new_target_location() - my_sound.play() - score = score + 1 - screen.fill(pygame.color.THECOLORS['white']) - if score % 2 == 0: - speed = speed + 1 - - if snake_hit_wall() == True: - running = False - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) - score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) - screen.blit(score_label, (5, 10)) - #update the entire display - pygame.display.update() - -pygame.time.delay(1000) -pygame.quit() diff --git a/Day-7/finished/snake part 1.py b/Day-7/finished/snake part 1.py deleted file mode 100644 index ab2071b..0000000 --- a/Day-7/finished/snake part 1.py +++ /dev/null @@ -1,66 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.MOUSEMOTION: - print "mouse moved" - #SECTION 2- YOUR CODE HERE FOR WHEN THE MOUSE IS MOVED - - if event.type == pygame.KEYDOWN: - print "key pressed" - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake part 2.py b/Day-7/finished/snake part 2.py deleted file mode 100644 index f7088ce..0000000 --- a/Day-7/finished/snake part 2.py +++ /dev/null @@ -1,79 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT: - direction = "right" - if event.key == pygame.K_LEFT: - direction = "left" - if event.key == pygame.K_UP: - direction = "up" - if event.key == pygame.K_DOWN: - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake part 3.py b/Day-7/finished/snake part 3.py deleted file mode 100644 index cbf9769..0000000 --- a/Day-7/finished/snake part 3.py +++ /dev/null @@ -1,85 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 -target_x = 400 -target_y = 400 -target_color = (255,0,0) -target_radius = 10 - - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT: - direction = "right" - if event.key == pygame.K_LEFT: - direction = "left" - if event.key == pygame.K_UP: - direction = "up" - if event.key == pygame.K_DOWN: - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) - - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake part 4.py b/Day-7/finished/snake part 4.py deleted file mode 100644 index 7962861..0000000 --- a/Day-7/finished/snake part 4.py +++ /dev/null @@ -1,89 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 -target_x = 400 -target_y = 400 -target_color = (255,0,0) -target_radius = 10 - -def new_target_location(): - global target_x, target_y - target_x = random.randint(30, 610) - target_y = random.randint(30, 450) - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT: - direction = "right" - if event.key == pygame.K_LEFT: - direction = "left" - if event.key == pygame.K_UP: - direction = "up" - if event.key == pygame.K_DOWN: - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) - - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake part 5.py b/Day-7/finished/snake part 5.py deleted file mode 100644 index 040204f..0000000 --- a/Day-7/finished/snake part 5.py +++ /dev/null @@ -1,95 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 -target_x = 400 -target_y = 400 -target_color = (255,0,0) -target_radius = 10 - -def new_target_location(): - global target_x, target_y - target_x = random.randint(30, 610) - target_y = random.randint(30, 450) - - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT: - direction = "right" - if event.key == pygame.K_LEFT: - direction = "left" - if event.key == pygame.K_UP: - direction = "up" - if event.key == pygame.K_DOWN: - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - - snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) - target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) - if doRectsOverlap(snake_rect, target_rect): - new_target_location() - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) - - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake part 6.py b/Day-7/finished/snake part 6.py deleted file mode 100644 index 8464cf9..0000000 --- a/Day-7/finished/snake part 6.py +++ /dev/null @@ -1,103 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 -target_x = 400 -target_y = 400 -target_color = (255,0,0) -target_radius = 10 - -def new_target_location(): - global target_x, target_y - target_x = random.randint(30, 610) - target_y = random.randint(30, 450) - -def snake_hit_wall(): - if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: - return True - else: - return False - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT: - direction = "right" - if event.key == pygame.K_LEFT: - direction = "left" - if event.key == pygame.K_UP: - direction = "up" - if event.key == pygame.K_DOWN: - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - - snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) - target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) - if doRectsOverlap(snake_rect, target_rect): - new_target_location() - - if snake_hit_wall() == True: - running = False - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) - - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/snake part 7.py b/Day-7/finished/snake part 7.py deleted file mode 100644 index f39653e..0000000 --- a/Day-7/finished/snake part 7.py +++ /dev/null @@ -1,108 +0,0 @@ -#Credit the Invent With Python book (http://inventwithpython.com) -#for doRectsOverlap and isPointInsideRect functions - -#used to detect collisions in our game -def doRectsOverlap(rect1, rect2): - for a, b in [(rect1, rect2), (rect2, rect1)]: - # Check if a's corners are inside b - if ((isPointInsideRect(a.left, a.top, b)) or - (isPointInsideRect(a.left, a.bottom, b)) or - (isPointInsideRect(a.right, a.top, b)) or - (isPointInsideRect(a.right, a.bottom, b))): - return True - - return False - -#used the by the doRectsOverlap function (won't be called directly from game code) -def isPointInsideRect(x, y, rect): - if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): - return True - else: - return False - -import pygame, sys, random -pygame.init() -screen = pygame.display.set_mode([640,480]) -black = [0, 0, 0] - -#the game's variables -#SECTION 1 - YOUR CODE HERE FOR CREATING VARIABLES AND FUNCTIONS -snake_x = 100 -snake_y = 100 -snake_color = (0,233,0) -snake_radius = 25 -direction = "right" -speed = 2 -target_x = 400 -target_y = 400 -target_color = (255,0,0) -target_radius = 10 -score = 0 -myfont = pygame.font.SysFont("Arial", 22) - -def new_target_location(): - global target_x, target_y - target_x = random.randint(30, 610) - target_y = random.randint(30, 450) - -def snake_hit_wall(): - if snake_x < snake_radius or snake_x > 640 - snake_radius or snake_y < snake_radius or snake_y > 480 - snake_radius: - return True - else: - return False - -running = True -#game loop -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - #SECTION 3 - YOUR CODE HERE FOR WHEN A KEY IS PRESSED - if event.key == pygame.K_RIGHT: - direction = "right" - if event.key == pygame.K_LEFT: - direction = "left" - if event.key == pygame.K_UP: - direction = "up" - if event.key == pygame.K_DOWN: - direction = "down" - - #pause for 20 milliseconds - pygame.time.delay(20) - #make the screen completely black - screen.fill(black) - - #logic for moving everything in the game and checking collisions - #SECTION 4 - YOUR CODE HERE FOR CHANGING VARIABLES AND CHECKING FOR COLLISIONS - if direction == "right": - snake_x = snake_x + speed - if direction == "left": - snake_x = snake_x - speed - if direction == "up": - snake_y = snake_y - speed - if direction == "down": - snake_y = snake_y + speed - - snake_rect = pygame.Rect(snake_x-snake_radius, snake_y-snake_radius, snake_radius*2,snake_radius*2) - target_rect = pygame.Rect(target_x-target_radius, target_y-target_radius, target_radius*2,target_radius*2) - if doRectsOverlap(snake_rect, target_rect): - new_target_location() - speed = speed + 1 - score = score + 1 - - if snake_hit_wall() == True: - running = False - - #draw everything on the screen - #SECTION 5 - YOUR CODE HERE FOR DRAWING EVERYTHING - pygame.draw.circle(screen, snake_color, [snake_x,snake_y], snake_radius) - pygame.draw.circle(screen, target_color, [target_x,target_y], target_radius) - score_label = myfont.render(str(score), 1, pygame.color.THECOLORS['white']) - screen.blit(score_label, (5, 10)) - #update the entire display - pygame.display.update() - - -pygame.quit() diff --git a/Day-7/finished/zoop.wav b/Day-7/finished/zoop.wav deleted file mode 100644 index 066ecb4..0000000 Binary files a/Day-7/finished/zoop.wav and /dev/null differ