Showing posts with label circles. Show all posts
Showing posts with label circles. Show all posts

Sunday, May 10, 2015

PaA: Hacking a circle

This hackable file is the most simple file using the graphics library. It creates a window, then adds a circle on it. It looks like this:


Understanding the onecircle.py script
First, let's ake a look a the file:

# Import everything from Graphics
from graphics import *

# Build a window
win = GraphWin('Test 1', 600, 600)
win.setBackground('white')

# Draw one circle
poly = Circle( Point( 300, 300 ), 200 )
poly.setFill( 'red' )
poly.draw(win)

# Add some text
mytext = Text( Point(450, 520), 'by Random Bytes' )
mytext.setFill('red')
mytext.setSize( 30 )
mytext.draw(win)

# Wait to stop
win.getMouse()
win.close()

Step 1 - Import useful code from other files

Python is such a useful language because it is possible to borrow code from other file,to do pretty much anything that you can think about. In this file, we need to tell our script to import all objects and function from the file graphics.py . Without this import statement, it wouldn't not be possible to create a window object (GraphWin), a point (Point), a circle (Circle) and some on-screen text (Text). The import code is made here of a single line:

from graphics import *