示例#1
0
def main():

    app = QApplication(sys.argv)
    PixmapCanvas.keyPressEvent = key_press
    canvas = PixmapCanvas(1000, 1000, init, update, 100)
    canvas.show()
    sys.exit(app.exec())
示例#2
0
def main():
    app = QApplication(sys.argv)
    PixmapCanvas.keyPressEvent = key_press
    PixmapCanvas.mousePressEvent = mouse_press
    canvas = PixmapCanvas(WIDTH,
                          HEIGHT,
                          init_func=init,
                          update_func=update,
                          anim_period=20)
    canvas.show()
    sys.exit(app.exec())
示例#3
0
def mouse_press(c: PixmapCanvas, event):

    x = event.pos().x()
    y = event.pos().y()

    row = int(x / c.cell_width)
    col = int(y / c.cell_height)

    c.gol.change(row, col)

    c.timeout(False)
示例#4
0
def update(c: PixmapCanvas, next_move=True):

    print("updating")

    c.p.fillRect(0, 0, c.width(), c.height(), Qt.white)

    if next_move:
        print("next move")
        c.gol.next_move()

    for row in range(c.gol.rows):
        for col in range(c.gol.cols):

            if c.gol.is_alive(row, col):
                c.p.fillRect(c.cell_width * row, c.cell_height * col,
                             c.cell_width, c.cell_height, Qt.black)
示例#5
0
def update(c: PixmapCanvas):

    eaten = c.snake.move()

    if c.snake.alive:
        c.p.setBrush(Qt.black)
    else:
        c.p.setBrush(Qt.red)
        c.timer.stop()
    c.p.setPen(Qt.NoPen)

    draw_rect(c, c.snake.body[0].x, c.snake.body[0].y)

    if not eaten:
        c.p.setBrush(Qt.white)

        draw_rect(c, c.tail.x, c.tail.y)
    else:
        c.snake.new_food()

        c.p.setBrush(Qt.green)

        draw_rect(c, c.snake.food.x, c.snake.food.y)

    c.tail = c.snake.body[-1]
示例#6
0
def init(c: PixmapCanvas):

    print("init")
    c.gol = GameOfLife(ROWS, COLS, solid_borders=SOLID_BORDERS, rules=RULES)
    c.timer.stop()
    c.timeout(False)

    c.cell_width = c.width() / c.gol.rows
    c.cell_height = c.height() / c.gol.cols

    print(c.cell_width, c.cell_height)
示例#7
0
def init(c: PixmapCanvas):

    c.background_color("white")
    c.snake = Snake()
    c.cell_width = c.width() / c.snake.width
    c.cell_height = c.height() / c.snake.height

    c.p.setPen(Qt.NoPen)
    c.p.setBrush(Qt.black)

    draw_rect(c, c.snake.body[0].x, c.snake.body[0].y)

    c.p.setBrush(Qt.green)

    draw_rect(c, c.snake.food.x, c.snake.food.y)

    c.tail = c.snake.body[0]
示例#8
0
def key_press(c: PixmapCanvas, event):

    if event.key() == Qt.Key_C:
        c.gol.clear()
        c.timer.stop()
        c.timeout()

    if event.key() == Qt.Key_R:
        c.gol.random(RANDOM_PERCENT_TRUE)
        c.timer.stop()
        c.timeout()

    if event.key() == Qt.Key_Space:
        if not c.timer.isActive():
            c.timeout()

    if event.key() == Qt.Key_P:
        if c.timer.isActive():
            c.timer.stop()
        else:
            c.timer.start(c.anim_period)