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):
|
def foreground(self):
|
||||||
"""Activate the application."""
|
"""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.system.request_event(wasp.EventMask.TOUCH |
|
||||||
wasp.EventMask.SWIPE_UPDOWN |
|
wasp.EventMask.SWIPE_UPDOWN |
|
||||||
wasp.EventMask.SWIPE_LEFTRIGHT)
|
wasp.EventMask.SWIPE_LEFTRIGHT)
|
||||||
|
@ -113,35 +118,27 @@ class SnakeGameApp():
|
||||||
elif event[0] == wasp.EventType.RIGHT:
|
elif event[0] == wasp.EventType.RIGHT:
|
||||||
self.snake.set_dir(15,0)
|
self.snake.set_dir(15,0)
|
||||||
else:
|
else:
|
||||||
self.running = False
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def tick(self, ticks):
|
def tick(self, ticks):
|
||||||
"""Notify the application that its periodic tick is due."""
|
"""Notify the application that its periodic tick is due."""
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
|
||||||
def food_location(self):
|
def food_location(self):
|
||||||
x = randint(0,15) * 15
|
x = randint(0,15) * 15
|
||||||
y = randint(0,15) * 15
|
y = randint(0,15) * 15
|
||||||
self.food = [x,y]
|
self.food = [x,y]
|
||||||
|
|
||||||
def _draw(self):
|
|
||||||
wasp.watch.drawable.fill()
|
|
||||||
if self.running:
|
|
||||||
self.update()
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
draw = wasp.watch.drawable
|
draw = wasp.watch.drawable
|
||||||
"""Draw the display from scratch."""
|
"""Draw the display from scratch."""
|
||||||
|
|
||||||
if (self.snake.eat(self.food)):
|
if (self.snake.eat(self.food)):
|
||||||
self.food_location()
|
self.food_location()
|
||||||
draw.fill(x=self.food[0],y=self.food[1],w=15,h=15,bg=0x00ff)
|
|
||||||
self.snake.update()
|
self.snake.update()
|
||||||
if (self.snake.end_game()):
|
if (self.snake.end_game()):
|
||||||
if self.snake.len > self.highscore:
|
if len(self.snake.body) > self.highscore:
|
||||||
self.highscore = self.snake.len
|
self.highscore = len(self.snake.body)
|
||||||
self.running = False
|
self.running = False
|
||||||
wasp.watch.vibrator.pulse()
|
wasp.watch.vibrator.pulse()
|
||||||
self.snake = Snake()
|
self.snake = Snake()
|
||||||
|
@ -152,41 +149,43 @@ class SnakeGameApp():
|
||||||
draw.string('Highscore: '+str(self.highscore-1),0,180,width=240)
|
draw.string('Highscore: '+str(self.highscore-1),0,180,width=240)
|
||||||
draw.reset()
|
draw.reset()
|
||||||
return True
|
return True
|
||||||
self.snake.show()
|
if self.running:
|
||||||
|
self.snake.show()
|
||||||
|
draw.fill(x=self.food[0],y=self.food[1],w=15,h=15,bg=0x00ff)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# Based on https://www.youtube.com/watch?v=OMoVcohRgZA
|
# Based on https://www.youtube.com/watch?v=OMoVcohRgZA
|
||||||
class Snake():
|
class Snake():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.body = []
|
self.body = [[120,120]]
|
||||||
self.body.append([120,120])
|
|
||||||
self.xdir = 0
|
self.xdir = 0
|
||||||
self.ydir = 0
|
self.ydir = 0
|
||||||
self.len = 1
|
self.justate = False
|
||||||
|
self.oldtail = [0,0]
|
||||||
|
|
||||||
def set_dir(self,x,y):
|
def set_dir(self,x,y):
|
||||||
self.xdir = x
|
self.xdir = x
|
||||||
self.ydir = y
|
self.ydir = y
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
self.oldtail = self.body[0].copy()
|
||||||
head = self.body[-1].copy()
|
head = self.body[-1].copy()
|
||||||
self.body = self.body[1:]
|
if not self.justate:
|
||||||
|
self.body = self.body[1:]
|
||||||
|
self.justate = False
|
||||||
head[0] += self.xdir
|
head[0] += self.xdir
|
||||||
head[1] += self.ydir
|
head[1] += self.ydir
|
||||||
self.body.append(head)
|
self.body.append(head)
|
||||||
|
|
||||||
def grow(self):
|
|
||||||
head = self.body[-1]
|
|
||||||
self.len += 1
|
|
||||||
self.body.append(head)
|
|
||||||
#self.update()
|
|
||||||
|
|
||||||
def eat(self,pos):
|
def eat(self,pos):
|
||||||
x = self.body[-1][0]
|
x = self.body[-1][0]
|
||||||
y = self.body[-1][1]
|
y = self.body[-1][1]
|
||||||
if (x == pos[0] and y == pos[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 True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -194,23 +193,14 @@ class Snake():
|
||||||
x = self.body[-1][0]
|
x = self.body[-1][0]
|
||||||
y = self.body[-1][1]
|
y = self.body[-1][1]
|
||||||
if (x >= 240 or x < 0) or (y >= 240 or y < 0):
|
if (x >= 240 or x < 0) or (y >= 240 or y < 0):
|
||||||
print("Inside 1")
|
|
||||||
return True
|
return True
|
||||||
for i in range(len(self.body)-1):
|
for i in range(len(self.body)-1):
|
||||||
part = self.body[i]
|
part = self.body[i]
|
||||||
if (part[0] == x and part[1] == y):
|
if (part[0] == x and part[1] == y):
|
||||||
print("Inside 2")
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
draw = wasp.watch.drawable
|
draw = wasp.watch.drawable
|
||||||
if self.len == 1: #vanish old and show new
|
draw.fill(x=self.oldtail[0],y=self.oldtail[1],w=15,h=15,bg=0x0000)
|
||||||
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[-1][0]+1,y=self.body[-1][1]+1,w=13,h=13,bg=0xffff)
|
||||||
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.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