def move_snake(self): newx = self.snake[-1][0] + self.direction[0] newy = self.snake[-1][1] + self.direction[1] if newx >= self.width: newx -= self.width elif newx < 0: newx += self.width if newy >= self.height: newy -= self.height elif newy < 0: newy += self.height # Check if the snake crashed into itself if (newx,newy) in self.snake[1:]: for i in range(4): self.send(self.width*self.height*'\x00') self.wait(0.2) self.draw_snake() self.wait(0.2) text.showtext(self, str(len(self.snake)), spacing=0) self.wait(3) return True if (newx,newy) == self.point_to_eat: self.snake = self.snake + [(newx,newy)] self.new_point_to_eat() else: self.snake = self.snake[1:] + [(newx,newy)] self.draw_snake() return False
def run(self): MAZE_WIDTH = 8 # maze units, not pixels! MAZE_HEIGHT = 8 BLINK_DELAY = 0.3 self.controls = ['Up', 'Down', 'Left', 'Right'] while True: self.new_maze(MAZE_WIDTH, MAZE_HEIGHT) starttime = time() blinktime = starttime posblink = False goalblink = False while True: now = time() if now < blinktime + BLINK_DELAY / 2: key = self.waitforanykey(blinktime + BLINK_DELAY / 2 - now) else: key = 0 if key == 1: newposition = (self.position[0], self.position[1] - 1) elif key == 2: newposition = (self.position[0], self.position[1] + 1) elif key == 3: newposition = (self.position[0] - 1, self.position[1]) elif key == 4: newposition = (self.position[0] + 1, self.position[1]) elif key == 0: newposition = self.position goalblink = not goalblink if goalblink: posblink = not posblink blinktime = time() if self.check_pos(newposition): self.position = newposition if self.position == self.goal: break self.draw(posblink, goalblink) seconds = int(time() - starttime) self.clear() self.wait(1) text.showtext(self, str(seconds), spacing=0) self.wait(5) self.clear() self.wait(1)
def fall(self): if self.test_block(self.blockx,self.blocky+1,self.blockframe): self.blocky += 1 return # Block hits bottom for pixel in BLOCKS[self.blocktyp][self.blockframe]: self.canvas[self.blocky + pixel[1]] = \ self.canvas[self.blocky + pixel[1]][0:self.blockx + pixel[0]] + \ [1] + self.canvas[self.blocky + pixel[1]][self.blockx + pixel[0] + 1:] self.blocky = -20 for i in range(0, len(self.canvas)): if self.canvas[i] == self.width * [1]: self.blink(i) self.canvas = [self.width * [0]] + self.canvas[0:i] + \ self.canvas[i+1:] self.rows += 1 self.delay -= 0.02 self.new_block() self.falldelay = self.delay if not self.test_block(self.blockx,self.blocky,self.blockframe): self.blink(-1,0.2) text.showtext(self, str(self.rows), spacing=0) self.wait(3) self.newgame()