def __init__(self, game, name): Scene.__init__(self, game, name) self.current_board = None self.target_depth = TARGET_DEPTH self.radius = 20 self.padding = 8 self.px = 10 self.py = 20 # self.eval_function = eval_functions.F1() # self.eval_function = eval_functions.F2() self.eval_function = eval_functions.F2() self.game_state = WHITE_TO_MOVE_STATE self._hoverX = None self._hoverY = None self._think_until = -1 self._display_until = -1 self._display = '' self._consecutive_passes = 0 self._highlightX = 0 self._highlightY = 0 self._highlight_until = -1 r = 2 * self.radius + 2 * self.padding self.reset_button = self.append(Button(self.game, 9 * r, 8 * r, 75, 30, 'Reset')) self.reset_button.callback = self.__reset_game self.tag1 = '' self.tag2 = ''
def __init__(self, game, name=None, num_boids=8, w=10): Scene.__init__(self, game, name) self.color = 'black' for n in range(num_boids): self.append(Boid(self.game, w)) for n in range(3): self.append(Ball(self.game, 30, 10, 'green')) for n in range(1): self.append(Ball(self.game, 30, 20, 'red'))
def handle_keydown(self, event): Scene.handle_keydown(self, event) if event.key == 'a' or event.key == 'ArrowLeft': self._set_direction(LEFT) elif event.key == 'd' or event.key == 'ArrowRight': self._set_direction(RIGHT) elif event.key == 'w' or event.key == 'ArrowUp': self._set_direction(UP) elif event.key == 's' or event.key == 'ArrowDown': self._set_direction(DOWN)
def handle_keydown(self, event): Scene.handle_keydown(self, event) if event.key == 'a' or event.key == 'ArrowLeft': self.key = 'LEFT' elif event.key == 'd' or event.key == 'ArrowRight': self.key = 'RIGHT' elif event.key == 'w' or event.key == 'ArrowUp': self.key = 'UP' elif event.key == 's' or event.key == 'ArrowDown': self.key = 'DOWN'
def handle_mouseup(self, event): Scene.handle_mouseup(self, event) x, y = self.__find_cell(event.x, event.y) if DEBUG: console.log('click: cell=' + x + ',' + y + ' ' + self.current_board.is_piece(x, y)) if self.game_state == WHITE_TO_MOVE_STATE: move = self.current_board.is_move(x, y) if DEBUG: self.__display_message(" " + x + "," + y) if move is not None: self.__make_move(move)
def __init__(self, game, name): Scene.__init__(self, game, name) self.sound_add_apple = None self.mode = GAME_BEGIN self.snake = Snake(game) self.append(self.snake) self.apples = [] self.add_apple(BOARD_SIZE - 3, BOARD_SIZE // 2) self.reset_board() self.sound_add_apple = self.game.load_audio('add_apple') self.sound_collision = self.game.load_audio('collision') self.sound_eat_apple = self.game.load_audio('eat_apple')
def update(self, delta_time: float): Scene.update(self, delta_time) if self.key is not None: delta_angle = (delta_time / 1000.0) * A_SPEED for star in self: if self.key == 'LEFT': star.x, star.z = self.rotate(star.x, star.z, -delta_angle) elif self.key == 'RIGHT': star.x, star.z = self.rotate(star.x, star.z, delta_angle) elif self.key == 'UP': star.y, star.z = self.rotate(star.y, star.z, -delta_angle) elif self.key == 'DOWN': star.y, star.z = self.rotate(star.y, star.z, delta_angle) self.key = None
def update(self, delta_time): Scene.update(self, delta_time) if self.game_state == BLACK_TO_MOVE_STATE: if self._think_until < 0: self._think_until = self.game.get_time() + THINK_TIME if self.game.get_time() < self._think_until: self.__search_for_best_move(self.current_board) return if self.current_board.move_count() == 0: self.__make_move(None) return best_move = self.__search_for_best_move(self.current_board) if best_move is not None: self.__make_move(best_move) else: if self.current_board.move_count() == 0: self.__make_move(None)
def handle_mousemove(self, event): Scene.handle_mousemove(self, event) self.mouse_x = event.mouseX self.mouse_y = event.mouseY self.offsetX = event.offsetX self.offsetY = event.offsetY self.clientX = event.clientX self.clientY = event.clientY self.screenX = event.screenX self.screenY = event.screenY self.layerX = event.layerX self.layerY = event.layerY self.pageX = event.pageX self.pageY = event.pageY self.win_x = event.currentTarget.screenX self.win_y = event.currentTarget.screenY self.win_left = event.currentTarget.screenLeft self.win_top = event.currentTarget.screenTop self.win_app = event.currentTarget.navigator.appCodeName self.win_app_version = event.currentTarget.navigator.appVersion
def draw(self, ctx): global DEBUG Scene.draw(self, ctx) ctx.save() ctx.globalCompositeOperation = 'source-over' ctx.strokeStyle = 'black' ctx.lineWidth = 2 ctx.beginPath() board_width = BOARD_SIZE * CELL_SIZE ctx.rect(BOARD_MARGIN_LEFT, BOARD_MARGIN_TOP, board_width, board_width) ctx.stroke() if DEBUG: ctx.strokeStyle = '#CCCCCC' ctx.lineWidth = 1 ctx.beginPath() for n in range(1, BOARD_SIZE): ctx.moveTo(BOARD_MARGIN_LEFT, BOARD_MARGIN_TOP + n * CELL_SIZE) ctx.lineTo(BOARD_MARGIN_LEFT + board_width, BOARD_MARGIN_TOP + n * CELL_SIZE) ctx.moveTo(BOARD_MARGIN_LEFT + n * CELL_SIZE, BOARD_MARGIN_TOP) ctx.lineTo(BOARD_MARGIN_LEFT + n * CELL_SIZE, BOARD_MARGIN_TOP + board_width) ctx.stroke() ctx.restore()
def __init__(self, game, name, num_balls=8): Scene.__init__(self, game, name) self.my_sound = game.load_audio('mySound') for n in range(num_balls): self.append(BallSprite(self.game, 100, 10))
def draw(self, ctx): Scene.draw(self, ctx) self.__draw_board(ctx)
def draw(self, ctx): Scene.draw(self, ctx) ctx.save() ctx.globalCompositeOperation = 'source-over' # ctx.beginPath() ctx.font = self.font ctx.fillStyle = 'black' ctx.strokeStyle = 'black' ctx.lineWidth = 2 lh = self.line_height / 2 ctx.beginPath() ctx.moveTo(self.mouse_x - lh, self.mouse_y - lh) ctx.lineTo(self.mouse_x + lh, self.mouse_y + lh) ctx.moveTo(self.mouse_x + lh, self.mouse_y - lh) ctx.lineTo(self.mouse_x - lh, self.mouse_y + lh) ctx.closePath() ctx.stroke() yy = 20 ctx.fillText('Key down: {}'.format(self.key_down), 20, yy) yy = yy + self.line_height ctx.fillText('Mouse down: {}'.format(self.mouse_down), 20, yy) yy = yy + self.line_height yy = yy + self.line_height ctx.fillText('Mouse: {}, {}'.format(self.mouse_x, self.mouse_y), 20, yy) yy = yy + self.line_height ctx.fillText('Offset: {}, {}'.format(self.offsetX, self.offsetY), 20, yy) yy = yy + self.line_height ctx.fillText('Client: {}, {}'.format(self.clientX, self.clientY), 20, yy) yy = yy + self.line_height ctx.fillText('Layer: {}, {}'.format(self.layerX, self.layerY), 20, yy) yy = yy + self.line_height ctx.fillText('Page: {}, {}'.format(self.pageX, self.pageY), 20, yy) yy = yy + self.line_height ctx.fillText('Screen: {}, {}'.format(self.screenX, self.screenY), 20, yy) yy = yy + self.line_height yy = yy + self.line_height ctx.fillText('Canvas: {}'.format(self.game.canvas.localName), 20, yy) yy = yy + self.line_height ctx.fillText( 'width/height: {}, {}'.format(self.game.canvas.width, self.game.canvas.height), 40, yy) yy = yy + self.line_height ctx.fillText( 'left/top: {}, {}'.format(self.game.canvas.offsetLeft, self.game.canvas.offsetTop), 40, yy) yy = yy + self.line_height yy = yy + self.line_height ctx.fillText('Window: {}'.format(self.win_app), 20, yy) yy = yy + self.line_height ctx.fillText('x/y: {}, {}'.format(self.win_x, self.win_y), 40, yy) yy = yy + self.line_height ctx.fillText('left/top: {}, {}'.format(self.win_left, self.win_top), 40, yy) yy = yy + self.line_height yy = yy + self.line_height ctx.fillText(self.win_app_version, 20, yy) ctx.lineWidth = 1 ctx.strokeStyle = 'blue' ctx.rect(0, 0, self.game.canvas.width, self.game.canvas.height) ctx.stroke() ctx.strokeStyle = 'green' ctx.beginPath() ctx.moveTo(300, 300 - lh) ctx.lineTo(300, 300 + lh) ctx.moveTo(300 - lh, 300) ctx.lineTo(300 + lh, 300) ctx.closePath() ctx.stroke() ctx.restore()
def handle_keydown(self, event): Scene.handle_keydown(self, event) self.key_down = event.key
def handle_mousedown(self, event): Scene.handle_mousedown(self, event) self.mouse_down = True
def handle_mouseup(self, event): Scene.handle_mouseup(self, event) self.mouse_down = False
def update(self, delta_time: float): Scene.update(self, delta_time) pass
def __init__(self, game, name, num_stars=100): Scene.__init__(self, game, name) self.background_color = "rgb(0,0,0)" self.key = None for n in range(num_stars): self.append(Star(self.game))
def __init__(self, game, name, num_balls=8): Scene.__init__(self, game, name) for n in range(num_balls): self.append(Ball(self.game, 100, 10))
def handle_mousemove(self, event): Scene.handle_mousemove(self, event) if self.game_state == WHITE_TO_MOVE_STATE: self._hoverX, self._hoverY = self.__find_cell(event.x, event.y) else: self._hoverX, self._hoverY = None, None
def handle_mousedown(self, event): Scene.handle_mousedown(self, event)