def draw(): clear() # Draw snake for snake in snakes: square(snake.x, snake.y, 9, "black") # Draw food square(food.x, food.y, 9, "red")
def flash(tile): "Flash tile in grid." glow, dark = tiles[tile] square(tile.x, tile.y, 200, glow) update() sleep(0.5) square(tile.x, tile.y, 200, dark) update() sleep(0.5)
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 world(): bgcolor('black') path.color('white') for index in range(len(tiles)): tile = tiles[index] if tile > 0: x = (index % 20) * 20 - 200 y = 180 - (index // 20) * 20 square(x,y) if tile == 1: path.up() path.goto(x + 10, y + 10) path.dot(2, 'white')
def move(): # Movement for Pacman 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') # Movement for ghosts 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() # checking for collisions. for point, course in ghosts: if abs(pacman - point) < 20: return ontimer(move, 100)
def draw(): p1.move(p1Aim) p1Head = p1.copy() p2.move(p2Aim) p2Head = p1.copy() if not inside(p1Head) or p1Head in p2Body: print("Blue Won") return if not inside(p2Head) or p2Head in p1Body: print("Red Won") return p1Body.add(p1Head) p2Body.add(p2Head) square(p1.x, p1.y, 3, 'red') square(p2.x, p2.y, 3, 'blue') update() ontimer(draw(), 50)
def move(): global food #global snakes # Extend snake length head = snakes[0].copy() head.move(direction) if head in snakes or not isInside(head): square(head.x, head.y, 9, "red") update() return snakes.insert(0, head) if abs(food - head) < 8: food = vector(randrange(-200, 200), randrange(-200, 200)) else: snakes.pop() draw() ontimer(move, 100)
def move(): 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')
def draw(): p1xy.move(p1aim) p1head = p1xy.copy() p2xy.move(p2aim) p2head = p2xy.copy() if not inside(p1head) or p1head in p2body: print("PLayer Blue Has Won") return if not inside(p2head) or p2head in p1body: print("Player Red Won") return p1body.add(p1head) p2body.add(p2head) square(p1xy.x, p1xy.y, 3, 'red') square(p2xy.x, p2xy.y, 3, 'blue') update() ontimer(draw, 50)
def move(): head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: # if snake eat it's own body square(head.x, head.y,9,'red') update() messagebox.showinfo("End","Game Over! Please Try Again") bye() snake.append(head) if head == food: # if snake eats the food here print(f"Snake: {len(snake)}") food.x = randrange(-15,15) * 10 food.y = randrange(-15,15) * 10 print(f"{food.x} -- {food.y}") else: snake.pop(0) clear() #clear the console for body in snake: square(body.x,body.y,9,'black') print(f"{body.x} -- {body.y}") square(food.x, food.y,9,'green') update() ontimer(move,100)
def move(): head = snake[-1].copy() head.move(aim) snake.append(head) if not inside(head): square(head.x, head.y, 9, 'green') update() return if food == snake: print("Score: ", len(snake)) food.x = randrange(-15, 15)*10 food.y = randrange(-15, 15)*10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'red') update() square(food.x, food.y, 9, 'black') ontimer(move, 100)
def move(): """Move the snake forward""" head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') print("GAME OVER") update() return snake.append(head) if head == food: print("Snake point: ", len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'blue') square(food.x, food.y, 9, 'black') update() ontimer(move, 100)
def move(): "Move snake forward one segment." head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) if head == food: print('Snake:', len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 9, 'black') square(food.x, food.y, 9, 'green') update() ontimer(move, 100)
def draw(): p1xy.move(p1Aim) p1Head = p1xy.copy() p2xy.move(p2Aim) p2Head = p2xy.copy() if not inside(p1Head) or p1Head in p2Body: print("Player blue won") return if not inside(p2Head) or p2Head in p1Body: print("Player red won") return p1Body.add(p1Head) p2Body.add(p2Head) square(p1xy.x, p1xy.y, 3, 'red') square(p2xy.x, p2xy.y, 3, 'blue') update() ontimer(draw, 50)
def draw(): p1_xy.move(p1_aim) p1_head = p1_xy.copy() p2_xy.move(p2_aim) p2_head = p2_xy.copy() if not inside(p1_head) or p1_head in p2_body: # print("player blue won") messagebox.showinfo("Winner", "Blue Won this time") bye() if not inside(p2_head) or p2_head in p1_body: messagebox.showinfo("Winner", "Red Won this time") bye() p1_body.add(p1_head) p2_body.add(p2_head) square(p1_xy.x, p1_xy.y, 3, 'red') square(p2_xy.x, p2_xy.y, 3, 'blue') update() ontimer(draw, 50)
def draw(): "Advance players and draw game." p1xy.move(p1aim) p1head = p1xy.copy() p2xy.move(p2aim) p2head = p2xy.copy() if not inside(p1head) or p1head in p2body: print('Player blue wins!') return if not inside(p2head) or p2head in p1body: print('Player red wins!') return p1body.add(p1head) p2body.add(p2head) square(p1xy.x, p1xy.y, 3, 'red') square(p2xy.x, p2xy.y, 3, 'blue') update() ontimer(draw, 50)
def move(): head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, 'red') update() return snake.append(head) if head == food: global score food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 else: snake.pop(0) clear() for body in snake: if (body is snake[-1]): square(body.x, body.y, 9, 'black') else: square(body.x, body.y, 9, 'grey') square(food.x, food.y, 9, 'green') update() ontimer(move, 100)
def move(): head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x,head.y,9,'red') update() return snake.append(head) if head == food: print("snake:",len(snake)) food.x = randrange(-15,15)*10 food.y = randrange(-15,15)*10 else: snake.pop(0) clear() for body in snake: square(body.x,body.y,9,'green') square(food.x,food.y,9,'yellow') update() ontimer(move,100)
def move(): head = snake[-1].copy() head.move(aim) snake.append(head) rest = snake.copy() rest.pop() if not inside(head) or head in rest: for body in snake: square(body.x, body.y, 10, "red") update() win.clearscreen() pen.clear() pen.goto(0, -50) pen.color("red") pen.write("Score: {} ".format((len(rest) - 1) * 10), align="center", font=("Courier", 24, "normal")) restart = win.textinput("Game over !", "Do you want to restart ? (y/n)") if restart == "y": win.resetscreen() run_game() return if food == head: print("Snake point : ", len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 pen.clear() pen.write("Score: {} ".format((len(snake)-1)*10), align="center", font=("Courier", 24, "normal")) else: snake.pop(0) clear() for body in snake: square(body.x, body.y, 10, "Green") square(food.x, food.y, 10, "black") update() ontimer(move, 100)