apps: snake: fix a couple of errors
Signed-off-by: Johannes Wache <jbwa@posteo.de>
This commit is contained in:
parent
726d5c8554
commit
6d3f2126bf
1 changed files with 25 additions and 35 deletions
|
@ -90,7 +90,12 @@ class SnakeGameApp():
|
|||
|
||||
def foreground(self):
|
||||
"""Activate the application."""
|
||||
self._draw()
|
||||
wasp.watch.drawable.fill()
|
||||
if self.running:
|
||||
self.update()
|
||||
else:
|
||||
self.snake.show()
|
||||
wasp.watch.drawable.fill(x=self.food[0],y=self.food[1],w=15,h=15,bg=0x00ff)
|
||||
wasp.system.request_event(wasp.EventMask.TOUCH |
|
||||
wasp.EventMask.SWIPE_UPDOWN |
|
||||
wasp.EventMask.SWIPE_LEFTRIGHT)
|
||||
|
@ -113,35 +118,27 @@ class SnakeGameApp():
|
|||
elif event[0] == wasp.EventType.RIGHT:
|
||||
self.snake.set_dir(15,0)
|
||||
else:
|
||||
self.running = False
|
||||
return True
|
||||
|
||||
def tick(self, ticks):
|
||||
"""Notify the application that its periodic tick is due."""
|
||||
self.update()
|
||||
|
||||
|
||||
def food_location(self):
|
||||
x = randint(0,15) * 15
|
||||
y = randint(0,15) * 15
|
||||
self.food = [x,y]
|
||||
|
||||
def _draw(self):
|
||||
wasp.watch.drawable.fill()
|
||||
if self.running:
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
draw = wasp.watch.drawable
|
||||
"""Draw the display from scratch."""
|
||||
|
||||
if (self.snake.eat(self.food)):
|
||||
self.food_location()
|
||||
draw.fill(x=self.food[0],y=self.food[1],w=15,h=15,bg=0x00ff)
|
||||
self.snake.update()
|
||||
if (self.snake.end_game()):
|
||||
if self.snake.len > self.highscore:
|
||||
self.highscore = self.snake.len
|
||||
if len(self.snake.body) > self.highscore:
|
||||
self.highscore = len(self.snake.body)
|
||||
self.running = False
|
||||
wasp.watch.vibrator.pulse()
|
||||
self.snake = Snake()
|
||||
|
@ -152,41 +149,43 @@ class SnakeGameApp():
|
|||
draw.string('Highscore: '+str(self.highscore-1),0,180,width=240)
|
||||
draw.reset()
|
||||
return True
|
||||
if self.running:
|
||||
self.snake.show()
|
||||
draw.fill(x=self.food[0],y=self.food[1],w=15,h=15,bg=0x00ff)
|
||||
return True
|
||||
|
||||
|
||||
# Based on https://www.youtube.com/watch?v=OMoVcohRgZA
|
||||
class Snake():
|
||||
def __init__(self):
|
||||
self.body = []
|
||||
self.body.append([120,120])
|
||||
self.body = [[120,120]]
|
||||
self.xdir = 0
|
||||
self.ydir = 0
|
||||
self.len = 1
|
||||
self.justate = False
|
||||
self.oldtail = [0,0]
|
||||
|
||||
def set_dir(self,x,y):
|
||||
self.xdir = x
|
||||
self.ydir = y
|
||||
|
||||
def update(self):
|
||||
self.oldtail = self.body[0].copy()
|
||||
head = self.body[-1].copy()
|
||||
if not self.justate:
|
||||
self.body = self.body[1:]
|
||||
self.justate = False
|
||||
head[0] += self.xdir
|
||||
head[1] += self.ydir
|
||||
self.body.append(head)
|
||||
|
||||
def grow(self):
|
||||
head = self.body[-1]
|
||||
self.len += 1
|
||||
self.body.append(head)
|
||||
#self.update()
|
||||
|
||||
def eat(self,pos):
|
||||
x = self.body[-1][0]
|
||||
y = self.body[-1][1]
|
||||
if (x == pos[0] and y == pos[1]):
|
||||
self.grow()
|
||||
self.justate = True
|
||||
# Color food white so it appears as a body part:
|
||||
wasp.watch.drawable.fill(x=(self.body[-1][0]),y=(self.body[-1][1]),w=15,h=15,bg=0x0000)
|
||||
wasp.watch.drawable.fill(x=self.body[-1][0]+1,y=self.body[-1][1]+1,w=13,h=13,bg=0xffff)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -194,23 +193,14 @@ class Snake():
|
|||
x = self.body[-1][0]
|
||||
y = self.body[-1][1]
|
||||
if (x >= 240 or x < 0) or (y >= 240 or y < 0):
|
||||
print("Inside 1")
|
||||
return True
|
||||
for i in range(len(self.body)-1):
|
||||
part = self.body[i]
|
||||
if (part[0] == x and part[1] == y):
|
||||
print("Inside 2")
|
||||
return True
|
||||
return False
|
||||
|
||||
def show(self):
|
||||
draw = wasp.watch.drawable
|
||||
if self.len == 1: #vanish old and show new
|
||||
draw.fill(x=(self.body[0][0]-self.xdir),y=(self.body[0][1]-self.ydir),w=15,h=15,bg=0x0000)
|
||||
draw.fill(x=self.body[0][0]+1,y=self.body[0][1]+1,w=13,h=13,bg=0xffff)
|
||||
else: # vanish last and show first
|
||||
draw.fill(x=self.body[0][0],y=self.body[0][1],w=15,h=15,bg=0x0000)
|
||||
draw.fill(x=self.oldtail[0],y=self.oldtail[1],w=15,h=15,bg=0x0000)
|
||||
draw.fill(x=self.body[-1][0]+1,y=self.body[-1][1]+1,w=13,h=13,bg=0xffff)
|
||||
#for i in range(self.len):
|
||||
# draw.fill(x=self.body[i][0]+1,y=self.body[i][1]+1,w=13,h=13,bg=0xffff)
|
||||
|
||||
|
|
Loading…
Reference in a new issue