Difference between revisions of "CSC111 Moving Graphic Objects"
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:48, 20 April 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:48, 20 April 2014 (EDT) | ||
---- | ---- | ||
− | + | <br /> | |
+ | <tanbox>'''Note:''' Depending on the installation of your Python and Tkinter program (that Python uses to perform graphics), the code below may or may not work. On some installation of Tkinter, the code shows animation and objects moving, on some other computers the moving of items does not show up. | ||
+ | </tanbox> | ||
+ | <br /> | ||
=A car that moves out of the canvas= | =A car that moves out of the canvas= | ||
− | [[Image:CarPolyGraphicsMovingRight.png| | + | [[Image:CarPolyGraphicsMovingRight.png|350px|right]] |
<br /> | <br /> | ||
The example program below requires the graphics library available on [[CSC111 Event-Driven Programming with Graphics111.py 2014| this page]]. | The example program below requires the graphics library available on [[CSC111 Event-Driven Programming with Graphics111.py 2014| this page]]. | ||
Line 9: | Line 12: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | # | + | # carMovesRight.py |
# D. Thiebaut | # D. Thiebaut | ||
# Demonstrates the user the graphics111 library with | # Demonstrates the user the graphics111 library with | ||
# moveable objects, menu, and event-driven programming | # moveable objects, menu, and event-driven programming | ||
− | # | + | # This program creates a car, and then moves it to the right |
− | # | + | # by the amount in deltaX until the car disappears from the |
+ | # canvas. | ||
from graphics111 import * | from graphics111 import * |
Latest revision as of 09:17, 21 April 2014
--D. Thiebaut (talk) 09:48, 20 April 2014 (EDT)
Note: Depending on the installation of your Python and Tkinter program (that Python uses to perform graphics), the code below may or may not work. On some installation of Tkinter, the code shows animation and objects moving, on some other computers the moving of items does not show up.
A car that moves out of the canvas
The example program below requires the graphics library available on this page.
# carMovesRight.py
# D. Thiebaut
# Demonstrates the user the graphics111 library with
# moveable objects, menu, and event-driven programming
# This program creates a car, and then moves it to the right
# by the amount in deltaX until the car disappears from the
# canvas.
from graphics111 import *
from random import seed
from random import randrange
from time import sleep
MAXWIDTH = 800
MAXHEIGHT = 600
class Car:
def __init__( self, x, y, width, height, color ):
self._x = x
self._y = y
self._width = width
self._height = height
self._color = color # list of 3 ints between 0 and 255
# build the body
self._top = Rectangle( x+width//4, y-height//2,
width//2, height//2,
color )
self._body = Rectangle( x, y, width, height, color )
# build the wheels
self._w1 = Wheel( x + width//4, y + height, width//5 )
self._w2 = Wheel( x + 3*width//4, y + height, width//5 )
def setRandomColor( self ):
self._top.setRandomColor()
self._body.setRandomColor()
def getTotalHeight( self ):
return self._height*2 + self._width//10
def getTotalWidth( self ):
return self._width
def draw( self, canvas ):
self._canvas = canvas
self._body.draw( canvas )
self._top.draw( canvas )
self._w1.draw( canvas )
self._w2.draw( canvas )
def move( self, canvas, dx, dy ):
self._body.move( canvas, dx, dy )
self._top.move( canvas, dx, dy )
self._w1.move( canvas, dx, dy )
self._w2.move( canvas, dx, dy )
# ========================================================
# Main Program
# ========================================================
def main():
# make the menu and car global so that they will
# be accessible to the call-back function
global menu, car
# open the window and get access to its canvas
win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
canvas = win.canvas()
# draw something
p1 = Polygon( (100, 100, 150, 50, 350, 50, 400, 100), (255, 0, 0 ) )
p1.draw( canvas )
car = Car( 150, 75, 100, 20, (250, 250, 0 ) )
car.draw( canvas )
deltaX = 5
for i in range( MAXWIDTH//deltaX ):
car.move( canvas, deltaX, 0 )
# wait and respond to events, or comment out and make the program
# stop as soon as car disappears.
win.wait()
win.close()
main()