from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle
from kivy.utils import platform
+from kivy.clock import Clock
from erdslangetjie.level import LevelList
from erdslangetjie.data import load_image
raise RuntimeError('No entry point')
self.player_tile = None
self.nemesis_tile = None
+ self.timer_set = False
self.player.pos = self.level_obj.enter_pos
if platform() != 'android':
def do_move(self, direction):
if not self.level_obj:
return
- self.nemesis.move(self.level_obj)
- self.draw_nemesis()
self.player.move(direction, self.level_obj)
self.draw_player()
self.check_state()
- self.reset_timer()
+ if not self.timer_set:
+ self.reset_timer()
- def timed_move(self):
+ def timed_move(self, event):
if not self.level_obj:
return
- self.nemesis.move(self.level_obj)
+ self.nemesis.move(self.level_obj, self.check_caught)
self.draw_nemesis()
self.check_state()
self.reset_timer()
def reset_timer(self):
- pass
+ self.timer_set = True
+ Clock.unschedule(self.timed_move)
+ Clock.schedule_once(self.timed_move, 0.5)
+
+ def check_caught(self):
+ return self.nemesis.pos == self.player.pos
+
+ def reset_level(self):
+ Clock.unschedule(self.timed_move)
+ self.timer_set = False
+ self.remove_widget(self.nemesis_tile)
+ self.nemesis.reset_pos()
+ if self.level_obj:
+ self.level_obj.load_tiles()
+ self.player.pos = self.level_obj.enter_pos
+ self.remove_widget(self.player_tile)
+ self.view.scroll_x = 0
+ self.view.scroll_y = 0
+ self.build()
+ self.draw_nemesis()
+ self.draw_player()
+ return True
+ return False
def check_state(self):
if self.level_obj.at_exit(self.player.pos):
# Jump to next level
self.level_obj = self.level_list.advance_to_next_level()
- self.remove_widget(self.nemesis_tile)
- self.nemesis.reset_pos()
- if self.level_obj:
- self.level_obj.load_tiles()
- self.player.pos = self.level_obj.enter_pos
- self.remove_widget(self.player_tile)
- self.view.scroll_x = 0
- self.view.scroll_y = 0
- self.build()
- self.draw_nemesis()
- self.draw_player()
- else:
+ if not self.reset_level():
self.app.game_over(True)
- elif self.nemesis.pos == self.player.pos:
+ elif self.check_caught():
# Caught
self.app.game_over(False)
super(Nemesis, self).__init__()
self.sprite = load_image('sprites/nemesis.png')
self.reset_pos()
+ self.been = []
- def move(self, level):
+ def move(self, level, caught):
if not self.on_board():
# Advance towards the map a step at a time
self.pos = (self.pos[0] + 1, self.pos[1])
self.pos = level.enter_pos
return False
# AI goes here
+ steps = 0
+ self.been.append(self.pos)
+ for direction in [(1, 0), (-1, 0), (0, -1), (0, 1)] * 2:
+ new_pos = (self.pos[0] + direction[0], self.pos[1] + direction[1])
+ if self.can_move(direction, level) and new_pos not in self.been:
+ self.pos = new_pos
+ self.been.append(new_pos)
+ steps += 1
+ if steps >= 2:
+ break
+ if caught():
+ break
+ if steps == 0:
+ self.been = []
return True
def reset_pos(self):