bowling.py

Created by schraf

Created on July 07, 2022

7.43 KB

D’autres jeux pour la NUMWORKS sur ma chaine Youtube : Rubika, solitaire, Mission missile, démineur, carré magique, TRON, défis de blanche…

Une vidéo pour en savoir plus sur ce programme


from math import *
from kandinsky import *
from time import sleep,monotonic
from ion import *

COUL={
 "grey":color(0xDC,0xDC,0xDC),
 "red":color(0xFF,0,0),
 "black":color(0,0,0),
 "white":color(255,255,255),
}
CAR={'0':'XXXX XX XX XXXX','1':'XX  X  X  X XXX',\
'2':'XXX  XXXXX  XXX','3':'XXX  X XX  XXXX',\
'4':'X XX XXXX  X  X','5':'XXXX  XXX  XXXX',\
'6':'XXXX  XXXX XXXX','7':'XXX  X  X  X  X',\
'8':'XXXX XXXXX XXXX','9':'XXXX XXXX  X  X',\
'S':' XXX  XXX  XXX ','F':'XXXX  XX X  X  ',\
'D':'XX X XX XX XXX ','T':'XXX X  X  X  X ',\
'T':'XXX X  X  X  X ','/':'     X X X     ',\
'X':'   X X X X X   ','+':'    X XXX X    '}

Fast=0.03
Slow=0.04
w,h=320,222
t=13
mg,mh=(w-16*t) // 2,(h-16*t) // 2
jeu=[]
Pin=[[7,5,5,3,3,3,1,1,1,1],[8,7,9,6,8,10,5,7,9,11],[0,0,0,0,0,0,0,0,0,0]]

def ResetPins():
 for lc in range(10):
  Pin[2][lc]=1

def DrawPins():
 for lc in range(10):
  if Pin[2][lc]==1:C_On(Pin[0][lc],Pin[1][lc])
  else:C_Off(Pin[0][lc],Pin[1][lc])

def DrawBg():
 global jeu
 jeu=[]
 for l in range(16):
  jeu.append(["grey" for c in range(16)])
 for (l,c) in [(1,2),(2,2),(3,2),(3,1),(4,2),(6,2),(5,2),(7,1),(7,2),\
      (1,15),(2,15),(3,15),(3,16),(4,15),(5,15),(6,15),(7,16),\
      (7,15),(16,1),(16,2),(16,15),(16,16) ]:
  jeu[l-1][c-1]="red"
 fill_rect(0,0,320,222,COUL["grey"])
 fill_rect(mg+3*t-1,mh,1,7*t,COUL["black"])
 fill_rect(mg+12*t-1,mh,1,7*t,COUL["black"])
 fill_rect(mg+13*t-1,mh,1,9*t,COUL["black"])

def affCell(Row,Col):
 r,c=Row-1,Col-1
 fill_rect(mg+c*t,mh+r*t,t-1,t-1,COUL[jeu[r][c]])

def C_On(Row,Col):
  jeu[Row-1][Col-1]="black"
  affCell(Row,Col)

def C_Off(Row,Col):
  jeu[Row-1][Col-1]="grey"
  affCell(Row,Col)

def ShowPlayer():
 C_On(1,1)
 C_On(2,1)

def aff():
 for l in range(16):
  for c in range(16):
   fill_rect(mg+c*t,mh+l*t,t-1,t-1,COUL[jeu[l][c]])

def AffCar(Right,Left,Charac):
 CArray=CAR[Charac]
 for i in range(5):
  for CAD in range(3):
   if CArray[3*i+CAD]=="X":C_On(Right+i,Left+CAD)
   else:C_Off(Right+i,Left+CAD)

def ClrScr():
 for (x,y) in [(1,1),(2,1),(1,16),(2,16)]:
  C_Off(x,y)
 for col in range(3,15):
  for row in range(1,8):C_Off(row,col)
 for row in range(8,16):
  for col in range(1,17):C_Off(row,col)
 for col in range(3,15):C_Off(16,col)
 PrevR=-1
 PrevC=-1

def ChSpeed(n):
 global StartSp
 if etat=="Settings":
  if n==1:
   StartSp=Fast
   AffCar(10,10,"F")
  else:
   StartSp=Slow
   AffCar(10,10,"S")

def Setup():
 global etat,AddOnFrame,ExtraBall
 global Score,BalR,BalC,BallDir,Mvt,timer
 timer=False
 DrawBg()
 ClrScr()
 etat="Settings"
 Fast=20
 Slow=45
 AffCar(10,5,"1")
 ChSpeed(0)
 Score=0
 ResetPins()
 DrawPins()
 aff()
 BalR=1
 BalC=14
 BallDir="L"
 Mvt=False
 ExtraBall=0
 AddOnFrame=[None for i in range(10)]

def DisplayFrame():
 global etat
 etat="DispFrame"
 ClrScr()
 ShowPlayer()
 if Tour<10:AffCar(10,5,str(Tour))
 else:
  AffCar(10,1,"1")
  AffCar(10,5,"0")
 if SpareLastFrame:
  AffCar(10,9,"/")
 else:
  if StrikeCount>0:AffCar(10,9,"X")
  if StrikeCount>1:AffCar(10,13,"X")

def Go():
 global etat,Tour,CurrPlayer,BallSpeed,BalR,BalC,BallDir,FirstBall
 global TotScor,StrikeCount
 global SpareLastFrame,Mvt
 if etat=="Settings":
  Tour=1
  CurrPlayer=1
  TotScor=0
  for lc in range(10):AddOnFrame[lc]=0
  StrikeCount=0
  SpareLastFrame=False
  DisplayFrame()
  sleep(0.5)
  return
 if etat=="DispScr":
  if Tour<11:
   DisplayFrame()
   sleep(0.5)
   return
  else:
   Setup()
   sleep(0.5)
   return
 ClrScr()
 ResetPins()
 DrawPins()
 BallSpeed=0.05
 etat="StartB"
 Mvt=True
 FirstBall=True
 BalR=1
 BalC=14
 BallDir="L"
 C_On(BalR,BalC)
 if timer==False:StartT()
 ShowPlayer()

def Bowl():
 global etat,BallSpeed
 if etat!="WaitB":return
 BallSpeed=0.1
 if PrevR>0 and PrevC>0:C_Off(PrevR,PrevC)
 etat="Bowling"

def PinStillS():
 PinCount=0
 for lc in range(10):PinCount+=Pin[2][lc]
 return PinCount

def CalcScore(PinsHit,TenExtra):
 global TotScor,AddOnFrame
 sleep(0.2)
 if not(TenExtra):TotScor+=PinsHit
 for lc in range(10):
  if AddOnFrame[lc]>0:
   TotScor+=PinsHit
   AddOnFrame[lc] -= 1

def CheckHit():
 global Pin
 for i,(a,b,c) in enumerate([(7,6,10),(5,5,10),(5,6,11),(3,4,10),(3,5,11),(3,6,12)]):
   if BalR==a and BalC>b and BalC<c:Pin[2][i]=0
 if (
  BalR==1
  and (BalC==4 or BalC==12)
  and Pin[2][6]==1
  and Pin[2][7]==0
  and Pin[2][8]==0
  and Pin[2][9]==1
 ):
  Pin[2][6]=0
  Pin[2][9]=0
  return
 if BalR==1 and BalC>3 and BalC<10 and BalC!=8:Pin[2][6]=0
 if BalR==1 and BalC>3 and BalC<11:Pin[2][7]=0
 if BalR==1 and BalC>5 and BalC<13:Pin[2][8]=0
 if BalR==1 and BalC>6 and BalC<13 and BalC!=8:Pin[2][9]=0

def MoveBall():
 global etat,BallSpeed,BalR,BalC,PrevR,PrevC,BallDir,FirstBall
 global TotScor,StrikeCount
 global AddOnFrame,SpareLastFrame,B1PinH,ExtraBall
 PrevR=BalR
 PrevC=BalC
 if etat=="StartB":
  BalR+=1
  C_Off(PrevR,PrevC)
  C_On(BalR,BalC)
  if BalR==16:
   etat="Bend"
   BallSpeed=StartSp
   return
 if etat=="Bend":
  BalC -= 1
  C_Off(PrevR,PrevC)
  C_On(BalR,BalC)
  etat="WaitB"
  return
 if etat=="WaitB":
  if BallDir=="L":
   BalC -= 1
   if BalC<3:
    BalC=4
    BallDir="R"
  else:
   BalC+=1
   if BalC>13:
    BalC=12
    BallDir="L"
  C_Off(PrevR,PrevC)
  C_On(BalR,BalC)
 if etat=="Bowling":
  C_Off(PrevR,PrevC)
  if BalR<8:
   DrawPins()
  if BalR>1:
   BalR -= 1
   CheckHit()
   C_On(BalR,BalC)
  else:
   PinM=PinStillS()
   if ExtraBall>0:
     CalcScore(10-PinM,True)
     ExtraBall -= 1
     if ExtraBall>0:
       if PinM==0:
        ClrScr()
        ShowPlayer()
        ResetPins()
        DrawPins()         
       etat="StartB"
       Mvt=True
       BalR=1
       BalC=14
       C_On(BalR,BalC)
       if timer==False:
        StartT()
     else:
        DisplayScore()
     return   
   if FirstBall:
    B1PinH=10-PinM 
    if PinM>0:
     CalcScore(B1PinH,False)
     StrikeCount=0
     SpareLastFrame=False
     etat="StartB"
     Mvt=True
     FirstBall=False  
     BalR=1
     BalC=14
     C_On(BalR,BalC)
     if timer==False:
      StartT()
    else:
     CalcScore(10,False)
     SpareLastFrame=False
     AddOnFrame[Tour-1]=2
     if StrikeCount<2:StrikeCount+=1
     if Tour==10:
      ExtraBall=2
      ClrScr()
      ShowPlayer()
      ResetPins()
      DrawPins()
      etat="StartB"
      Mvt=True
      BalR=1
      BalC=14
      C_On(BalR,BalC)
      if timer==False:StartT()
      return
     DisplayScore()
   else: # second
    if PinM==0:
     CalcScore(10-B1PinH,False)
     StrikeCount=0
     SpareLastFrame=True
     AddOnFrame[Tour-1]=1
     if Tour==10:
      ExtraBall=1
      ClrScr()
      ShowPlayer()
      ResetPins()
      DrawPins()
      etat="StartB"
      Mvt=True
      BalR=1
      BalC=14
      C_On(BalR,BalC)
      if timer==False:StartT()
      return
     DisplayScore()
    else:
     CalcScore(10-(B1PinH+PinM),False)
     StrikeCount=0
     SpareLastFrame=False
     DblBall=0
     DisplayScore()

def DisplayScore():
 global Mvt,etat,ExtraBall,Tour
 sleep(.2)
 if PrevR>0 and PrevC>0:
  C_Off(PrevR,PrevC)
 Mvt=False
 etat="DispScr"
 ClrScr()
 ShowPlayer()
 WScore(TotScor)
 if (SpareLastFrame or StrikeCount>0) and Tour<10:
  AffCar(10,14,"+")
 Tour+=1
 ExtraBall=0


def WScore(Score):
 AffCar(10,10,str(Score % 10))
 if Score >= 10:
  AffCar(10,6,str((Score % 100) // 10))
 if Score>99:
  AffCar(10,2,str((Score % 1000) // 100))

def StartT():
 global BallSpeed,Mvt
 t=monotonic()
 while True:
  if Mvt==False:
   return
  else:
   if monotonic()-t>BallSpeed:
    MoveBall()
    t=monotonic()
    if keydown(KEY_ZERO):
     Bowl()

Setup()

while True:
 if keydown(KEY_PLUS):ChSpeed(1)
 elif keydown(KEY_MINUS):ChSpeed(0)
 elif keydown(KEY_EXE):Go()

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 cookies policy.