Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add centre helper function
  • Loading branch information
Jdplays committed Nov 15, 2025
commit 90402eec3339f56bb2c0add13c09868cc4816bad
3 changes: 3 additions & 0 deletions src/jamkit/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def right(): self.dx, self.dy = 1, 0
s.onkey(left, "Left")
s.onkey(right, "Right")
s.listen()

def centre(self, grid: Grid):
self.x, self.y, self.dx, self.dy = grid.cols//2, grid.rows//2, 1, 0

class Item:
def __init__(self, grid: Grid, color="red"):
Expand Down
15 changes: 7 additions & 8 deletions tests/turtle_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pi_eater.py — using jamkit.py
from jamkit.turtle import Grid, Head, ImageItem, Item, draw_tail, hits
from jamkit.turtle import Grid, Head, ImageItem, draw_tail, hits
from jamkit import sprites

# 32 x 40, 30
Expand All @@ -8,7 +8,7 @@
head.bind_arrows()

raspberry = ImageItem(g, sprites.raspberry)
pi_bad = Item(g, color="red")
bug = ImageItem(g, sprites.bug)

score = 0
tail = [] # list of (x,y)
Expand Down Expand Up @@ -37,16 +37,16 @@ def step():
raspberry.place(avoid=set(tail + [(head.x, head.y)]))

# if hit bug
if hits(head, pi_bad):
if hits(head, bug):
game_over = True
g.message("GAME OVER — press R to restart", y=0, color="white")
g.message("GAME OVER - press R to restart", y=0, color="white")
g.update()
return

# draw
g.clear()
raspberry.draw()
pi_bad.draw()
bug.draw()
draw_tail(g, tail, color="green")
head.draw()
g.write_hud(f"Score: {score}")
Expand All @@ -56,11 +56,10 @@ def reset():
global score, grow, game_over, tail
score, grow, game_over = 0, 0, False
tail.clear()
head.x, head.y, head.dx, head.dy = g.cols//2, g.rows//2, 1, 0
head.centre(g)
raspberry.place()
pi_bad.place()
bug.place()
g.clear()
g.write_hud(f"Score: {score}")
g.update()

g.screen = g.screen # access for keys
Expand Down