1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| from pygame import *
font.init()
scr = display.set_mode((1150,700))
fond = image.load('backgroundJeu.jpg')
scr.blit(fond,(0,0))
display.flip()
class Clavier(list,object):
font = font.Font(font.get_default_font(),30)
screen = display.get_surface()
def __init__(self,x,y):
class Bouton(object):
img_unset = image.load('boutonLettre.png')
img_set = image.load('boutonLettre2.png')
rectangle = img_unset.get_rect()
def __init__(self,x,y,lettre):
self.rect = Bouton.rectangle.copy()
self.lettre = lettre
self.set = 0
self.lettre_img = Clavier.font.render(self.lettre,1,(255,255,255))
self.lettre_rect = self.lettre_img.get_rect(center=self.rect.center)
self.rect.topleft = x,y
@property
def image(self):
bouton = (Bouton.img_set if self.set else Bouton.img_unset).copy()
bouton.blit(self.lettre_img,self.lettre_rect)
return bouton
self.x = x
self.y = y
for e,lettre in enumerate('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
px = e%13 * 75 + self.x
py = e//13 * 75 + self.y
self.append(Bouton(px,py,lettre))
self.draw()
def draw(self,index=None):
if index != None:
bouton = self[index]
r = self.screen.blit(bouton.image,bouton.rect)
display.update(r)
else:
r = []
for b in self:
r.append(self.screen.blit(b.image,b.rect))
display.update(r)
def lire_lettre(self,mouse_x,mouse_y):
#~ retourne la lettre correspondante au clic de souris
x = (mouse_x - self.x)//75
y = (mouse_y - self.y)//75
index = y * 13 + x
if 0 <=index < 26 and not self[index].set and self[index].rect.collidepoint((mouse_x,mouse_y)):
self[index].set = 1
self.draw(index)
return self[index].lettre
def clear(self):
#~ reinitialise le clavier
for b in self:
b.set = 0
self.draw()
c = Clavier(100,540)
while 1:
ev = event.wait()
if ev.type == MOUSEBUTTONUP and ev.button == 1:
lettre = c.lire_lettre(*ev.pos)
if lettre: print(lettre)
if ev.type == KEYDOWN and ev.key == K_ESCAPE:
c.clear()
if ev.type == QUIT: break |
Partager