def drawground(): s = turtle.Shape("compound") ground = ( (-320, 120), (-280, 41), (-240, 27), (-200, 59), (-160, 25), (-120, 43), (-80, 56), (-40, 20), (0, 20), (40, 20), (80, 44), (120, 28), (160, 66), (200, 29), (240, 64), (280, 34), (320, 140), (320, 0), (-320, 0), ) s.addcomponent(ground, "#8B4513", "#8B4513") turtle.register_shape("ground", s)
def makeshape(): B = 25 # base unit size turtle.begin_poly() turtle.fd(B) # roof turtle.rt(45) turtle.fd(B * 3/4) # windshield turtle.lt(45) turtle.fd(B) # hood turtle.rt(90) turtle.fd(B * 3/4) # front turtle.rt(90) turtle.fd(B * 1/7) turtle.lt(90) turtle.circle(-B/2, 180) # front tire turtle.lt(90) turtle.fd(B) turtle.lt(90) turtle.circle(-B/2, 180) # back tire turtle.lt(90) turtle.fd(B * 1/7) turtle.rt(90) turtle.fd(B * 5/6) # back turtle.end_poly() poly = turtle.get_poly() turtle.register_shape('car', poly)
def initialize_plot(self, positions): self.positions = positions self.minX = minX = min(x for x,y in positions.values()) maxX = max(x for x,y in positions.values()) minY = min(y for x,y in positions.values()) self.maxY = maxY = max(y for x,y in positions.values()) ts = turtle.getscreen() if ts.window_width > ts.window_height: max_size = ts.window_height() else: max_size = ts.window_width() self.width, self.height = max_size, max_size turtle.setworldcoordinates(minX-5,minY-5,maxX+5,maxY+5) turtle.setup(width=self.width, height=self.height) turtle.speed("fastest") # important! turtle is intolerably slow otherwise turtle.tracer(False) # This too: rendering the 'turtle' wastes time turtle.hideturtle() turtle.penup() self.colors = ["#d9684c","#3d658e","#b5c810","#ffb160","#bd42b3","#0eab6c","#1228da","#60f2b7" ] for color in self.colors: s = turtle.Shape("compound") poly1 = ((0,0),(self.cell_size,0),(self.cell_size,-self.cell_size),(0,-self.cell_size)) s.addcomponent(poly1, color, "#000000") turtle.register_shape(color, s) s = turtle.Shape("compound") poly1 = ((0,0),(self.cell_size,0),(self.cell_size,-self.cell_size),(0,-self.cell_size)) s.addcomponent(poly1, "#000000", "#000000") turtle.register_shape("uncolored", s)
def drawfus(): # spaceship! global basesize B = basesize turtle.begin_poly() turtle.fd(B) turtle.rt(90) turtle.fd(B) turtle.rt(90) turtle.fd(B) turtle.rt(90) turtle.fd(B) turtle.fd(2.25 * B) turtle.rt(90) turtle.fd(B) turtle.rt(90) turtle.fd(B) turtle.rt(90) turtle.fd(B) turtle.fd(3 * B) turtle.rt(-90) turtle.fd(1.25 * B) turtle.end_poly() poly = turtle.get_poly() # c'est le poly... yveslemaire.poly turtle.register_shape('fusee', poly)
def maketree(name, scale, L): turtle.home() turtle.begin_poly() stack = [ (0, 0) ] maketree_r(stack, L, scale) turtle.end_poly() poly = turtle.get_poly() turtle.register_shape(name, poly)
def makepop(fn, *args): turtle.home() turtle.begin_poly() fn(*args) turtle.end_poly() name = 'pop%d' % len(POPS) turtle.register_shape(name, turtle.get_poly()) POPS.append(name)
def reg_bullet(): turtle.home() turtle.setpos(0, -5) turtle.begin_poly() turtle.circle(5, None, None) turtle.end_poly() circ = turtle.get_poly() turtle.register_shape('bullet', circ)
def radar_chart(data): # Some "typical" test data #print "Hello" length=len(data) # stores the length of the data provided turtle.home() # Sets the turtle to position (0,0) division=360/length #what angle is needed for invidual lines poslist=[] #list to store current position valpos=[] #list to store position j=0 turtle.hideturtle() #hides the arrow #Draw the foundation of the Radar Chart for i in range(length): # Loop until all the given data is plotted turtle.forward(200) #move turtle forward turtle.dot(10,"black") # Draw the black dot at the end of each data nowpos=turtle.pos() # store the current position poslist.append(nowpos) #append the current position to list #turtle.hideturtle() turtle.setpos(nowpos[0]+10,nowpos[1]) #get the turtle to new postion to write data turtle.write(data[i], True, align="center") # Write the label of data turtle.setpos(nowpos[0],nowpos[1]) #return to the previous position turtle.back(200) #return home turtle.left(division) # rotate by the specific angle turtle.home() # return to turtle home #Connect the ends points of the radar chart for i in poslist: # turtle.setpos(i[0],i[1]) #turtle.setpos(i[j],i[j+1]) #turtle.forward(100) #turtle.home() #turtle.degree(division) #turtle.heading() #turtle.forward(100) turtle.setpos(poslist[0][0],poslist[0][1]) turtle.home() #Draw green Dots for i in range(length): incval=data[i] turtle.forward(incval*2) turtle.dot(15,"green") nowpos=turtle.pos() valpos.append(nowpos) turtle.back(incval*2) turtle.left(division) turtle.begin_poly() turtle.fill(True) #Fill the green Dots for i in valpos: turtle.setpos(int(i[0]),int(i[1])) turtle.setpos(valpos[0][0],valpos[0][1]) turtle.end_poly() p = turtle.get_poly() turtle.register_shape("jpt", p) turtle.color("Green", "Green") turtle.begin_fill() #turtle.p(80) turtle.end_fill() turtle.fill(False)
def makeground(): for i in range(GROUNDLEN-3, GROUNDLEN+9): turtle.home() turtle.begin_poly() turtle.fd(i) turtle.end_poly() name = 'gr%d' % i turtle.register_shape(name, turtle.get_poly()) GROUND.append(name)
def __init__(self, ulx, uly, lrx, lry): # refer to counter as class variable name = 'BigRect.%d' % BigRect._counter BigRect._counter = BigRect._counter + 1 turtle.register_shape(name, ( (ulx, uly), (lrx, uly), (lrx, lry), (ulx, lry) )) super().__init__(0, 0, 0, 1, name, 'yellow')
def reg_sun(): global sundiam turtle.home() turtle.setpos(0, -sundiam / 2) turtle.begin_poly() turtle.circle(sundiam/2, None, None) turtle.end_poly() circ = turtle.get_poly() turtle.register_shape('sun', circ)
def makepipes(): for i in range(PIPEMIN, PIPEUNITS): name = ht2name(i) ulx = i * PIPEUNIT lrx = 0 uly = 0 lry = PIPEWIDTH turtle.register_shape(name, ( (ulx, uly), (lrx, uly), (lrx, lry), (ulx, lry) ))
def makeshapes(): B = 25 # base unit size for i in range(1, 16): name = 'a%d' % i turtle.register_shape(name, makepoly(B, B/i, B + B*i*0.2)) SEQ.append(name) # flip sequence except for first one for i in range(len(SEQ)-1, 0, -1): SEQ.append(SEQ[i])
def mkHand(name, length): #注册turtle形状,建立表针turtle turtle.reset() Skip(-length*0.1) #开始记录多边形的顶点,当前的位置是多边形的第一个顶点 turtle.begin_poly() turtle.forward(length*1.1) #停止记录多边形的顶点,当前位置是多边形的最后一个顶点,将于第一个顶点相连 turtle.end_poly() #返回最后记录的多边形 handForm = turtle.get_poly() turtle.register_shape(name, handForm)
def mkHand(name, length): # 注册Turtle形状,建立表针TuConsolartle turtle.reset() Skip(-length * 0.1) # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。 turtle.begin_poly() turtle.forward(length * 1.1) # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。 turtle.end_poly() # 返回最后记录的多边形。 handForm = turtle.get_poly() turtle.register_shape(name, handForm)
def create_leg_shape(): WN.tracer(0) #do not draw screen. t = turtle.Turtle() t.pen(speed=0, shown=False, pendown=False) t.lt(90) t.fd(10) t.begin_poly() t.fd(100) t.circle(7) t.end_poly() p = t.get_poly() turtle.register_shape('leg', p)
def maketweendata(kf1, kf2, steps, scale): global _kfsegments assert len(kf1) == len(kf2) # must be able to match segments up L = [] for i in range(len(kf1)): [ (x1, y1), (x2, y2) ] = normalize(kf1, i, scale) # Euclidean distance gives segment length seglen = ( (x2 - x1) ** 2 + (y2 - y1) ** 2 ) ** 0.5 # make line segment into shape turtle.home() turtle.begin_poly() turtle.fd(seglen) turtle.end_poly() name = 'kf%d' % _kfsegments _kfsegments += 1 turtle.register_shape(name, turtle.get_poly()) # and compute initial heading heading = getheading(x1, y1, x2, y2) # extract out corresponding segment from key frame 2 [ (x1b, y1b), (x2b, y2b) ] = normalize(kf2, i, scale) # use it to compute deltas for x, y, and heading; this is # where we need to be after N steps dx = x1b - x1 dy = y1b - y1 dh = getheading(x1b, y1b, x2b, y2b) - heading # weird special case that cropped up between BKF3 and BKF4 of # bird flap, where the computed delta in the heading takes the # long way around, as it were - adjust it to compensate if dh > 180: dh = dh - 360 elif dh < -180: dh = dh + 360 dx /= steps dy /= steps dh /= steps # place everything in a container c = Tween() c.name = name c.x, c.y = x1, y1 c.heading = heading c.dx, c.dy = dx, dy c.dh = dh L.append(c) return L
def reg_enemy(): global basesize B = 2*basesize enemyship = turtle.Shape("compound") enemy_mesh = ((0, -1*B), (1*B, -0.33*B), (1*B, 0.33*B), (0, 1*B), (-1*B, 0)) left_antenna = ((-0.33*B, -0.66*B), (-B, -B), (-0.66*B, -0.33*B)) right_antenna = ((-0.33*B, 0.66*B), (-B, B), (-0.66*B, 0.33*B)) enemyship.addcomponent(enemy_mesh, "red", "green") enemyship.addcomponent(left_antenna, "red", "green") enemyship.addcomponent(right_antenna, "red", "green") turtle.register_shape("enemy", enemyship)
def __init__(self, dx, x_cor, y_cor,width, speed, color): Turtle.__init__(self) turtle.register_shape("pad.gif") self.shape("pad.gif") self.ht() self.dx = dx self.x_cor = x_cor self.y_cor = y_cor self.width=width self.speed=speed self.goto(self.x_cor, self.y_cor) self.color=color self.st()
def drawfus_alt(): global basesize B = basesize ship = turtle.Shape("compound") mesh = ((1 * B, 0), (2 * B, 2 * B), (-2 * B, 0), (2 * B, -2 * B), (1 * B, 0)) ship.addcomponent(mesh, "black", "black") redship = turtle.Shape("compound") redship.addcomponent(mesh, "red", "red") turtle.register_shape("fusee", ship) turtle.register_shape("fusee reac", redship)
def reg_ship(): global basesize B = basesize ship = turtle.Shape("compound") mesh = ((1*B, 0), (2*B, 2*B), (-2*B, 0), (2*B, -2*B), (1*B, 0)) ship.addcomponent(mesh, "#555555", "#555555") redship = turtle.Shape("compound") reaction = ((1.5*B, 1*B), (2*B, 0), (1.5*B, -1*B), (1*B, 0)) redship.addcomponent(mesh, "#555555", "#555555") redship.addcomponent(reaction, "yellow", "yellow") turtle.register_shape('lander', ship) turtle.register_shape('powered_lander', redship)
def drawfus_alt(): global basesize B = basesize ship = turtle.Shape("compound") mesh = ((1*B,0), (2*B, 2*B), (-2*B,0), (2*B, -2*B), (1*B,0) ) ship.addcomponent(mesh, "#555555", "#555555") redship = turtle.Shape("compound") reaction = ((1.5*B, 1*B), (2*B, 0), (1.5*B, -1*B), (1*B, 0)) redship.addcomponent(mesh, "#555555", "#555555") redship.addcomponent(reaction, "yellow", "yellow") # réacteur, (c) Antonin turtle.register_shape('fusee', ship) turtle.register_shape('fusee reac', redship)
def showParticles(self, _loc): turtle.tracer(50000, delay=0) turtle.register_shape("dot", ((-3, -3), (-3, 3), (3, 3), (3, -3))) turtle.register_shape("tri", ((-3, -2), (0, 3), (3, -2), (0, 0))) turtle.speed(0) turtle.setworldcoordinates(0,(self.maze.resolution+1)*3,(self.maze.resolution+1)*3,0) turtle.up() turtle.clearstamps() lines = turtle.Turtle() lines.color("black") lines.pensize(5) for i in range(len(self.maze.layout)): for j in range(len(self.maze.layout[0])): if self.maze.layout[i][j][2]=='X': lines.penup() lines.goto(j*(self.maze.resolution)-1,(i+1)*self.maze.resolution-1) lines.pendown() lines.forward(self.maze.resolution) lines.penup() if self.maze.layout[i][j][1] == 'X': lines.penup() lines.goto(j * (self.maze.resolution) - 1, i * self.maze.resolution) lines.pendown() lines.forward(self.maze.resolution) lines.penup() turtle.shape('tri') for p in self.particles: #print(p.x, p.y) turtle.setposition(p.x,p.y) heading = ((p.orientation +math.pi/2) / (2 * math.pi)) * 360 turtle.setheading(heading) turtle.color("red") turtle.stamp() #turtle.update() turtle.shape('dot') turtle.color("blue") turtle.setposition(self.bestParticle.x, self.bestParticle.y) turtle.stamp() turtle.shape('turtle') turtle.color("green") turtle.setposition(_loc[0], _loc[1]) headingr = ((_loc[2] +math.pi/2) / (2 * math.pi)) * 360 turtle.setheading(headingr) #heading = (self.particles[self.prWithHeighestW].orientation / (2 * math.pi)) * 360 #turtle.setheading(heading) turtle.update()
def makeshipshape(): turtle.home() # return to known location & orientation # cockpit is a trapezoid - figure out angles and length of side adj = (S / 3 - S / 8) / 2 thetarad = math.atan2(S, adj) theta = math.degrees(thetarad) hyp = S / math.sin(thetarad) turtle.begin_poly() turtle.bk(S * 1/2 / 2) # origin = center of wing - move to back turtle.lt(90) # left wing turtle.fd(S / 2) turtle.rt(30) # left rear turret turtle.bk(S / 8) turtle.fd(S / 8) turtle.rt(60) turtle.fd(S * 1/2) turtle.rt(60) # left front turret turtle.fd(S / 8) turtle.bk(S / 8) turtle.rt(30) turtle.fd(S / 3) # join of wing and left side of cockpit turtle.lt(theta) # left side of cockpit turtle.fd(hyp) turtle.rt(theta) turtle.fd(S / 8) # front of cockpit turtle.rt(theta) # right side of cockpit turtle.fd(hyp) turtle.lt(theta) # join of right side of cockpit and wing turtle.fd(S / 3) # right wing turtle.rt(30) # right front turret turtle.bk(S / 8) turtle.fd(S / 8) turtle.rt(60) turtle.fd(S * 1/2) turtle.rt(60) # right rear turret turtle.fd(S / 8) turtle.bk(S / 8) turtle.rt(30) turtle.fd(S / 2) turtle.end_poly() poly = turtle.get_poly() turtle.register_shape('spaceship', poly)
def maketileshape(): turtle.home() # return to known location & orientation turtle.begin_poly() # square for i in range(4): turtle.fd(B) turtle.rt(90) turtle.end_poly() poly1 = turtle.get_poly() # don't put this inside begin_poly...end_poly or it draws an extra line turtle.goto( (B / 2, -B * 2/3 - B/6) ) turtle.begin_poly() # circle, inside square turtle.circle(B / 3) turtle.end_poly() poly2 = turtle.get_poly() cs = turtle.Shape('compound') # args are poly, fill color, line color cs.addcomponent(poly1, 'gray', 'black') cs.addcomponent(poly2, 'blue', 'gray42') turtle.register_shape('tile', cs)
def makeshape(): """ Define the shapes of the various elements of the game. :return: None """ B = 15 s1 = turtle.Shape("compound") landerS = ((B, B-5), (B-5, B), (-B+5, B), (-B, B-5), (-B, -B+5), (-B+5, -B), (B-5, -B), (B, -B+5)) leg1 = ((B-5, -B), (B, -B), (B+3, -B-10), (B, -B-10), (B+6, -B-10), (B+3, -B-10)) leg2 = ((-B+5, -B), (-B, -B), (-B-3, -B-10), (-B, -B-10), (-B-6, -B-10), (-B-3, -B-10)) s1.addcomponent(landerS, "white", 'black') s1.addcomponent(leg1, "white", "black") s1.addcomponent(leg2, "white", "black") turtle.register_shape('lander', s1) s2 = turtle.Shape("compound") s2.addcomponent(landerS, "white", "black") s2.addcomponent(leg1, "white", "black") s2.addcomponent(leg2, "white", "black") motor = ((0, -B), (B/3, -B*2), (0, -B*2 -2), (-B/3, -B*2)) s2.addcomponent(motor, "red", "red") turtle.register_shape('lander with motor', s2) turtle.begin_poly() turtle.rt(90) turtle.fd(60) turtle.lt(90) turtle.circle(60) turtle.end_poly() poly = turtle.get_poly() turtle.register_shape('sun', poly) s = turtle.Shape("compound") groundS = ((-320, 120), (-280, 41), (-240, 27), (-200, 59), (-160, 25), (-120, 43), (-80, 56), (-40, 20), (0, 20), (40, 20), (80, 44), (120, 28), (160, 66), (200, 29), (240, 64), (280, 34), (320, 140), (320, 0), (-320, 0)) s.addcomponent(groundS, "black", "black") turtle.register_shape('ground', s)
border.goto(250, -250) border.goto(-250, -250) border.goto(-250, 250) border.goto(250, 250) border.penup() # random_maze ( how many blocks ) #################################the food and the score########################## score = 0 pos_list = [] stamp_list = [] food_pos = [] food_stamps = [] turtle.register_shape('car3.gif') car = turtle.clone() car.shape("car3.gif") turtle.hideturtle() car.showturtle() turtle.register_shape('car.gif') car = turtle.clone() car.shape("car.gif") turtle.penup() pos_list = [] stamp_list = [] food_pos = [] food_stamps = [] number_of_burgers = random.randint(5, 15) turtle.register_shape("burger.gif")
import turtle class Direction(Enum): STOP = 0 UP = 1 DOWN = 2 RIGHT = 3 LEFT = 4 SCREEN = 750 GRID = 600 STEP = 20 DELAY = 0.12 BORD = GRID // 2 - 10 SNAKE_START = (-30, 30) FOOD_START = (-30, 110) SCORE_POS = (320, 320) RECORD_POS = (260, 320) APPLE_LOGO_POS = (300, 330) TROPHY_LOGO_POS = (240, 330) BG_PATH = os.getcwd() + '/resources/sn.gif' APPLE_PATH = os.getcwd() + '/resources/apple.gif' TROPHY_PATH = os.getcwd() + '/resources/trophy.gif' turtle.register_shape(APPLE_PATH) turtle.register_shape(TROPHY_PATH)
import turtle import random turtle.register_shape('fireball.gif') turtle.register_shape('sc.gif') turtle.register_shape('pizza.gif') character = turtle.clone() character.shape('sc.gif') height = 600 character.penup() character.goto(0, -height / 2 + 100) width = 800 unitsize = 20 turtle.tracer(1, 0) turtle.setup(width, height) #turtle.hideturtle() turtle.penup() counter_fireballs = 0 fire_list = [] step = 20 bottom = -height / 2 + 100 def create_fire(): y_pos = height / 2 - 40 min_x = -int(width / 2 / unitsize) + 1 max_x = int(width / 2 / unitsize) - 1 x_pos = random.randint(min_x, max_x) * unitsize fire = turtle.clone()
box.hideturtle() box.goto(300, -200) box.pendown() box.goto(300, 200) box.goto(-300, 200) box.goto(-300, -200) box.goto(300, -200) box.penup() win.tracer(0) # Turns off the screen updates # Snake head head = turtle.Turtle() head.speed(0) turtle.register_shape("sq", ((0, 0), (0, 20), (20, 20), (20, 0))) head.shape("sq") head.color("green") head.penup() head.goto(-300, -180) head.direction = "stop" # Snake food food = turtle.Turtle() food.speed(0) food.shape("sq") food.color("blue") food.penup() food.goto(0, 100) segments = []
import turtle import math wn = turtle.Screen() wn.bgcolor("black") wn.title("Maze Game") wn.setup(700, 700) #registershapes turtle.register_shape("wizardright.gif") turtle.register_shape("wizardleft.gif") turtle.register_shape("treasure.gif") turtle.register_shape("wall.gif") #create pen class Pen(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("circle") self.color("white") self.penup() self.speed(0) class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("wizardright.gif") self.color("blue")
import turtle import math import random from tkinter import * from threading import Thread a = turtle.Screen() a.title('Difficult Level') a.bgcolor('black') a.setup(700, 760) a.tracer(0) # register shapes - registration of our images turtle.register_shape("mg_chucha_front.gif") turtle.register_shape("mg_chucha_right.gif") turtle.register_shape("mg_chucha_left.gif") turtle.register_shape("mg_corn.gif") turtle.register_shape("mg_enemy.gif") turtle.register_shape("mg_wal.gif") class Pen(turtle.Turtle): # self refers to the object that we are calling the method def __init__(self): # because Pen is child class of Turtle class . So we also have to initialise Turtle class turtle.Turtle.__init__(self) self.shape('mg_wal.gif') self.color('white') # turtle usually leaves a little trail behind because the pen is down in this case. We want pen to be up becasue we don't want to draw anything self.penup() #Speed doesn't referes to the speed of the motion. It's the animation speed. This is that which we need for out total graphice to work as a game #And set the speed as 0 means that speed is fastest. self.speed(0)
def random_color(self): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) self.color(r,g,b) s=Square(5) s.random_color() turtle.mainloop() ''' from turtle import Turtle import turtle turtle.begin_poly() turtle.register_shape("strawberry",(turtle.get_poly())) import turtle turtle.pu() turtle.forward(50) for i in range (6): turtle.pd() turtle.right(60) turtle.forward(50) turtle.mainloop() turtle.end_poly() class Hexagon(Turtle): def __init__(self,size): self.shapesize(size) self.shape(strawberry) c=Hexagon(200)
# ------------------------------------------------------------------------ # coding=utf-8 # ------------------------------------------------------------------------ # # Created by Martin J. Laubach on 2011-11-15 # Modified by Joseph Tosey on 2011-11-16 # # ------------------------------------------------------------------------ import turtle import random turtle.tracer(50000, delay=0) turtle.register_shape("dot", ((-3,-3), (-3,3), (3,3), (3,-3))) class Maze(object): def __init__(self, maze): self.maze = maze self.width = len(maze[0]) self.height = len(maze) turtle.setworldcoordinates(0, 0, self.width, self.height) self.blocks = [] self.beacons = [] for y, line in enumerate(self.maze): for x, block in enumerate(line): if block: nb_y = self.height - y - 1 self.blocks.append((x, nb_y)) if block == 2: self.beacons.extend(((x, nb_y), (x, nb_y+1), (x+1, nb_y+1)))
timestep = int(timestep) print('timestep; ' + str(timestep)) print('score: ' + str(score_int)) if timestep == 0: return 1 else: return timestep turtle.register_shape("trash.gif") #Add trash picture # Make sure you have downloaded this shape # from the Google Drive folder and saved it # in the same folder as this Python script food = turtle.clone() food.shape("trash.gif") #Locations of food food_pos = [(100,100), (-100,100), (-100,-100), (100,-100)] food_stamps = [] # Write code that: #1. moves the food turtle to each food position for i in food_pos : food.goto(i)
#Space Invaders - Part 10 #Sound (MacOSX / Linux Only) #Python 2.7 on Mac import turtle import os import math import random #Set up the screen wn = turtle.Screen() wn.bgcolor("black") wn.title("Space Invaders") wn.bgpic("~/Desktop/space_invaders_background.gif") #Register the shapes turtle.register_shape("~/Desktop/invader.gif") turtle.register_shape("~/Desktop/player.gif") #Draw border border_pen = turtle.Turtle() border_pen.speed(0) border_pen.color("white") border_pen.penup() border_pen.setposition(-300,-300) border_pen.pendown() border_pen.pensize(3) for side in range(4): border_pen.fd(600) border_pen.lt(90) border_pen.hideturtle()
SQUARE_SIZE = 20 START_LENGTH = 5 pos_list = [] stamp_list = [] food_pos = [] food_stamps = [] snake = turtle.clone() snake.shape("circle") snake.color('blue') turtle.hideturtle() background = turtle.clone() turtle.register_shape("adam.gif") background.shape("adam.gif") background.goto(0, 0) background.pendown() background.stamp() edges = turtle.clone() turtle.register_shape("taco.gif") edges.shape("taco.gif") edges.penup() edges.goto(400, 250) edges.pendown() edges.stamp() edges.goto(400, -250) edges.stamp() edges.goto(-400, -250)
def second_game(): tracer(0) # ghost = turtle.clone() ghost.penup() # go = turtle.clone() go.ht() turtle.register_shape("small_ghost.gif") ghost.shape("small_ghost.gif") # door = turtle.clone() turtle.register_shape("Door.gif") door.shape("Door.gif") door.penup() door.goto(0, 325) ghost.goto(0, -400) turtle.ht() turtle.speed(0) # right_wall = turtle.clone() right_wall.color("red") right_wall.st() right_wall.penup() right_wall.shape("square") right_wall.speed(1000000) right_wall.shapesize(60) right_wall.goto(1000, 0) # left_wall = turtle.clone() left_wall.color("red") left_wall.st() left_wall.penup() left_wall.shape("square") left_wall.speed(100000) left_wall.shapesize(60) left_wall.goto(-1000, 0) # rx = right_wall.xcor() # lx = left_wall.xcor() isSafe = True #temp=turtle.clone() temp.st() temp.penup() temp.shape("square") temp.goto(0, 0) def collide(ghost, left_wall, right_wall): global isSafe if (left_wall.xcor() + 600) >= ghost.xcor() or (right_wall.xcor() - 600) <= ghost.xcor(): print("collided") isSafe = False return True return False def col_with_door(ghost, door): if ghost == door: return False distance = math.sqrt( math.pow(ghost.xcor() - door.xcor(), 2) + math.pow(ghost.ycor() - door.ycor(), 2)) if distance <= 30: return True else: return False def walls_move(): global isSafe rx = right_wall.xcor() lx = left_wall.xcor() while isSafe: collide(ghost, left_wall, right_wall) right_wall.goto(rx, 0) rx = rx - 1 left_wall.goto(lx, 0) lx = lx + 1 ghost.clear() update() time.sleep(0.01) if collide(ghost, left_wall, right_wall): go.write("GAME OVER!!! TRY AGAIN IN 1000 YEARS!!!", align="center", font=("david", 30, "normal")) time.sleep(4) #print("Reached") quit() if col_with_door(ghost, door): #turtle.clearscreen() # turtle.bgpic("heaven_bg1.png") # turtle.write("CONGRATS! YOU'VE MADE IT TO HEAVEN!!", align = "center", font = ("david", 30, "normal")) isSafe = False ghost.hideturtle() door.hideturtle() left_wall.hideturtle() right_wall.hideturtle() third_game() # t = threading.Thread(name = 'walls', target = walls_move) #t.start() def key1(): ghost.setheading(90) ghost.fd(4.5) update() def key2(): ghost.setheading(180) ghost.fd(4.5) update() def key3(): ghost.setheading(270) ghost.fd(4.5) update() def key4(): ghost.setheading(360) ghost.fd(4.5) update() Screen().onkey(key1, "Up") Screen().onkey(key2, "Left") Screen().onkey(key3, "Down") Screen().onkey(key4, "Right") Screen().listen() walls_move()
turtle.fd(0) # set the animation speed to max turtle.speed(0) turtle.bgcolor("black") turtle.bgpic("starfield.gif") turtle.title("StarWars") # hide the default turtle turtle.ht() # This saves memory turtle.setundobuffer(1) # This speeds up drawing turtle.tracer(0) screen = turtle.Screen() # Register shapes turtle.register_shape("enemy.gif") turtle.register_shape("ally.gif") class Ufo(turtle.Turtle): def __init__(self, ufo_shape, color, init_x, init_y): turtle.Turtle.__init__(self, shape=ufo_shape) self.speed(0) # animation speed in the range 0..10 self.penup() # start drawing self.color(color) self.fd(0) self.goto(init_x, init_y) self.speed = 1 def move(self): self.fd(self.speed)
global direction direction = RIGHT print("You pressed the right key!") turtle.onkeypress(up, UP_ARROW) # Create listener for up key #3. Do the same for the other arrow keys ####WRITE YOUR CODE HERE!! turtle.onkeypress(down, DOWN_ARROW) turtle.onkeypress(left, LEFT_ARROW) turtle.onkeypress(right, RIGHT_ARROW) turtle.listen() for color in color_list: print(color) turtle.register_shape(color) food = turtle.clone() rand_color = random.randint(0, len(color_list) - 1) rand_Shape = color_list[rand_color] food.shape(rand_Shape) food_pos = [(100, 100), (-100, 100), (-100, -100), (100, -100)] food_stamps = [] for pos in food_pos: food.goto(pos) st = food.stamp() food_stamps.append(st) food.ht()
import turtle import random import time turtle.tracer(1, 0) SIZE_X = 900 SIZE_Y = 600 turtle.setup(SIZE_X, SIZE_Y) #size. turtle.penup() SQUARE_SIZE = 50 turtle.register_shape("Space_Shipp.gif") START_LENGTH = 1 #Initialize lists space = turtle.clone() space.goto(0, -200) space.shape("Space_Shipp.gif") turtle.hideturtle() arrow_circle = turtle.clone() arrow_circle.shape('circle') LEFT_ARROW = "Left" RIGHT_ARROW = "Right" SPACEBAR = "space" TIME_STEP = 0
import random import time import turtle countdownT = turtle.clone() countdownT.ht() turtle.tracer(1, 0) enemy = turtle.clone() enemy.st() enemy.penup() enemy.goto(200, -100) turtle.register_shape("ghost.gif") enemy.shape("ghost.gif") does_player_have_food = False import timer def check_time(): if timer.choose == 0: countdownT.clear() countdownT.penup() countdownT.goto(0, 0) countdownT.pendown() countdownT.pencolor("white") countdownT.write("Player won!", font=("Ariel", 48, "normal")) time.sleep(3) quit() turtle.ontimer(check_time, TIME_STEP) #enemy.shape("circle") #screan size
#Space Invaders - Part 2 #Set up the screen #Python 3.7 on Win import turtle import winsound import math import random #Set up the screen wn = turtle.Screen() wn.bgcolor("black") wn.title("Space Invaders") wn.bgpic("space_invaders_background.gif") #Register the shapes turtle.register_shape('computerman.gif') #Draw border border_pen = turtle.Turtle() border_pen.speed(0) border_pen.color('white') border_pen.penup() border_pen.setposition(-300, -300) border_pen.pendown() border_pen.pensize(3) for side in range(4): border_pen.fd(600) border_pen.lt(90) border_pen.hideturtle() #Set the score to 0
def controller(): """set controller function for game""" win.listen() win.onkey(snoopy.up, 'Up') win.onkey(snoopy.down, 'Down') win.onkey(snoopy.left, 'Left') win.onkey(snoopy.right, 'Right') win.onkey(quit, 'q') win.onkey(start, 's') win.onkey(instruct, 'space') ##########PLAYER CLASS SETUP############## turtle.register_shape("snoopy.gif") turtle.register_shape("woodstock_2.gif") class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.penup() self.speed(0) def up(self): """moves player up maze by 24 pixels""" if (self.xcor(), self.ycor() + 24) not in barriers and ( self.xcor(), self.ycor() + 24) not in winner: self.goto(self.xcor(), self.ycor() + 24) if (self.xcor(), self.ycor()) in trivia_list:
def __init__(self, x, y): Turtle.__init__(self) turtle.register_shape("rectangle", ((0, 0), (0, y), (x, y), (x, 0), (0, 0))) self.shape("rectangle")
turtle.setup(size_x, size_y) square_size = 20 good_food_names = ["apple", "banana", "strawberry", "orange"] good_food_clones = [] bad_food_names = ["bad_apple", "bad_banana", "bad_strawberry", "bad_orange"] bad_food_clones = [] other_names = ["gun", "handbomb", "rocket"] other_clones = [] turtle.hideturtle() # make good_food_clones list for i in range(len(good_food_names)): obj = turtle.clone() shape_name = good_food_names[i] + ".gif" turtle.register_shape(shape_name) obj.shape(shape_name) good_food_clones.append(obj) # make bad_food_clones list for i in range(len(bad_food_names)): obj1 = turtle.clone() shape_name = bad_food_names[i] + ".gif" turtle.register_shape(shape_name) obj1.shape(shape_name) bad_food_clones.append(obj1) # make other_clones list for i in range(len(other_names)): obj2 = turtle.clone() shape_name = other_names[i] + ".gif"
import turtle import random turtle.tracer(1, 0) SIZE_X = 800 SIZE_Y = 500 square_size = 30 turtle.penup() turtle.hideturtle() turtle.register_shape('plane1.gif') turtle.goto(-300, 250) turtle.showturtle() plane = turtle.clone() plane.shape('plane1.gif') plane.goto(100, 100) turtle.hideturtle() number_of_types = 4 food_pos = [] food_drop_pos = [] food_drop_stamp = [] food_stamp = [] turtle.penup() turtle.hideturtle() food = turtle.clone() food_type = 0 drop_time = 200 drf = 0
turtle.ontimer(move_snake, TIME_STEP) move_snake() score_turtle = turtle.clone() score = 0 def counter(): global score score += 1 score_turtle.clear() score_turtle.write(score) score_turtle.goto(390, 240) turtle.goto(400, 260) turtle.register_shape("trash.gif") food = turtle.clone() food.shape("trash.gif") #locations of food food_pos = [(100, 100), (-100, 100), (-100, -100), (100, -100)] food_stamps = [] for this_food_pos in food_pos: food.goto(this_food_pos) stampoo = food.stamp() food_stamps.append(stampoo) turtle.mainloop()
# Space Invaders # set up the screen import turtle import os import math import random # set up the screen wn = turtle.Screen() wn.bgcolor("black") wn.title("vinod Space War") wn.bgpic("Space.gif") #registers the shape turtle.register_shape("enemy.gif") turtle.register_shape("shooter.gif") #draw border border_pen = turtle.Turtle() border_pen.speed(0) border_pen.color("white") border_pen.penup() border_pen.setposition(-300, -300) border_pen.pendown() border_pen.pensize(3) for side in range(4): border_pen.fd(600) border_pen.lt(90) border_pen.hideturtle()
def Game(): faces = ["sadnizar-final.gif", "happynizar-final.gif"] turtle.setup(1000, 1000) player = turtle.Turtle() scoreCounter = turtle.Turtle() screen = turtle.Screen() screen.setup(1000, 1000) screen.bgpic("DUCKBACKGROUND.gif") nizar_width = 200 nizar_height = 67 turtle.listen() global counter counter = 0 #creating borderscounter title = turtle.Turtle() title.penup() title.goto(0, 400) title.write("Catch the nizars!!", font=("Comic Sans MS", 40), align="center") title.hideturtle() border = turtle.Turtle() border.speed(0) border.penup() border.goto(-400, -400) border.pendown() border.goto(-400, 400) border.goto(400, 400) border.goto(400, -400) border.goto(-400, -400) border.hideturtle() scoreCounter.penup() scoreCounter.hideturtle() scoreCounter.goto(370, 400) #creating the recycle bin turtle.register_shape("nada-transparent.gif") turtle.register_shape('boaz-transparent.gif') player.penup() turtle.register_shape("mahmoud-final.gif") player.shape("mahmoud-final.gif") player.goto(0, -300) ######################3def StartGame(): #for happy nizars def scoreup(): global counter scoreCounter.clear() scoreCounter.penup() scoreCounter.hideturtle() scoreCounter.goto(370, 400) counter = counter + 2 scoreCounter.write("Score = " + str(counter), font=("Comic Sans MS", 30), align="center") #for sad nizars //we need minus 1 counter def scoredown(): global counter scoreCounter.clear() scoreCounter.penup() scoreCounter.hideturtle() scoreCounter.goto(370, 400) counter = counter - 1 scoreCounter.write("Score = " + str(counter), font=("Comic Sans MS", 30), align="center") #making the player move def right(): if player.xcor() > 280: player.setpos(280, -300) else: player.forward(80) def left(): if player.xcor() < -280: player.setpos(-280, -300) else: player.back(80) turtle.onkeypress(right, "Right") turtle.onkeypress(left, "Left") def falling_trash(): global counter nizars = [] trash = turtle.Turtle() trash.hideturtle() trash.setheading(270) trash.penup() #spawns trash/nizar's bootiful face nizars.append(trash.setpos(random_xpos, 400)) turtle.register_shape("happynizar-final.gif") turtle.register_shape("sadnizar-final.gif") trash.shape(random.choice(faces)) trash.showturtle() while trash.pos()[1] > -299: leftside_nizar = trash.xcor() - 100 rightside_nizar = trash.xcor() + 100 topside_nizar = trash.ycor() + 33.5 player_width = 500 trash.forward(5) #collision if topside_nizar <= -120 and rightside_nizar >= player.xcor( ) - 250 and rightside_nizar <= player.xcor( ) + 250 and leftside_nizar >= player.xcor( ) - 250 and leftside_nizar <= player.xcor() + 250: if trash.shape() == 'happynizar-final.gif': scoreup() print('ok') elif trash.shape() == 'sadnizar-final.gif': scoredown() print('not ok') # score+=1 trash.hideturtle() if counter == 10: scoreCounter.goto(0, 0) scoreCounter.color('blue') scoreCounter.write("YOU WON!!!", font=("Comic Sans MS", 100), align="center") quit() if counter <= -2: scoreCounter.goto(0, 0) scoreCounter.color('red') scoreCounter.write("YOU LOSE!!!", font=("Comic Sans MS", 100), align="center") answer2 = input('would you like to restart?') if answer2 == 'yes': turtle.clearscreen() counter += 2 Game() print('yes') if trash.shape() == 'happynizar-final.gif' and trash.ycor() <= -100: scoredown() #falling trash function while True: xpos = [-240, -160, 80, 0, 80, 160, 240] random_xpos = random.choice(xpos) random_xpos2 = random.choice(xpos) falling_trash()
#river location turtle.tracer(1, 0) ##river drawing turtle.penup() turtle.goto(0, 210) turtle.pendown() turtle.goto(1000, 210) turtle.goto(-1000, 210) turtle.penup() turtle.goto(-1000, -170) turtle.pendown() turtle.goto(1000, -170) turtle.tracer(1, 1) turtle.register_shape("index.gif") bin1 = turtle.clone() bin1.shape("index.gif") bin1.penup() bin1.goto(-240, 350) turtle.register_shape("RecycleBinIPyramidlid.gif") bin2 = turtle.clone() bin2.shape("RecycleBinIPyramidlid.gif") bin2.penup() bin2.goto(0, 350) turtle.register_shape("paperbin.gif") bin3 = turtle.clone() bin3.shape("paperbin.gif") bin3.penup()
## ## for the explosion add winsound.PlaySound("explosion", winsound.SND_ASYNC) ## for the laser add winsound.PlaySound("laser", winsound.SND_ASYNC) end = False # Criar tela para o jogo screen = turtle.Screen() screen.bgcolor("black") screen.title("Space Invaders") screen.bgpic("space_background.gif") # adiciona imagem de fundo # Podemos registrar imagens para serem nossos objetos turtle.register_shape("invader.gif") turtle.register_shape("player.gif") # Desenhar borda para a tela borda = turtle.Turtle() borda.speed(0) # 0 eh a maior velocidade possivel borda.color("White") borda.penup() # faz com que ao mover a pen, nao crie um traco. borda.setposition(-300, -300) borda.pendown() # permite que a pen crie um traco borda.pensize(3) for side in range(4): borda.fd(600) borda.lt(90) borda.hideturtle()
#creating score score = 0 scorestr = "score: %s" %score score_pen =t.Turtle() score_pen.color("white") score_pen.speed(0) score_pen.penup() score_pen.setposition(-290,280) score_pen.pensize(1) score_pen.write(scorestr,False,"left",font=("Cookies",15,"normal")) score_pen.hideturtle() #player(ship) ui player = t.Turtle() t.register_shape("Pyramid.gif") player.shape("Pyramid.gif") #player.shapesize(0.2,0.2) #player.color("blue") player.penup() player.speed(0) # 0 - fastest speed player.setposition(0,-250) player.setheading(90) # 90 - facing towards upward direction #player movement player_spd = 30 def move_left(): x = player.xcor() # set x coordinate of player as value of x i.e 0 x -= player_spd # set x = -15 every time we press left key if x<-280: # boundary checking for left side x = -280
caterpillar.shape("square") caterpillar.color("red") caterpillar.speed(0) caterpillar.penup() caterpillar.hideturtle() caterpillar2 = turtle.Turtle() caterpillar2.shape("square") caterpillar2.color("blue") caterpillar2.speed(0) caterpillar2.penup() caterpillar2.hideturtle() leaf = turtle.Turtle() leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14)) turtle.register_shape("leaf", leaf_shape) leaf.shape("leaf") leaf.color("green") leaf.penup() leaf.hideturtle() leaf.speed(0) game_started = False text_turtle = turtle.Turtle() text_turtle.write("Press SPACE to start", align="center", font=("Arial", 16, "bold")) text_turtle.hideturtle() score_turtle = turtle.Turtle() score_turtle.hideturtle()
def reg_speed_bar(level): s = turtle.Shape("compound") rect = ((level, 0), (level, 10), (0, 10), (0, 0)) s.addcomponent(rect, "#FF3000", "#FF3000") turtle.register_shape('speed', s)
t.pd() t.begin_fill() t.circle(50) t.end_fill() t.pu() t.goto(0, 0) earth = t.Turtle() t.pencolor(0, 200, 255) t.pu() shape = t.Shape('compound') t.pu() t.begin_poly() t.circle(25) t.end_poly() shape.addcomponent(t.get_poly(), (0, 200, 255)) t.register_shape('earth', shape) earth.shape('earth') earth.pu() moon = t.Turtle() shape = t.Shape('compound') t.begin_poly() t.circle(10) t.end_poly() shape.addcomponent(t.get_poly(), (255, 255, 255)) t.register_shape('moon', shape) moon.shape('moon') earth.goto(0, -100) moon.speed(10) moon.pencolor(255, 255, 255) while True: earth.fd(3.5)
# deals the initial two cards for i in range(2): a.addCard(p.hand) a.addCard(dealer.hand) # calculates scores playerScore = getScore(p.hand, playerScore) dealerScore = getScore(dealer.hand, dealerScore) # score text text.setposition(320,-350) text.write('Player Score: '+str(playerScore)+' \nDealer Score: ???\nPlayer Money: $' +str(playerMoney),font=('Arial',18)) # first player card turtle.register_shape(a.images[p.hand[0]]) playerHand = turtle.Turtle(a.images[p.hand[0]], visible=False) playerHand.pu() playerHand.setposition(playerCoords[0],playerCoords[1]) playerHand.showturtle() redrawCard(playerCoords,playerHand) # second player card turtle.register_shape(a.images[p.hand[1]]) playerHand = turtle.Turtle(a.images[p.hand[1]], visible=False) playerHand.pu() playerHand.setposition(playerCoords[0],playerCoords[1]) playerHand.showturtle() redrawCard(playerCoords,playerHand)
import random import time t.bgcolor('yellow') """ ========================================================================== """ """ CREATE GAME ELEMENTS """ """ ========================================================================== """ caterpillar = t.Turtle() caterpillar.shape('square') caterpillar.speed(0) caterpillar.penup() caterpillar.hideturtle() leaf = t.Turtle() leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14)) t.register_shape('leaf', leaf_shape) leaf.shape('leaf') leaf.color('green') leaf.hideturtle() text_turtle = False text_turtle = t.Turtle() text_turtle.write('Press SPACE to start', align='center', font=('Arial', 18, 'bold')) text_turtle.hideturtle() score_turtle = t.Turtle() score_turtle.hideturtle() score_turtle.speed(0) """ ========================================================================== """
__author__ = 'cherieho' # ------------------------------------------------------------------------ # coding=utf-8 # ------------------------------------------------------------------------ # # Created by Martin J. Laubach on 2011-11-15 # # ------------------------------------------------------------------------ import math import turtle import random turtle.tracer(50000, delay=0) turtle.register_shape("dot", ((-3,-3), (-3,3), (3,3), (3,-3))) turtle.register_shape("tri", ((-3, -2), (0, 3), (3, -2), (0, 0))) turtle.speed(0) turtle.title("Find the robot") UPDATE_EVERY = 0 DRAW_EVERY = 0 class Maze(object): def __init__(self, maze, half_width, half_height): self.maze = maze self.half_width = half_width self.half_height = half_height self.width = half_width * 2.0 self.height = half_height * 2.0 turtle.setworldcoordinates(-self.half_width, -self.half_height, self.half_width, self.half_height) self.blocks = []
import turtle import os import math import random #set up screen wn = turtle.Screen() wn.title("space invasion") wn.bgcolor("black") wn.bgpic("background.gif") #register the shapes turtle.register_shape("enemy.gif") turtle.register_shape("player.gif") #draw border border_pen = turtle.Turtle() border_pen.speed(0) border_pen.color("white") border_pen.penup() border_pen.setposition(-300, -300) border_pen.pendown() border_pen.pensize(3) for side in range(4): border_pen.fd(600) border_pen.lt(90) border_pen.hideturtle() #set the score to 0 score = 0