How to Make Turtles Move Together? - python-2.7

Here is my code (Python beginner, please bear any of the unprofessional code), and basically what I want is to let the two turtles move on the same circle together (as you may guess, my task is about simulating a spaceship chasing the ISS). In my code, first turtle will move around the circle, then the second turtle:
from turtle import *
rocket=Turtle()
ISS=Turtle()
counter=1
title("ISS")
screensize(750,750)
ISS.hideturtle()
rocket.hideturtle()
ISS.penup()
ISS.left(90)
ISS.fd(250)
ISS.left(90)
ISS.showturtle()
ISS.pendown()
rocket.penup()
rocket.fd(250)
rocket.left(90)
rocket.showturtle()
rocket.pendown()
while counter==1:
ISS.speed(1)
rocket.speed(2)
ISS.circle(250)
rocket.circle(250)
My teacher has told me that "threading" will work for this, but I don't quite understand that. It would be much appreciated if someone can help me out with this ;)

There is a turtle limitation that doesn't allow it to work multithreaded.
Although, you don't have to move the turtle around the whole circle, you can just move it part way. Also, I think you've misunderstood what speed does. It's just the speed that the turtle draws at.
from turtle import *
def move(thing, distance):
thing.circle(250, distance)
def main():
rocket = Turtle()
ISS = Turtle()
rocket.speed(10)
ISS.speed(10)
counter = 1
title("ISS")
screensize(750, 750)
ISS.hideturtle()
rocket.hideturtle()
ISS.penup()
ISS.left(90)
ISS.fd(250)
ISS.left(90)
ISS.showturtle()
ISS.pendown()
rocket.penup()
rocket.fd(250)
rocket.left(90)
rocket.showturtle()
rocket.pendown()
while counter == 1:
move(ISS, 3)
move(rocket, 4)
if __name__ == '__main__':
main()
I took out the repeated step of moving the thing, either the ISS or the rocket, and made that a function. I upped the speed of the drawing to 10, because I thought it just looked smoother. The ISS now moves only like 3/4 as far as the rocket on each step.

In an event-driven world like turtle, you shouldn't do this:
counter = 1
...
while counter==1:
This has the potential of locking out events. Also, there's no way to quit this program cleanly. Instead of moving different distances in a while loop, let's move a constant distance but with different delays on in an ontimer() event (should work with Python 2 or 3):
from turtle import Turtle, Screen
RADIUS = 250
ISS_DELAY = 100 # milliseconds
ROCKET_DELAY = 75 # milliseconds
CURSOR_SIZE = 20
def move_ISS():
ISS.circle(RADIUS, 1)
screen.ontimer(move_ISS, ISS_DELAY)
def move_rocket():
rocket.circle(RADIUS, 1)
# rocket slows to ISS' speed once docked
delay = ISS_DELAY if rocket.distance(ISS) <= CURSOR_SIZE else ROCKET_DELAY
screen.ontimer(move_rocket, delay)
screen = Screen()
screen.title("ISS")
screen.setup(750, 750)
ISS = Turtle("square", visible=False)
ISS.speed('fastest')
ISS.penup()
ISS.left(180)
ISS.sety(RADIUS)
ISS.showturtle()
ISS.pendown()
rocket = Turtle("triangle", visible=False)
rocket.speed('fastest')
rocket.penup()
rocket.left(90)
rocket.setx(RADIUS)
rocket.showturtle()
rocket.pendown()
move_ISS()
move_rocket()
screen.exitonclick()
You can click anywhere on the window at any time to exit the program cleanly with respect to the event handler. And I threw in some logic to make the space ship stick with the ISS once they dock rather than fly past.
This also can be done with threads, but all the graphical operations have to be channeled through the main thread to avoid Tkinter errors (a Python3-only solution):
from threading import Thread, active_count
from turtle import Turtle, Screen
from queue import Queue # use for thread-safe communications
from time import sleep
RADIUS = 250
ISS_DISTANCE = 3
ROCKET_DISTANCE = 4
CURSOR_SIZE = 20
QUEUE_SIZE = 1
def move_ISS(turtle):
while True:
actions.put((turtle.circle, RADIUS, ISS_DISTANCE))
sleep(0.1)
def move_rocket(turtle):
while True:
# rocket slows to ISS' speed once docked
distance = ISS_DISTANCE if rocket.distance(ISS) <= CURSOR_SIZE else ROCKET_DISTANCE
actions.put((turtle.circle, RADIUS, distance))
sleep(0.1)
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
actions = Queue(QUEUE_SIZE)
screen = Screen()
screen.title("ISS")
screen.setup(750, 750)
ISS = Turtle("square", visible=False)
ISS.speed('fastest')
ISS.penup()
ISS.left(180)
ISS.sety(RADIUS)
ISS.showturtle()
ISS.pendown()
ISS_thread = Thread(target=move_ISS, args=[ISS], daemon=True)
rocket = Turtle("triangle", visible=False)
rocket.speed('fastest')
rocket.penup()
rocket.left(90)
rocket.setx(RADIUS)
rocket.showturtle()
rocket.pendown()
rocket_thread = Thread(target=move_rocket, args=[rocket], daemon=True)
ISS_thread.start()
rocket_thread.start()
process_queue()
screen.exitonclick()

Related

Registering screen change in python?

So using tkinter I have made a simple program that changes the color of all shapes on the canvas to a random color every second ,what I am looking for is a way to register every screen change and write down the time between screen changes in a separate file.I also need to do it without the help of too many external libraries if possible.
My code so far:
#!/usr/bin/python -W ignore::DeprecationWarning
import Tkinter
import tkMessageBox
import time
import random
colors = ['DarkOrchid1','chocolate3','gold2','khaki2','chartreuse2','deep pink','white','grey','orange']
top = Tkinter.Tk()
global b
b=0
C = Tkinter.Canvas(top, bg="blue", height=600, width=800)
bcg1=C.create_rectangle(0,0,800,100,fill=random.choice(colors))
bcg2=C.create_rectangle(0,100,800,200,fill=random.choice(colors))
bcg3=C.create_rectangle(0,200,800,300,fill=random.choice(colors))
bcg4=C.create_rectangle(0,300,800,400,fill=random.choice(colors))
bcg5=C.create_rectangle(0,400,800,500,fill=random.choice(colors))
bcg6=C.create_rectangle(0,500,800,600,fill=random.choice(colors))
bcgs=[bcg1,bcg2,bcg3,bcg4,bcg5,bcg6]
coord = 10, 50, 240, 210
rect1=C.create_rectangle(0,0,100,100,fill="green")
rect2=C.create_rectangle(700,500,800,600,fill="green")
rect3=C.create_rectangle(0,500,100,600,fill="green")
rect4=C.create_rectangle(700,0,800,100,fill="green")
def color():
global b
global bcgs
global color
if b==0:
C.itemconfig(rect1,fill='green')
C.itemconfig(rect2,fill='green')
C.itemconfig(rect3,fill='green')
C.itemconfig(rect4,fill='green')
b=1
count=0
for i in range(len(bcgs)):
C.itemconfig(bcgs[i],fill=random.choice(colors))
elif b==1:
C.itemconfig(rect1,fill='red')
C.itemconfig(rect2,fill='red')
C.itemconfig(rect3,fill='red')
C.itemconfig(rect4,fill='red')
b=0
for i in range(len(bcgs)):
C.itemconfig(bcgs[i],fill=random.choice(colors))
top.after(2000, color)
C.pack()
color()
top.mainloop()
Unless you use a real time OS, you will never get perfect timing. You can bank on being a few milliseconds off the mark. To see how much, you can calculate the difference in time.time(). For the best accuracy, move the after call to the first thing in the function.
That plus some other improvements:
#!/usr/bin/python -W ignore::DeprecationWarning
import Tkinter
import time
import random
from itertools import cycle
colors = ['DarkOrchid1','chocolate3','gold2','khaki2','chartreuse2','deep pink','white','grey','orange']
rect_colors = cycle(['green', 'red'])
top = Tkinter.Tk()
C = Tkinter.Canvas(top, bg="blue", height=600, width=800)
bcg1=C.create_rectangle(0,0,800,100,fill=random.choice(colors))
bcg2=C.create_rectangle(0,100,800,200,fill=random.choice(colors))
bcg3=C.create_rectangle(0,200,800,300,fill=random.choice(colors))
bcg4=C.create_rectangle(0,300,800,400,fill=random.choice(colors))
bcg5=C.create_rectangle(0,400,800,500,fill=random.choice(colors))
bcg6=C.create_rectangle(0,500,800,600,fill=random.choice(colors))
bcgs=[bcg1,bcg2,bcg3,bcg4,bcg5,bcg6]
coord = 10, 50, 240, 210
rect1=C.create_rectangle(0,0,100,100,fill="green")
rect2=C.create_rectangle(700,500,800,600,fill="green")
rect3=C.create_rectangle(0,500,100,600,fill="green")
rect4=C.create_rectangle(700,0,800,100,fill="green")
rects = [rect1,rect2,rect3,rect4]
last_time = time.time()
def color():
top.after(2000, color)
global last_time
rect_color = next(rect_colors)
for item in rects:
C.itemconfig(item, fill=rect_color)
for item in bcgs:
C.itemconfig(item, fill=random.choice(colors))
print("{} seconds since the last run".format(time.time() - last_time))
last_time = time.time()
C.pack()
color()
top.mainloop()

Using kivy.garden.gauge in screenmanager

I'm working on a display project that will have two screens each showing gauges and I can't get the kivy garden gauge code to work with a screenmanager.
It works fine as a standalone app, but I can't find any instructions to help me incorporate it into a screen. The kivy calculator example works fine inside a screenmanager screen, but that's only .kv code, my GaugeApp class won't run any way I've tried when placed in a screenmanager screen.
Here's the working standalone code:
from kivy.garden.gauge import Gauge
from kivy.app import App
dirflag = 1
value = 50
from kivy.uix.slider import Slider
from kivy.properties import NumericProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
class GaugeApp(App):
increasing = NumericProperty(1)
begin = NumericProperty(50)
begin2 = NumericProperty(25)
begin3 = NumericProperty(75)
step = NumericProperty(1)
def build(self):
box = FloatLayout(orientation='horizontal', padding=5)
self.gauge = Gauge(value=50, size_gauge=156, size_text=25, pos_hint={'x': .5, 'y': .2})
self.gauge2 = Gauge(value=10, size_gauge=226, size_text=25, pos_hint={'x': .22, 'y': .3})
self.gauge3 = Gauge(value=10, size_gauge=156, size_text=25, pos_hint={'x': .02, 'y': .2})
box.add_widget(self.gauge)
box.add_widget(self.gauge2)
box.add_widget(self.gauge3)
Clock.schedule_interval(lambda *t: self.gauge_increment(), 0.1)
return box
def gauge_increment(self):
begin = self.begin
begin += self.step * self.increasing
if begin > 0 and begin < 100:
self.gauge.value = begin
else:
self.increasing *= -1
self.begin = begin
begin2 = self.begin2
begin2 += self.step * self.increasing
if begin2 > 0 and begin2 < 100:
self.gauge2.value = begin2
else:
self.increasing *= -1
self.begin2 = begin2
# Main program
GaugeApp().run()
Any help to incorporate this into a screen layout will be greatly appreciated.
it's long ago but if You still have the problem:
https://groups.google.com/d/msg/kivy-users/I0_xBF3DlAM/9M2ppsWnngQJ
I copied the "garden.gauge" folder in c:\.kivy\garden\ giving it a new name. Then making the changes to init.py as described in the link. Now it works.

Pygame, my sprite looks wierd

I'm working on this project for Literature class and I'm planning on making a game with pygame based of this book we were reading.
Here's the code:
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
romeo = pygame.image.load('rome.png.png')
romeo = pygame.transform.scale(romeo, (50, 50))
romeox = 10
romeoy = 10
while True: # the main game loop
keys = pygame.key.get_pressed()
if keys[K_RIGHT]:
romeox += 5
if keys[K_LEFT]:
romeox -= 5
if keys[K_UP]:
romeoy -=5
if keys[K_DOWN]:
romeoy += 5
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.blit(romeo, (romeox, romeoy))
pygame.display.update()
fpsClock.tick(FPS)
Unfortunately, the sprite seems to leave a trail of it's own copies while it moves.
Here
Please help!
Seems like you forgot to clear the screen every iteration
# Fill surface with black color
DISPLAYSURF.fill((0, 0, 0))
Insert it above the line that blits the sprite onto the screen.

"Invalid Handle Object" when plotting 2 figures Matlab

I'm having a difficult time understanding the paradigm of Matlab classes vs compared to c++. I wrote code the other day, and I thought it should work. It did not... until I added
<handle
after the classdef.
So I have two classes, landmarks and robot, both are called from within the simulation class. This is the main loop of obj.simulation.animate() and it works, until I try to plot two things at once.
DATA.path is a record of all the places a robot has been on the map, and it's updated every time the position is updated.
When I try to plot it, by uncommenting the two marked lines below, I get this error:
??? Error using ==> set
Invalid handle object.
Error in ==> simulation>simulation.animate at 45
set(l.lm,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
%INITIALIZE GLOBALS
global DATA XX
XX = [obj.robot.x ; obj.robot.y];
DATA.i=1;
DATA.path = XX;
%Setup Plots
fig=figure;
xlabel('meters'), ylabel('meters')
set(fig, 'name', 'Phil''s AWESOME 80''s Robot Simulator')
xymax = obj.landmarks.mapSize*3;
xymin = -(obj.landmarks.mapSize*3);
l.lm=scatter([0],[0],'b+');
%"UNCOMMENT ME"l.pth= plot(0,0,'k.','markersize',2,'erasemode','background'); % vehicle path
axis([xymin xymax xymin xymax]);
%Simulation Loop
for n = 1:720,
%Calculate and Set Heading/Location
XX = [obj.robot.x;obj.robot.y];
store_data(XX);
if n == 120,
DATA.path
end
%Update Position
headingChange = navigate(n);
obj.robot.updatePosition(headingChange);
obj.landmarks.updatePerspective(obj.robot.heading, obj.robot.x, obj.robot.y);
%Animate
%"UNCOMMENT ME" set(l.pth, 'xdata', DATA.path(1,1:DATA.i), 'ydata', DATA.path(2,1:DATA.i));
set(l.lm,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
rectangle('Position',[-2,-2,4,4]);
drawnow
This is the classdef for landmarks
classdef landmarks <handle
properties
fixedPositions; %# positions in a fixed coordinate system. [ x, y ]
mapSize; %Map Size. Value is side of square
x;
y;
heading;
headingChange;
end
properties (Dependent)
apparentPositions
end
methods
function obj = landmarks(mapSize, numberOfTrees)
obj.mapSize = mapSize;
obj.fixedPositions = obj.mapSize * rand([numberOfTrees, 2]) .* sign(rand([numberOfTrees, 2]) - 0.5);
end
function apparent = get.apparentPositions(obj)
currentPosition = [obj.x ; obj.y];
apparent = bsxfun(#minus,(obj.fixedPositions)',currentPosition)';
apparent = ([cosd(obj.heading) -sind(obj.heading) ; sind(obj.heading) cosd(obj.heading)] * (apparent)')';
end
function updatePerspective(obj,tempHeading,tempX,tempY)
obj.heading = tempHeading;
obj.x = tempX;
obj.y = tempY;
end
end
end
To me, this is how I understand things. I created a figure l.lm that has about 100 xy points. I can rotate this figure by using
set(l.lm,'XData',obj.landmarks.apparentPositions(:,1),'YData',obj.landmarks.apparentPositions(:,2));
When I do that, things work. When I try to plot a second group of XY points, stored in DATA.path, it craps out and I can't figure out why.
I need to plot the robots path, stored in DATA.path, AND the landmarks positions. Ideas on how to do that?
Jonas:
I'm not saying you're wrong, because I don't know the answer, but I have code from another application that plots this way without calling axes('NextPlot','add');
if dtsum==0 & ~isempty(z) % plots related to observations
set(h.xf, 'xdata', XX(4:2:end), 'ydata', XX(5:2:end))
plines= make_laser_lines (z,XX(1:3));
set(h.obs, 'xdata', plines(1,:), 'ydata', plines(2,:))
pfcov= make_feature_covariance_ellipses(XX,PX);
set(h.fcov, 'xdata', pfcov(1,:), 'ydata', pfcov(2,:))
end
drawnow
The above works on the other code, but not mine. I'll try implementing your suggestion and let you know.
When you call plot multiple times on the same figure, the previous plot is by default erased, and the handle to the previous plot points to nothing. Thus the error.
To fix this, you need to set the NextPlot property of the axes to add. You can do this by calling hold on (that's what you'd do if you were plotting from command line), or you can write
fig=figure;
%# create a set of axes where additional plots will be added on top of each other
%# without erasing
axes('NextPlot','add');
If you want, you can store the axes handle as well, and use plot(ah,x,y,...) to make sure that you plot into the right set of axes and not somewhere strange if you happen to click on a different figure window between the time the figure is opened and the plot command is issued.

How to programmatically move a window slowly, as if the user were doing it?

I am aware of the MoveWindow() and SetWindowPos() functions. I know how to use them correctly. However, what I am trying to accomplish is move a window slowly and smoothly as if a user is dragging it.
I have yet to get this to work correctly. What I tried was getting the current coordinates with GetWindowRect() and then using the setwindow and movewindow functions, incrementing Right by 10 pixels each call.
Any ideas?
Here is what I had beside all my definitions.
while(1)
{
GetWindowRect(notepad,&window);
Sleep(1000);
SetWindowPos(
notepad,
HWND_TOPMOST,
window.top - 10,
window.right,
400,
400,
TRUE
);
}
If you want smooth animation, you'll need to make it time-based, and allow Windows to process messages in between movements. Set a timer, and respond to WM_TIMER notifications by moving the window a distance based on the elapsed time since your animation started. For natural-looking movement, don't use a linear function for determining the distance - instead, try something like Robert Harvey's suggested function.
Pseudocode:
//
// animate as a function of time - could use something else, but time is nice.
lengthInMS = 10*1000; // ten second animation length
StartAnimation(desiredPos)
{
originalPos = GetWindowPos();
startTime = GetTickCount();
// omitted: hwnd, ID - you'll call SetTimer differently
// based on whether or not you have a window of your own
timerID = SetTimer(30, callback);
}
callback()
{
elapsed = GetTickCount()-startTime;
if ( elapsed >= lengthInMS )
{
// done - move to destination and stop animation timer.
MoveWindow(desiredPos);
KillTimer(timerID);
}
// convert elapsed time into a value between 0 and 1
pos = elapsed / lengthInMS;
// use Harvey's function to provide smooth movement between original
// and desired position
newPos.x = originalPos.x*(1-SmoothMoveELX(pos))
+ desiredPos.x*SmoothMoveELX(pos);
newPos.y = originalPos.y*(1-SmoothMoveELX(pos))
+ desiredPos.y*SmoothMoveELX(pos);
MoveWindow(newPos);
}
I found this code which should do what you want. It's in c#, but you should be able to adapt it:
increment a variable between 0 and 1 (lets call it "inc" and make it global) using small increments (.03?) and use the function below to give a smooth motion.
Math goes like this:
currentx=x1*(1-smoothmmoveELX(inc)) + x2*smoothmmoveELX(inc)
currenty=y1*(1-smoothmmoveELX(inc)) + y2*smoothmmoveELX(inc)
Code:
public double SmoothMoveELX(double x)
{
double PI = Atn(1) * 4;
return (Cos((1 - x) * PI) + 1) / 2;
}
http://www.vbforums.com/showthread.php?t=568889
A naturally-moving window would accelerate as it started moving, and decelerate as it stopped. The speed vs. time graph would look like a bell curve, or maybe the top of a triangle wave. The triangle wave would be easier to implement.
As you move the box, you need to steadily increase the number of pixels you are moving the box each time through the loop, until you reach the halfway point between point a and point b, at which you will steadily decrease the number of pixels you are moving the box by. There is no special math involved; it is just addition and subtraction.
If you are bored enough, you can do loopback VNC to drag the mouse yourself.
Now, as for why you would want to I don't know.