def main(): # creating a window window = Screen() # window.bgcolor("orange") remo = Turtle() remo.shape("turtle") remo.color("green") remo.speed(50) for i in range(36): remo.circle(100) remo.left(10) remo.color("red") for i in range(36): remo.circle(80) remo.left(10) remo.color("yellow") for i in range(36): remo.circle(60) remo.left(10) window.exitonclick()
def main(): swarmSize = 100 t = Turtle() win = Screen() win.setworldcoordinates(-600, -600, 600, 600) t.speed(10) t.hideturtle() t.tracer(15) for i in range(swarmSize): if random.randrange(100) == 0: LeaderFish() else: FocalFish() for i in range(5): Obstacle() for turn in range(1000): for schooler in Schooler.swarm: schooler.getNewHeading() for schooler in Schooler.swarm: schooler.setHeadingAndMove() win.exitonclick()
class World(object): def __init__(self, index, size_x, size_y): self.index = index self.canvas = Screen() self.canvas.setup(size_x, size_y) def show_world(self): #self.canvas.ontimer(god.slowly_kill_humans(), 100) self.canvas.exitonclick()
def draw_art(): window = Screen() window.bgcolor('cyan') angie = Turtle() angie.shape('turtle') angie.color('blue') angie.speed(2000) # angie.left(105) for j in range(80): angie.right(5) draw_rhombus(angie, 100) angie.left(90) angie.forward(300) # Close window window.exitonclick()
def main(): swarmSize = 50 t = Turtle() win = Screen() win.setworldcoordinates(-600,-600,600,600) t.speed(10) t.hideturtle() t.tracer(15) for i in range(swarmSize): FocalFish() for turn in range(300): for schooler in Schooler.swarm: schooler.getNewHeading() for schooler in Schooler.swarm: schooler.setHeadingAndMove() win.exitonclick()
def main(): swarmSize = 30 t = Turtle() win = Screen() win.setworldcoordinates(-600,-600,600,600) t.speed(10) t.tracer(15) t.hideturtle() for i in range(swarmSize): Schooler() #for turn in range(1000): while True: try: for schooler in Schooler.swarm: schooler.moveAllBoidsToNewPositions() except KeyboardInterrupt: break win.exitonclick()
from collections import namedtuple colors = colorgram.extract("images/image.jpg", 35) colors_tuples = [] for color in colors: r = color.rgb.r g = color.rgb.g b = color.rgb.b colors_tuples.append((r, g, b)) # code for turtle t = Turtle() # code for screen scr = Screen() scr.colormode(255) t.penup() t.hideturtle() t.speed(0) # code to set custom x and y co-ordinate sety = -200 t.setposition(-200, -200) for position in range(1, 101): color = choice(colors_tuples) t.dot(20, color) t.forward(50) if position % 10 == 0: sety += 50 t.setposition(-200, sety) scr.exitonclick()
def move_forward(): t.forward(10) def move_left(): t.left(15) def move_right(): t.right(15) def move_back(): t.backward(10) def clear_the_screen(): t.clear() t.penup() t.home() t.pendown() scn.listen() scn.onkey(key="w", fun=move_forward) scn.onkey(key="a", fun=move_left) scn.onkey(key="d", fun=move_right) scn.onkey(key="s", fun=move_back) scn.onkey(key="c", fun=clear_the_screen) scn.exitonclick()
# After every movement process finish and start again, we update the screen # and wait for 0.1 seconds to move the squares again. screen.update() time.sleep(0.1) snake.move() # Collision between Snake and Food if snake.head.distance(food) < 15: food.goRandomLocation() scoreboard.increaseScore() snake.extendSnake() # Collision between Snake and the walls if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor( ) > 280 or snake.head.ycor() < -280: gameFinished = True scoreboard.gameOver() # Collision between Snake's head and it's tail # We sliced our snake.squares list from 1 to last # Because the first square in the list is head itself # It will immediately game over if we don't slice the list # Because head is always in collision with itself :p for square in snake.squares[1:]: if snake.head.distance(square) < 10: gameFinished = True scoreboard.gameOver() screen.exitonclick()
for i in cols: # Ensure we are not storing background shades of white, only spot colours we want to recreate if int(i.rgb.r) < 230 or int(i.rgb.g) < 230 or int(i.rgb.b) < 230: colours.append((int(i.rgb.r), int(i.rgb.g), int(i.rgb.b))) # Initialise object references t = Turtle() s = Screen() # Initial Turtle Window graphics setup t.pensize(20) t.speed("fastest") t.hideturtle() s.delay(0) s.colormode(255) # Loop through all squares for x in range(10): for y in range(10): # Move to next square and set random colour t.penup() t.setpos(x * 50 - 250, y * 50 - 250) rand_colour = choice(colours) t.pencolor(rand_colour) t.pendown() t.forward(1) # Keep Turtle window on screen until user clicks to exit s.exitonclick()
def main(): # TODO 1: Configure screen screen = Screen() screen.setup(width=SCREEN_WIDTH, height=SCREEN_HEIGHT) screen.bgcolor(SCREEN_BACKGROUND_COLOR) screen.tracer(0) # Add borders border = Border() border.createBorder() # TODO 2: Configure initial snake snake = Snake() food = Food() scoreboard = Scoreboard() # TODO 3: Move the snake screen.listen() screen.onkey(snake.up, "Up") screen.onkey(snake.down, "Down") screen.onkey(snake.left, "Left") screen.onkey(snake.right, "Right") #global paused #def unpause(): # global paused # paused = not paused #screen.onkey(unpause, "p") game_is_on = True while game_is_on: #while paused: # sleep(0.2) screen.update() sleep(SLEEP_TIME) snake.move() # TODO 4: Detect collision with food snake_food_collision_distance = ( food.width() + snake.head.width()) / 2 - COLLISION_ERROR if snake.head.distance(food) < snake_food_collision_distance: scoreboard.score += 1 snake.add_segment() food.refresh() for segment in snake.tail: while segment.position() == food.position(): food.clear() food.refresh() scoreboard.refresh() # TODO 5: Detect collision with walls pass_x_wall = ( snake.head.xcor() < -SCREEN_WIDTH / 2 + snake.head.width() or snake.head.xcor() > SCREEN_WIDTH / 2 - snake.head.width()) pass_y_wall = ( snake.head.ycor() < -SCREEN_HEIGHT / 2 + snake.head.width() or snake.head.ycor() > SCREEN_HEIGHT / 2 - snake.head.width()) wall_collision = pass_x_wall or pass_y_wall if wall_collision: scoreboard.resetHighestScore() snake.resetSnake() # TODO 6: Detect collision with tail tail_head_collision_distance = snake.head.width() - COLLISION_ERROR for segment in snake.tail[1:]: if segment.distance(snake.head) < tail_head_collision_distance: scoreboard.resetHighestScore() snake.resetSnake() screen.exitonclick()
import turtle # import the module from turtle import Turtle, Screen # from the turtle module import the turtle class # print(turtle, turtle.Turtle()) timmy = Turtle() # create timmy object print(timmy) timmy.shape('turtle') timmy.color('Red') timmy.speed(10) for loop in range(36): timmy.forward(100) timmy.left(100) timmy.circle(90, 90) my_screen = Screen() print(my_screen.canvheight) my_screen.exitonclick() # continue on running
from turtle import Turtle, Screen t_obj = Turtle() t_obj.shape("turtle") t_obj.color("red") for _ in range(4): t_obj.forward(10) t_obj.right(90) screen_obj = Screen() screen_obj.exitonclick()
import turtle my_window = turtle.Screen() Paula = turtle.Turtle() Paula.forward(150) my_window.exitonclick() import turtle as tur my_window = tur.Screen() Paula = tur.Turtle() Paula.forward(150) my_window.exitonclick() from turtle import Screen, Turtle, forward my_window = Screen() Paula = Turtle() Tom = Turtle() Paula.forward(150) Tom.forward(50) my_window.exitonclick() from turtle import * my_window = Screen() Paula = Turtle() Tom = Turtle() Paula.forward(150) Tom.forward(50) my_window.exitonclick()
if factor < 1: raise ValueError except ValueError: print('Value must be an integer greater than 0') factor = 0 total = 1 for i in range(1, factor + 1): total = total * i print(total) """ Exercise 14: page 206 """ for i in range(7, 0, -1): print('*' * i) """ Exercise 18: page 207 """ from turtle import Turtle, Screen trtle = Turtle() ts = Screen() distance = 2.5 for i in range(0, 1000, int(distance)): trtle.fd(i + distance) trtle.left(90) ts.exitonclick()
import turtle from turtle import Turtle, Screen my_turtle = Turtle() my_win = Screen() def draw_spiral(my_turtle, line_len): if line_len > 0: my_turtle.forward(line_len) my_turtle.right(90) draw_spiral(my_turtle, line_len - 5) draw_spiral(my_turtle, line_len=100) my_win.exitonclick()
background.listen() background.onkey(key='Up', fun=r_paddle.up) background.onkey(key='Down', fun=r_paddle.down) background.onkey(key='o', fun=l_paddle.up) background.onkey(key='q', fun=l_paddle.down) while game_is_on: time.sleep(0.05) ball.move() background.update() x = ball.xcor() y = ball.ycor() if y > 280 or y < -280: ball.bounce_wall() elif x > 350 and ball.distance(r_paddle) < 50 or x < -350 and ball.distance(l_paddle) < 50: ball.bounce_paddle() elif x > 350: ball.reset_position() scoreboard.l_point() elif x < -350: scoreboard.r_point() ball.reset_position() # detect collision with wall # detect collision with paddle # collision angle change # speed of ball change background.exitonclick()
# Generate car in bottom lanes (rides from left to right) lane_number = randint(1, 5) # Choose lane number cars.append(Car(0, (-300, lane_number * -40))) # Create car at bottom lane heading East counter = 0 # reset counter for car in cars: car.move() # If car gets outside window remove it from list if car.xcor() < -340 or car.xcor() > 340: cars.remove(car) # Collision with car (player's coordinates +/-20 from car center) if (car.xcor() - 20 <= player.xcor() <= car.xcor() + 20) \ and (car.ycor() - 20 <= player.ycor() <= car.ycor() + 20): is_game_on = False level.game_over() # Check if player gets to the other side of the road if player.ycor() > 280: game_speed *= 0.9 # Speed up game at next level player.set_start_position() level.update_score() counter += 1 window.exitonclick()
bot.color("green"); bot.speed("slowest"); #bot.setpos(x, y) #bot.st() bot.circle(50); bot.clear() window = Screen(); window.bgcolor("yellow"); #draw_triangle(3, 100, 100) #draw_square(4, 200, 200) #draw_circle(300, 300) # 1. don't show trutle shape. # 2. we need to draw multiple squres (360/10 = 36 squares) # 3. each squre we should start with different angle (10 degrees). # 4. for each square, createa turtle and call square function. bot = Turtle() #bot.ht() bot.color("blue", "green"); bot.speed("fast"); bot.begin_fill() for i in range(0, 36): print (" square " + str(i * 10)) draw_triangle(bot, 10) bot.end_fill() window.exitonclick()
from turtle import Screen, Turtle pantalla = Screen() pantalla.setup(425, 225) pantalla.screensize(400, 200) tortuga = Turtle() tortuga.forward(100) tortuga.left(90) tortuga.forward(100) tortuga.left(90) tortuga.forward(100) tortuga.left(90) tortuga.forward(100) pantalla.exitonclick()
from turtle import Turtle, Screen amy = Turtle() amy.shape('turtle') amy.color('purple') amy.forward(100) my_screen = Screen() my_screen.exitonclick()
from turtle import Turtle, Screen, position timmy = Turtle() timmy.shape('turtle') timmy.color("blue") timmy.circle(100) my_Screen = Screen() my_Screen.exitonclick()
bob.setheading(bob.towards(bobNew_xy)) # place the turtles and show them jeff.setpos(jeff_xy) jeff.showturtle() jeff.pendown() bob.setpos(bob_xy) bob.showturtle() bob.pendown() # bob's motion is in a straight line def moveStraight(): bob.fd(bob.speed()) playGround.ontimer(moveStraight, DELAY) # jeff's motion is towards bob def moveToward(): if bob.position() != jeff.position(): jeff.setheading(jeff.towards(bob)) jeff.fd(jeff.speed()) playGround.ontimer(moveToward, DELAY) moveStraight() moveToward() playGround.exitonclick()
from turtle import Turtle, Screen def yellowHouse(side_length): wn.register_shape("brick", ((0, 0), (-0.5, -0.5), (0.5, -0.5))) house = Turtle('brick', visible=False) house.shapesize(stretch_wid=side_length, outline=5) house.color("yellow", wn.bgcolor()) house.penup() for angle in range(360, 0, -90): house.setheading(angle) house.stamp() house.forward(side_length) house.stamp() wn = Screen() wn.title("Yellow House") wn.bgcolor("blue") yellowHouse(200) wn.exitonclick()
def main(): t = Turtle() my_win = Screen() t.width(12) t.speed(10) t.left(90) t.up() t.backward(100) t.down() t.color("brown") tree(75, t) my_win.exitonclick()