Created
June 1, 2015 11:13
-
-
Save Druid-of-Luhn/fd8204a722ecc2b4aea1 to your computer and use it in GitHub Desktop.
PyLogo is a lightweight library that uses PyGame to provide a LOGO-like drawing interface for Python, to provide a simple and fun way to learn programming in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
PyLogo is a lightweight library that uses PyGame to provide a LOGO-like | |
drawing interface for Python, to provide a simple and fun way to learn | |
programming in Python. | |
""" | |
import pygame | |
from math import cos, radians, sin | |
# Functions that 'from pylogo import *' exports | |
__all__ = [ | |
"start", | |
"draw", | |
"moveto", | |
"forward", | |
"back", | |
"left", | |
"right", | |
"penup", | |
"pendown" | |
] | |
_pylogo_FPS = 60 # Constant: frames per second of event loop | |
_pylogo_initialised = False # Whether pylogo has been initialised | |
_pylogo_colour = 0, 0, 0 # Colour to draw in | |
def start(w, h): | |
""" | |
Initialise the PyLogo module. | |
""" | |
global _pylogo_screen | |
global _pylogo_clock | |
global _pylogo_turtle | |
global _pylogo_initialised | |
pygame.init() | |
# Create a window with the given dimensions | |
_pylogo_screen = pygame.display.set_mode((w, h)) | |
# Fill the screen with white | |
_pylogo_screen.fill((255, 255, 255)) | |
# Create the game clock | |
_pylogo_clock = pygame.time.Clock() | |
# Place the turtle in the centre | |
_pylogo_turtle = [ w / 2, h / 2, 0 ] | |
# Draw lines by default | |
pendown() | |
# Set initialised to true | |
_pylogo_initialised = True | |
def draw(): | |
""" | |
Draw the lines and listen for events. | |
""" | |
# Make sure pylogo is started first | |
if not _pylogo_initialised: | |
print "PyLogo not initialised, do so with `start` function first." | |
return | |
# Display everything in the window | |
pygame.display.flip() | |
# Keep the window open and listen for input | |
_pylogo_eventloop() | |
# Quit after the loop is done | |
pygame.quit() | |
def _pylogo_eventloop(): | |
running = True | |
while running: | |
# Run through the event queue | |
for event in pygame.event.get(): | |
# If the widnow is closed or the q/Q key is pressed | |
if event.type == pygame.QUIT or\ | |
(event.type == pygame.KEYDOWN and event.key == pygame.K_q): | |
# Do not continue the loop | |
running = False | |
# Maintain a constant rate | |
_pylogo_clock.tick(_pylogo_FPS) | |
def moveto(x, y): | |
""" | |
Jump directly to a point without drawing. | |
""" | |
# Directly set x;y coordinates | |
_pylogo_turtle[0], _pylogo_turtle[1] = x, y | |
def forward(distance): | |
""" | |
Move a given distance in the direction pointed to, | |
drawing if the pen is down. | |
""" | |
# Original position and angle | |
start_x = _pylogo_turtle[0] | |
start_y = _pylogo_turtle[1] | |
angle = _pylogo_turtle[2] | |
# Calculate the new position (right-angle triangle trigonometry) | |
x = start_x + cos(angle) * distance | |
y = start_y + sin(angle) * distance | |
# Move the turtle to the new position | |
moveto(x, y) | |
# Draw the line if pen down | |
if _pylogo_drawing: | |
pygame.draw.aaline(\ | |
_pylogo_screen, _pylogo_colour,\ | |
(start_x, start_y), (x, y)) | |
def back(distance): | |
""" | |
Move a given distance backwards from the direction pointed to, | |
drawing if the pen is down. | |
""" | |
left(180) | |
forward(distance) | |
left(180) | |
def left(angle): | |
""" | |
Turn a given angle (degrees) to the left of the current direction. | |
""" | |
_pylogo_turtle[2] -= radians(angle) | |
def right(angle): | |
""" | |
Turn a given angle (degrees) to the right of the current direction. | |
""" | |
left(-angle) | |
def penup(): | |
""" | |
Prevent lines from being drawn. | |
""" | |
global _pylogo_drawing | |
_pylogo_drawing = False | |
def pendown(): | |
""" | |
Allow lines to be drawn. | |
""" | |
global _pylogo_drawing | |
_pylogo_drawing = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment