demineur.py

Created by schraf

Created on June 26, 2020

1.28 KB

Explications en vidéo

Ce jeu ne fonctionnera que sur votre calculatrice, pas sur l’émulateur.

Pour le lancer, tapez >>go() ou >>go(30) si vous voulez que la machine cache 30 mines au lieu de 20


from kandinsky import *
from ion import keydown
from random import choice

fr = fill_rect
N,G,V = 3*(0,),3*(120,),(0,255,0)

def wait(b): 
 while True:
  for i in b:
   if keydown(i):
    while keydown(i): True
    return i

def cpte(x,y,b): return len([i for (i,j) in b if abs(i-x)<2 and abs(j-y)<2])

def place(d,b = []):
 while len(b) < d:
  x, y = choice(range(15)), choice(range(10))
  if (x,y) not in b and x+y > 0 and x+y < 23 and \
     cpte(14,9,b) < 2 and cpte(0,0,b) < 2:
   b.append((x,y))
 return b
  
def grille():
 for i in range(16):
  if i<11:fr(8,21+20*i,301,1,N)
  fr(8+20*i,21,1,200,N)
 
def fin(b,t):
 for (x,y) in b: draw_string('*',13+20*x,23+20*y,G)
 sc = 0
 for i in range(150):
  if get_pixel(9+20*(i%15),22+20*(i//15))[0]==120: sc+=1
 aff(t + ' Score=' + str(sc))
  
def aff(t): draw_string(t + (' '*40),8,2,N)
 
def ca(x,y,c=G): fr(9+20*x,22+20*y,19,19,c)
 
def dep(x,y,t):
 ca(x,y,G)
 return max(0,min(14,x-1+t%3)),max(0,min(9,y-6+t//6))
  
def go(d=20):
 grille()
 x,y,b = 0,0,place(d)
 ca(14,9,V)
 while True:
  ca(x,y,N)
  nb = cpte(x,y,b)
  aff(str(nb)+' mine'+'s'*(nb>1))
  t = wait([5,30,31,32,36,38,42,43,44])
  if t==5: return
  x,y = dep(x,y,t)
  if (x,y) in b: 
   fin(b, 'PERDU')
   return
  else: 
   if (x,y) == (14,9):
    fin(b,'BRAVO')
    return

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.