|File Size:||15 kb|
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
from tkinter import *
canvas = Canvas(tk,width=500, height=100)
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)
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.
from tkinter import *
canvas = Canvas(tk, width=400, height=400)
canvas.create_oval(240, 240, 400,320,outline="blue", fill="yellow")
canvas.create_rectangle(180,180,280,210, fill ="green")
The code below shows the x,y coordinates on the canvas
from tkinter import *
canvas = Canvas(tk, width=500, height=500)
#function to set-up x,y coordinates to show
canvas.itemconfigure(tag, text="(%r, %r)" % (event.x, event.y))
#Call to show coordinates
tag = canvas.create_text(250, 25, text="", anchor="nw")
#Draw shapes or call functions here.
#This goes at the very end!
See the notes and book above.
A list is an ordered sequence of values, where each value is identified by an index.
The notes and examples can be downloaded above.
Go to File new file
Save the new file as YourNameMadlibs in the Documents Folder.
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)
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:
#prints 3 drink, 3 food and 3 dessert menu items with prices
#ask the user for the drink choice, and return the drink.
#ask the user for the food choice and return the food
#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_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.
Go to File new file
Save the new file as YourNamePractice in the Documents Folder.
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.
name = input('What is your 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.
- key or mouse control
The more challenging the better the grade. A simple game can be challenging.
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.
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
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.
Create a picture or design that includes the following:
- random numbers
- if statements
- mouseX, mouseY
USE THE FOLLOWING DESIGN FOR YOUR CODE!
size(500,400); // you choose the size of the canvas
background(255); // you choose the background color
//Enter all the code for your picture inside the draw() function.
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:
• 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.