Skip to content

Commit

Permalink
Clean up the snake code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
deshipu committed Sep 7, 2017
1 parent 0ce5543 commit 27b0ddd
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions games/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

pew.init()
screen = pew.Pix()

game_speed = 4
snake = [(0, 0)]
dx = 1
dy = 0
apple_x = 4
apple_y = 4
snake = [(2, 4)]
dx, dy = 1, 0
apple_x, apple_y = 6, 4
screen.pixel(apple_x, apple_y, 2)

while True:
Expand All @@ -18,26 +17,26 @@
screen.pixel(x, y, 1)
x, y = snake[-1]
screen.pixel(x, y, 3)

pew.show(screen)
pew.tick(1 / game_speed)

keys = pew.keys()
if keys & pew.K_UP and dy == 0:
dx = 0
dy = -1
dx, dy = 0, -1
elif keys & pew.K_LEFT and dx == 0:
dx = -1
dy = 0
dx, dy = -1, 0
elif keys & pew.K_RIGHT and dx == 0:
dx = 1
dy = 0
dx, dy = 1, 0
elif keys & pew.K_DOWN and dy == 0:
dx = 0
dy = 1
dx, dy = 0, 1
x = (x + dx) % 8
y = (y + dy) % 8

if (x, y) in snake:
break
snake.append((x, y))

if x == apple_x and y == apple_y:
screen.pixel(apple_x, apple_y, 0)
apple_x, apple_y = snake[0]
Expand All @@ -55,4 +54,4 @@
for dx in range(-8, text.width):
screen.blit(text, -dx, 1)
pew.show(screen)
pew.tick(1.0/8)
pew.tick(1 / 12)

0 comments on commit 27b0ddd

Please sign in to comment.