tap_to_win.py

Created by caucaucybu

Created on December 05, 2023

7.62 KB

Petit jeu de farming. Vous pouvez jouer jusqu’à 2 joueurs et choisir un délai.
Si vous êtes à 2 le joueur rouge a comme touche: [4] : gauche [8] : haut [2] : bas [6] : droite [5] : ok
Et le joueur bleu : [3] : gauche [MULTIPLICATION] : haut [Ans] : bas [MOINS] : droite [PLUS] : ok


Vous pouvez avoir d’autres jeux comme un crossy_road sur mon compte NumWorks : Caucaucybu


from kandinsky import fill_rect as draw, draw_string as draw_txt
from time import monotonic
from ion import keydown

dark_col = lambda color: tuple([(col+255)/2 for col in color])
passive = lambda: None

class Money:
  def __init__(self, col=(255,0,0), keys={"left":(0,),"up":(1,),"down":(2,),"right":(3,),"ok":(4,)}, nthplayer=0, display = True):
    self.money = 0
    self.time = monotonic()
    self.col, self.nthplayer = col, nthplayer
    self.DISPLAY = display
    
    self.delay = 1
    self.delayCost = 10
    self.boost = 3
    self.boostCost = 10
    self.x, self.y = 0, 0
    
    self.bonus = 1
    self.bonusCounter = 0
    self.bonusTime = monotonic()
    self.bonusIsPress = False
    self.bonusEnd = False
    
    self.keys = dict()
    self.notKeys = dict()
    for i in keys["left"]:
      self.keys[i] = self.left
    for i in keys["up"]:
      self.keys[i] = self.up
    for i in keys["down"]:
      self.keys[i] = self.down
    for i in keys["right"]:
      self.keys[i] = self.right
    for i in keys["ok"]:
      self.keys[i] = self.ok
      self.notKeys[i] = self.not_ok
    
    for i in range(60):
      if not self.keys.get(i):
        self.keys[i] = passive
      if not self.notKeys.get(i):
        self.notKeys[i] = passive
  
  def display_button(self): 
    if not self.DISPLAY:
      return
    if self.nthplayer:
      if self.x:
        col = dark_col(dark_col(self.col))
      else:
        col = self.col
      draw(20, -60 + 85*self.nthplayer, 160, 50, (255,255,255))
      if self.bonusIsPress:
        draw(30, -50 + 85*self.nthplayer, 140, 30, col)
      else:
        draw(20, -60 + 85*self.nthplayer, 160, 50, col)
      draw_txt("+$"+str(self.bonus)+"/click", 50, -43 + 85*self.nthplayer, (0,0,0), col)
    else:
      if self.x:
        col = dark_col((100,100,100))
      else:
        col = self.col
      draw(20, 30, 160, 130, (255,255,255))
      if self.bonusIsPress:
        draw(35, 45, 130, 100, col)
      else:
        draw(20, 30, 160, 130, col)
      draw_txt("+$"+str(self.bonus)+"/click", 50, 86, (0,0,0), col)
  
  def display_boost(self):
    if not self.DISPLAY:
      return
    if self.nthplayer:
      if self.money < self.boostCost:
        col = dark_col(self.col)
      else:
        col = self.col
      if self.x and not self.y:
        background = col
        col = (0,0,0)
      else:
        col = col
        background = (255,255,255)
      draw(200, 18 + 20*self.nthplayer, 120, 18, (255,255,255))
      draw_txt('$'+str(self.boostCost), 200, 18 + 20*self.nthplayer, col, background)
    else:
      if not self.x or self.y:
        col = (0,0,0)
      else:
        col = self.col
      if self.money < self.boostCost:
        col = dark_col(col)
      draw(200, 20, 120, 60, (255,255,255))
      draw_txt("Upgrade", 200, 20, col)
      draw_txt("boost :", 200, 40, col)
      draw_txt('$'+str(self.boostCost), 200, 60, col)
  
  def display_delay(self):
    if not self.DISPLAY:
      return
    if self.nthplayer:
      if self.money < self.delayCost:
        col = dark_col(self.col)
      else:
        col = self.col
      if self.x and self.y:
        background = col
        col = (0,0,0)
      else:
        col = col
        background = (255,255,255)
      draw(200, 100 + 20*self.nthplayer, 120, 18, (255,255,255))
      draw_txt('$'+str(self.delayCost), 200, 100 + 20*self.nthplayer, col, background)
    else:
      if self.x and self.y:
        col = self.col
      else:
        col = (0,0,0)
      if self.money < self.delayCost:
        col = dark_col(col)
      draw(200, 90, 120, 60, (255,255,255))
      draw_txt("Upgrade", 200, 90, col)
      draw_txt("delay :", 200, 110, col)
      draw_txt('$'+str(self.delayCost), 200, 130, col)
    
  def display_money(self, diff=0, coefBoost=1, coefDelay=1):
    if not self.DISPLAY:
      return
    if self.nthplayer:
      draw(20, -80 + 85*self.nthplayer, 10*len(str(self.money)) + 10, 18, (255,255,255))
      self.money += diff
      draw_txt('$'+str(self.money),  20, -80 + 85*self.nthplayer, self.col)
    else:
      draw(20, 0, 10*len(str(self.money))+10, 18, (255,255,255))
      self.money += diff
      draw_txt('$'+str(self.money), 20, 0, (0,0,0))
    self.boostCost = round(self.boostCost*coefBoost)
    self.delayCost = round(self.delayCost*coefDelay)
    self.display_boost()
    self.display_delay()
  
  def frame(self):
    if monotonic() > self.time + self.delay:
      self.display_money(self.boost)
      self.time = monotonic()
    if monotonic() > self.bonusTime + .4 and self.bonusEnd:
      self.bonus, self.bonusCounter = 1, 0
      self.bonusIsPress = False
      self.display_button()
      self.bonusEnd = False
  
  def left(self):
    if self.x:
      self.x = 0
    else:
      self.x = 1
    self.display_button()
    self.display_boost()
    self.display_delay()
  
  def up(self):
    if self.y:
      self.y = 0
    else:
      self.y = 1
    self.display_boost()
    self.display_delay()
  
  def down(self):
    self.up()
  
  def right(self):
    self.left()
  
  def ok(self):
    if self.x:
      if self.y:
        if self.money >= self.delayCost:
          self.delay *= .9
          self.display_money(-self.delayCost, 1, 1.5)
          self.display_delay()
      else:
        if self.money >= self.boostCost:
          self.boost = round(self.boost * 1.2)
          self.display_money(-self.boostCost, 1.7, 1)
          self.display_boost()
    else:
      self.bonusEnd = True
      self.display_money(self.bonus)
      self.bonusCounter += 1
      self.bonusTime = monotonic()
      if self.bonusCounter > self.bonus * 15:
        if self.bonus == 1:
          self.bonus = 5
        else:
          self.bonus += 5
      self.bonusIsPress = True
      self.display_button()
  
  def not_ok(self):
    if self.bonusIsPress:
      self.bonusIsPress = False
      self.display_button()


class Game:
  def __init__(self, nbPlayer, delay):
    self.keys = [True] * 60
    if nbPlayer == 1:
      self.players = [Money()]
    else:
      self.players = [
        Money(
          nthplayer=1,
          keys={'right':(38,),'up':(31,),'down':(43,),'ok':(37,),'left':(36,)}
        ),
        Money(
          nthplayer=2, col=(0,0,255),
          keys={'right':(46,),'up':(39,),'down':(51,),'ok':(45,),'left':(44,)}
        )
      ]
    self.time = monotonic()
    if nbPlayer == 1:
      self.players[0].display_money()
      self.players[0].display_button()
    else:
      draw_txt("Upgrade", 200, 0)
      draw_txt("boost :", 200, 18)
      draw_txt("Upgrade", 200, 82)
      draw_txt("delay :", 200, 100)
      for i in range(2):
        self.players[i].display_money()
        self.players[i].display_button()
    try:
      timed = delay - monotonic() + self.time
      while timed > 0:
        for i in self.players:
          i.frame()
        for i in range(60):
          if keydown(i):
            if self.keys[i]:
              self.keys[i] = False
              for j in self.players:
                j.keys[i]()
          else:
            self.keys[i] = True
            for j in self.players:
              j.notKeys[i]()
        timed = delay - monotonic() + self.time
        if timed < 0:
          timed = 0
        draw_txt("Delay : "+str(timed//60).replace(".0","")+":"+str(timed%60), 0, 200)
    except KeyboardInterrupt:
      pass
    print("Money :")
    for i in self.players:
      print(i.nthplayer, ":", i.money)


nbPlayer = 0
while not nbPlayer:
  try:
    nbPlayer = int(input("Enter player's number (1-2):\n -> "))
  except ValueError:
    print("You must give a number !")

delay = 0
while not delay:  
  try:
    delay = float(input("Enter a delay (seconds) or inf :\n ->"))
  except ValueError:
    print("You must give a number or inf !")
Game(nbPlayer, delay)