Difference between revisions of "CSC111 Programs for Week 10 2015"
(→Skeleton Program for Processing an Image) |
|||
Line 106: | Line 106: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | =Playing Checkers= | ||
<br /> | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | [[Image:Checkers0.png|right|150px]] | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # checkers0.py | ||
+ | # D. Thiebaut | ||
+ | # A first program to display an 8x8 chessboard | ||
+ | # in a graphic window. | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | WIDTH = 600 # width of the window | ||
+ | HEIGHT= 600 # height of the window | ||
+ | SIDE = 600//8 # the side of a cell of the 8x8 board | ||
+ | |||
+ | |||
+ | def displayBoard( win ): | ||
+ | """displays an 8x8 board in the graphic window. The board extends to the sides of the window''' | ||
+ | # add your code here... | ||
+ | return | ||
+ | |||
+ | def waitForClick( win, message ): | ||
+ | """ waitForClick: stops the GUI and displays a message. | ||
+ | Returns when the user clicks the window. The message is erased.""" | ||
+ | |||
+ | # wait for user to click mouse to start | ||
+ | startMsg = Text( Point( win.getWidth()/2, win.getHeight()-15 ), message ) | ||
+ | startMsg.draw( win ) # display message | ||
+ | win.getMouse() # wait | ||
+ | startMsg.undraw() # erase | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | # open the window | ||
+ | win = GraphWin( "Checkers", WIDTH, HEIGHT ) | ||
+ | |||
+ | # display 8x8 checkers board | ||
+ | displayBoard( win ) | ||
+ | |||
+ | # wait for user to click before closing everything... | ||
+ | waitForClick( win, "click to close" ) | ||
+ | |||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Checkers0.py== | ||
+ | <br /> | ||
+ | This program is the bare skeleton for display the board. You have to add the code inside the '''displayBoard()''' function. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
[[Category:CSC111]][[Category:Python]] | [[Category:CSC111]][[Category:Python]] |
Revision as of 14:25, 7 April 2015
--D. Thiebaut (talk) 05:48, 6 April 2015 (EDT)
Contents
Skeleton Program for Displaying an Image
The program below will work with this image, named "catGlasses.gif". It should be located in the same directory where the program is saved.
# imageProcessingSkel.py
# D. Thiebaut
# A skeleton program to start doing image processing
# in Python
from graphics import *
# image geometry
# 424x18
# make the window the same geometry
WIDTH = 424
HEIGHT = 418
IMAGEFILENAME = "catGlasses.gif"
#----------------------------------------------------------------
def waitForClick( win, message ):
""" waitForClick: stops the GUI and displays a message.
Returns when the user clicks the window. The message is erased."""
# wait for user to click mouse to start
startMsg = Text( Point( win.getWidth()/2, win.getHeight()-15 ), message )
startMsg.draw( win ) # display message
win.getMouse() # wait
startMsg.undraw() # erase
def main():
# open the window
win = GraphWin( "Image Editor", WIDTH, HEIGHT )
# open the cat image
cat = Image( Point(WIDTH//2, HEIGHT//2), IMAGEFILENAME )
cat.draw( win )
waitForClick( win, "click to close" )
win.close()
main()
Skeleton Program for Processing an Image
# pixelProcessingSkel.py
# D. Thiebaut
# Simple skeleton program for processing
# the pixels of an image
from graphics import *
# image geometry
# 424x18
# make the window the same geometry
WIDTH = 424
HEIGHT = 418
IMAGEFILENAME = "catGlasses.gif"
#----------------------------------------------------------------
def waitForClick( win, message ):
""" waitForClick: stops the GUI and displays a message.
Returns when the user clicks the window. The message is erased."""
# wait for user to click mouse to start
startMsg = Text( Point( win.getWidth()/2, win.getHeight()-15 ), message )
startMsg.draw( win ) # display message
win.getMouse() # wait
startMsg.undraw() # erase
def makeRed( win, img ):
""" set the red component of all the pixels to 255, the
maximum value.
"""
global WIDTH, HEIGHT
for x in range( WIDTH ):
for y in range( HEIGHT ):
red, green, blue = img.getPixel( x, y )
img.setPixel( x, y, color_rgb(255, green, blue ) )
def main():
win = GraphWin( "Image Editor", WIDTH, HEIGHT )
img = Image( Point(WIDTH//2, HEIGHT//2), IMAGEFILENAME )
img.draw( win )
makeRed( win, img )
waitForClick( win, "click to close" )
win.close()
main()
Playing Checkers
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.
# checkers0.py
# D. Thiebaut
# A first program to display an 8x8 chessboard
# in a graphic window.
#
from graphics import *
WIDTH = 600 # width of the window
HEIGHT= 600 # height of the window
SIDE = 600//8 # the side of a cell of the 8x8 board
def displayBoard( win ):
"""displays an 8x8 board in the graphic window. The board extends to the sides of the window'''
# add your code here...
return
def waitForClick( win, message ):
""" waitForClick: stops the GUI and displays a message.
Returns when the user clicks the window. The message is erased."""
# wait for user to click mouse to start
startMsg = Text( Point( win.getWidth()/2, win.getHeight()-15 ), message )
startMsg.draw( win ) # display message
win.getMouse() # wait
startMsg.undraw() # erase
def main():
# open the window
win = GraphWin( "Checkers", WIDTH, HEIGHT )
# display 8x8 checkers board
displayBoard( win )
# wait for user to click before closing everything...
waitForClick( win, "click to close" )
win.close()
main()
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.
Checkers0.py
This program is the bare skeleton for display the board. You have to add the code inside the displayBoard() function.