Python while loop with no expressions - python

Is is possible to have a while loop in Python with no expressions?
I know in other languages you can do something like:
while(flag) {};
I'm trying to do something similar in Python but cannot find an answer.
Here is what I have so far:
import turtle
from random import randrange
def is_in_screen(t, w): #CHECKS TO SEE IF STILL IN SCREEN
flag = True
r = w.window_width() / 2
l = r * -1
u = w.window_height() / 2
d = u * -1
x_cor = t.xcor()
y_cor = t.ycor()
if (x_cor < l or x_cor > r or y_cor < d or y_cor > u):
flag = False
return flag
def move_to(t, w): #MOVE IN RANDOM DIRECTION AND RANDOM DISTANCE
t.forward(randrange(1, 100))
if (randrange(1, 2) == 1):
t.left(randrange(1, 180))
else:
t.right(randrange(1, 180))
return is_in_screen(t, w)
def random_movement(t1, t2, w):
while (move_to(t1, w) and move_to(t2, w)): #<<<<<<<<LOOP IN QUESTION
i = 0
def main():
t1 = turtle.Turtle()
t2 = turtle.Turtle()
w = turtle.Screen()
t1.color("green")
t2.color("purple")
random_movement(t1, t2, w)
w.exitonclick()
main()
The reason I'm trying to do no expressions is because I want the second turtle to not move if the first turtle goes out of bounds. Also, I do not want return statements in the function.

You're looking for the pass keyword.
while (flag):
pass

Below is a rework of your code with the while expr: pass that everyone's suggesting along with some other style and idiom changes to tighten up the code:
from turtle import Screen, Turtle
from random import randrange
def is_in_screen(turtle, screen):
r = screen.window_width() / 2
u = screen.window_height() / 2
x, y = turtle.position()
return -r < x < r and -u < y < u
def move_to(turtle, screen):
turtle.forward(randrange(1, 100))
turtle.left(randrange(-180, 180)) # negative left turn is a right turn
return is_in_screen(turtle, screen)
def random_movement(turtle_1, turtle_2, screen):
while move_to(turtle_1, screen) and move_to(turtle_2, screen): pass
screen = Screen()
t1 = Turtle()
t1.color("green")
t2 = Turtle()
t2.color("purple")
random_movement(t1, t2, screen)
screen.exitonclick()

Related

Animate algorithm

I want to visualize an algorithm (Graham scan) in python with tkinter.
I want to animate the algorithm and I am stuck.
I basically want to draw and delete lines but I don't understand canvas.after() well enough to make it work.
draw_line() returns the line object but when I call it in canvas.after(..., draw_line, ...) I don't see a way to get the return value or how to call another canvas.after() to change the color/delete that line if the function draw_line() hasn't been called yet because of the delay.
Thanks in advance.
from tkinter import *
import math
import random
class Point:
def __init__(self, _x, _y, _a=0):
self.x = _x
self.y = _y
self.angle = _a
def get_co(self):
return self.x, self.y
def draw_hull(hull):
for i in range(len(hull) - 1):
canvas.create_line(hull[i][0], hull[i][1], hull[i + 1][0], hull[i + 1][1], fill="red", width=2)
def draw_line(p1, p2, color="yellow"):
return canvas.create_line(p1.x, p1.y, p2.x, p2.y, fill=color, width=2)
def convex_hull(list_points):
# find bottom point
bottom_point = Point(math.inf, math.inf)
for point in list_points:
if point[1] < bottom_point.y:
bottom_point = Point(point[0], point[1])
# calculate angles between the bottom point and the other points
points = []
for point in list_points:
if point != bottom_point.get_co():
new_point = Point(point[0], point[1])
angle = calculate_angle(bottom_point, new_point)
new_point.angle = angle
points.append(new_point)
# sort the points by angle
swaps = None
while swaps != 0:
swaps = 0
for i in range(len(points) - 1):
point1 = points[i]
point2 = points[i + 1]
if point1.angle > point2.angle:
points[i], points[i + 1] = points[i + 1], points[i]
swaps += 1
# go through the points and add them to the convex hull
# if the angle between 3 points ever exeeds 180 degrees, discard the middle point
hull = [bottom_point, points[0]]
i = 1
while i < len(points):
####### DRAW LINE #######
canvas.after(i*500, draw_line, hull[-2], hull[-1])
##############
# check angle
angle = calculate_angle(hull[-2], hull[-1], points[i])
if angle == -1:
########## DELETE LINE ##########
# change color of line to red and delete it a bit later
# canvas.itemconfig(line, fill="red")
# canvas.after(i*500+250, canvas.delete, line)
####################
# pop the point of the stack
hull.pop()
else:
########## CHANGE COLOR OF LINE ##########
# change color of line to green
# canvas.itemconfig(line, fill="green")
####################
# move to the next point
hull.append(points[i])
i += 1
# add bottom point again for loop
hull.append(bottom_point)
# give easy return list (with coordinate-tupels not class objects)
output = []
for point in hull:
output.append(point.get_co())
return output
def calculate_angle(point1, point2, point3=None):
if point3 is None:
if point2.x - point1.x == 0:
return 90
elif point2.x - point1.x > 0:
return math.degrees(math.atan((point2.y - point1.y)/(point2.x - point1.x)))
else:
return 180 - math.degrees(math.atan((point2.y - point1.y)/(point1.x - point2.x)))
else:
v1 = Point(point1.x - point2.x, point1.y - point2.y)
v2 = Point(point3.x - point2.x, point3.y - point2.y)
det = (v1.x * v2.y) - (v2.x * v1.y)
if det < 0:
return 1
else:
return -1
window = Tk()
window.geometry("1000x600")
canvas = Canvas(window, width=1000, height=600)
canvas.pack()
POINTSIZE = 2
points = []
for i in range(100):
x = random.randint(50, 950)
y = random.randint(50, 550)
points.append((x, y))
canvas.create_oval(x - POINTSIZE, y - POINTSIZE, x + POINTSIZE, y + POINTSIZE, fill="black")
hull = convex_hull(points)
# draw_hull(hull)
window.mainloop()
If you have questions about the code, let me know. Because I dont know where to start to explain, since I made major changes to your code.
Anyway, I would be glad if you share your code again, once you are done with, on CodeReview and please do let me know. Because it was fun to work with and your code seems incomplete to me.
Happy Coding:
import tkinter as tk
import random
import math
class Point:
def __init__(self, _x, _y, _a=0):
self.x = _x
self.y = _y
self.angle = _a
return None
def get_co(self):
return self.x, self.y
class Window(tk.Tk):
def __init__(self,_w,_h):
super().__init__()
self.POINTSIZE = 2
self.points = []
self.swaps = None
self.count = 1
self.delay = 200
self.title('Graham scan simulation')
self.toolbar = tk.Frame(self,background='grey')
self.refresh_button = tk.Button(self.toolbar,text='Refresh',
command=self.refresh)
self.start_button = tk.Button(self.toolbar,text='Start',
command = self.convex_hull)
self.canvas = tk.Canvas(self,width=_w,height=_h)
self.toolbar.pack(side=tk.TOP,fill=tk.BOTH,expand=True)
self.refresh_button.pack(side=tk.LEFT)
self.start_button.pack(side=tk.LEFT)
self.canvas.pack(side=tk.BOTTOM,fill=tk.BOTH,expand=True)
def generate_points(self):
for point_instance in self.points:
yield point_instance
def find_bottom_point(self):
bottom_point = Point(math.inf,math.inf)
for point in self.generate_points():
if point.y < bottom_point.y:
bottom_point = point
return bottom_point
def calculate_angle(self,point1, point2):
if point2.x - point1.x == 0:
return 90
elif point2.x - point1.x > 0:
return math.degrees(math.atan((point2.y - point1.y)/(point2.x - point1.x)))
else:
return 180 - math.degrees(math.atan((point2.y - point1.y)/(point1.x - point2.x)))
def calculate_angels_by_bottom_point(self,bottom_point):
for point in self.generate_points():
if point != bottom_point:
angle = self.calculate_angle(bottom_point,point)
point.angle = angle
def sort_points(self,event_variable):
if self.swaps != 0:
self.swaps = 0
for i in range(len(self.points)-1):
point1 = self.points[i]
point2 = self.points[i + 1]
if point1.angle > point2.angle:
self.points[i], self.points[i + 1] = self.points[i + 1], self.points[i]
self.swaps += 1
if self.swaps == 0:
event_variable.set(1)
self.after(20,self.sort_points,event_variable)
def check_angle(self,point1,point2,point3):
v1 = Point(point1.x - point2.x, point1.y - point2.y)
v2 = Point(point3.x - point2.x, point3.y - point2.y)
det = (v1.x * v2.y) - (v2.x * v1.y)
if det < 0:
return 1
else:
return -1
def draw_line(self,p1,p2,color='yellow'):
return self.canvas.create_line(p1.x,p1.y, p2.x,p2.y, fill='yellow',tags='line')
def clear_red_lines(self,p1,p2):
shapes = self.canvas.find_withtag('line')
for shape in shapes:
if self.canvas.itemcget(shape,'fill') == 'red':
coords = self.canvas.coords(shape)
overlapped = self.canvas.find_overlapping(*coords)
for i in overlapped:
coords2 = self.canvas.coords(i)
if coords == coords2:
self.canvas.delete(i)
self.canvas.delete(shape)
def animated_draw(self,hull):
if self.count != len(self.points):
line = self.draw_line(hull[-2],hull[-1])
check_mark = self.check_angle(hull[-2],hull[-1],self.points[self.count])
if check_mark == -1:
self.canvas.itemconfig(line,fill='red')
self.after(self.delay-100,lambda:self.clear_red_lines(hull[-2],hull[-1]))
hull.pop()
else:
self.canvas.itemconfig(line,fill='green')
hull.append(self.points[self.count])
self.count += 1
self.after(self.delay,self.animated_draw,hull)
def convex_hull(self):
bottom_point = self.find_bottom_point()
self.calculate_angels_by_bottom_point(bottom_point)
event_variable = tk.IntVar(self,value=0)
self.sort_points(event_variable)
self.wait_variable(event_variable)
self.points.pop(0)
self.animated_draw(hull = [bottom_point, self.points[0]])
def generate_listpoints(self,_amount):
'''using a generator for memory purpose'''
for i in range(_amount):
x = random.randint(50, 950)
y = random.randint(50, 550)
yield x,y
def refresh(self):
self.swaps = None
self.count = 1
self.points= []
self.populate_canvas()
def populate_canvas(self):
self.canvas.delete('all')
for x,y in self.generate_listpoints(100):
self.points.append(Point(x, y))#instead of creating throwing instances away.
self.canvas.create_oval(x - self.POINTSIZE,
y - self.POINTSIZE,
x + self.POINTSIZE,
y + self.POINTSIZE,
fill="black")
root = Window(1000,600)
root.mainloop()

set mouse cursor position as objects heading

I want to set object u1's heading by the mouse cursors position inside the window.
import turtle
import pygame
import time
win = turtle.Screen()#window
win.title("eagle.py")
win.setup(1920,1080)
win.bgcolor("black")
win.bgpic("triangle")
c1 = turtle.Turtle()#cloud1
c1.speed(0)
c1.penup()
c1.setposition(-1075, 256)
c1.color("white")
c1.shape("triangle")
c_speed = 1 #cloudspeed
u1 = turtle.Turtle()#user1
mouse_pos = pygame.mouse.get_pos()
u1.shape("triangle")
u1.color("red")
u1.speed(0)
u1.setposition(0,0)
u1.setheading(mouse_pos)
u1.penup()
u_speed = 10 #playerspeed
def u1_r():
x = u1.xcor()
x += u_speed
u1.setx(x)
def u1_l():
x = u1.xcor()
x -= u_speed
u1.setx(x)
def u1_up():
y = u1.ycor()
y += u_speed
u1.sety(y)
def u1_down():
y = u1.ycor()
y -= u_speed
u1.sety(y)
while True:
win.update()
time.sleep(1/160)
c1.setx(c1.xcor() + c_speed)
if c1.xcor() > 1075:
c1.goto(-1075, 256)
win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")
The program keeps shutting down immediately after running. What have I done wrong?
The fact that you have the following:
win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")
inside a loop indicates that you don't have a basic understanding of the environment in which you're working. Let's start over, tossing pygame and time and just work within the turtle framework:
from turtle import Screen, Turtle
def player_r():
player.setheading(0)
player.setx(player.xcor() + player_speed)
def player_l():
player.setheading(180)
player.setx(player.xcor() - player_speed)
def player_up():
player.setheading(90)
player.sety(player.ycor() + player_speed)
def player_down():
player.setheading(270)
player.sety(player.ycor() - player_speed)
def move():
cloud.setx(cloud.xcor() + cloud_speed)
if cloud.xcor() > 940:
cloud.goto(-940, 256)
screen.update()
screen.ontimer(move)
screen = Screen()
screen.setup(1920, 1080)
screen.bgcolor('black')
screen.tracer(False)
cloud = Turtle()
cloud.shape('circle')
cloud.shapesize(1, 3)
cloud.color('white')
cloud.penup()
cloud.setposition(-940, 256)
cloud_speed = 1
player = Turtle()
player.shape('turtle')
player.color('red')
player.penup()
player_speed = 10
screen.onkeypress(player_r, 'd')
screen.onkeypress(player_l, 'a')
screen.onkeypress(player_up, 'w')
screen.onkeypress(player_down, 's')
screen.listen()
move()
screen.mainloop()
In your code, string is in heading. Instead, put variable.
u1 = turtle.Turtle()
mouse_pos = pygame.mouse.get_pos()
u1.heading(mouse_pos) # variable
Edit: This worked for me, triangle moving..
It seems pygame.init() is need at first.
u1.setheading has a number inside, not tuple. This is for angle. (man turtle)
import turtle
import pygame
import time
pygame.init()
win = turtle.Screen()#window
win.title("eagle.py")
win.setup(1920,1080)
win.bgcolor("black")
win.bgpic() # changed
c1 = turtle.Turtle()#cloud1
c1.speed(0)
c1.penup()
c1.setposition(-1075, 256)
c1.color("white")
c1.shape("triangle")
c_speed = 1 #cloudspeed
u1 = turtle.Turtle()#user1
mouse_pos = pygame.mouse.get_pos()
u1.shape("triangle")
u1.color("red")
u1.speed(0)
u1.setposition(0,0)
u1.setheading(0) # to be changed, setheading(int)
u1.penup()
u_speed = 10 #playerspeed
def u1_r():
x = u1.xcor()
x += u_speed
u1.setx(x)
def u1_l():
x = u1.xcor()
x -= u_speed
u1.setx(x)
def u1_up():
y = u1.ycor()
y += u_speed
u1.sety(y)
def u1_down():
y = u1.ycor()
y -= u_speed
u1.sety(y)
while True:
win.update()
time.sleep(1/160)
c1.setx(c1.xcor() + c_speed)
if c1.xcor() > 1075:
c1.goto(-1075, 256)
win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")
turtle.heading() expects a numeric argument specifying the heading in degrees.
I don't think you can use the pygame and turtle modules at the same time. Both have their own way of drawing graphics and getting the mouse's current position, so theoretically you can use either.
Regardless, of which one you're using, you'll need to compute the heading's angle from the mouse's x, y position.
Here's how to measure the angle to the origin of an x, y position (assuming its x value isn't zero):
import math
x, y = ... # Mouse position.
print(math.degrees(math.atan(y/x)))

Stop a moving Python turtle when it's close to another turtle

How do I stop a randomly moving turtle using a while loop when it comes with 50 units of another turtle?
I have one turtle that randomly selects a location and creates a large dot or hole, and another turtle that moves around randomly making 90 degree turns and moving forward 50 units every time. The randomly moving turtle stops when it goes off of the end of the screen, but how do I also make the turtle stop when it gets to the hole created by the other turtle?
import random
import turtle
def turtlesClose(t1, t2):
if t1.distance(t2)<50:
return True
else:
return False
def isInScreen(win,turt):
leftBound = -win.window_width() / 2
rightBound = win.window_width() / 2
topBound = win.window_height() / 2
bottomBound = -win.window_height() / 2
turtleX = turt.xcor()
turtleY = turt.ycor()
stillIn = True
if turtleX > rightBound or turtleX < leftBound:
stillIn = False
if turtleY > topBound or turtleY < bottomBound:
stillIn = False
return stillIn
def main():
wn = turtle.Screen()
# Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
july.shape('turtle')
july.up()
july.goto(random.randrange(-250, 250, 1), random.randrange(-250, 250, 1))
july.down()
july.dot(100)
june.shape('turtle')
while isInScreen(wn,june):
coin = random.randrange(0, 2)
dist = turtlesClose(july, june)
if coin == 0:
june.left(90)
else:
june.right(90)
june.forward(50)
if dist == 'True':
break
main()
The problem with your code is this statement:
if dist == 'True':
You don't want quotes around True. Although this will work:
if dist == True:
The correct way to express this is:
if dist is True:
or better yet:
if dist:
Otherwise your code seems to work. Below's a rewrite taking advantage of some turtle idioms and other code cleanup:
from random import randrange, choice
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def turtlesClose(t1, t2):
return t1.distance(t2) < 50
def isInScreen(window, turtle):
leftBound = -window.window_width() / 2
rightBound = window.window_width() / 2
topBound = window.window_height() / 2
bottomBound = -window.window_height() / 2
turtleX, turtleY = turtle.position()
return leftBound < turtleX < rightBound and bottomBound < turtleY < topBound
def main():
screen = Screen()
july = Turtle('circle')
july.shapesize(100 / CURSOR_SIZE)
july.up()
july.goto(randrange(-250, 250), randrange(-250, 250))
july.down()
june = Turtle('turtle')
while isInScreen(screen, june):
if turtlesClose(july, june):
break
turn = choice([june.left, june.right])
turn(90)
june.forward(50)
main()

python canvas.find_overlapping appears to have inverted y-axis

I am trying to find the color of the canvas under a Python turtle. I use canvas.find_overlapping but it is only successful when I negate the ycor, implying that the y-axis is inverted in the canvas object, compared to what is shown. Is there a problem with my code or is the y-axis inverted?
import turtle
wn = turtle.Screen()
maze_drawer = turtle.Turtle()
maze_drawer.color("purple")
maze_drawer.speed("fastest")
path_width = 15
def get_pixel_color(x, y):
c = turtle.Screen().getcanvas()
# -y should not work??
items = c.find_overlapping(x, -y, x, -y)
if len(items) > 0:
return c.itemcget(items[0], "fill") # get 0 object (canvas)
# draw simplified maze
wall_len = 0
for i in range(10):
maze_drawer.left(90)
wall_len += path_width
maze_drawer.forward(wall_len)
# navigate maze from center
maze_runner = turtle.Turtle()
maze_runner.color("green")
maze_runner.penup()
maze_runner.goto(-path_width, -path_width)
# test in y dir: maze_runner.setheading(90)
clear = True
while(clear):
maze_runner.forward(1)
color_at_turtle = get_pixel_color(maze_runner.xcor(), maze_runner.ycor())
if (color_at_turtle == "purple"):
clear = False
wn.exitonclick()
Neat use of tkinter pixel detection within turtle! If the inverted Y coordinate is bothersome, you can flip it from turtle's perspective:
from turtle import Screen, Turtle
screen = Screen()
width, height = screen.window_width() / 2, screen.window_height() / 2
screen.setworldcoordinates(-width, height, width, -height) # flip Y coordinate
Then your code doesn't have to think about negating Y as long as you know you're drawing upside down:
from turtle import Screen, Turtle
PATH_WIDTH = 15
def get_pixel_color(x, y):
canvas = screen.getcanvas()
items = canvas.find_overlapping(x, y, x, y)
if items:
return canvas.itemcget(items[0], "fill") # get 0 object (canvas)
return None
screen = Screen()
width, height = screen.window_width() / 2, screen.window_height() / 2
screen.setworldcoordinates(-width, height, width, -height)
maze_drawer = Turtle(visible=False)
maze_drawer.color("purple")
maze_drawer.speed("fastest")
# draw simplified maze
wall_len = 0
for _ in range(20):
maze_drawer.left(90)
wall_len += PATH_WIDTH
maze_drawer.forward(wall_len)
# navigate maze from center
maze_runner = Turtle()
maze_runner.color("dark green", "green")
maze_runner.penup()
maze_runner.goto(-PATH_WIDTH, -PATH_WIDTH)
def run_maze():
maze_runner.forward(1)
x, y = maze_runner.position()
color_at_turtle = get_pixel_color(x, y)
if color_at_turtle == "purple":
maze_runner.backward(PATH_WIDTH - 1)
maze_runner.left(90)
x, y = maze_runner.position()
if -width < x < width and -height < y < height:
screen.ontimer(run_maze, 10)
run_maze()
screen.exitonclick()

How to add a countdown timer to my turtle program?

I'm trying to make a game using turtle where the player has to click as many randomly distributed circles in a set amount of time, but I'm stumped on how to create a timer inside the game. Whenever I try to set the timer, it counts down before the game starts or counts down after 1 circle spawns. I want to have it count down as the game starts, and have the game end once the timer ends.
I'm also trying to create a score counter, but am I using circle.onclick wrong?
This is what I've been trying to do:
from turtle import Turtle, Screen
from random import random, randint
import time
CURSOR_SIZE = 20
def score():
num=0
print("Score: ",num)
def my_circle(color):
radius = randint(10, 50)
circle = Turtle('circle', visible=False)
circle.shapesize(radius / CURSOR_SIZE)
circle.color(color)
circle.penup()
while True:
nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
ny = randint(2 * radius - height // 2, height // 2 - radius * 2)
circle.goto(nx, ny)
for other_radius, other_circle in circles:
if circle.distance(other_circle) < 2 * max(radius, other_radius):
break
else:
break
circle.showturtle()
circle.onclick(lambda x, y, t=circle: t.hideturtle())
circle.onclick(num=num+1)
return radius, circle
screen = Screen()
screen.bgcolor("lightgreen")
screen.title("Speed Clicker")
width, height = screen.window_width(), screen.window_height()
circles = []
for _ in range(0, 20):
for i in range(40):
print(str(40-i)+" seconds remain")
time.sleep(1)
rgb = (random(), random(), random())
circles.append(my_circle(rgb))
screen.mainloop()
The last thing I'm curious about is if it's possible to have the timer and score printed ON the game, rather than on the Python Shell.
I've made a few alterations and this seems to work. I have put the score and the time left in the title bar of the game. Thanks
import turtle
from random import random, randint
import time
CURSOR_SIZE = 20
num=0
def increase_score():
global num
num += 1
def my_circle(color):
radius = randint(10, 50)
circle = turtle.Turtle('circle', visible=False)
circle.shapesize(radius / CURSOR_SIZE)
circle.color(color)
circle.penup()
while True:
nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
ny = randint(2 * radius - height // 2, height // 2 - radius * 2)
circle.goto(nx, ny)
for other_radius, other_circle in circles:
if circle.distance(other_circle) < 2 * max(radius, other_radius):
break
else:
break
circle.showturtle()
circle.onclick(lambda x,y,t=circle: (circle.hideturtle(), increase_score()))
return radius, circle
screen = turtle.Screen()
screen.bgcolor("lightgreen")
screen.title("Speed Clicker")
width, height = screen.window_width(), screen.window_height()
circles = []
gameLength = 5
# Higher number means faster blocks
# 1-10
difficulty = 10
startTime = time.time()
while True:
time.sleep(1/difficulty)
rgb = (random(), random(), random())
timeTaken = time.time() - startTime
circles.append(my_circle(rgb))
screen.title('SCORE: {}, TIME LEFT: {}'.format(num,int(round(gameLength - timeTaken,0))))
if time.time() - startTime > gameLength:
break
screen.title('FINISHED! FINAL SCORE: {}'.format(num))
screen.mainloop()

Categories