Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

今日のドローイングプログラム

Last updated at Posted at 2021-01-25

2021.1.25.

  • pygame2
  • pydroid3

key_a black screen mode / key_b image screen mode

black screen mode

image 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()
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?