ui.wait(1000) ui.close() if food.eaten(snake.get_head()): food.remove(ui) food.generate(ui, snake.get_coordinates(), walls.get_coordinates()) snake.extend_tail() snake.place(ui) food.place(ui) walls.place(ui) ui.show() '''Start program''' ui = SnakeUserInterface(WIDTH, HEIGHT) ui.set_animation_speed(ANIMATION_SPEED) init_snake_coordinates = INIT_SNAKE_COORDINATES direction = INIT_DIRECTION walls = [] level = None # level = read_file('SnakeInput4.txt') # Uncomment if you want to play levels if level is not None: walls, init_snake_coordinates, direction = prepare_data_level(level) snake = Snake(init_snake_coordinates) walls = Walls(walls) food = Food((ui.random(MAX_X), ui.random(MAX_Y))) food.generate(ui, snake.get_coordinates(), walls.get_coordinates()) while True:
__author__ = 'Helsloot' WIDTH = 20 HEIGHT = 10 x = 0 y = 0 start_speed = 10 from ipy_lib import SnakeUserInterface ui = SnakeUserInterface(WIDTH, HEIGHT, 1) image = ui.WALL ui.set_animation_speed(start_speed) def processEvent(event): ui.print_(event.name + " " + event.data + "\n") if event.name == "alarm": processAnimation() if event.name == 'arrow': processArrow(event.data) if event.name == 'letter': processKey(event.data) def processAnimation(): global x, y if x < WIDTH: ui.place(x, y, image) ui.show() ui.place(x, y, ui.EMPTY)
# process all events that come in def process_event(event): # print_event(event) # Note: makes the program run slow if event.name == "letter": process_letter(event.data) elif event.name == "alarm": process_alarm(event.data) elif event.name == "arrow": process_arrow(event.data) # START PROGRAM # initialize the visual interface ui = SnakeUserInterface(UI_WIDTH, UI_HEIGHT) ui.show() # state variables current_color = ui.WALL current_position = START_POSITION current_speed = START_SPEED # initial settings ui.set_animation_speed(current_speed) ui.place(current_position[0], current_position[1], current_color) # infinite loop that parses the events while True: event = ui.get_event( ) # fetches the event using the built-in library function process_event(event) # event dispatcher
Author: Daan Helsloot (dht340) ''' WIDTH = 30 HEIGHT = 40 from Coordinate import Coordinate from SnakeCoordinateRow import SnakeCoordinateRow from ipy_lib import SnakeUserInterface start_speed = 5 apple_x = 0 apple_y = 0 current_direction = 'r' ui = SnakeUserInterface(WIDTH, HEIGHT) ui.set_animation_speed(start_speed) snake_instance = SnakeCoordinateRow() def process_event(event): if event.name == "alarm": chk_snake_tail_collision(snake_instance) process_animation() if event.name == 'arrow': set_direction(event.data) def create_snake(): global snake_instance snake_instance.add(Coordinate(0, 0)) snake_instance.add(Coordinate(1, 0))