def tap(x, y): "Store starting point or draw shape." start = state['start'] if start is None: state['start'] = vector(x, y) else: shape = state['shape'] end = vector(x, y) shape(start, end) state['start'] = None
def move(): "Move pacman and all ghosts." writer.undo() writer.write(state['score']) clear() if valid(pacman + aim): pacman.move(aim) index = offset(pacman) if tiles[index] == 1: tiles[index] = 2 state['score'] += 1 x = (index % 20) * 20 - 200 y = 180 - (index // 20) * 20 square(x, y) up() goto(pacman.x + 10, pacman.y + 10) dot(20, 'yellow') for point, course in ghosts: if valid(point + course): point.move(course) else: options = [ vector(5, 0), vector(-5, 0), vector(0, 5), vector(0, -5), ] plan = choice(options) course.x = plan.x course.y = plan.y up() goto(point.x + 10, point.y + 10) dot(20, 'red') update() for point, course in ghosts: if abs(pacman - point) < 20: return ontimer(move, 100)
def move(): "Update object positions." bird.y -= 5 for ball in balls: ball.x -= 3 if randrange(10) == 0: y = randrange(-199, 199) ball = vector(199, y) balls.append(ball) while len(balls) > 0 and not inside(balls[0]): balls.pop(0) if not inside(bird): draw(False) return for ball in balls: if abs(ball - bird) < 15: draw(False) return draw(True) ontimer(move, 50)
def move(): "Move ball and targets." if randrange(40) == 0: y = randrange(-150, 150) target = vector(200, y) targets.append(target) for target in targets: target.x -= 0.5 if inside(ball): speed.y -= 0.35 ball.move(speed) dupe = targets.copy() targets.clear() for target in dupe: if abs(target - ball) > 13: targets.append(target) draw() for target in targets: if not inside(target): return ontimer(move, 50)
def tap(x, y): "Respond to screen tap." onscreenclick(None) x = floor(x, 200) y = floor(y, 200) tile = vector(x, y) index = len(guesses) if tile != pattern[index]: exit() guesses.append(tile) flash(tile) if len(guesses) == len(pattern): grow() onscreenclick(tap)
"""Ant, simple animation demo. Exercises 1. Wrap ant around screen boundaries. 2. Make the ant leave a trail. 3. Change the ant color based on position. Hint: colormode(255); color(0, 100, 200) """ from random import * from turtle import * from rohans2dtlkit import vector ant = vector(0, 0) aim = vector(2, 0) def wrap(value): "Wrap value around -200 and 200." return value # TODO def draw(): "Move ant and draw screen." ant.move(aim) ant.x = wrap(ant.x) ant.y = wrap(ant.y) aim.move(random() - 0.5)
6. How would you add a computer player? 6. Add a second ball. """ from random import choice, random from turtle import * from rohans2dtlkit import vector def value(): "Randomly generate value between (-5, -3) or (3, 5)." return (3 + random() * 2) * choice([1, -1]) ball = vector(0, 0) aim = vector(value(), value()) state = {1: 0, 2: 0} def move(player, change): "Move player position by change." state[player] += change def rectangle(x, y, width, height): "Draw rectangle at (x, y) with given width and height." up() goto(x, y) down() begin_fill()
def change(x, y): "Change pacman aim if valid." if valid(pacman + vector(x, y)): aim.x = x aim.y = y
from random import choice from turtle import * from rohans2dtlkit import floor, vector state = {'score': 0} path = Turtle(visible=False) writer = Turtle(visible=False) aim = vector(5, 0) pacman = vector(-40, -80) ghosts = [ [vector(-180, 160), vector(5, 0)], [vector(-180, -160), vector(0, 5)], [vector(100, 160), vector(0, -5)], [vector(100, -160), vector(-5, 0)], ] tiles = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Exercises 1. Speed up tile flash rate. 2. Add more tiles. """ from random import choice from time import sleep from turtle import * from rohans2dtlkit import floor, square, vector pattern = [] guesses = [] tiles = { vector(0, 0): ('red', 'dark red'), vector(0, -200): ('blue', 'dark blue'), vector(-200, 0): ('green', 'dark green'), vector(-200, -200): ('yellow', 'khaki'), } def grid(): "Draw grid of tiles." square(0, 0, 200, 'dark red') square(0, -200, 200, 'dark blue') square(-200, 0, 200, 'dark green') square(-200, -200, 200, 'khaki') update() def flash(tile): "Flash tile in grid."
"""Cannon, hitting targets with projectiles. Exercises 1. Keep score by counting target hits. 2. Vary the effect of gravity. 3. Apply gravity to the targets. 4. Change the speed of the ball. """ from random import randrange from turtle import * from rohans2dtlkit import vector ball = vector(-200, -200) speed = vector(0, 0) targets = [] def tap(x, y): "Respond to screen tap." if not inside(ball): ball.x = -199 ball.y = -199 speed.x = (x + 200) / 25 speed.y = (y + 200) / 25 def inside(xy): "Return True if xy within screen."
"""Tron, classic arcade game. Exercises 1. Make the tron players faster/slower. 2. Stop a tron player from running into itself. 3. Allow the tron player to go around the edge of the screen. 4. How would you create a computer player? """ from turtle import * from rohans2dtlkit import square, vector p1xy = vector(-100, 0) p1aim = vector(4, 0) p1body = set() p2xy = vector(100, 0) p2aim = vector(-4, 0) p2body = set() def inside(head): "Return True if head inside screen." return -200 < head.x < 200 and -200 < head.y < 200 def draw(): "Advance players and draw game." p1xy.move(p1aim) p1head = p1xy.copy()
"""Snake, classic arcade game. Exercises 1. How do you make the snake faster or slower? 2. How can you make the snake go around the edges? 3. How would you move the food? 4. Change the snake to respond to arrow keys. """ from turtle import * from random import randrange from rohans2dtlkit import square, vector food = vector(0, 0) snake = [vector(10, 0)] aim = vector(0, -10) def change(x, y): "Change snake direction." aim.x = x aim.y = y def inside(head): "Return True if head inside boundaries." return -200 < head.x < 190 and -200 < head.y < 190
def tap(x, y): "Move bird up in response to screen tap." up = vector(0, 30) bird.move(up)
"""Flappy, game inspired by Flappy Bird. Exercises 1. Keep score. 2. Vary the speed. 3. Vary the size of the balls. 4. Allow the bird to move forward and back. """ from random import * from turtle import * from rohans2dtlkit import vector bird = vector(0, 0) balls = [] def tap(x, y): "Move bird up in response to screen tap." up = vector(0, 30) bird.move(up) def inside(point): "Return True if point on screen." return -200 < point.x < 200 and -200 < point.y < 200 def draw(alive):