Jed Rembold and Eric Roberts
Week of October 14th
GArc
s how you
need to specify the location! That will be important for getting Pacman
properly centeredGArc
class represents an arc formed
by taking a section of the perimeter of an oval.GArc
class is a
GFillableObject
, and so you can call
.set_filled()
on a
GArc
objectdef filled_arc():
gw = GWindow(400, 400)
arc = GArc(50, 50,
350, 350,
90, 135)
arc.set_color("orange")
arc.set_filled(True)
gw.add(arc)
def create_pacman():
pacman = GArc(
GW_WIDTH / 2 - PACMAN_RADIUS, #centered x
GW_HEIGHT / 2 - PACMAN_RADIUS, #centered y
2 * PACMAN_RADIUS, #width
2 * PACMAN_RADIUS, #height
45, #starting angle
270, #sweep angle
)
pacman.set_filled(True)
pacman.set_fill_color("yellow")
return pacman
pacman = create_pacman()
gw.add(pacman)
step
callback function and
define an interval event listener to call that function every 20 msstep
function is called,
it should move Pacman forward (to the right) by the number of pixels
indicated by the provided constantdef step():
pacman.move(PACMAN_SPEED, 0)
gw.set_interval(step, 20)
step
callback function, so it
must be added as an attribute to the
GWindow
(called
gw
here)step
function to use this new variableset_starting_angle
and get_starting_angle
methods for
GArc
s to flip Pacman around to face the
other direction!def step():
pacman.move(|||something involving the direction|||, 0)
if |||left or right edge of Pacman extends off screen|||:
|||reverse Pacman direction by switching direction variable value|||
|||turn Pacman around by changing starting angle of arc|||
|||initialize direction variable|||
gw.set_interval(step, 20)
def step():
pacman.move(gw.dx, 0)
if (pacman.get_x() + 2 * PACMAN_RADIUS > GW_WIDTH #right edge
or pacman.get_x() < 0 #left edge
):
gw.dx *= -1 #flip it
pacman.set_start_angle(pacman.get_start_angle() + 180)
pacman = create_pacman()
gw.dx = PACMAN_SPEED
gw.set_interval(step, 20)
GWindow
with gw.remove(obj)
, but that requires you
to have a variable obj
that was assigned
your particular GObject
.
pacman
) and for the background colored
rectangle (bg
), but not the pills!GObject
that has already been added to the
window
.get_element_at(x,y)
method for
GWindows..get_element_at(x,y)
a pair
of coordinates, \(x\) and \(y\)
GObject
at that location,
it will return it to you! So save that object in a variable!None
>>> leftmost_pill = gw.get_element_at(20, 200)
>>> print(leftmost_pill)
GOval(20, 190.0, 20, 20)
.get_element_at
is just the topmost object
found at that location. So often times you might want to do a comparison
afterwards with an if
statement to ensure it
is the object you are interested in.
def step():
...
# Adjusting for which side mouth is on
if gw.dx > 0:
x = pacman.get_x() + PACMAN_RADIUS * 1.5
else:
x = pacman.get_x() + PACMAN_RADIUS * 0.5
y = pacman.get_y() + PACMAN_RADIUS
# Getting anything there
obj = gw.get_element_at(x,y)
# Removing that thing if present and if it isn't the bg
if obj is not None and obj is not bg:
gw.remove(obj)
...