Пример #1
0
 def change_screen_button_action(self, button, tup):
     if (button.button_id == self.start_game_button.button_id) and (
             tup[0] == "choose_dimensions"):
         self.change_screen(tup[0])
     if button.button_id == self.dimensions_next.button_id:
         self.change_screen(tup[0])
     if (button.button_id
             == self.manual_mode.button_id) and (tup[0] == "main_game"):
         global game
         game = Game(int(self.d_box.get_text()),
                     100,
                     500,
                     200,
                     600,
                     bg=pygame.Color("brown"),
                     mode="manual",
                     box_colour=pygame.Color("yellow"))
         self.change_screen(tup[0])
     if (button.button_id
             == self.challenger_mode.button_id) and (tup[0] == "main_game"):
         game = Game(int(self.d_box.get_text()),
                     100,
                     500,
                     200,
                     600,
                     bg=pygame.Color("brown"),
                     mode="challenger",
                     box_colour=pygame.Color("yellow"))
         game.randomise()
         self.sw = stopwatch.Stopwatch(screen,
                                       font=pygame.font.Font(
                                           "freesansbold.ttf", 50),
                                       text_spacing=5,
                                       bg_colour=pygame.Color("lightgreen"))
         self.sw.set_center((int(WIDTH / 2), 50))
         self.first_move = False
         game.first_move_played = False
         self.change_screen(tup[0])
     if (button.button_id == self.back_choose_mode.button_id) and (
             tup[0] == "choose_dimensions"):
         self.change_screen(tup[0])
     if (button.button_id
             == self.back_main_game.button_id) and (tup[0]
                                                    == "choose_mode"):
         self.change_screen(tup[0])
     if button.button_id == self.back_end_screen.button_id:
         self.d_box.set_text("")
         self.change_screen(tup[0])
Пример #2
0
    def __init__(self, cSpeed=0, cHeight=0, cMusic='a', ):
        self.speed = self.startSpeed = cSpeed # 0-9
        self.startHeight = cHeight # 0-5
        self.score = 0
        self.bag = bag.bag(7)
        self.lines = 0
        self.timer = stopwatch.Stopwatch()
        self.pause = False
        
        # generate height garbage

        self.field = field.Field()

        # populate blocks
        self.currPiece = piece.Piece(self.bag.grab())
        self.nextPiece = piece.Piece(self.bag.grab())
Пример #3
0
def main():

    n = int(sys.argv[1])

    stdio.writeln('Nanoseconds per operation')

    #-------------------------------------------------------------------
    # Empty loop
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(n):
            pass
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Empty loop')

    #-------------------------------------------------------------------
    # Integer addition
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = i + j
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Integer addition')

    #-------------------------------------------------------------------
    # Integer subtraction
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = i - j
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Integer subtraction')

    #-------------------------------------------------------------------
    # Integer multiplication
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = i * j
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Integer multiplication')

    #-------------------------------------------------------------------
    # Integer division
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = i // j
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Integer division')

    #-------------------------------------------------------------------
    # Integer remainder
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = i % j
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Integer remainder')

    #-------------------------------------------------------------------
    # Comparison
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            b = (i < j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Comparison')

    #-------------------------------------------------------------------
    # Floating point addition
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = float(i) + float(j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Floating point addition')

    #-------------------------------------------------------------------
    # Floating point subtraction
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = float(i) - float(j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Floating point subtraction')

    #-------------------------------------------------------------------
    # Floating point multiplication
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = float(i) * float(j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Floating point multiplication')

    #-------------------------------------------------------------------
    # Floating point division
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = float(i) / float(j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Floating point division')

    #-------------------------------------------------------------------
    # Function call
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = f(i, j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Function call')

    #-------------------------------------------------------------------
    # math.sin
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n // 10 + 1):
        for j in range(1, n + 1):
            k = math.sin(i + j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n) * 10.0
    stdio.writef('%7.1f: %s\n', freq, 'math.sin')

    #-------------------------------------------------------------------
    # math.atan2
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n // 10 + 1):
        for j in range(1, n + 1):
            k = math.atan2(i, j)
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n) * 10.0
    stdio.writef('%7.1f: %s\n', freq, 'math.atan2')

    #-------------------------------------------------------------------
    # random.random
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n // 10 + 1):
        for j in range(1, n + 1):
            k = random.random()
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n) * 10.0
    stdio.writef('%7.1f: %s\n', freq, 'random.random')

    #-------------------------------------------------------------------
    # Integer addition
    #-------------------------------------------------------------------

    timer = stopwatch.Stopwatch()
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            k = i + j
    elapsed = timer.elapsedTime()
    freq = 1.0E9 * elapsed / float(n) / float(n)
    stdio.writef('%7.1f: %s\n', freq, 'Integer addition again')
Пример #4
0
    def play(self):
        """ Actually play the game. """
        print "pygame initializing...",
        pygame.init()
        print "done"
        
        self.screen = pygame.display.set_mode((750, 750))
        pygame.display.set_caption("Kyle's Nottris")
        # load resources
        class Resources:
            pass
        print "image loading..."
        self.resources = Resources()
        print " instructions...",
        self.resources.controls = pygame.image.load('images/instructions.png').convert()
        print "done"
        self.resources.blocks = []
        print " blocks...",
        self.resources.blocks.append(pygame.image.load('images/t.png').convert())
        print "t,",
        self.resources.blocks.append(pygame.image.load('images/l.png').convert())
        print "l,",
        self.resources.blocks.append(pygame.image.load('images/j.png').convert())
        print "j,",
        self.resources.blocks.append(pygame.image.load('images/s.png').convert())
        print "s,",
        self.resources.blocks.append(pygame.image.load('images/z.png').convert())
        print "z,",
        self.resources.blocks.append(pygame.image.load('images/i.png').convert())
        print "i,",
        self.resources.blocks.append(pygame.image.load('images/o.png').convert())
        print "o"

        print " loading field...",
        self.resources.field = pygame.image.load('images/field.png').convert()
        print "done"
        print "loading font...",
        self.resources.font = pygame.font.get_default_font()
        self.resources.font = pygame.font.Font(self.resources.font, 28)
        print "done"

        print "loading complete"
        counter = stopwatch.Stopwatch()

        # show controls and wait for enter
        self.render(bg=False, blocks=False, piece=False, time=False, score=False, lines=False, next=False, intro=True)
        while True:
            event = pygame.event.poll()
            if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    break
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        
        dirty = True
        self.timer.start()

        while self.newPiece(): # possible to spawn a new block (and do it)
            self.render(bg=True, score=True, lines=True, next=True)
            while self.currPiece.goDrop(self.field):
                drop = False
                dropTime = lvl2time.lvl2time(self.speed)
                counter.start()
                while counter.check() < dropTime:
                    pygame.time.wait(5) # share the processor
                    # event handling
                    event = pygame.event.poll()
                    if event.type == KEYDOWN:
                        if event.key == K_LEFT:
                            self.currPiece.goLeft(self.field)
                            dirty = True
                        if event.key == K_RIGHT:
                            self.currPiece.goRight(self.field)
                            dirty = True
                        if event.key == K_z or event.key == K_UP:
                            self.currPiece.goRotCW(self.field)
                            dirty = True
                        if event.key == K_x:
                            self.currPiece.goRotCCW(self.field)
                            dirty = True
                    if (event.type == KEYDOWN and event.key == K_ESCAPE) or event.type == QUIT:
                        pygame.quit()
                        sys.exit()

                    if dirty:
                        self.render()
                        dirty = False

                    # soft drop
                    keys = pygame.key.get_pressed()
                    if keys[K_DOWN]:
                        drop = True
                    if drop: # and counter.check() > datetime.timedelta(seconds=.05):
                        pygame.time.wait(5)
                        self.currPiece.score += 1
                        break

                self.render()

            self.recieve(self.field.addPiece(self.currPiece))
            self.clearLines(self.field.clearLines(self.speed))
            #self.speed += 1
            if self.lines > self.speed * 10 - 1: # every ten lines gives speed lvl-up
                self.speed += 1
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb  5 13:09:10 2020

COIS 4050H: Assignment #1, Problem 4
@author: S. Chapados

PROGRAM FILE
"""

import Matrix, stopwatch, math

SIZE = int(math.pow(2, 5))

timer = stopwatch.Stopwatch()

A = Matrix.Matrix(SIZE)
A.setRandomValues()

B = Matrix.Matrix(SIZE)
B.setRandomValues()

if SIZE < 25:
    print("A:")
    A.printMatrix()
    print("\nB:")
    B.printMatrix()

C = Matrix.Matrix(SIZE)
Пример #6
0
import cli
import stopwatch as sw
from maze import Maze

if __name__ == "__main__":
    m = Maze()
    sw = sw.Stopwatch()

    algorithms = [
        Maze.Solve.C,
        Maze.Solve.DEPTH,
        Maze.Solve.BREADTH
    ]

    index = cli.menu("Which algorithm do you want to use?", *[algorithm.value for algorithm in algorithms], result=True)
    algorithm = algorithms[index]

    cli.line()

    print("Loading maze...")
    sw.start()
    m.load_maze()
    sw.stop()
    print(sw.elapsed_str)

    cli.line()

    print("Solving maze...")
    sw.start()
    m.solve((), (), algorithm)
    sw.stop()
Пример #7
0
# pickle.dump(events, open('events.out', 'wb'))

# events_raw = pickle.load(open('events.out', 'rb'))
# events = [(e[0], e[1], round(e[2]-e[1], ndigits=3)) for e in events_raw]
# n = 5
# seqs = [events[i*n:(i+1)*n] for i in range(len(events)//n)]

# for i in range(len(seqs)):
#     tracker.pass_events(seqs[i])
#     print(tracker.get_best_agent())

pass_buffer = []
pass_ready = [False]
pass_cv = threading.Condition()

clock = stopwatch.Stopwatch()
clock.start()

print('BeatTracker online.')
threading.Thread(target=midi_in.get_input,
                 args=(pass_buffer, pass_ready, pass_cv, clock)).start()
threading.Thread(target=play.click, args=(clock, )).start()
tracker = bt.BeatTracker()

while True:
    with pass_cv:
        while not pass_ready[0]:
            pass_cv.wait()
        seq = pass_buffer[:]
        pass_ready[0] = False
Пример #8
0
    def __init__(self, screen_type):
        self.screen = screen_type
        test_width = pygame.font.Font("freesansbold.ttf",
                                      20).render("Start!", True,
                                                 (0, 0, 0)).get_width()

        self.pause_status = False
        self.time_pause = -1
        self.start_game_button = Button(
            screen,
            coordinates=(int(WIDTH / 2) - int(test_width / 2),
                         3 * int(HEIGHT / 4)),
            button_id="001",
            text="Start!",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("choose_dimensions", )),
            animation_speed=1)
        self.d_box = text_box.TextBox(screen,
                                      coordinates=(int(WIDTH / 2) - 25,
                                                   int(HEIGHT / 4)),
                                      max_characters=1,
                                      restricted_list=list(range(0, 10)),
                                      default_width=50)

        test_width = pygame.font.Font("freesansbold.ttf",
                                      20).render("Next>", True,
                                                 (0, 0, 0)).get_width()
        self.dimensions_next = Button(
            screen,
            coordinates=(int(WIDTH / 2) - int(test_width / 2),
                         4 * int(HEIGHT / 5)),
            button_id="002",
            text="Next>",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("choose_mode", )))

        test_width = pygame.font.Font("freesansbold.ttf",
                                      20).render("Manual", True,
                                                 (0, 0, 0)).get_width()
        self.manual_mode = Button(
            screen,
            coordinates=(int(WIDTH / 2) - int(test_width / 2),
                         int(HEIGHT / 2)),
            button_id="003",
            text="Manual",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("main_game", )))

        test_width = pygame.font.Font("freesansbold.ttf",
                                      20).render("Challenger", True,
                                                 (0, 0, 0)).get_width()
        self.challenger_mode = Button(
            screen,
            coordinates=(int(WIDTH / 2) - int(test_width / 2),
                         self.manual_mode.rect.bottom + 20),
            button_id="004",
            text="Challenger",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("main_game", )))

        self.back_choose_mode = Button(
            screen,
            coordinates=(20, HEIGHT - 40),
            button_id="005",
            text="Back",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("choose_dimensions", )))

        test_width = pygame.font.Font("freesansbold.ttf",
                                      20).render("Back", True,
                                                 (0, 0, 0)).get_width()
        self.back_main_game = Button(
            screen,
            coordinates=(WIDTH - test_width - 20, HEIGHT - 40),
            button_id="006",
            text="Back",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("choose_mode", )))

        test_width = pygame.font.Font("freesansbold.ttf",
                                      20).render("Play Again", True,
                                                 (0, 0, 0)).get_width()
        self.back_end_screen = Button(
            screen,
            coordinates=(int(WIDTH / 2) - int(test_width / 2),
                         3 * int(HEIGHT / 4)),
            button_id="007",
            text="Play Again!",
            font=pygame.font.Font("freesansbold.ttf", 20),
            action_on_enter=(self.change_screen_button_action,
                             ("choose_dimensions", )))

        self.sw = stopwatch.Stopwatch(screen,
                                      font=pygame.font.Font(
                                          "freesansbold.ttf", 50),
                                      text_spacing=5,
                                      bg_colour=pygame.Color("lightgreen"))
        self.sw.set_center((int(WIDTH / 2), 50))
        self.first_move = False
Пример #9
0
    if (msg.topic == "stopwatch/webInput/armed"):
        if (msg.payload == "true"):
            arm(True)
        else:
            arm(False)


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(mqttHost, mqttPort, 60)

client.loop_start()

stopwatch = stopwatch.Stopwatch()

old_input = stopwatch.get_input()


def arm(_state):
    if _state == True:
        stopwatch.armed = True
        stopwatch.led_Red.set_state(True)
        client.publish("stopwatch/armed", "true", retain=True)
    else:
        stopwatch.armed = False
        stopwatch.led_Red.set_state(False)
        client.publish("stopwatch/armed", "false", retain=True)

Пример #10
0
    #conn = psycopg2.connect("dbname='wahdb' user='******' host='wah-database.cg46hbeal28c.eu-west-1.rds.amazonaws.com' password='******'")
    #cur = conn.cursor()
    pass
except:
    print("I am unable to connect to the database")
    sys.exit(1)

mypath = "..\\..\\test\\capAlertTestAreas\\"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

out = {}

for file in onlyfiles:
    with open(join(mypath, file)) as myfile:

        with stopwatch.Stopwatch() as sw:
            [namespace, doc] = loadandvalidateCAP(myfile.read())

            namespaces = {'cap': namespace}

            identifier = doc.xpath("//cap:identifier/text()",
                                   namespaces=namespaces)[0]

            coords = doc.xpath("//cap:polygon/text()",
                               namespaces=namespaces)[0]
            coords = coords.split()
            coords = [coord.split(',') for coord in coords]
            coords = [(item[1], item[0])
                      for item in coords]  # reverse coordinates

            coordinates = ["{} {}".format(item[0], item[1]) for item in coords]