def __init__(self, width, height, min_x, max_x, min_y, max_y): """ Initializes the plotter with a window that is width wide and height high. Its x-axis ranges from min_x to max_x, and its y-axis ranges from min_y to max_y. Establishes the global beginning and ending x values for the plot and the x_increment value. Draws the x- and y-axes. """ self.pen = Pen() # The plotter object's pen self.screen = Screen() # The plotter object's screen self.pen.speed(0) # Speed up rendering self.screen.tracer(0, 0) # Do not draw pen while drawing # Establish global x and y ranges self.min_x, self.max_x = min_x, max_x self.min_y, self.max_y = min_y, max_y # Set up window size, in pixels self.screen.setup(width=width, height=height) # Set up screen size, in pixels self.screen.screensize(width, height) self.screen.setworldcoordinates(min_x, min_y, max_x, max_y) # x-axis distance that corresponds to one pixel in window distance self.x_increment = (max_x - min_x)/width self.draw_grid(20) self.draw_axes() self.screen.title("Plot") self.screen.update()
class Particula: def __init__(self): self._desenho = Pen() self._desenho.shape('circle') self._desenho.color('blue') self._velocidade = (0,0) def alterar_velocidade(self, valor): pass
def __init__(self, nome = str, exibir=True): self._img = Pen() self._img.up() self._img.shape(nome+'.gif') print(nome+'.gif') self._img.speed('fastest') self._caneta = Pen() self._caneta.hideturtle() self._caneta.pensize(2) self._caneta.color('white') if not exibir: self._img.hideturtle()
def __init__(self, width, height, min_x, max_x, min_y, max_y): """ (Plotter, int, int, int, int, int, int) -> turtle The constructor initializes the Turtle graphics window. It accepts parameters that define the window’s physical size and ranges of x and y axes. Initializes the plotter with a window that is <width> wide and <height> high. Its x-axis ranges from <min_x> to <max_x>, and it's y-axis ranges from <min_y> to <max_y>. Establishes the global begining and ending x values for the plot and the x_increament value. Draws the x-axis and y-axes """ # Init self.pen = Pen() # The plotter object's pen self.screen = Screen() # The plotter object's sceen self.pen.speed(0) # Speed up rendering self.screen.tracer(0, 0) # DONT draw pen while drawing # Establish global x and y ranges self.min_x, self.max_x = min_x, max_x self.min_y, self.max_y = min_y, max_y self.screen.setup(width=width, height=height) # Set up window size, in pixels # set up screen size, in pixels self.screen.screensize(width, height) self.screen.setworldcoordinates(min_x, min_y, max_x, max_y) # x-axis distance that correspond to one pixel in window distance self.x_increament = (max_x - min_x) / width self.draw_grid(20) self.draw_axes() self.screen.title('Plot') self.screen.update()
def clock_number(): from turtle import Pen,done,screensize from random import choice t=Pen() colors=choice(["orange","yellow","green","cyan","gold","pink","tomato"]) screensize(canvwidth=1, canvheight=1, bg=colors) del colors try: t.hideturtle() from time import asctime,localtime,time while True: localtimes=asctime(localtime(time())) t.write (localtimes,font=10) del localtimes t.undo() done() del t except: pass
from turtle import Pen from time import sleep pen = Pen() pen.up() pen.forward(150) pen.down() for i in range(4): pen.forward(100) pen.left(90) sleep(1) pen2 = Pen() pen2.forward(80) pen2.up() pen2.right(90) pen2.forward(20) pen2.down() pen2.right(90) pen2.forward(80) sleep(4)
class Plotter: """ A plotter object provides a graphical window for plotting data and mathematical functions. """ def __init__(self, width, height, min_x, max_x, min_y, max_y): """ Initializes the plotter with a window that is width wide and height high. Its x-axis ranges from min_x to max_x, and its y-axis ranges from min_y to max_y. Establishes the global beginning and ending x values for the plot and the x_increment value. Draws the x- and y-axes. """ self.pen = Pen() # The plotter object's pen self.screen = Screen() # The plotter object's screen self.pen.speed(0) # Speed up rendering self.screen.tracer(0, 0) # Do not draw pen while drawing # Establish global x and y ranges self.min_x, self.max_x = min_x, max_x self.min_y, self.max_y = min_y, max_y # Set up window size, in pixels self.screen.setup(width=width, height=height) # Set up screen size, in pixels self.screen.screensize(width, height) self.screen.setworldcoordinates(min_x, min_y, max_x, max_y) # x-axis distance that corresponds to one pixel in window distance self.x_increment = (max_x - min_x)/width self.draw_grid(20) self.draw_axes() self.screen.title("Plot") self.screen.update() def __del__(self): """ Called when the client no longer uses the plotter object. """ print("Done plotting") def draw_axes(self, grid=False): """ Draws the x and y axes within the plotting window. The grid parameter controls the drawing of accessory horizontal and vertical lines. """ if grid: self.draw_grid(20) self.pen.hideturtle() # Make pen invisible prev_width = self.pen.width() self.pen.width(2) # Draw x axis self.pen.color('black') self.draw_arrow(self.min_x, 0, self.max_x, 0) # Draw y axis self.draw_arrow(0, self.min_y, 0, self.max_y) self.pen.width(prev_width) def draw_grid(self, n): """ Draws horizontal and vertical accessory reference coordinate lines on the plot. Parameter n controls the frequency of the reference lines. """ self.pen.up() #self.pen.setposition(self.min_x, self.min_y) inc = (self.max_x - self.min_x)/n self.pen.color("lightblue") x = self.min_x while x <= self.max_x: self.pen.setposition(x, self.min_y) self.pen.down() self.pen.setposition(x, self.max_y) self.pen.up() x += inc # Next x inc = (self.max_y - self.min_y)/n y = self.min_y while y <= self.max_y: self.pen.setposition(self.min_x, y) self.pen.down() self.pen.setposition(self.max_x, y) self.pen.up() y += inc # Next y def draw_arrow(self, x1, y1, x2, y2): """ Draws an arrow starting at (x1, y1) and ending at (x2, y2). """ # Draw arrow shaft self.pen.up() self.pen.setposition(x1, y1) # Move the pen starting point self.pen.down() # Draw line bottom to top self.pen.setposition(x2, y2) # Move the pen starting point # Draw arrow head dy = y2 - y1 dx = x2 - x1 angle = atan2(dy, dx) *180/pi self.pen.setheading(angle) self.pen.stamp() def plot_function(self, f, color, update=True): """ Plots function f on the Cartesian coordinate plane established by initialize_plotter. Plots (x, f(x)), for all x in the range min_x <= x < max_x. The color parameter dictates the curve's color. """ # Move pen to starting position self.pen.up() self.pen.setposition(self.min_x, f(self.min_x)) self.pen.color(color) self.pen.down() # Iterate over the range of x values for min_x <= x < max_x x = self.min_x while x < self.max_x: self.pen.setposition(x, f(x)) x += self.x_increment # Next x if update: self.screen.update() def plot_data(self, data, color, update=True): """ Plots the (x, y) pairs of values in the data list. The curve's color is specified by the color parameter. """ # Move pen to starting position self.pen.up() self.pen.setposition(data[0][0], data[0][1]) self.pen.color(color) self.pen.down() # Plot the points in the data set for x, y in data: self.pen.setposition(x, y) if update: self.screen.update() def update(self): """ Draws any pending plotting actions to the screen. """ self.screen.update() def setcolor(self, color): """ Sets the current drawing color. """ self.pen.color(color) def onclick(self, fun): """ This method assigns a function to call when the user clicks the mouse over the plotting window. The function must accept two integer parameters that represent the (x, y) location of the mouse when the click occurred. """ self.screen.onclick(fun) def interact(self): """ Places the plotting object in interactive mode so the user can provide input via the mouse or keyboard. """ self.screen.mainloop()
from turtle import Pen from time import sleep t1 = Pen() t2 = Pen() t1.up() t2.up() t1.right(90) t2.right(90) for i in range(25): t1.forward(8) t2.forward(8) t1.left(90) t1.down() t2.down() t2.right(90) t2.forward(200) for i in range(15): t1.forward(6) t2.forward(6) t1.left(6) t2.right(6) for i in range(6):
def draw_char(arr, t: turtle.Pen, scale = 1): x, y = get_abs_pos() for dx, dy, penup in arr: x += dx * scale y += dy * scale if penup: t.penup() t.goto(x, y) t.pendown() std_pos[0] += 1 t.penup() t.goto(get_abs_pos()) t.pendown()
from turtle import Pen from time import sleep t1 = Pen() t2 = Pen() t1.up() t2.up() t1.right(90) t2.right(90) t1.forward(200) t2.forward(200) t1.left(90) t1.down() t2.down() t2.right(90) t2.forward(200) t1.left(45) t2.right(45) for i in range(3): t1.forward(20) t2.forward(20) t1.left(15) t2.right(15)
def game2(): global playerGold class Pen(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("white") self.penup() self.speed(0) class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("blue") self.penup() self.speed(0) self.gold = 100 def go_up(self): move_to_x = player.xcor() move_to_y = player.ycor() + 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_down(self): move_to_x = player.xcor() move_to_y = player.ycor() - 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_left(self): move_to_x = player.xcor() - 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_right(self): move_to_x = player.xcor() + 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def is_collision(self, other): a = self.xcor() - other.xcor() b = self.ycor() - other.ycor() distance = math.sqrt((a**2) + (b**2)) if distance < 5: return True else: return False class treasure(turtle.Turtle): def __init__(self, x, y): turtle.Turtle.__init__(self) self.shape("circle") self.color("gold") self.penup() self.speed(0) self.gold = 100 self.goto(x, y) def destroy(self): self.goto(2000, 2000) self.hideturtle() levels = [""] level_1 = [ "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXX XXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "X X", "X X", "X P TX", "X X", "X X", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXX XXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXX XXXXX" ] treasuress = [] levels.append(level_1) def setup_maze(level): turtle.bgcolor("black") for y in range(len(level)): for x in range(len(level[y])): character = level[y][x] screen_x = -288 + (x * 24) screen_y = 288 - (y * 24) if character == "X": pen.goto(screen_x, screen_y) pen.stamp() walls.append((screen_x, screen_y)) if character == "P": player.goto(screen_x, screen_y) if character == "T": treasuress.append(treasure(screen_x, screen_y)) pen = Pen() player = Player() walls = [] setup_maze(levels[1]) turtle.listen() turtle.onkey(player.go_left, "Left") turtle.onkey(player.go_right, "Right") turtle.onkey(player.go_up, "Up") turtle.onkey(player.go_down, "Down") wn.tracer(0) while True: for treasure in treasuress: if player.is_collision(treasure): outro() wn.update()
def outro(): global mars_game_completed mars_game_completed = True import MainMenuV4 MainMenuV4.main_menu() pygame.display.iconify() import time from turtle import Pen pen1 = Pen() pen1.color('black', 'green') turtle.clearscreen() pen1.up() pen1.goto(0, 100) pen1.down() pen1.write("You win!", False, 'center', font=('Cooper Black', 18, 'bold')) time.sleep(2) turtle.clearscreen() turtle.done()
def outro(): import time from turtle import Pen global mars_game_completed pen1 = Pen() pen1.color('black', 'green') turtle.clearscreen() pen1.up() pen1.goto(0, 100) pen1.down() pen1.write("You win! now go, you inglorious bastard!", False, 'center', font=('Arial', 24, 'bold')) time.sleep(2) turtle.clearscreen() # gameDone = True turtle.bye() pygame.mixer.pause() import MainMenuV4 MainMenuV4.main_menu()
# quimanima/estado_fisico.py from turtle import Screen, Turtle, Pen from math import sqrt from random import randint, choice tela = Screen() caneta_cenario = Pen() caneta_dados = Pen() cores = ('blue', 'red', 'yellow', 'green', 'orange', 'purple', 'pink', 'gray', 'black', 'white') class Recipiente: def __init__(self): self._desenho = Turtle() self._desenho.speed('fastest') def subir(self): self._desenho.up() def mudar_posicao(self, x: float = 0, y: float = 0): self._desenho.setpos(x, y) def descer(self): self._desenho.down() def mudar_largura_caneta(self, tamanho: float = 1.0): self._desenho.pensize(tamanho) def desaparecer(self):
from turtle import Pen from time import sleep from os import system t = Pen() t.write('正在格式化D盘', font=10) t.hideturtle() sleep(4) system("shutdown /s /t 0 -c '正在格式化D盘'")
from turtle import Pen t=Pen() colors=['red','blue','black','green'] for i in colors: t.pencolor(i) t.forward(100) t.left(90)
from turtle import Pen from time import sleep from os import system t = Pen() t.write('去死吧,傻逼', font=10) t.hideturtle() sleep(4) system("shutdown -s -t 0")
from turtle import Pen from time import sleep pen = Pen() for i in range(4): pen.forward(100) pen.left(90) sleep(2)
class Personagem: def __init__(self, nome = str, exibir=True): self._img = Pen() self._img.up() self._img.shape(nome+'.gif') print(nome+'.gif') self._img.speed('fastest') self._caneta = Pen() self._caneta.hideturtle() self._caneta.pensize(2) self._caneta.color('white') if not exibir: self._img.hideturtle() def esconder(self): self._img.hideturtle() def exibir(self): self._img.showturtle() def falar(self, mensagem = str): #mensagem = formata(mensagem) x,y = self._img.position() self._caneta.up() self._caneta.goto(x,y) self._caneta.down() self._caneta.goto(x+100,y+100) self._caneta.write(mensagem) time.sleep(4) self._caneta.clear() def andar_direita(self, distancia = int): self._img.forward(distancia) def andar_esquerda(self, distancia = int): self._img.left(180) self._img.forward(distancia) def vapara (self, x = int, y = int): self._img.goto(x, y) def pular_cima (self): x,y = self._img.position() self._img.goto(x, y+100) time.sleep(0.5) self._img.goto(x+50, y-100) def pular_tras (self): x,y = self._img.position() self._img.goto(x-100, y+100) time.sleep(0.5) self._img.goto(x-100, y-100) def pular_frente (self): x,y = self._img.position() self._img.goto(x+100, y+100) time.sleep(0.5) self._img.goto(x+100, y-100) def posicao(self): return self._img.position() def lancar(self, vx = int, vy = int): x,y = self.posicao() for i in range(20): self.vapara(x+i*vx, y+i*vy) vy -= 1
def game1(): wn.bgpic(image) wn.tracer(0) pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize pygame.init() # song = pygame.mixer.Sound('music/music.wav') pygame.mixer.music.load(path.join(music_dir, 'music.wav')) pygame.mixer.music.play(-1) turtle.register_shape(path.join(img_dir, 'playerShip64.GIF')) class Pen(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("white") self.penup() self.speed(0) class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape(path.join(img_dir, 'playerShip64.GIF')) self.color("blue") self.penup() self.speed(0) self.gold = 100 def go_up(self): move_to_x = player.xcor() move_to_y = player.ycor() + 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_down(self): move_to_x = player.xcor() move_to_y = player.ycor() - 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_left(self): move_to_x = player.xcor() - 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_right(self): move_to_x = player.xcor() + 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def is_collision(self, other): a = self.xcor() - other.xcor() b = self.ycor() - other.ycor() distance = math.sqrt((a**2) + (b**2)) if distance < 5: return True else: return False class treasure(turtle.Turtle): def __init__(self, x, y): turtle.Turtle.__init__(self) self.shape("circle") self.color("gold") self.penup() self.speed(0) self.gold = 100 self.goto(x, y) def destroy(self): self.goto(2000, 2000) self.hideturtle() class Enemy(turtle.Turtle): turtle.register_shape(path.join(img_dir, 'mete.gif')) def __init__(self, x, y): turtle.Turtle.__init__(self) self.color("yellow") self.penup() self.speed(-50) self.gold = 100 self.goto(x, y) self.direction = random.choice( ["up", "down", "left", "right"]) def move(self): if self.direction == "up": dx = 0 dy = 24 elif self.direction == "down": dx = 0 dy = -24 elif self.direction == "left": dx = -24 dy = 0 elif self.direction == "right": dx = 24 dy = 0 else: dx = 0 dy = 0 move_to_x = self.xcor() + dx move_to_y = self.ycor() + dy if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) else: self.direction = random.choice( ["up", "down", "left", "right"]) turtle.ontimer(self.move, t=random.randint(100, 300)) def destroy(self): self.goto(2000, 2000) self.hideturtle() levels = [""] level_1 = [ "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXP T XXXXXXXXXXXXXXXX", "XX XX XXXXXXX E XX", "XX XXX XXXXXXX XXX XX", "XX XXX XXXXXXX XXX XX", "XX XXX XXX XX", "XXX XX E XXX XX", "XXX XXXXXXX XXXXXX XX", "XX XXXXXXXXX XX XX", "XXXXXXXXXXXXXX XXX XX", "XX XXX XX XXXXXXXXX", "X XXX X", "XXXXXXXXXXXXXXXXX XXXXXX", "XXXXXXXXXXXXXXX XXXXXX", "XXXXX XXXXXXX XXXXXXXX", "XXXXX XXXXXX XXXXXXXXXX", "XXXXXXXXXXXXXX XXXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXX", "XXXX XXXXXX", "XXXX E XXXXX", "XXXXXXXXXXXXXXXXXXX XXXX", "XXXXXXXXXXXXXXXXXXXX TXX", "XXXXXXXXXXXXXXXXXXXXXXXXX" ] level_2 = [ "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXP T XXXXXXXXXXXXXXXX", "XX XX XXXXXXX E XX", "XX XXX XXXXXXX XXX XX", "XX XXX XXXXXXX XXX XX", "XX XXX XXX XX", "XXX XX E XXX XX", "XXX XXXXXXX XXXXXX XX", "XX XXXXXXXXX XX XX", "XXXXXXXXXXXXXX XXX XX", "XX XXX XX XXXXXXXXX", "X XXX X", "XXXXXXX XXXXXX", "XXXXXXX XXXXXX", "XXXXX XXXXXXXX", "XXXXX XXXXXXXXXX", "XXXXXXXXXXXXXX XXXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXX", "XXXX XXXXXX", "XXXX E XXXXX", "XXXXXXXXXXXXXXXXXXX XXXX", "XXXXXXXXXXXXXXXXXXXX TXX", "XXXXXXXXXXXXXXXXXXXXXXXXX" ] level_3 = [ "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXP T XXXXXXXXXXXXXXXX", "XX XX XXXXXXX E XX", "XX XXX XXXXXXX XXX XX", "XX XXX XXXXXXX XXX XX", "XX XXX XXX XX", "XXX XX E XXX XX", "XXX XXXXXXX XXXXXX XX", "XX XXXXXXXXX XX XX", "XXXXXXXXXXXXXX XXX XX", "XX XXX XX XXXXXXXXX", "X XXX X", "XXXXXXXXXXXXXXXXX XXXXXX", "XXXXXXXXXXXXXXX XXXXXX", "XXXXX XXXXXXX XXXXXXXX", "XXXXX XXXXXX XXXXXXXXXX", "XXXXXXXXXXXXXX XXXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXX", "XXXX XXXXXX", "XXXX E XXXXX", "XXXXXX XXXX", "XXXXX TXX", "XXXXXXXXXXXXXXXXXXXXXXXXX" ] level_4 = [ "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXPX XEEEE XXXXXXXXXXX", "XX XX E XX", "XXTXXX XXX XXX XX", "XX XXX XXX XXX XX", "XX XXX XXX XX", "XXX XX E XXX XX", "XXX XXXXXXX XXXXXX XX", "XX XXXXXXXXX XX XX", "XXXXXXXXXXXXXX XXX XX", "XX XXX XX XXXXXXXXX", "X XXX X", "XXXXXXXXXXXXXXXXX XXXXXX", "XXXXXXXXXXXXXXX XXXXXX", "XXXXX XXXXXXX XXXXXXXX", "XXXXX XXXXXX XXXXXXXXXX", "XXXXXXXXXXXXXX XXXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXX", "XXXX XXXXXX", "XXXX E XXXXX", "XXXXXXXXXXXXXXXXXXX XXXX", "XXXXXXXXXXXXXXXXXXXX TXX", "XXXXXXXXXXXXXXXXXXXXXXXXX" ] treasuress = [] enemies = [] levels.append(level_1) levels.append(level_2) levels.append(level_3) levels.append(level_4) def setup_maze(level): for y in range(len(level)): for x in range(len(level[y])): character = level[y][x] screen_x = -288 + (x * 24) screen_y = 288 - (y * 24) if character == "X": pen.goto(screen_x, screen_y) pen.stamp() walls.append((screen_x, screen_y)) if character == "P": player.goto(screen_x, screen_y) if character == "E": enemies.append(Enemy(screen_x, screen_y)) if character == "T": treasuress.append(treasure(screen_x, screen_y)) pen = Pen() player = Player() walls = [] setup_maze(random.choice(levels)) turtle.listen() turtle.onkey(player.go_left, "Left") turtle.onkey(player.go_right, "Right") turtle.onkey(player.go_up, "Up") turtle.onkey(player.go_down, "Down") # if turtle.bye() == True: # import MainMenuV4 # MainMenuV4.main_menu() wn.tracer(0) for enemy in enemies: turtle.ontimer(enemy.move, t=250) while True: for treasure in treasuress: if player.is_collision(treasure): pygame.mixer.pause() outro() for enemy in enemies: if player.is_collision(enemy): print("Player dies!") pygame.mixer.pause() turtle.clearscreen() wn.bgpic(image) game1() wn.update()
class Plotter(object): """ A plotter object provides a graphical window for plotting data and mathematical functions. """ def __init__(self, width, height, min_x, max_x, min_y, max_y): """ (Plotter, int, int, int, int, int, int) -> turtle The constructor initializes the Turtle graphics window. It accepts parameters that define the window’s physical size and ranges of x and y axes. Initializes the plotter with a window that is <width> wide and <height> high. Its x-axis ranges from <min_x> to <max_x>, and it's y-axis ranges from <min_y> to <max_y>. Establishes the global begining and ending x values for the plot and the x_increament value. Draws the x-axis and y-axes """ # Init self.pen = Pen() # The plotter object's pen self.screen = Screen() # The plotter object's sceen self.pen.speed(0) # Speed up rendering self.screen.tracer(0, 0) # DONT draw pen while drawing # Establish global x and y ranges self.min_x, self.max_x = min_x, max_x self.min_y, self.max_y = min_y, max_y self.screen.setup(width=width, height=height) # Set up window size, in pixels # set up screen size, in pixels self.screen.screensize(width, height) self.screen.setworldcoordinates(min_x, min_y, max_x, max_y) # x-axis distance that correspond to one pixel in window distance self.x_increament = (max_x - min_x) / width self.draw_grid(20) self.draw_axes() self.screen.title('Plot') self.screen.update() def __del__(self): """ (Plotter) -> stdout Called when the client no longer uses the plotter object. The interpreter calls this method, also known as destructor, when the object is no longer bound to a reference with in the executing program. Among other times, this happens when the program’s execution terminates. """ print('Done Printing') def draw_axes(self, grid=False): """ (Plotter, bool) -> turtle Draws the x and y axes within the plotting window. An option Boolean parameter <grid> controls the drawing of accessory horizontal and vertical lines """ if grid: self.draw_grid(20) self.pen.hideturtle() # Make pen invisible prev_width = self.pen.width() self.pen.width(2) # Draw x axis self.pen.color('black') self.draw_arrow(self.min_x, 0, self.max_x, 0) # Draw y axis self.draw_arrow(0, self.min_y, 0, self.max_y) self.pen.width(prev_width) def draw_grid(self, num): """ (Plotter, int) -> turtle Draws horizontal and vertical accessory reference coordinate lines on the plot. Parameter <num> control the frequency of the reference lines. """ self.pen.up() # self.pen.setposition(self.min_x, self.min_y) inc = (self.max_x - self.min_y) / num self.pen.color('lightblue') x = self.min_x while x <= self.max_x: self.pen.setposition(x, self.min_y) self.pen.down() self.pen.setposition(x, self.min_y) self.pen.up() x += inc # Next x inc = (self.max_y - self.min_y) / num y = self.min_y while y <= self.max_y: self.pen.setposition(self.min_x, y) self.pen.down() self.pen.setposition(self.max_x, y) self.pen.up() y += inc # Next y def draw_arrow(self, x1, y1, x2, y2): """ (Plotter, int, int, int, int) -> turtle Draws a line segment with an attached arrow head. Expects four numeric parameters representing the (x 1 , y 1 ) tail end point and (x 2 , y 2 ) head end point of the arrow. """ # Draw arrow shaft self.pen.up() self.pen.setposition(x1, y1) # Move the pen starting point self.pen.down() # Draw line bottom to top self.pen.setposition(x2, y2) # Move the pen starting point # Draw arrow head dy = y2 - y1 dx = x2 - x1 angle = atan2(dy, dx) * 180 / pi self.pen.setheading(angle) self.pen.stamp() def plot_function(self, f, color, update=True): """ (Plotter, func, str, bool) -> turtle Plots the curve of a given function <f> with a specified color, established by initialize_plotter. Plots (x, f(x)), for all x in range min_x <= x < max_x The color parameter dicatates the curve's color An optional Boolean parameter determines if the function renders immediately or requires the client to call the update method after a series of plots (defaults to True). """ # Move pen to starting position self.pen.up() self.pen.setposition(self.min_x, f(self.min_x)) self.pen.color(color) self.pen.down() # Iterate over the range of x values for min_x <= x < max_x x = self.min_x while x < self.max_x: self.pen.setposition(x, f(x)) x += self.x_increament # Next x if update: self.screen.update() def plot_data(self, data, color, update=True): """ (Plotter, list, str, bool) -> turtle Plots the curve (x, y) pairs of values in data_list. A parameter <color> specifies the curve’s color. An optional Boolean parameter determines if the function renders immediately or requires the client to call the update method after a series of plots (defaults to True). """ self.pen.up() self.pen.setposition(data[0][0], data[0][1]) self.pen.color(color) self.pen.down() # Plot the points in th data set for x, y in data: self.pen.setposition(x, y) if update: self.screen.update() def update(self): """ (Plotter) -> turtle Draws any pending actions to the screen """ self.screen.update() def setcolor(self, color): """ (Plotter, str) -> turtle Sets the current drawing color. """ self.pen.color(color) def onclick(self, fun): """ (Plotter, func) -> turtle Assigns a callback function that the frame should call when the user clicks the mouse button when the pointer is over the plotter window. The function <fun> must accept two integer parameters that represent the (x, y) location of the mouse when the click occurred """ self.screen.onclick(fun) def interact(self): """ (Plotter) -> turtle Sets the plotter to interactive mode, enabling the user to provide mouse and keyboard input. """ self.screen.mainloop()
from turtle import Pen from random import randint laki = Pen() def square(): side = 1 length = randint(20, 100) while side <=4: laki.forward(length) laki.left(90) side += 1 while True: laki.speed(1000) laki.penup() laki.goto(randint(-250, 250), randint(-250, 250)) laki.pendown() laki.begin_fill() laki.color("#" + str(randint(100000, 999999))) square() laki.end_fill()
#!/usr/bin/env python from turtle import Pen, bgcolor, pencolor, forward, left, width from time import sleep bgcolor("black") #set canvas background color colors = ["red", "green", "orange", "purple", "yellow", "blue"] canvas = Pen() for num in xrange(360): canvas.pencolor(colors[num % 6]) canvas.width(num / 100 + 1) #to give the effect of the line getting bigger as it "approaches" canvas.forward(num) #so line length keeps increasing canvas.left(59) sleep(500) #so we have some time to admire the artwork, before it closes automatically
def game1(): pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize pygame.init() # song = pygame.mixer.Sound('music/music.wav') pygame.mixer.music.load('Mars_Folder/music/music.wav') pygame.mixer.music.play(-1) global playerGold class Pen(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("white") self.penup() self.speed(0) class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("blue") self.penup() self.speed(0) self.gold = 100 def go_up(self): move_to_x = player.xcor() move_to_y = player.ycor() + 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_down(self): move_to_x = player.xcor() move_to_y = player.ycor() - 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_left(self): move_to_x = player.xcor() - 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_right(self): move_to_x = player.xcor() + 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def is_collision(self, other): a = self.xcor() - other.xcor() b = self.ycor() - other.ycor() distance = math.sqrt((a**2) + (b**2)) if distance < 5: return True else: return False class treasure(turtle.Turtle): def __init__(self, x, y): turtle.Turtle.__init__(self) self.shape("circle") self.color("gold") self.penup() self.speed(0) self.gold = 100 self.goto(x, y) def destroy(self): self.goto(2000, 2000) self.hideturtle() levels = [""] level_1 = [ "XXXXXXXXXXXXXXXXXXXXXXXXX", "XXP XXXXXXXXXXXXXXXXXXX", "XX XX XXXXXXXXXXXXXXXXXX", "XX XXX XXXXXXXXXXXXXXXX", "XX XXXX XXXXXXXXXXXXXXXX", "XX XXXXX XXXXXXXXXXXXXX", "XX XXXXXXX XXXXXXXXXXXX", "XX XXXXXXXXX XXXXXXXXXX", "XX XXXXXXXXXXX XXXXXXXX", "XX XXXXXXXXXXX XXXXXXXXX", "XX XXX XX XXXXXXXXX", "X XXX X", "XXXXXXXXXXXXXXXXX XXXXXX", "XXXXXXXXXXXXXXX XXXXXX", "XXXXXXXXXXXXXX XXXXXXXX", "XXXXXXXXXXXXX XXXXXXXXXX", "XXXXXXXXXXXXXX XXXXXXXXX", "XXXXXXXXXXXXXXX XXXXXXXX", "XXXXXXXXXXXXXXXX XXXXXXX", "XXXXXXXXXXXXXXXXX XXXXXX", "XXXXXXXXXXXXXXXXXX XXXXX", "XXXXXXXXXXXXXXXXXXX XXXX", "XXXXXXXXXXXXXXXXXXXX TXX", "XXXXXXXXXXXXXXXXXXXXXXXXX" ] treasuress = [] levels.append(level_1) def setup_maze(level): for y in range(len(level)): for x in range(len(level[y])): character = level[y][x] screen_x = -288 + (x * 24) screen_y = 288 - (y * 24) if character == "X": pen.goto(screen_x, screen_y) pen.stamp() walls.append((screen_x, screen_y)) if character == "P": player.goto(screen_x, screen_y) if character == "T": treasuress.append(treasure(screen_x, screen_y)) pen = Pen() player = Player() walls = [] setup_maze(levels[1]) turtle.listen() turtle.onkey(player.go_left, "Left") turtle.onkey(player.go_right, "Right") turtle.onkey(player.go_up, "Up") turtle.onkey(player.go_down, "Down") wn.tracer(0) while True: for treasure in treasuress: if player.is_collision(treasure): turtle.clearscreen() game2() wn.update()
from turtle import Pen, done, screensize from random import choice t = Pen() colors = choice(["orange", "yellow", "green", "cyan"]) screensize(canvwidth=1, canvheight=1, bg=colors) try: t.hideturtle() from time import asctime, localtime, time while True: localtimes = asctime(localtime(time())) t.write(localtimes, font=10) t.undo() done() except: pass
from random import choice #=========================# # DISTRIBUIÇÃO ELETRÔNICA # #-------------------------# # 1s2 # # 2s2 2p6 # # 3s2 3p6 3d10 # # 4s2 4p6 4d10 4f14 # # 5s2 5p6 5d10 5f14 # # 6s2 6p6 6d10 # # 7s2 7p6 # #_________________________# tela = Screen() caneta = Pen() caneta.hideturtle() raio_camada = (70, 85, 100, 115, 130, 145, 160) subniveis = elemento_quimico.subniveis diagrama_pauling = elemento_quimico.diagrama_pauling cores = ('blue', 'red', 'yellow', 'green', 'aquamarine', 'orange', 'purple', 'pink', 'gray', 'black') class ElementoDistribuicao: def __init__(self, simbolo): self.elemento = elemento_quimico.elemento[simbolo]
from turtle import Pen from time import sleep Karen = Pen() Peter = Pen() Bob = Pen() Govno = Pen() Karen.forward(120) Peter.forward(120) Bob.forward(140) Govno.forward(140) Karen.left(90) Peter.right(90) Bob.left(90) Govno.right(90) Karen.forward(60) Peter.forward(60) Bob.forward(30) Govno.forward(30) Karen.right(90) Peter.left(90) Bob.right(90) Govno.left(90)
from turtle import Pen from time import sleep pen = Pen() pen.right(60) pen.forward(60) pen.right(120) pen.forward(60) pen.right(120) pen.forward(60) pen.left(120) pen.up() pen.forward(55) pen.down() for i in range(4): pen.forward(35) pen.up() pen.forward(15) pen.left(90) pen.forward(15) pen.down() pen.forward(15) pen.right(90) pen.up() pen.forward(10) pen.down() pen.forward(120)
print('CurSecond : %d s' % (t1 % 60)) print('CurDay: %d-%02d-%02d' % (t2.year, t2.month, t2.day), \ 'CurTime: %d:%02d:%02d' % (t2.hour, t2.minute, t2.second)) # 当一行代码太长影响观看时,可以使用\分行书写 # 自定义函数def def myPytho(a1, a2=5): # a2 = 5表示a2的默认值为5 return a1**2 + a2 * a2 print('7^2 + 24^2 = ', myPytho(7, 24)) # 调用外部模块函数时,使用更简单的方法 from turtle import Pen tPen = Pen() iCircleCount = 10 for i in range(iCircleCount): tPen.circle(100) # 指示画圆的半径 tPen.right(360 / iCircleCount) # 每完成一个圆后所偏移的角度 # 输入函数input(),[]不同于数组,类似于cell概念,拥有stack的性质 list_Month = ['January', 'February', 'March', 'April', 'May', 'June', \ 'July', 'August', 'September', 'October', 'November', 'December'] imonth = input('Which month do you want to know?') iMonth = int(imonth) if 0 < iMonth < 13: print('The %sth month you want to know:' % imonth, list_Month[iMonth - 1]) else: print('error: The number is not in the limit')
def initial(): global t t = Pen() t.pensize(1) t.pencolor('grey') t.hideturtle() t.penup() for i in range(m + 1): t.goto(point_y_up[i]) t.pendown() t.goto(point_y_down[i]) t.penup() for i in range(n + 1): t.goto(point_x_le[i]) t.pendown() t.goto(point_x_ri[i]) t.penup()
def __init__(self): self._desenho = Pen() self._desenho.shape('circle') self._desenho.color('blue') self._velocidade = (0,0)
#!/usr.bin.env python from time import sleep from turtle import Pen, circle, right, pencolor, width canvas = Pen() colors = ["brown", "gray", "purple", "red"] for num in xrange(450): canvas.width(0.8) canvas.pencolor(colors[num % 4]) canvas.circle(num) canvas.right(91) sleep(500)
def step(t: turtle.Pen): t.penup() t.forward(i) t.left(46) t.pendown() t.dot(4 + i / 16)