python_data_types_and_lists.docx |
pythonmore.docx |
functions.docx |
lists_in_python.docx |
list_examples.docx |
using_tkinter_for_better_graphics2018.docx |
making_all_the_shapes_in_your_python_tkinter_picture_move.docx |
making_an_object_react_to_something.docx |
Python - Tkinter
Tkinter - graphics package
Create a picture that draws a scene or tells a story. Your picture should have functions and movement with keys and/or loops. The more you put into it, the better your grade. Make sure you put in comments about the shapes and functions.
Python Book Chapter 12 pages 163-190 Using Tkinter for Better Graphics
Making Objects move with Keys
Download the file making_an_object_react_to_something. It is also on pages 186-188 in the pdf or 212-214 in to browser. Add the code to move shapes with keys on to your picture. Commet out the
Color Chart
from tkinter import *
tk=Tk()
canvas = Canvas(tk,width=500, height=100)
canvas.pack()
Assignments:
1. Put the code to show the x,y coordinates on the picture you made before.
Add some shapes to your picture, and use the coordinates to help.
2. Create a new program that will write a limerick with each line having a different font, size and color.
In the downloaded book, text is on page 180 and in the browser, it is on page 206.
Save the program in DOCUMENTS as YourName Poem.
Look up and find a clean limerick on the internet.
Add your name in text at the end.
canvas.create_text(220, 400, text='My name is Laine Agee"',fill='blue', font=('Courier', 20))
2 .a) Create a function called poem(). Put all the lines of the poem tabbed under the function.
Add code to put a button on the canvas. The button will all the function poem when it is clicked.
btn = Button(tk, text="click me", command=poem)
btn.pack()
3. Open the document making all the shapes in your python tkinter picture move.
Start a new file called Picture Move and save it in Documents.
Draw a picture that will have parts or all move. The code for the picture should be in a function.
Use the examples in the document to make all or parts of your picture move. Add text to tell what is happening.
*********************************************************************
Example:
from tkinter import *
tk=Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
canvas.create_line(0,0,500,500)
canvas.create_rectangle(10,10,160,60,fill='red')
canvas.create_oval(240, 240, 400,320,outline="blue", fill="yellow")
canvas.create_rectangle(180,180,280,210, fill ="green")
canvas.create_polygon(50,100,150,100,150,200, fill='red')
The code below shows the x,y coordinates on the canvas
#Canvas set-up
from tkinter import *
tk=Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
#function to set-up x,y coordinates to show
def moved(event):
canvas.itemconfigure(tag, text="(%r, %r)" % (event.x, event.y))
#Call to show coordinates
canvas.bind("<Motion>", moved)
tag = canvas.create_text(250, 25, text="", anchor="nw")
#Draw shapes or call functions here.
#This goes at the very end!
tk.mainloop()
See the notes and book above.
Lists
A list is an ordered sequence of values, where each value is identified by an index.
The notes and examples can be downloaded above.
Open Python
Go to File new file
Save the new file as YourNameMadlibs in the Documents Folder.
import random
Create 3 lists with parts of speech, nouns, verbs, adjectives. Each list should have at least 5 words.
nouns = ['cat', 'dog', 'bird','horse', 'turtle']
Create a function for each part of speech list that shuffles the list and returns the first word in the list.(nouns[0])
Write a story in print. The story should contain at least 5 sentences and calls the functions multiple times.
Instead of writing the nouns, verbs and adjectives, call the function that will return a random word.
Use \n to start a new line in the print to keep the words from going past the screen.
print('Once upon a time there was a', get_adjective(), get_nouns()+'.')
#Once upon a time there was a beautiful frog.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Save the new file as YourNameRestaurant in the Documents Folder.
Write a restaurant program using the following functions:
def menu():
#prints 3 drink, 3 food and 3 dessert menu items with prices
def get_drink():
#ask the user for the drink choice, and return the drink.
def get_food():
#ask the user for the food choice and return the food
def get_dessert():
#ask the user for the dessert choice and return the dessert
def total_cost(drink, food, dessert):
# get the price for each and add them up for the total
total=0
if (drink=='coke'):
total +=1.99
elif (drink=='lemonade'):
total+=2.50
else:
total+=3.00
print('$'+str(total))
menu()
drink=get_drink()
food=get_food()
dessert=get_dessert()
total_cost (drink, food, dessert)
Functions - A function performs a task. It can print or return a value. Functions can have parameters, which is information to the function. Read the function document above.
Go to codehs.org. Go to #4, 13, 14 & 15 do the first 4 exercises in each. Make sure you watch the videos.
Open Python
Go to File new file
Save the new file as YourNamePractice in the Documents Folder.
#Laine Agee
#3/26/19
Have a conversation. Ask 3 or 4 questions using input and set the answers to string and int variables. Use the variables in a print response.
print('Hello.')
name = input('What is your name ')
print('Hello,',name)
age = int(input('How old are you?'))
Add 2 more input questions. The answer printed will be contained in an if-elif-else.
Ask the user to enter 4 numbers. Print the average of the numbers.
Go to codehs.org and sign up. You will complete #3 and then we will begin to code in the Python shell.
Start every new Python program in the shell. Open Python, go to FILE and New File.
The first time you save your program, save it in the Documents Folder name it, your name program name.
Processing Final Game Project
When you decide on the theme of your game, write a description of your plan with basic instructions, and sketch what the screen will look like. Show it to Ms. Agee before you start to write code.
The game should start with a title screen with the title, your name and instructions. Get the game working first, and put the instructions in last. The game should have a score, lives or levels and a way to win or lose.
Required code:
- instructions
- comments
- functions
- variables
- movement
- key or mouse control
- score
The more challenging the better the grade. A simple game can be challenging.
Pong
A Pong game has a paddle that moves with the mouse or arrows, and a ball that bounces randomly. If the ball hits an edge that does not have the paddle, it will bounce. If it hits the paddle, it will bounce and add to the score. If it misses the paddle and hits the wall, you lose.
Download the instructions and the ball program above.
- Start with the ball that bounces. You will need to resize the ball and the window when you write the game.
- Add a paddle and collision() function that checks if the ball hits the paddle. If it hits, it should bounce in the opposite direction.
- Your pong game needs a score. The score will increase each time it hits the paddle. You may copy the score from pacman.
- If the ball misses the paddle, the game can end with noLoop() or start again.
- Anything else you add will be considered extra.
The notes and beginning Pacman program are attached above.
1. Add if statements to check for the x & y coordinates for the pacman to match each dot and that the dot is white.
2. Add the keyPressed code.
3. Add more dots in lines and columns to the game.
For extra points, add ghosts, levels or anything else to make your game special.
Assignment 3:
Create a program that has a theme and uses functions to draw an animation with the following:
• variables to control motion
• shapes and colors
• if statements
• comments
• text
Your animation can tell a story or be part of a game.
Processing is a combination of Java and Art. Processing is designed to make it easy to make animated, interactive, and artistic programs.
You will find everything you need at processing.org.
Assignment 2:
Create a picture or design that includes the following:
- shapes
- colors
- random numbers
- variables
- if statements
- comments
- text
- mouseX, mouseY
USE THE FOLLOWING DESIGN FOR YOUR CODE!
//Your Name
void setup()
{
size(500,400); // you choose the size of the canvas
background(255); // you choose the background color
}
void draw()
{
//Enter all the code for your picture inside the draw() function.
}
ASSIGNMENT:
Your goal is to draw one of the following things/scenes.
• a house on a street
• a car or truck on a road
• an animal
• a robot
• an alien
• any other scene that you can think of
Your drawing must include:
• A sized canvas
• A background color
• At least 10 shapes, including all of the following:
• line
• triangle
• rectangle
• ellipse
• some shapes must be filled, using different colors
• multiple stroke thicknesses. Example: strokeWeight(5);
If you need help, go to processing.org and click on Reference.