Attachment '2014-01-31-flyword.py'

Download

   1 #!/usr/bin/env python
   2 # coding: utf
   3 '''
   4 Написать программу: введённое слово отображается по центру экрана, а затем буквы разбегаются в разные стороны
   5 '''
   6 
   7 import pygame, random
   8 from pyginput import Input
   9 
  10 size=800,600
  11 pygame.init()
  12 scr=pygame.display.set_mode(size)
  13 font=pygame.font.Font(None, 72)
  14 white=pygame.Color("white")
  15 black=pygame.Color("black")
  16 red=pygame.Color("tomato")
  17 
  18 class Letter:
  19     def __init__(self, c, font, color):
  20         self.char=c
  21         self.font=font
  22         self.color=color
  23         self.speed=random.random()*2-1,random.random()*2-1
  24         self.pos=0,0
  25         self.refresh()
  26 
  27     def refresh(self):
  28         self.surface=self.font.render(self.char,True,self.color)
  29 
  30 
  31 inp=Input(u"Слово:")
  32 letters,deads=[],[]
  33 w=0
  34 
  35 letters=[Letter(c, font, white) for c in inp.input(scr, (10,10))]
  36 
  37 wwidth=sum(l.surface.get_width() for l in letters)
  38 x,y=(scr.get_width()-wwidth)/2,(scr.get_height()-letters[0].surface.get_height())/2
  39 for l in letters:
  40     l.pos=x,y
  41     x+=l.surface.get_width()
  42 
  43 def int2(seq): return int(seq[0]), int(seq[1])
  44 
  45 def redraw():
  46     scr.fill(black)
  47     for l in deads+letters:
  48         scr.blit(l.surface, int2(l.pos))
  49 
  50 def recalc(speed=1.):
  51     for l in letters:
  52         l.pos=l.pos[0]+speed*l.speed[0],l.pos[1]+speed*l.speed[1]
  53 
  54 def gone():
  55     global letters
  56     letters=[l for l in letters if scr.get_rect().colliderect(int2(l.pos),l.surface.get_size())]
  57     return not letters
  58 
  59 def shoot((x,y)):
  60     global letters
  61     for l in letters:
  62         if l.pos[0]<=x<=l.pos[0]+l.surface.get_width() and l.pos[1]<=y<=l.pos[1]+l.surface.get_height():
  63             letters.remove(l)
  64             l.color=red
  65             l.refresh()
  66             deads.append(l)
  67 
  68 again=True
  69 pygame.time.set_timer(pygame.USEREVENT, 50)
  70 while again:
  71     bang = None
  72     event = pygame.event.wait()
  73     if event.type == pygame.QUIT:
  74         again = False
  75     elif event.type == pygame.MOUSEBUTTONDOWN:
  76         bang = event.pos
  77     elif event.type == pygame.USEREVENT:
  78         recalc(3.)
  79     if bang: shoot(bang)
  80     again&=not gone()
  81     redraw()
  82     pygame.display.flip()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.