''' Created on 6 dec. 2013 Author: Daan Helsloot ''' from ipy_lib import SnakeUserInterface ui = SnakeUserInterface(40, 30, 0.5) def processEvent(event): ui.print_(event.name + " " + event.data + "\n") if event.name == "click": processClick(event) elif event.name == "other": processSpacebar(event) def processClick(event): click_coordinates = event.data.split() x = int(click_coordinates[0]) y = int(click_coordinates[1]) ui.place(x, y, ui.WALL) ui.show() def processSpacebar(event): ui.clear() ui.show() while True: event = ui.get_event()
print('GAME OVER') 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())
# 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
for element in self.row: if element != self.row[0]: old_x, old_y = element.change_coordinate(old_x, old_y) def check_coordinate(self, coordinate): result = False for element in self.row: if element.coordinate() == coordinate.coordinate(): result = True return result # CONSTANTS X_RANGE = 32 Y_RANGE = 24 ui = SnakeUserInterface(X_RANGE, Y_RANGE) fps = 5 count = 0 direction = 'r' snake = CoordinateRow([Coordinate(1, 0), Coordinate(0, 0)]) walls_list = CoordinateRow([]) def change_direction(event=None, mystic=False): global direction if event is None: # In case of level direction_change = direction else: # event is an event: direction_change = event.data if mystic:
''' Created on 6 dec. 2013 Author: Daan Helsloot ''' from ipy_lib import SnakeUserInterface ui = SnakeUserInterface(40, 30, 0.5) def processEvent(event): ui.print_(event.name + " " + event.data + "\n") if event.name == "click": processClick(event) elif event.name == "other": processSpacebar(event) def processClick(event): click_coordinates = event.data.split() x = int(click_coordinates[0]) y = int(click_coordinates[1]) ui.place(x, y, ui.WALL) ui.show() def processSpacebar(event): ui.clear() ui.show() while True:
else: self.y -= 1 def down(self): if self.y >= HEIGHT - 1: self.y = 0 else: self.y += 1 def process_event(new_event): if new_event.name == "arrow": if new_event.data == "l": head_of_snake.left() elif new_event.data == "r": head_of_snake.right() elif new_event.data == "u": head_of_snake.up() elif new_event.data == "d": head_of_snake.down() ui.place(head_of_snake.x, head_of_snake.y, ui.SNAKE) ui.show() ui.clear() ui = SnakeUserInterface(WIDTH, HEIGHT, SCALE) head_of_snake = HeadOfSnake(x_coordinate, y_coordinate) while True: event = ui.get_event() process_event(event)
__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)
Created on 8th of december 2013 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))
# split the data on space, and return the coordinates as integers def get_coordinates_from_data(data): data = data.split() return int(data[0]), int(data[1]) # onClick -> place a block on the spot def process_click(data): x,y = get_coordinates_from_data(data) ui.place(x, y, ui.WALL) ui.show() # process all events that come in def process_event(event): print_event(event) # Note: makes the program run slow if event.name == "click" : process_click(event.data) elif event.name == "other": process_other(event.data) # START PROGRAM # initialize the visual interface ui = SnakeUserInterface(UI_WIDTH, UI_HEIGHT) ui.show() # 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