How to draw a stickman with turtle graphics in Python idle? - windows

I am not trying to make a game, just a drawing. However I am not sure how to proceed.
I'm using this article: https://lti.flvsgl.com/flvs-cat-content/jviqsic79osifb0qmtbb1dhea2/flvs-cat-session/educator_foundofprog_v18_gs/global/interactives/python_idle/python_idle.htm
My code so far is:
# Writing Functions
import turtle
def main():
jim = turtle.turtle()
jim.speed(10)
#draw a stickman
# move to position
jim.setpos(0, -20)
jim.left(90)
# draw body
jim.color("black")
jim.forward(60)
# draw head
jim.color("black")
jim.forward(90)
jim.circle(45)
# move to position
jim.penup()
jim.setpos(20, -20) #Sets the position of the turtle
jim.left(90)
jim.pendown()
main()
but I don't know how to make it work.

Here you go:
import turtle
def main():
jim = turtle.Turtle() #Remember the capital T
jim.speed(0) #A speed of 0 makes him go as fast as possible
arm_length = 100 #Change these if you want
leg_length = 120
def reset(): #reset sends him back to the center
jim.pu() #pu is short for penup. Either work, you can use them interchangeably
jim.setpos(0,0)
jim.pd() #pd is short for pendown
#draw a stickman
reset()
#head
jim.seth(90) #seth is short for set_heading, and it changes the direction jim is facing.
jim.fd(30)
jim.rt(90)
jim.circle(50)
reset()
#arm 1
jim.seth(160)
jim.fd(arm_length/2)
jim.rt(20)
jim.fd(arm_length/2)
reset()
#arm 2
jim.seth(20)
jim.fd(arm_length/2)
jim.lt(20)
jim.fd(arm_length/2)
reset()
#leg 1
jim.seth(270)
jim.fd(50)
jim.seth(230)
jim.fd(leg_length/2)
jim.lt(40)
jim.fd(leg_length/2)
reset()
#leg 2
jim.seth(270)
jim.fd(50)
jim.seth(310)
jim.fd(leg_length/2)
jim.rt(40)
jim.fd(leg_length/2)
main()

Related

How do we tell if two sprites are colliding in Pygame Zero?

I recently started coding in Pygame Zero before starting Pygame. I was making a game and the I need to see if two of the sprites are colliding. How should I approach this?
The two sprites:
[1]: https://i.stack.imgur.com/bRcqC.png
[2]: https://i.stack.imgur.com/FwlES.png
import pgzrun
import rando[enter image description here][1]m
WIDTH=400
HEIGHT=400
score=0
game_over=False
coin=Actor("coin")
fox=Actor("fox")
x=random.randint(0,WIDTH)
y=0
xf=170
fox.pos=(xf,150)
coin.pos=(x, 0 )
def draw():
screen.fill((144,238,144))
fox.draw()
coin.draw()
screen.draw.text("Score: "+str(score),(20,20),fontsize=30)
def relocate():
global x
global y
y+=1
coin.pos=(x, y )
def move():
if keyboard.left :
fox.x-=1
if keyboard.right:
fox.x+=1
def collide():
global score
global x
global y
if fox.rect.center==coin.rect.center:
score+=1
coin.pos=(x, 0 )
def update():
global x
global y
relocate()
if y>WIDTH-10:
x=random.randint(0,WIDTH)
y=0
coin.pos=(x, y )
move()
collide()
pgzrun.go()
To tell if two sprites collide, use the colliderect() method.
Change the line:
if fox.rect.center==coin.rect.center:
to
if fox.colliderect(coin):
Actors have all the same attributes and methods as Rect, including methods like .colliderect() which can be used to test whether two actors have collided. – Pygame Zero Builtins

psychopy polygon on top of image

using psychopy ver 1.81.03 on a mac I want to draw a polygon (e.g. a triangle) on top of an image.
So far, my image stays always on top and thus hides the polygon, no matter the order I put them in. This also stays true if I have the polygon start a frame later than the image.
e.g. see inn the code below (created with the Builder before compiling) how both a blue square and a red triangle are supposed to start at frame 0, but when you run it the blue square always covers the red triangle!?
Is there a way to have the polygon on top? Do I somehow need to merge the image and polygon before drawing them?
Thank you so much for your help!!
Sebastian
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy2 Experiment Builder (v1.81.03), Sun Jan 18 20:44:26 2015
If you publish work using this script please cite the relevant PsychoPy publications
Peirce, JW (2007) PsychoPy - Psychophysics software in Python. Journal of Neuroscience Methods, 162(1-2), 8-13.
Peirce, JW (2009) Generating stimuli for neuroscience using PsychoPy. Frontiers in Neuroinformatics, 2:10. doi: 10.3389/neuro.11.010.2008
"""
from __future__ import division # so that 1/3=0.333 instead of 1/3=0
from psychopy import visual, core, data, event, logging, sound, gui
from psychopy.constants import * # things like STARTED, FINISHED
import numpy as np # whole numpy lib is available, prepend 'np.'
from numpy import sin, cos, tan, log, log10, pi, average, sqrt, std, deg2rad, rad2deg, linspace, asarray
from numpy.random import random, randint, normal, shuffle
import os # handy system and path functions
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)
# Store info about the experiment session
expName = u'test_triangle_over_square' # from the Builder filename that created this script
expInfo = {'participant':'', 'session':'001'}
dlg = gui.DlgFromDict(dictionary=expInfo, title=expName)
if dlg.OK == False: core.quit() # user pressed cancel
expInfo['date'] = data.getDateStr() # add a simple timestamp
expInfo['expName'] = expName
# Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = _thisDir + os.sep + 'data/%s_%s_%s' %(expInfo['participant'], expName, expInfo['date'])
# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
extraInfo=expInfo, runtimeInfo=None,
originPath=None,
savePickle=True, saveWideText=True,
dataFileName=filename)
#save a log file for detail verbose info
logFile = logging.LogFile(filename+'.log', level=logging.EXP)
logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file
endExpNow = False # flag for 'escape' or other condition => quit the exp
# Start Code - component code to be run before the window creation
# Setup the Window
win = visual.Window(size=(1280, 800), fullscr=True, screen=0, allowGUI=False, allowStencil=False,
monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
blendMode='avg', useFBO=True,
)
# store frame rate of monitor if we can measure it successfully
expInfo['frameRate']=win.getActualFrameRate()
if expInfo['frameRate']!=None:
frameDur = 1.0/round(expInfo['frameRate'])
else:
frameDur = 1.0/60.0 # couldn't get a reliable measure so guess
# Initialize components for Routine "trial"
trialClock = core.Clock()
ISI = core.StaticPeriod(win=win, screenHz=expInfo['frameRate'], name='ISI')
square = visual.ImageStim(win=win, name='square',units='pix',
image=None, mask=None,
ori=0, pos=[0, 0], size=[200, 200],
color=u'blue', colorSpace='rgb', opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-1.0)
polygon = visual.ShapeStim(win=win, name='polygon',units='pix',
vertices = [[-[200, 300][0]/2.0,-[200, 300][1]/2.0], [+[200, 300][0]/2.0,-[200, 300][1]/2.0], [0,[200, 300][1]/2.0]],
ori=0, pos=[0, 0],
lineWidth=1, lineColor=[1,1,1], lineColorSpace='rgb',
fillColor=u'red', fillColorSpace='rgb',
opacity=1,interpolate=True)
# Create some handy timers
globalClock = core.Clock() # to track the time since experiment started
routineTimer = core.CountdownTimer() # to track time remaining of each (non-slip) routine
#------Prepare to start Routine "trial"-------
t = 0
trialClock.reset() # clock
frameN = -1
# update component parameters for each repeat
# keep track of which components have finished
trialComponents = []
trialComponents.append(ISI)
trialComponents.append(square)
trialComponents.append(polygon)
for thisComponent in trialComponents:
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
#-------Start Routine "trial"-------
continueRoutine = True
while continueRoutine:
# get current time
t = trialClock.getTime()
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *square* updates
if frameN >= 0 and square.status == NOT_STARTED:
# keep track of start time/frame for later
square.tStart = t # underestimates by a little under one frame
square.frameNStart = frameN # exact frame index
square.setAutoDraw(True)
# *polygon* updates
if frameN >= 0 and polygon.status == NOT_STARTED:
# keep track of start time/frame for later
polygon.tStart = t # underestimates by a little under one frame
polygon.frameNStart = frameN # exact frame index
polygon.setAutoDraw(True)
# *ISI* period
if t >= 0.0 and ISI.status == NOT_STARTED:
# keep track of start time/frame for later
ISI.tStart = t # underestimates by a little under one frame
ISI.frameNStart = frameN # exact frame index
ISI.start(0.5)
elif ISI.status == STARTED: #one frame should pass before updating params and completing
ISI.complete() #finish the static period
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
routineTimer.reset() # if we abort early the non-slip timer needs reset
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in trialComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# check for quit (the Esc key)
if endExpNow or event.getKeys(keyList=["escape"]):
core.quit()
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
else: # this Routine was not non-slip safe so reset non-slip timer
routineTimer.reset()
#-------Ending Routine "trial"-------
for thisComponent in trialComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
win.close()
core.quit()
As per Jonas' comment above, PsychoPy uses a layering system in which subsequent stimuli are drawn on top of previous stimuli (as in his code examples).
In the graphical Builder environment, drawing order is represented by the vertical order of stimulus components: stimuli at the top are drawn first, and ones lower down are progressively layered upon them.
You can change the order of stimulus components by right-clicking on them and selecting "Move up", "move down", etc as required.
Sebastian, has, however, identified a bug here, in that the intended drawing order is not honoured between ImageStim and ShapeStim components. As a work-around, you might be able to replace your ShapeStim with a bitmap representation, displayed using an ImageStim. Multiple ImageStims should draw correctly (as do multiple ShapeStims). To get it to draw correctly on top of another image, be sure to save it as a .png file, which supports transparency. That way, only the actual shape will be drawn on top, as its background pixels can be set to be transparent and will not mask the the underlying image.
For a long-term solution, I've added your issue as a bug report to the PsychoPy GitHub project here:
https://github.com/psychopy/psychopy/issues/795
It turned out to be a bug in the Polygon component in Builder.
This is fixed in the upcoming release (1.82.00). The changes needed to make the fix can be seen at
https://github.com/psychopy/psychopy/commit/af1af9a7a85cee9b4ec8ad5e2ff1f03140bd1a36
which you can add to your own installation if you like.
cheers,
Jon

How to Update the Information in Tkinter Window?

(Python beginner, excuse me if the question is too childish) Below the label which says “Hello”, create a label or bar or whatever to show the updating positions of my two turtles (what I mean by updating is that as a turtle moves the two coordinates of its position changes at the same time)
import Tkinter
import turtle
def run_turtles(*args):
for t, d in args:
t.circle(250, d)
root.after_idle(run_turtles, *args)
root = Tkinter.Tk()
root.withdraw()
frame = Tkinter.Frame(bg='black')
Tkinter.Label(frame, text=u'Hello', bg='grey', fg='white').pack(fill='x')
canvas = Tkinter.Canvas(frame, width=750, height=750)
canvas.pack()
frame.pack(fill='both', expand=True)
turtle1 = turtle.RawTurtle(canvas)
turtle2 = turtle.RawTurtle(canvas)
turtle1.ht(); turtle1.pu()
turtle1.left(90); turtle1.fd(250); turtle1.lt(90)
turtle1.st(); turtle1.pd()
turtle2.ht(); turtle2.pu()
turtle2.fd(250); turtle2.lt(90)
turtle2.st(); turtle2.pd()
root.deiconify()
run_turtles((turtle1, 3), (turtle2, 4))
root.mainloop()
Thank You Very Very Much!!
Save a reference to the Label: turtleLabel = Tkinter.Label(frame, text=u'Hello', bg='grey', fg='white')
And then in your run_turtles-function you can update the label by setting its text:
turtleLabel['text'] = "Here be coordinates"
You can also change the text with the configure method:
turtleLabel.configure(text="Here be coordinates")
Note that you can't call pack in the same statement that you create the widget; packreturns None which would negate trying to save the reference to the widget.

How to Combine Tkinter windows?

I have two groups of codes and the first part is a turtle graphics window and second part is a Tkinter window. How should I those two parts together to one window?
My first part of the code
from turtle import *
def move(thing, distance):
thing.circle(250, distance)
def main():
rocket = Turtle()
ISS = Turtle()
bgpic('space.gif')
register_shape("ISSicon.gif")
ISS.shape("ISSicon.gif")
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()
rocket.fillcolor("white")
while counter == 1:
move(ISS, 3)
move(rocket, 4)
main()
Second part
from Tkinter import *
control=Tk()
control.title("Control")
control.geometry("200x550+100+50")
cline0=Label(text="").pack()
cline1=Label(text="Speed (km/s)").pack()
control.mainloop()
Thanks a lot ;)
Uhm, I'm not sure if mixing them is a good idea. This turtle module frequently uses the update command from Tcl, and this will very likely cause problems when more involved code is added in the mix (it is nice that apparently turtle can live with it). Anyway, one way to mix both is by using RawTurtle in place of Turtle, so you can pass your own Canvas which turtle will adjust for its needs.
Here is an example (I also replaced the infinite loop by an infinite re-schedule, basically):
import Tkinter
import turtle
def run_turtles(*args):
for t, d in args:
t.circle(250, d)
root.after_idle(run_turtles, *args)
root = Tkinter.Tk()
root.withdraw()
frame = Tkinter.Frame(bg='black')
Tkinter.Label(frame, text=u'Hello', bg='grey', fg='white').pack(fill='x')
canvas = Tkinter.Canvas(frame, width=750, height=750)
canvas.pack()
frame.pack(fill='both', expand=True)
turtle1 = turtle.RawTurtle(canvas)
turtle2 = turtle.RawTurtle(canvas)
turtle1.ht(); turtle1.pu()
turtle1.left(90); turtle1.fd(250); turtle1.lt(90)
turtle1.st(); turtle1.pd()
turtle2.ht(); turtle2.pu()
turtle2.fd(250); turtle2.lt(90)
turtle2.st(); turtle2.pd()
root.deiconify()
run_turtles((turtle1, 3), (turtle2, 4))
root.mainloop()

PyQt QTableView not displaying icons after update to PyQt 4.5.1

I'll try to be as clear as possible, though this is all a bit muddled in my head.
I have a PyQt application that has been working for about a year now. After updating to PyQt 4.5.1 (from 4.3.3) none of my icons appear in the QTableView anymore (this update was concurrent with an update to python 2.6.5 from 2.5.1). Reverting to the older python and PyQt, everything works as expected.
The breakdown is this:
I am using the model-view methodology. My model, when requested via a Qt.DecorationRole in the data() method, will return a custom object (ColorSwatch) that is a subclass of the QIcon class. This has always worked (with the caveat that I, for reasons I don't understand, have to recast it as a QVariant first). After updating to PyQt 4.5.1 it appears to run correctly (i.e. I am not getting any errors), but the icon does not draw (though the space where it would be drawn is "reserved" i.e. the text has been shifted to the right to make way for this invisible icon).
Here are some things that I have tried:
I have verified that the ColorSwatch class does still function. This same class is used to draw icons into a contextual menu - and they appear correctly.
I have verified that the data() method is actually getting called and is returning this ColorSwatch object (recast into a QVariant <- though I have tested without this recasting as well).
Pouring snake blood onto my keyboard and lighting it afire.
Nothing so far has given me any clue as to what I should do. Any hints would be greatly appreciated. Thanks.
Here is some of the (potentially) relevant code (note that paramObj.get_icon() returns a ColorSwatch object):
#---------------------------------------------------------------------------
def data(self, index, role=QtCore.Qt.DisplayRole):
"""
Returns the text or formatting for a particular cell, depending on the
role supplied.
"""
blah
blah
blah
elif role == QtCore.Qt.DecorationRole:
if platform.system()=='Darwin':
return QtGui.QIcon(paramObj.get_icon())
else:
return QtCore.QVariant(paramObj.get_icon())
and
import os
import tempfile
import sys
import colorsys
import copy
import fnmatch
import time
from PyQt4 import QtGui
from PyQt4 import QtCore
################################################################################
class ColorSwatch(QtGui.QIcon):
"""
A subclass of QIcon, this class draws a colored paint chip with a border
The color and size are determined at construction time, and cannot
be changed later.
"""
#---------------------------------------------------------------------------
def __init__(self, r=1, g=1, b=1, br = 0, bg = 0, bb = 0, w=20, h=20):
"""
Constructor for the ColorSwatch class. Takes the passed arguments and
creates a square icon filled with the given color and with a border
color determined by br, bg, bb. All colors should be in floating point
format.
"""
QtGui.QIcon.__init__(self)
#normalize the color
r8, g8, b8 = self.normalize_color((r, g, b))
#convert the r, g, b values to 8 bit colors
r8, g8, b8 = self.fp_to_8b_color((r8, g8, b8))
#Create the pixmap and painter objects
paintChip = QtGui.QPixmap(w, h)
painter = QtGui.QPainter()
painter.begin(paintChip)
#fill the swatch
baseColor = QtGui.QColor(r8, g8, b8)
painter.fillRect(0, 0, w, h, baseColor)
#if any of the values were super brights (>1), draw a smaller, white
#box inset to make sure the user knows
if r > 1 or g > 1 or b > 1:
painter.fillRect(5, 5, w-10, h-10, QtGui.QColor(255, 255, 255))
#if all values are 0, put a faint x through the icon
# # # brush = QtGui.QBrush()
# # # brush.setColor(QtGui.QColor(30, 30, 30))
painter.setPen(QtGui.QColor(200, 200, 200))
if r ==0 and g == 0 and b == 0:
painter.drawLine(0, 0, w, h)
painter.drawLine(w-1, 0, -1, h)
# # #
# # # #normalize the color
# # # r8, g8, b8 = self.normalize_color((r8, g8, b8))
#now draw the border(s)
#convert the r, g, b values to 8 bit colors
r8, g8, b8 = self.fp_to_8b_color((br, bg, bb))
#draw the border
painter.setPen(QtGui.QColor(r8, g8, b8))
painter.drawRect(0,0,w-1,h-1)
#if any of the values were super brights (>1), draw a border around the
#inset box as well.
if r > 1 or g > 1 or b > 1:
painter.drawRect(5,5,w-11,h-11)
#done drawing
painter.end()
#add it (both to the normal and the selected modes)
self.addPixmap(paintChip, QtGui.QIcon.Normal)
self.addPixmap(paintChip, QtGui.QIcon.Selected)
#---------------------------------------------------------------------------
def fp_to_8b_color(self, color):
"""
Convert a floating point color value (passed in the form of a three
element tuple) to a regular 8-bit 0-255 value. Returns a 3 item tuple.
"""
r = max(min(int(color[0]*255),255),0)
g = max(min(int(color[1]*255),255),0)
b = max(min(int(color[2]*255),255),0)
return (r,g,b)
#---------------------------------------------------------------------------
def normalize_color(self, color):
"""
"normalizes" a color value so that if there are any super-whites, it
balances all the other floating point values so that we end up with a
"real" color. Negative values will result in undefined behavior.
Mainly used to make the color chip "look right" when using super whites.
"""
maxValue = max(color)
if maxValue > 1:
return (color[0]/maxValue, color[1]/maxValue, color[2]/maxValue)
else:
return color
Ivo answered my question above.
the actual code that works is:
#---------------------------------------------------------------------------
def data(self, index, role=QtCore.Qt.DisplayRole):
"""
Returns the text or formatting for a particular cell, depending on the
role supplied.
"""
blah
blah
blah
elif role == QtCore.Qt.DecorationRole:
if platform.system()=='Darwin':
return QtGui.QIcon(paramObj.get_icon())
else:
return QtCore.QVariant(QtGui.QIcon(paramObj.get_icon()))
#Note that it is first cast as a QIcon before
#being cast as a QVariant.
Thanks again Ivo.

Resources