if k == 'w': move.forward(5) elif k == 'a': move.left(5) elif k == 's': move.back(5) elif k == 'd': move.right(5) screen.ontimer(move_it, 25) keys = [] move = Turtle() screen = Screen() move.speed(0) screen.onkeypress(lambda:key_add('w'), "w") screen.onkeypress(lambda:key_add('a'), "a") screen.onkeypress(lambda:key_add('s'), "s") screen.onkeypress(lambda:key_add('d'), "d") screen.onkeyrelease(lambda:key_remove('w'), 'w') screen.onkeyrelease(lambda:key_remove('a'), 'a') screen.onkeyrelease(lambda:key_remove('s'), 's') screen.onkeyrelease(lambda:key_remove('d'), 'd') screen.listen() screen.ontimer(move_it, 25) screen.mainloop()
class TetrisBoard(object): def __init__(self, cols, rows): self.cols, self.rows = cols, rows self.screen = Screen() self.screen.screensize(BLOCKWIDTH*cols-50, BLOCKWIDTH*rows-50) self.screen.setup(BLOCKWIDTH*cols+12, BLOCKWIDTH*rows+12) self.screen.title("Turtle Tetris") self.screen.bgcolor("black") self.writer = Turtle() self.writer.ht() self.label = None self.grid = {} self.screen.tracer(False) for row in range(rows): for col in range(cols): self.grid[(col, row)] = TetrisTurtle(col, row) self.screen.tracer(True) self.brick = TetrisBrick(self) self.result = 0 self.LEVEL = 0.6 self.keybuffer = KeyBuffer(self.screen, ["Right", "Left", "Up", "Down", "space", "Escape"]) self.reset() self.screen.listen() self.t1 = time() def reset(self): self.result = 0 self.LEVEL = 0.600 self.screen.tracer(False) self.writer.clear() if self.label: self.writer.clearstamp(self.label) for x in range(COLUMNS): for y in range(ROWS): self.grid[(x,y)].fillcolor("") self.screen.tracer(True) self.state = "NEWBRICK" def blink(self, y, n=1): for _ in range(n): for color in ("white", "black"): self.screen.tracer(False) for x in range(COLUMNS): self.grid[(x,y)].pencolor(color) sleep(self.LEVEL/10.0) self.screen.tracer(True) def display_result(self): tb = self tb.writer.color("white", "gray20") tb.writer.shape("square") tb.writer.shapesize(5, 15) tb.writer.goto(-4 ,0) self.label = tb.writer.stamp() tb.writer.goto(-2,3) tb.writer.write(str(tb.result) + " rows!", align="center", font = ("Courier", 24, "bold") ) tb.writer.goto(-2,-22) tb.writer.write("New game : <spacebar>", align="center", font = ("Courier", 16, "bold") ) tb.writer.goto(-2,-42) tb.writer.write("Quit : <escape>", align="center", font = ("Courier", 16, "bold") ) def getcolor(self, col, row): return self.grid[(col, row)].fillcolor() def setcolor(self, col, row, color): return self.grid[(col, row)].fillcolor(color) def rowfree(self, row): return not any([self.getcolor(col, row) for col in range(COLUMNS)]) def rowfull(self, row): return all([self.getcolor(col, row) for col in range(COLUMNS)]) def cleanup(self, shp): try: ymax = max([y for (x,y) in shp]) except ValueError: self.state = "FINIS" return currenty = ymax while currenty > 0: if self.rowfull(currenty): self.blink(currenty, 2) self.result += 1 if self.result == 8: self.LEVEL = 0.4 elif self.result == 20: self.LEVEL = 0.25 y = currenty while True: self.screen.tracer(False) for c in range(COLUMNS): self.setcolor(c, y, self.getcolor(c, y-1)) self.screen.tracer(True) if self.rowfree(y): break else: y -= 1 else: currenty -= 1 tetris.state = "NEWBRICK" def run(self): tb = self b = self.brick ### actions to be done unconditionally if tb.state == "NEWBRICK": if b.reset(): self.t1 = time() tb.state = "FALL" else: tb.state = "FINIS" t2 = time() if tb.state == "FALL" and t2 - self.t1 > self.LEVEL: b.down() b.apply("Step") self.t1 = t2 ### actions bound to key events key = self.keybuffer.getkey() if key: if tb.state == "FALL": if key == "Left": b.shiftleft() elif key == "Right": b.shiftright() elif key == "Down": b.drop() tb.state = "CLEANUP" elif key == "Up": b.turn() elif key == "space": tb.state = "BREAK" b.apply(key) elif tb.state == "BREAK": if key == "space": tb.state = "FALL" elif tb.state == "ADE": if key == "space": tb.reset() tb.state = "NEWBRICK" elif key == "Escape": tb.screen.bye() if tb.state == "CLEANUP": tb.cleanup(b.shape1) if tb.state == "FINIS": tb.display_result() tb.state = "ADE" self.screen.ontimer(self.run, 100)
class Game: def __init__(self): # game object has a screen, a turtle, a basic snake and a food self.screen = Screen() self.artist = Turtle(visible=False) self.artist.up() self.artist.speed("slowest") self.snake = Snake() self.food = Food(100, 0) self.counter = 0 # this will be used later self.commandpending = False # as will this self.screen.tracer(0) # follow it so far? self.screen.listen() self.screen.onkey(self.snakedown, "Down") self.screen.onkey(self.snakeup, "Up") self.screen.onkey(self.snakeleft, "Left") self.screen.onkey(self.snakeright, "Right") def nextFrame(self): self.artist.clear() if (self.snake.nextposition[0], self.snake.nextposition[1]) == (self.food.x, self.food.y): self.snake.eatFood() self.food.changelocation() else: self.snake.moveOneStep() if self.counter == 10: self.food.changestate() # makes the food flash slowly self.counter = 0 else: self.counter += 1 self.food.drawself(self.artist) # show the food and snake self.snake.drawself(self.artist) self.screen.update() self.screen.ontimer(lambda: self.nextFrame(), 100) def snakeup(self): if not self.commandpending: self.commandpending = True self.snake.moveup() self.commandpending = False def snakedown(self): if not self.commandpending: self.commandpending = True self.snake.movedown() self.commandpending = False def snakeleft(self): if not self.commandpending: self.commandpending = True self.snake.moveleft() self.commandpending = False def snakeright(self): if not self.commandpending: self.commandpending = True self.snake.moveright() self.commandpending = False
global swimSpeed swimSpeed += 5 def keyDown(): global swimSpeed swimSpeed -= 5 def keyHome(): bob.goto(0, 0) def swim(): bob.forward(swimSpeed) ocean.ontimer(swim, 200) ocean.onkey(keyLeft, "Left") ocean.onkey(keyRight, "Right") ocean.onkey(keyUp, "Up") ocean.onkey(keyDown, "Down") #ocean.onkey(keyHome, "h") ocean.onkey((lambda: bob.goto(0,0)), "h") print("Awake the ocean") ocean.listen() ocean.ontimer(swim, 200) print("Now Rest") ocean.mainloop() print("All is done")
if not self.commandpending: self.commandpending = True self.snake.moveup() self.commandpending = False def snakedown(self): if not self.commandpending: self.commandpending = True self.snake.movedown() self.commandpending = False def snakeleft(self): if not self.commandpending: self.commandpending = True self.snake.moveleft() self.commandpending = False def snakeright(self): if not self.commandpending: self.commandpending = True self.snake.moveright() self.commandpending = False game = Game() screen = Screen() screen.ontimer(lambda: game.nextFrame(), 100) screen.mainloop()
class TetrisBoard(object): def __init__(self, cols, rows): self.cols, self.rows = cols, rows self.screen = Screen() self.screen.screensize(BLOCKWIDTH * cols - 50, BLOCKWIDTH * rows - 50) self.screen.setup(BLOCKWIDTH * cols + 12, BLOCKWIDTH * rows + 12) self.screen.title("Turtle Tetris") self.screen.bgcolor("black") self.writer = Turtle() self.writer.ht() self.label = None self.grid = {} self.screen.tracer(False) for row in range(rows): for col in range(cols): self.grid[(col, row)] = TetrisTurtle(col, row) self.screen.tracer(True) self.brick = TetrisBrick(self) self.result = 0 self.LEVEL = 0.6 self.keybuffer = KeyBuffer( self.screen, ["Right", "Left", "Up", "Down", "space", "Escape"]) self.reset() self.screen.listen() self.t1 = time() def reset(self): self.result = 0 self.LEVEL = 0.600 self.screen.tracer(False) self.writer.clear() if self.label: self.writer.clearstamp(self.label) for x in range(COLUMNS): for y in range(ROWS): self.grid[(x, y)].fillcolor("") self.screen.tracer(True) self.state = "NEWBRICK" def blink(self, y, n=1): for _ in range(n): for color in ("white", "black"): self.screen.tracer(False) for x in range(COLUMNS): self.grid[(x, y)].pencolor(color) sleep(self.LEVEL / 10.0) self.screen.tracer(True) def display_result(self): tb = self tb.writer.color("white", "gray20") tb.writer.shape("square") tb.writer.shapesize(5, 15) tb.writer.goto(-4, 0) self.label = tb.writer.stamp() tb.writer.goto(-2, 3) tb.writer.write(str(tb.result) + " rows!", align="center", font=("Courier", 24, "bold")) tb.writer.goto(-2, -22) tb.writer.write("New game : <spacebar>", align="center", font=("Courier", 16, "bold")) tb.writer.goto(-2, -42) tb.writer.write("Quit : <escape>", align="center", font=("Courier", 16, "bold")) def getcolor(self, col, row): return self.grid[(col, row)].fillcolor() def setcolor(self, col, row, color): return self.grid[(col, row)].fillcolor(color) def rowfree(self, row): return not any([self.getcolor(col, row) for col in range(COLUMNS)]) def rowfull(self, row): return all([self.getcolor(col, row) for col in range(COLUMNS)]) def cleanup(self, shp): try: ymax = max([y for (x, y) in shp]) except ValueError: self.state = "FINIS" return currenty = ymax while currenty > 0: if self.rowfull(currenty): self.blink(currenty, 2) self.result += 1 if self.result == 8: self.LEVEL = 0.4 elif self.result == 20: self.LEVEL = 0.25 y = currenty while True: self.screen.tracer(False) for c in range(COLUMNS): self.setcolor(c, y, self.getcolor(c, y - 1)) self.screen.tracer(True) if self.rowfree(y): break else: y -= 1 else: currenty -= 1 tetris.state = "NEWBRICK" def run(self): tb = self b = self.brick ### actions to be done unconditionally if tb.state == "NEWBRICK": if b.reset(): self.t1 = time() tb.state = "FALL" else: tb.state = "FINIS" t2 = time() if tb.state == "FALL" and t2 - self.t1 > self.LEVEL: b.down() b.apply("Step") self.t1 = t2 ### actions bound to key events key = self.keybuffer.getkey() if key: if tb.state == "FALL": if key == "Left": b.shiftleft() elif key == "Right": b.shiftright() elif key == "Down": b.drop() tb.state = "CLEANUP" elif key == "Up": b.turn() elif key == "space": tb.state = "BREAK" b.apply(key) elif tb.state == "BREAK": if key == "space": tb.state = "FALL" elif tb.state == "ADE": if key == "space": tb.reset() tb.state = "NEWBRICK" elif key == "Escape": tb.screen.bye() if tb.state == "CLEANUP": tb.cleanup(b.shape1) if tb.state == "FINIS": tb.display_result() tb.state = "ADE" self.screen.ontimer(self.run, 100)
#if ((self.x > 300) or (self.x < -300)): # self.dx *= -1 #if ((self.y > 300) or (self.y < -300)): # self.dy *= -1 colors = ['red', 'blue', 'green', 'orange', 'purple', 'black', 'plum'] bubbles = [] def blowBubble(x, y): color1 = random.choice(colors) color2 = random.choice(colors) b = Bubble(x, y, color1, color2) b.randomBump() bubbles.append(b) print('Number of bubbles', len(bubbles)) def tictoc(): # Simulate the passage of time for each bubbble for b in bubbles: b.sim() ocean.update() ocean.ontimer(tictoc, 20) ocean.onclick(blowBubble) ocean.ontimer(tictoc, 20)
currentMinuteInternal = datetime.datetime.now().minute degree = degree + -0.5 * currentMinuteInternal hourHand.setheading(degree) wn.ontimer(moveHourHand, 60000) # moving minute hand def moveMinuteHand(): currentMinuteInternal = datetime.datetime.now().minute degree = (currentMinuteInternal - 15) * -6 currentSecondInternal = datetime.datetime.now().second degree = degree + (-currentSecondInternal * 0.1) minuteHand.setheading(degree) wn.ontimer(moveMinuteHand, 1000) # moving second hand def moveSecondHand(): currentSecondInternal = datetime.datetime.now().second degree = (currentSecondInternal - 15) * -6 secondHand.setheading(degree) wn.ontimer(moveSecondHand, 1000) # on timer infinite loop wn.ontimer(moveHourHand, 1) wn.ontimer(moveMinuteHand, 1) wn.ontimer(moveSecondHand, 1) wn.exitonclick()
if blocks: # stop all changes if/when all blocks turn white screen.ontimer(change, DELAY) HALF_SIZE = SIZE // 2 screen = Screen() screen.colormode(WHITE[0]) screen.register_shape("block", ((HALF_SIZE, -HALF_SIZE), (HALF_SIZE, HALF_SIZE), (-HALF_SIZE, HALF_SIZE), (-HALF_SIZE, -HALF_SIZE))) screen.tracer(GRID ** 2) # ala @PyNuts turtle = Turtle(shape="block", visible=False) turtle.speed("fastest") turtle.up() Block = namedtuple('Block', ['position', 'color', 'stamp']) blocks = list() HALF_GRID = GRID // 2 for x in range(-HALF_GRID, HALF_GRID): for y in range(-HALF_GRID, HALF_GRID): turtle.goto(x * SIZE, y * SIZE) color = [randrange(ceil(INCREASE), DARK) for primary in WHITE] turtle.color(color) blocks.append(Block(turtle.position(), color, turtle.stamp())) screen.ontimer(change, DELAY) screen.exitonclick()