def move(): """ To update object position. """ bird.y -= 1 for ball in balls: ball.x -= 1 if r.randrange(10) == 0: y = r.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) t.ontimer(move, 50)
def move(): writer.undo() writer.write(state["score"]) t.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) t.up() t.goto(pacman.x + 10, pacman.y + 10) t.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 t.up() t.goto(point.x + 10, point.y + 10) t.dot(20, "red") t.update() for point, course in ghosts: if abs(pacman - point) < 20: return t.ontimer(move, 100)
def tap(x, y): """ To swap the tiles. """ x = floor(x, 100) y = floor(y, 100) mark = vector(x, y) for neighbor in neighbors: spot = mark + neighbor if spot in tiles and tiles[spot] is None: number = tiles[mark] tiles[spot] = number square(spot, number) tiles[mark] = None square(mark, None)
def load(): count = 1 for y in range(-200, 200, 100): for x in range(-200, 200, 100): mark = vector(x, y) tiles[mark] = count count += 1 tiles[mark] = None for count in range(1000): neighbor = r.choice(neighbors) spot = mark + neighbor if spot in tiles: number = tiles[spot] tiles[spot] = None tiles[mark] = number mark = spot
def move(): """ For movement of ball and target. """ if r.randrange(40) == 0: y = r.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() """ Dupe is duplicate. """ targets.clear() for target in dupe: if abs(target - ball) > 13: targets.append(target) draw() for target in targets: if not inside(target): return t.ontimer(move, 50)
import random as r import turtle as t from class_prog import vector, floor tiles = {} neighbors = [ vector(100, 0), vector(-100, 0), vector(0, 100), vector(0, -100) ] def load(): count = 1 for y in range(-200, 200, 100): for x in range(-200, 200, 100): mark = vector(x, y) tiles[mark] = count count += 1 tiles[mark] = None for count in range(1000): neighbor = r.choice(neighbors) spot = mark + neighbor if spot in tiles: number = tiles[spot] tiles[spot] = None
import random as r import turtle as t from class_prog import vector def value(): """ this will generate my value from [-5, -3] to [3, 5] """ return (3 + r.random() * 2) * r.choice([1, -1]) ball = vector(0, 0) angle = 30 aim = vector(value(), value()) """ projection or movement """ def draw(): """ move the ball and draw the screen """ ball.move(aim) x = ball.x y = ball.y if x < -200 or x > 200: aim.x = -aim.x if y < -200 or y > 200: aim.y = -aim.y t.clear() t.goto(x, y) t.dot(10, "red")
import random as r import turtle as t from class_prog import vector ball = vector(-200, -200) speed = vector(0, 0) targets = [] def inside(xy): return -200 < xy.x < 200 and -200 < xy.y < 200 def tap(x, y): """ Will respond to the screen. """ if not inside(ball): ball.x = -199 ball.y = -199 speed.x = (x + 200) / 25 speed.y = (y + 200) / 25 def draw(): """ Draw the ball and targets. """ t.clear() for target in targets: t.goto(target.x, target.y) t.dot(20, "blue") if inside(ball): t.goto(ball.x, ball.y)
import random as r import turtle as t from class_prog import vector ant = vector(0, 0) aim = vector(2, 0) def wrap(value): return value def draw(): ant.move(aim) ant.x = wrap(ant.x) ant.y = wrap(ant.y) aim.move(r.random() - 0.5) aim.rotate(r.random() * 10 - 5) t.clear() t.goto(ant.x, ant.y) t.dot(4) if running: t.ontimer(draw, 100) t.setup(420, 420, 370, 0) t.hideturtle() t.tracer(False) t.up() running = True
import turtle as t from random import randrange from class_prog import square, vector food = vector(0, 0) snake = [vector(10, 0)] aim = vector(0, -10) targets = [] def change(x, y): aim.x = x aim.y = y def inside(head): return -200 < head.x < 200 and -200 < head.y < 200 def move(): """ To move snake one segment forward. """ head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 10, "Red") t.update() return snake.append(head) if head == food: """ Means if snake eats food. """
def change(x, y): """ If moment valid then change pacman aim. """ if valid(pacman + vector(x, y)): aim.x = x aim.y = y
3. Change where pacman starts. 4. Make the ghosts faster/slower. 5. Make the ghosts smarter. """ from random import choice import turtle as t from class_prog import vector, floor state = {"score": 0} path = t.Turtle(visible=False) writer = t.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, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
import turtle as t from class_prog import square, vector """ For player 1. """ player1xy = vector(-300, 0) player1aim = vector(4, 0) player1body = set() """ For player 2. """ player2xy = vector(300, 0) player2aim = vector(-4, 0) player2body = set() def inside(head): """ Return true if head inside boundary. """ return -400 < head.x < 400 and -400 < head.y < 400 def draw(): player1xy.move(player1aim) player1head = player1xy.copy() player2xy.move(player2aim) player2head = player2xy.copy() """ SUJIT: I added missing Line 74 in class_prog [return self._hash] """ if not inside(player1head) or (player1head in player2body): print("The blue player has won") return if not inside(player2head) or (player2head in player1body): print("The red player has won") return
def tap(x, y): """ To move our bird in response. """ up = vector(0, 50) if -1000 < x < 1000 and -1000 < y < 1000: bird.move(up)
import random as r import turtle as t from class_prog import vector bird = vector(0, 0) balls = [] def inside(point): """ To return true if point on screen. """ return -200 < point.x < 200 and -200 < point.y < 200 def tap(x, y): """ To move our bird in response. """ up = vector(0, 50) if -1000 < x < 1000 and -1000 < y < 1000: bird.move(up) def draw(alive): """ To draw screen objects. """ """ Alive is boolean. """ t.clear() t.goto(bird.x, bird.y) if alive: t.dot(10, "green") else: t.dot(10, "red") for ball in balls: