Hello and thank you for your ongoing help. I would like to make the randomised colors produced below using RGB values darker, is this possible and any help received would be much appreciated! Please note that I have commented out the heart rate inputs and simply set it at 50bmp. If you change the value, you should see the animation change colour and speed. Hope this makes sense! thanks again, Tina
##from pulsesensor import Pulsesensor
from Tkinter import *
import random
import time
import struct
##p = Pulsesensor()
##p.startAsyncBPM()
tk = Tk()
WIDTH=1280
HEIGHT=720
canvas = Canvas(tk, height=HEIGHT, width= WIDTH)
#canvas = Canvas(tk, bg="light gray", height=HEIGHT, width= WIDTH)
tk.title("drawing")
canvas.pack()
def getColor(speedCoef, green):
prop = speedCoef - 0.2
prop /= 2
red = prop
if red < 0:
red = 0
if red > 1:
red = 1
red *= 255
blue = 255 - red
#print("red: %d green: %d blue: %d" % (red, green, blue))
rgb = (red, green, blue)
response = "#"
response += struct.pack('BBB', *rgb).encode('hex')
return response
##below is the class to create multiple balls that are coloured
##and move and detect the edge and bounce
class Ball:
def __init__(self, color, size):
xPos = random.randrange(90, WIDTH-90)
yPos = random.randrange(90, HEIGHT-90)
self.size = size
#stipple="gray25"
self.shape = canvas.create_oval (xPos-size*0.5, yPos-size*0.5, xPos+size*0.5, yPos+size*0.5, fill=color, outline = '', stipple="gray75")
self.xspeed =random.randrange(3,5)
self.yspeed =random.randrange (3,5)
self.green = random.randrange(10, 50)
def update(self, speedCoef):
color = getColor(speedCoef, self.green)
canvas.itemconfig(self.shape, fill=color)
canvas.move(self.shape, self.xspeed * speedCoef, self.yspeed * speedCoef)
pos = canvas.coords(self.shape)
middleX = (pos[0] + pos[2]) * 0.5
middleY = (pos[1] + pos[3]) * 0.5
newRadius = self.size * 0.5 * ((-(speedCoef-10) + 4) * 0.25)
canvas.coords(self.shape, middleX-newRadius, middleY-newRadius, middleX+newRadius, middleY+newRadius)
if middleY>=HEIGHT-30 or middleY<=30:
self.yspeed=-self.yspeed
if middleX >=WIDTH-30 or middleX <=30:
self.xspeed=-self.xspeed
colors=["#290404"]
##this is make 100 balls
balls=[]
for i in range (10):
balls.append(Ball(random.choice(colors), random.randrange(150, 170)))
##index = 10
##this is to call the balls
while True:
## bpm = p.BPM
bpm = 150
if bpm >0:
print("BPM: %d" % bpm)
speed = bpm * 0.01
speed *= speed
## the following 2 numbers define the BPM range (0.2 = 45bpm, 2.2 = 150bpm)
if speed < 0.2:
speed = 0.2
if speed > 1.2:
speed = 2.2
#change bg color by taking speed (from bpm above) and updating the canvas
bgcolor = getColor(speed, 25)
canvas.configure(background=bgcolor)
#print("speed: %f" % speed)
for ball in balls:
ball.update(speed)
tk.update()
## time.sleep(0.01)
tk.mainloop()
p.stopAsyncBPM()
Aucun commentaire:
Enregistrer un commentaire