def main(): screen = Screen() screen.setup(width=500, height=800) user_bet = turtles_colors.index( screen.textinput(title="Make your bet", prompt="Which turtle color will win?")) start_position = 0 for turtle in turtles_colors: color = turtle turtle = Turtle() turtles.append(turtle) turtle.penup() turtle.color(color) turtle.shape("turtle") turtle.setposition( ((-int(screen.window_width() / 2) + 40), int(screen.window_height() / 2) - 50 - start_position)) start_position += 30 winner = race((int(screen.window_width() / 2) - 40)) for turtle in turtles: print(turtles_colors[turtles.index(turtle)].title() + " finished at: " + str(turtle.position()[0])) check_win(winner, user_bet) screen.exitonclick()
def set_up(axis=True, bgcolor='white'): s = Screen() s.setup(700, 700) s.bgcolor(bgcolor) if axis: axis_drawer = Turtle() axis_drawer.color('#A4AEF3') axis_drawer.speed('fastest') axis_drawer.penup() axis_drawer.goto(-s.window_width() / 2 + 30, 0) axis_drawer.pendown() axis_drawer.goto(s.window_width() / 2 - 30, 0) axis_drawer.stamp() axis_drawer.penup() axis_drawer.goto(0, -s.window_height() / 2 + 30) axis_drawer.setheading(90) axis_drawer.pendown() axis_drawer.goto(0, s.window_height() / 2 - 30) axis_drawer.stamp() axis_drawer.penup() axis_drawer.hideturtle() axis_drawer.goto(-s.window_width() / 2 + 30, 5) axis_drawer.write(f"-{int(s.window_width() / 2)}") axis_drawer.goto(s.window_width() / 2 - 50, 5) axis_drawer.write(f"+{int(s.window_width() / 2)}", ) axis_drawer.goto(5, -s.window_height() / 2 + 30) axis_drawer.write(f"-{int(s.window_height() / 2)}") axis_drawer.goto(5, s.window_height() / 2 - 30) axis_drawer.write(f"+{int(s.window_height() / 2)}") return s
class PongGame: def __init__(self): self.running = True self.screen = Screen() self.screen.setup(width=900, height=900) self.boundaries = { "ceiling": self.screen.window_height() // 2 - 60, "right_wall": self.screen.window_width() // 2 - 30, "floor": self.screen.window_height() // -2 + 60, "left_wall": self.screen.window_width() // -2 + 30 } self.player1 = Paddle("left", self.boundaries) self.player2 = Paddle("right", self.boundaries) self.ball = Ball(self.boundaries, (self.player1, self.player2)) self.scoreboard = Scoreboard() self.screen.onkeypress(self.player1.move_up, "w") self.screen.onkeypress(self.player1.move_down, "s") self.screen.onkeypress(self.player2.move_up, "Up") self.screen.onkeypress(self.player2.move_down, "Down") self.screen.onkeyrelease(self.player1.stop, "w") self.screen.onkeyrelease(self.player1.stop, "s") self.screen.onkeyrelease(self.player2.stop, "Up") self.screen.onkeyrelease(self.player2.stop, "Down") self.screen.listen() def run(self): self.ball.tick() self.player1.tick() self.player2.tick() if self.ball.xcor()-1 <= self.boundaries["left_wall"]: self.scoreboard.player2() self.ball.start_over() elif self.ball.xcor()+1 >= self.boundaries["right_wall"]: self.scoreboard.player1() self.ball.start_over() elif (self.ball.ycor() >= self.boundaries["ceiling"] or self.ball.ycor() <= self.boundaries["floor"]): self.ball.y_direction *= -1 elif self.ball.xcor() < 0: player_x, player_y = self.player1.position() if self.ball.xcor() <= player_x and self.ball.ycor() <= player_y + 69 and self.ball.ycor() >= player_y - 69: self.ball.x_direction *= -1 else: player_x, player_y = self.player2.position() if self.ball.xcor() >= player_x and self.ball.ycor() <= player_y + 69 and self.ball.ycor() >= player_y - 69: self.ball.x_direction *= -1 if self.scoreboard.player1score >= 3 or self.scoreboard.player2score >= 3: self.scoreboard.game_over() self.running = False if self.running: self.screen.ontimer(self.run, 1)
def turtle_race(): screen = Screen() distance = [] screen.setup(width=500, height=400) right_width_limit = screen.window_width() - 35 turtles = [("blue", Turtle(shape="turtle")), ("red", Turtle(shape="turtle")), ("green", Turtle(shape="turtle")), ("yellow", Turtle(shape="turtle")), ("black", Turtle(shape="turtle"))] for i in range(len(turtles)): turtle = turtles[i][1] distance.append(0) turtle.speed(4) turtle.color(turtles[i][0]) turtle.penup() turtle.goto(x=-240, y=100 - 50 * i) bet = screen.textinput( "MAKE THE BET", prompt="Select the color of winning turtle").lower() while max(distance) <= right_width_limit: for i in range(len(turtles)): step = randint(1, 10) turtles[i][1].forward(step) distance[i] += step if bet == turtles[distance.index(max(distance))][0]: print("YOU WON") else: print( f"You lost the bet,{turtles[distance.index(max(distance))][0]} won the race" ) screen.exitonclick()
class Drawer: def __init__(self, amount: int): self.screen = Screen() self.screen.bgcolor("black") self.screen.screensize(canvwidth=512, canvheight=512) self.w = self.screen.window_width() self.h = self.screen.window_height() t = Turtle() draw_background(t) atexit.register(self.save) self.pointers = [Pen(self.w, self.h) for _ in range(amount)] def save(self): print("SAVING!!!!!!!!!") # get the current screen ts = self.pointers[0].t1.getscreen() # save the drawing to a post script ts.getcanvas().postscript(file="art_save.eps") def start(self): while True: for i in self.pointers: i.draw() turtle.update()
def __init__(self, screen: Screen): super(Scoreboard, self).__init__() self.speed("fastest") self.color("white") self.playerAScore = 0 self.playerBScore = 0 self.hideturtle() self.playerALocation = (-screen.window_width() / 2, screen.window_height() / 2) self.playerBLocation = (-screen.window_width() / 2, screen.window_height() / 2) self.__display_score()
def main(): image = 'spots.jpg' rgbs = get_colors(image, 30) turtle = Turtle() screen = Screen() turtle.speed(20) turtle.penup() turtle.setpos(-screen.window_width(), -300) draw_dots(turtle, screen, rgbs) screen.exitonclick()
class Game(): def __init__(self): self.win = Screen() self.win.bgcolor('pink') # self.win.setup(width=w, height=h, startx=None, starty=None) self.border = Border() width, height = self.win.window_width(), self.win.window_height() print(width, height) self.border.draw_self((width, height)) self.turtle = Arrow() self.win.onkey(self.quit, 'q') self.win.onkey(self.arrow_left, 'Left') self.win.onkey(self.arrow_right, 'Right') self.win.onkey(self.arrow_up, 'Up') self.win.onkey(self.arrow_down, 'Down') self.win.listen() self.win.onclick(self.check_coord) self.increase_speed() self.move_arrow() def check_coord(self, x, y): print(x, y) def quit(self): self.win.bye() def move_arrow(self): self.turtle.move() self.win.ontimer(self.move_arrow, 50) def arrow_left(self): self.turtle.turn_left() def arrow_right(self): self.turtle.turn_right() def arrow_up(self): self.turtle.turn_up() def arrow_down(self): self.turtle.turn_down() def increase_speed(self): self.turtle.speedup() self.win.ontimer(self.increase_speed, 3000) def main(self): self.win.mainloop()
def __init__(self, screen: Screen): self.screen = screen self.width = screen.window_width() - 2 * OFFSET self.height = screen.window_height() - 2 * OFFSET - SCORE_SPACE self.turtle = Turtle() self.turtle.hideturtle() self.lanes = [] self.player = Player(min_x=-self.width / 2, max_x=self.width / 2, min_y=-self.height / 2 + 10, max_y=self.height / 2 - 10) self.game_over = GameOver() self.__prepare_listener() self.__prepare_field() self.__prepare_lanes() self.__prepare_game_over()
def fill_coordinate(coordinate, num_rows, num_columns, fill_color="green", space_from_edge=10): turtle.color(fill_color) turtle.fillcolor(fill_color) rows = num_rows + 1 columns = num_columns + 1 screen = Screen() width, height = screen.window_width(), screen.window_height() distanceX = width / columns distanceY = height / rows # x -> represents columns # y -> represents rows Y, X = coordinate[0], coordinate[1] # this is tested in MAC environment and the coordinate calculation is based on the mac-os display grid startX = -width / 2 + X * distanceX + space_from_edge startY = height / 2 - Y * distanceY - space_from_edge turtle.begin_fill() turtle.up() turtle.goto(startX, startY) turtle.down() # draw top turtle.forward(distanceX) # draw right turtle.right(90) turtle.forward(distanceY) # draw bottom turtle.right(90) turtle.forward(distanceX) # draw left turtle.right(90) turtle.forward(distanceY) turtle.right(90) turtle.end_fill() # reset colors back to default turtle.color("black") turtle.fillcolor("white") return []
class Window: def __init__(self, w=.9, h=.9, x=0, y=0): self.screen = Screen() self.screen.setup(w, h, x, y) self.screen.bgcolor("wheat") self.screen.colormode(255) self.screen_width = (((self.screen.window_width()) * .98) // 12) * 12 self.screen_height = (((self.screen.window_height()) * .95) // 12) * 12 self.width = self.screen_width / 2 self.height = self.screen_height / 2 def pen(self): turt = RawTurtle(self.screen) turt.ht() turt.speed(0) turt.up() turt.goto(-self.width, self.height) turt.down() return turt
def main(): # Setup board and its components game_board = Screen() game_board.setup(width=800, height=600) game_board.bgcolor("black") game_board.tracer(0) game_board.listen() scoreboard = Scoreboard((0, game_board.window_height()/2-40)) paddle_one = Paddle(((game_board.window_width()/2)-40, 25)) paddle_two = Paddle((-(game_board.window_width()/2)+40, 25)) ball = Ball() # Setup generic keys game_board.onkey(game_board.bye, "Escape") game_board.onkey(ball.increase_speed, "KP_Add") game_board.onkey(ball.decrease_speed, "KP_Subtract") # Setup player keys game_board.onkey(paddle_one.up, "Up") game_board.onkey(paddle_one.down, "Down") game_board.onkey(paddle_two.up, "q") game_board.onkey(paddle_two.down, "a") # Run the game game_is_on = True while game_is_on: game_board.update() time.sleep(ball.game_speed) ball.move() # bounce off top/bottom wall if ball.ycor() > (game_board.window_height()/2-20) or ball.ycor() < -game_board.window_height()/2+20: ball.change_direction() # hit paddle if (ball.xcor() > game_board.window_width()/2 - 70 and paddle_one.distance(ball) < 50) or \ (ball.xcor() < -game_board.window_width()/2 + 70 and paddle_two.distance(ball) < 50): ball.change_direction(True) # Randomly increase the game speed a little if randint(0, 3) == 2: ball.game_speed *= 0.9 # hit back wall and score point if ball.xcor() > game_board.window_width()/2 - 20: scoreboard.update_score(1) ball.reset_game() if ball.xcor() < -game_board.window_width()/2 + 20: scoreboard.update_score(0) ball.reset_game() game_board.exitonclick()
def get_screen(): screen = Screen() screen.setup(*conf.SCREEN_SIZE) screen.bgcolor(conf.BACKGROUND) screen.tracer(conf.TRACER) screen.delay(conf.DELAY) screen.title(conf.TITLE) screen.speed = conf.SPEED screen.winheight = screen.window_height() * .95 screen.winwidth = screen.window_width() * .95 screen.width = screen.winwidth // 2 screen.height = screen.winheight // 2 screen.blockwidth = conf.BLOCK_WIDTH screen.increment = screen.blockheight = conf.BLOCK_HEIGHT screen.base = screen.height screen.start = -screen.width + screen.blockwidth screen.blocks = int(((screen.width * 2) - (screen.blockwidth * 2)) // (screen.blockwidth + 1)) screen.gradient = gradient(conf.GRADIENT, screen.blocks) return screen
def draw_graph(num_rows, num_columns, space_from_edge=10): columns = num_columns + 1 rows = num_rows + 1 screen = Screen() width, height = screen.window_width(), screen.window_height() x = -(width / 2 - space_from_edge) distanceX = width / columns for _ in range(columns): turtle.penup() turtle.goto(x, (height / 2)) turtle.pendown() turtle.goto((x, -(height / 2))) x += distanceX y = height / 2 - space_from_edge distanceY = height / rows for _ in range(rows): turtle.penup() turtle.goto((width / 2), y) turtle.pendown() turtle.goto((-(width / 2)), y) y -= distanceY
def d(): t.setheading(0) t.forward(100) def right(): t.setheading(0) t.forward(100) def s(): t.setheading(270) t.forward(100) def down(): t.setheading(270) t.forward(100) screen =Screen() screen.window_height() screen.window_width() screen.listen() screen.onkey(up, "Up") screen.onkey(down, "Down") screen.onkey(left, "Left") screen.onkey(right, "Right") screen.onkeypress(a, "a") screen.onkeypress(s, "s") screen.onkeypress(w, "w") screen.onkeypress(d, "d") screen.mainloop()
while gameIsOn: screen.update() if playerOne.distance(ball) < 15: BALL_UP = True BALL_RIGHT = True BALL_DOWN = False BALL_LEFT = False elif playerTwo.distance(ball) < 15: BALL_DOWN = True BALL_LEFT = True BALL_UP = False BALL_RIGHT = False if ball.xcor() >= (screen.window_width() // 2) or ball.xcor() < (-1 * screen.window_width() // 2): ball.penup() ball.setpos(0, 0) randomNum = randrange(10) print(randomNum) if randomNum > 5: BALL_UP = True BALL_RIGHT = True BALL_DOWN = False BALL_LEFT = False else: BALL_UP = True BALL_LEFT = True BALL_DOWN = False BALL_RIGHT = False
Simple version of the classic Asteroids video game. Implemented with Turtle graphics from Python standard turtle module. Adapted from "Python Programming Fundamentals" by Kent Lee http://knuth.luther.edu/~leekent/IntroToComputing/ """ from time import sleep from turtle import Turtle, Screen import turtle import random import math screen = Screen() screenMinX = -screen.window_width() / 2 screenMinY = -screen.window_height() / 2 screenMaxX = screen.window_width() / 2 screenMaxY = screen.window_height() / 2 screen.setworldcoordinates(screenMinX, screenMinY, screenMaxX, screenMaxY) screen.bgcolor("black") offscreen_x = screenMinX - 100 t = Turtle() t.penup() t.ht() t.speed(0) t.goto(0, screenMaxY - 20) t.color('grey')
# Нарисуйте «бабочку» из окружностей. Используйте функцию, рисующую окружность. import time from turtle import Turtle, Screen count = 8 screen = Screen() WIDTH = screen.window_width() turtle = Turtle(visible=False) turtle.shape('turtle') turtle.penup() turtle.goto(-WIDTH/2 + 200, 0) turtle.pendown() turtle.right(-90) for _ in range(count): turtle.circle(-50, 180) turtle.circle(-10, 180) time.sleep(1)
# 180 degrees means "draw a semicircle" self.t.circle(5 * ss / 2, 180) c = bw spr.addCurr(c) else: self.t.setheading(270) # 270 degrees is straight down self.t.circle(5 * ss / 2, 180) c += ss spr.addCurr(c) # mandelbrot = Mandelbrot() # # for x in range(-150, 255): # for y in range(-150, 255): # mandelbrot.computeCardinality(turtleConvert(x, y)) # self.t.color(mandelbrot.getColor()) # self.t.goto(x, y) # self.t.up() # self.t.goto(x+1, -150) # self.t.down() # screen.update() def turtleConvert(x, y): # converts from turtle pixels to the complex plane return complex(x / 100, y / 100) screen = Screen() dummy = Display(screen, 1 / 2 - screen.window_width() / 2, screen.window_height() / 2 - 1 / 2) screen.mainloop()
from turtle import Turtle, Screen # import another_module franklin = Turtle() # my_number = another_module.another_variable my_screen = Screen() franklin.shape('turtle') franklin.color('green', 'black') franklin.isvisible() franklin.forward(99) print(my_screen.canvheight) print(my_screen.window_width()) my_screen.exitonclick() from prettytable import PrettyTable table = PrettyTable() table.add_column('Pokemon Name', ['Pikachu', 'Squirtle', 'Charmander']) table.add_column('Type', ['Electric', 'Water', 'Fire']) table.align = "l" print(table)
def generate_25_dots_with_5_per_line(): # 5 dots per line # default is pointing north, turn it to east leonardo.right(90) overall_height = screen.canvheight.real overall_width = screen.canvwidth.real number_of_lines = int(input("How many number of lines do you want to draw? ")) number_of_dots_per_line = int(input("How many number of dots per line? ")) dot_size = 10 v_spacing = (screen.window_height()) / (number_of_lines + 1) h_spacing = (screen.window_width()) / (number_of_dots_per_line + 1) for line in range(1, number_of_lines + 1): y = line * v_spacing for dot in range(1, number_of_dots_per_line + 1): x = dot * h_spacing leonardo.penup() leonardo.goto(x, y) leonardo.pendown() leonardo.dot(10, random.choice(rgbs)) print(leonardo.shapesize()) screen.exitonclick()
from turtle import Turtle, Screen, Shape from random import randint # SCREEN screen = Screen() screen.setup(600, 400) # width, height screen.tracer(0) # We'll handle displaying of frames ourselves # PLAY AREA play_top = screen.window_height() / 2 - 100 # top of screen minus 100 units play_bottom = -screen.window_height() / 2 + 100 # 100 from bottom play_left = -screen.window_width() / 2 + 50 # 50 from left play_right = screen.window_width() / 2 - 50 # 50 from right area = Turtle() area.hideturtle() area.penup() area.goto(play_right, play_top) area.pendown() area.goto(play_left, play_top) area.goto(play_left, play_bottom) area.goto(play_right, play_bottom) area.goto(play_right, play_top) # PADDLES L = Turtle() R = Turtle() L.penup() R.penup() # Paddles shape paddle_w_half = 10 / 2 # 10 units wide paddle_h_half = 40 / 2 # 40 units high paddle_shape = Shape("compound") paddle_points = ((-paddle_h_half, -paddle_w_half), (-paddle_h_half, paddle_w_half),
from turtle import Turtle,Screen s = Screen() s.setup(1000,600) altura = s.window_height() ancho = s.window_width() print("altura:",altura,"\nancho",ancho)
from breakout_game import BreakoutGame screen = Screen() screen.bgcolor("black") screen.setup(width=500, height=900) screen.title("Breakout") screen.tracer(0) # SET INITIAL LEVEL INITIAL_LEVEL = 2 # Screen Dimension Variables SCREEN_TOP = screen.window_height() / 2 SCREEN_BOTTOM = screen.window_height() / 2 * -1 SCREEN_LEFT = screen.window_width() / 2 * -1 SCREEN_RIGHT = screen.window_width() / 2 SCREEN_DIMS = [(SCREEN_LEFT, SCREEN_RIGHT), (SCREEN_BOTTOM, SCREEN_TOP)] # Initialise Game breakout_game = BreakoutGame(initial_level=INITIAL_LEVEL, screen_dims=SCREEN_DIMS) # The game is watching your moves screen.listen() screen.onkey(fun=breakout_game.paddle.move_left, key="Left") screen.onkey(fun=breakout_game.paddle.move_right, key="Right") game_is_on = True while game_is_on: # Update the screen
def segment_B(turtle): # right upper turtle.setposition(turtle.xcor() + 10 * SCALE, turtle.ycor() + 10 * SCALE) def segment_C(turtle): # right lower turtle.setposition(turtle.xcor() + 10 * SCALE, turtle.ycor() - 10 * SCALE) def segment_D(turtle): # bottom turtle.setheading(90) turtle.sety(turtle.ycor() - 20 * SCALE) def segment_E(turtle): # left lower turtle.setposition(turtle.xcor() - 10 * SCALE, turtle.ycor() - 10 * SCALE) def segment_F(turtle): # left upper turtle.setposition(turtle.xcor() - 10 * SCALE, turtle.ycor() + 10 * SCALE) def segment_G(turtle): # center turtle.setheading(90) segments = [segment_G, segment_F, segment_E, segment_D, segment_C, segment_B, segment_A] digits = Turtle('segment', False) digits.speed('fastest') digits.shape('segment') digits.penup() digits.setx(SPACING - screen.window_width() / 2) display_number(digits, "0123456789ABCDEF")
from random import randint import platform if (platform.system() == "Windows"): import winsound limit = int(input("Lütfen bir hedef skor giriniz")) screen = Screen() screen.setup(750, 500) if (platform.system() == "Windows"): winsound.PlaySound('hello.wav', winsound.SND_FILENAME) screen.tracer(0) play_top = screen.window_height() / 2 - 100 play_bottom = -screen.window_height() / 2 + 100 play_left = -screen.window_width() / 2 + 50 play_right = screen.window_width() / 2 - 50 area = Turtle() area.hideturtle() area.penup() area.goto(play_right, play_top) area.pendown() area.goto(play_left, play_top) area.goto(play_left, play_bottom) area.goto(play_right, play_bottom) area.goto(play_right, play_top) L = Turtle() R = Turtle() L.penup() R.penup()
from turtle import Turtle, Screen # Screen screen = Screen() screen.setup(640, 450) # width, height screen.tracer(0) # We'll handle displaying of frames ourselves screen.bgcolor("Indigo") # Area play_top = screen.window_height() / 2 - 45 # top of screen minus 45 units play_bottom = -screen.window_height() / 2 + 45 # 45 from bottom play_left = -screen.window_width() / 2 + 64 # 64 from left play_right = screen.window_width() / 2 - 64 # 64 from right border_width = 26 border = border_width / 2 area = Turtle() area.color("Thistle", "Azure") area.begin_fill() area.pensize(border_width) area.hideturtle() area.penup() area.goto(play_right, play_top) area.pendown() area.goto(play_left, play_top) area.goto(play_left, play_bottom) area.goto(play_right, play_bottom) area.goto(play_right, play_top) area.end_fill()
x1 = min(p[0] for p in poly) y1 = min(p[1] for p in poly) x2 = max(p[0] for p in poly) y2 = max(p[1] for p in poly) return x1 < x < x2 and y1 < y < y2 def notsafe(turtle): position = turtle.position() return inside(position, circle) and not inside(position, redcross) wn = Screen() wn.bgcolor('lightgreen') WIDTH = wn.window_width() layout = Turtle(visible=False) layout.speed('fastest') layout.penup() # turtle.shape("square") x=15 def north(x): turtle.seth(90) turtle.forward(x) def hitnorth(): north(x) def south(x):
from random import randint, choice from turtle import Turtle, Screen import time screen = Screen() screen.tracer(0) width, height = screen.window_width() - 30, screen.window_height( ) - 30 # -30 to account for window borders, etc. maker1 = Turtle(visible=False) maker2 = maker1.clone() maker3 = maker1.clone() maker4 = maker1.clone() maker1.goto(50, 50) # top right maker2.goto(50, -50) # bottom right maker3.goto(-50, -50) # bottom left maker4.goto(-50, 50) # top left n = randint(1, 360) m = randint(0, 100) r = randint(0, 50) d = choice([True, False]) def out_of_bounds(turtle): return not (-width // 2 < turtle.xcor() < width // 2 and -height // 2 < turtle.ycor() < height // 2)
# [] {} from turtle import Turtle, Screen from random import randint screen = Screen() screen.setup(width=800, height=400) user_bet = screen.textinput( title="Make your bet", prompt="Which turtle will win the race? Enter a color: ") colors = ["red", "orange", "yellow", "green", "blue", "purple"] x = -(screen.window_width() / 2) + 20 y = -100 turtles = [] for i in range(len(colors)): turtles.append(Turtle(shape="turtle")) turtles[i].penup() turtles[i].color(colors[i]) turtles[i].goto(x=x, y=y) y += 40 stop = False while not stop: for turtle in turtles: speed = randint(0, 20) turtle.forward(speed) if turtle.xcor() >= (screen.window_width() / 2) - 25: stop = True if turtle.color()[0] == user_bet: print( f"You've won! The {turtle.color()[0]} turtle is the winner!"