2021.1.25.
- pygame2
- pydroid3
key_a black screen mode / key_b image screen mode
black screen modeimage mode
#!/usr/bin/env python
import os
import pygame as pg
from pygame.locals import *
import random
# This makes the touchpad be usable as a multi touch device.
os.environ['SDL_MOUSE_TOUCH_EVENTS'] = '1'
bg = pg.image.load('982.jpg')
def main():
pg.init() # Different colors for different fingers.
colors = [
'red', 'gray',
]
# keyed by finger_id, and having dict as a value like this:
# {
# 'x': 20,
# 'y': 20,
# 'color': 'red',
# }
circles = {} # circles[finger_id]{'x','y','color','time'}
copy_circles = {} # copy_circles[finger_id]{'x','y','color','time'}
vanish = {} # vanish[finger_id] = { 'x', 'y', 'size', 'time' }
v_list = [] #
i = 0
width, height = (600, 900)
screen = pg.display.set_mode((width, height),SCALED)
clock = pg.time.Clock()
pg.display.set_caption('finger painting with multi-touch') # we hide the mouse cursor and keep it inside the window.
pg.event.set_grab(True)
pg.mouse.set_visible(False)
topsc = pg.display.get_surface() # top screen for drawing
b_top = pg.display.get_surface()
backsc = pg.display.get_surface() # back screen for background image
b_back = pg.display.get_surface()
buff = pg.display.get_surface() # buffer screen for copy top screen
buff = topsc.copy()
backsc.blit(bg,(0,0)) # bg = background image
#b_back.blit(bg,(0,0)) # b_back buffer
screen.blit(backsc,(0,0)) # blit background screen to screen
#screen.blit(topsc,(0,0))
b_top = screen.copy()
toggle = False
going = True
while going:
for e in pg.event.get():
# We look for finger down, finger motion, and then finger up.
if e.type == pg.FINGERDOWN:
pick_col = random.choice(list(colors))
circles[e.finger_id] = { 'color': pick_col,
'x': int(width * e.x), # x and y are 0.0 to 1.0
'y': int(height * e.y), # change into screen pixels.
'time' : pg.time.get_ticks(),
'end' : True
}
copy_circles[e.finger_id] = circles[e.finger_id]
elif e.type == pg.FINGERMOTION:
circles[e.finger_id].update({
'x': int(width * e.x),
'y': int(height * e.y),
'time' : pg.time.get_ticks(),
'end' : False
})
copy_circles[e.finger_id] = circles[e.finger_id]
elif e.type == pg.FINGERUP:
pick_col = random.choice(list(colors))
copy_circles[e.finger_id].update({
'color' : pick_col,
'x': int(width * e.x),
'y': int(height * e.y),
'time' : pg.time.get_ticks(),
'end' : True
})
del circles[e.finger_id]
elif (toggle == False and (e.type == pg.KEYUP and e.key == pg.K_a)):
b_top = screen.copy()
topsc = buff.copy()
screen.blit(topsc,(0,0)) # blit top screen to screen
toggle = True
elif (toggle == True and (e.type == pg.KEYUP and e.key == pg.K_b)):
topsc = b_top.copy()
#backsc.blit(bg,(0,0))
#screen.blit(backsc,(0,0))
screen.blit(topsc,(0,0))
#b_top = screen.copy()
toggle = False
elif ((e.type == pg.KEYDOWN and e.key in (pg.K_q, pg.K_ESCAPE)) or e.type == pg.QUIT):
going = False
# draw a circle for each finger.
for finger_id, circle in circles.items():
pg.draw.circle(screen, circle['color'], (circle['x'], circle['y']),1)
pg.draw.circle(buff, circle['color'], (circle['x'], circle['y']),1)
for finger_id, ob in copy_circles.items():
#v_list.insert(i,finger_id)
v_list.insert(i,i)
vanish[v_list[i]] = {'color':ob['color'], 'x':ob['x'], 'y':ob['y'], 'time':ob['time'], 'size':1}
i += 1
if ob['end'] == True:
pg.draw.circle(screen, ob['color'], (ob['x'], ob['y']), 7)
pg.draw.circle(buff, ob['color'], (ob['x'], ob['y']),7)
elif 2 < pg.time.get_ticks() - ob['time'] :
pick_col = random.choice(list(colors))
pg.draw.circle(screen, pick_col, (ob['x'], ob['y']), 7)
pg.draw.circle(buff, pick_col, (ob['x'], ob['y']),7)
for l, p in vanish.items() :
if 40 < p['time'] + 40 <= pg.time.get_ticks():
pg.draw.circle(screen, 'black', (p['x'], p['y']), 9)
pg.draw.circle(buff, 'black', (p['x'], p['y']),9)
p['time']=pg.time.get_ticks()
elif 5 < p['time'] + 20 <= pg.time.get_ticks():
pick_color = random.choice(list(colors))
pg.draw.circle(screen, pick_color, (p['x'], p['y']), 10)
pg.draw.circle(buff, pick_color, (p['x'], p['y']),10)
p['time']=pg.time.get_ticks()
elif p['time'] + 5 <= pg.time.get_ticks():
pick_color = random.choice(list(colors))
pg.draw.circle(screen, pick_color, (p['x'], p['y']), 6)
pg.draw.circle(buff, pick_color, (p['x'], p['y']),6)
clock.tick(60)
pg.display.update()
if __name__ == "__main__":
main()