karas2.py

Created by yaya-cout

Created on November 29, 2022

6.2 KB

Jeu dont le but est d’éviter les voitures (carrés noirs tombant du haut de l’écran) en jouant le carré vert. Le jeu est infini.

Les informations de score apparaissent lorsque que la première voiture a fini sa traversée. Les trois chiffres sont respectivement : le nombre total de voitures passées, le pourcentage de voitures évitées et le nombre de morts.

Vous pouvez désactiver les voitures horizontales en mettant horizontal à False

Le jeu peut être modifié en changeant les variables du début du script (désolé pour l’orthographe) : width sert à donner la taille des voitures et du joueur, space set à donner l’espace entre deux voitures, speed sert à donner la vitesse initiale (petit = rapide, grand = lent) et screen_x_end sert à donner la largeur de l’écran de jeu en pixels.

Le nom du jeu vient du mot “car race” en anglais, qui, écrit littéralement, donne “karas”

La version originale est disponible ici


from kandinsky import *
from time import *
from random import *
from ion import *
#import micropython
#micropython.kbd_intr(KEY_OK)
width=10
heidth=width
#space=2
space=0
speed=.5
screen_x_end=235
autoplay=False
max_speed=0.02
spown_frequency=5
vertical=True
horizontal=True

# Cheat
#width=20
#heidth=width
#max_speed=0
#autoplay=True
#spown_frequency=.25
#spown_frequency=10000

#monotonic_old=monotonic
#def monotonic():
#  return monotonic_old()*1000

class Car:
  def __init__(self,id,horizontal):
    self.id=id
    self.horizontal=horizontal
    pos=self.id*(width+space)
    if not horizontal:
      self.x=pos
      self.y=0
      self.color="red"
    else:
      self.x=0
      self.y=pos
      self.color="blue"
    try:
      self.iter+=1
    except:
      self.iter=0
    self.update_at=monotonic()+uniform(0,spown_frequency)
    fill_rect(self.x+1,self.y+1,width-1,heidth-1,self.color)
    if not self.horizontal:
      fill_rect(self.x+width+space,0,1,222,"black")
    else:
      fill_rect(0,self.y+width+space,screen_x_end,1,"black")

  def update(self):
    time_now=monotonic()
    if not time_now>self.update_at:
      return
    fill_rect(self.x+1,self.y+1,width-1,heidth-1,"white")
    if not self.horizontal:
      self.y+=heidth
    else:
      self.x+=width
    fill_rect(self.x+1,self.y+1,width-1,heidth-1,self.color)
    self.update_at=self.update_at+speed
    if not self.horizontal:
      fill_rect(self.x+width+space,0,1,self.y,"grey")
    else:
      fill_rect(0,self.y+width+space,self.x,1,"grey")
    reset=False
    if self.y>222 and not self.horizontal:
      reset=True
    if self.x>screen_x_end-2*(width+space) and self.horizontal:
      reset=True
    if reset:
      fill_rect(self.x+1,self.y+1,width-1,heidth-1,"white")
      self.__init__(self.id,self.horizontal)

  def touching(self,x,y):
    if x>=self.x and x<(self.x+heidth):
      if y>=self.y and y<(self.y+width):
        return True
    return False

class DummyCar:
  def __init__(self):
    self.iter=0
  def update(self):
    pass
  def touching(self,x,y):
    return False
def draw_player(x):
  fill_rect(x+1,y+1,heidth-1,width-1,"green")

car_lst=[]
vertical_number=0
horizontal_number=0
if vertical:
  vertical_number=int(screen_x_end/(width+space))
  Car(0,False)
  car_lst.append(DummyCar())
  for id in range(1,vertical_number):
    car_lst.append(Car(id,False))
  id=0
if horizontal:
  horizontal_number=int(222/(width+space))
  Car(0,True)
  car_lst.append(DummyCar())
  for id in range(1,horizontal_number):
    car_lst.append(Car(id,True))



fill_rect(0,0,1,222,"black")
x=(id-1)*(width+space)
y=200
lost=0
last_motion=monotonic()
while True:
  speed-=0.0001
  speed=max(max_speed,speed)
  total=0
  for car in car_lst:
    if car.touching(x,y):
      lost+=1
      car.__init__(car.id,car.horizontal)
    total+=car.iter
    car.update()
  draw_player(x)
  if total:
    draw_string("Total",screen_x_end,0)
    draw_string(str(total),screen_x_end,20)

    draw_string("Evités",screen_x_end,60)
    draw_string(str(total-lost),screen_x_end,80)

    draw_string("Touchés",screen_x_end,120)
    draw_string(str(lost),screen_x_end,140)

    draw_string("Evités",screen_x_end,180)
    draw_string(str(round(100-lost/total*100,1))[:4].strip(".")+"%     ",screen_x_end,200)
#    draw_string(str(lost),screen_x_end,200)
  can_move=(monotonic()-last_motion)>speed
#  can_move=(monotonic()-last_motion)>0.1
  if can_move and keydown(KEY_LEFT):
    if x-width+space<(width+space):
      continue
    fill_rect(x+1,y+1,heidth-1,width-1,"white")
    x-=width+space
    draw_player(x)
    last_motion=monotonic()
  if can_move and keydown(KEY_RIGHT):
    if x+width+space>(screen_x_end-width-space):
      continue
    fill_rect(x+1,y+1,heidth-1,width-1,"white")
    x+=width+space
    draw_player(x)
    last_motion=monotonic()
  if can_move and keydown(KEY_UP):
    if y-width+space<(width+space):
      continue
    fill_rect(x+1,y+1,heidth-1,width-1,"white")
    y-=heidth+space
    draw_player(x)
    last_motion=monotonic()
  if can_move and keydown(KEY_DOWN):
    if y+width+space>(222-width-space):
      continue
    fill_rect(x+1,y+1,heidth-1,width-1,"white")
    y+=heidth+space
    draw_player(x)
    last_motion=monotonic()
  if autoplay:
    iterations=0
    while True:
      moved=False
      if vertical:
        while car_lst[int(x/(width+space))].touching(x,y-heidth) or car_lst[int(x/(width+space))].touching(x,y):
          if x+width+space>(screen_x_end-width-space):
            break
          fill_rect(x+1,y+1,heidth-1,width-1,"white")
          x+=width+space
          draw_player(x)
          last_motion=monotonic()
          moved=True
        while car_lst[int(x/(width+space))].touching(x,y-heidth) or car_lst[int(x/(width+space))].touching(x,y):
          if x-width+space<(width+space):
            break
          fill_rect(x+1,y+1,heidth-1,width-1,"white")
          x-=width+space
          draw_player(x)
          last_motion=monotonic()
          moved=True
      if horizontal:
        while car_lst[int(y/(width+space))+vertical_number].touching(x-width,y) or car_lst[int(y/(width+space))+vertical_number].touching(x,y):
          if y+width+space>(222-width-space):
            break
          fill_rect(x+1,y+1,heidth-1,width-1,"white")
          y+=heidth+space
          draw_player(x)
          last_motion=monotonic()
          moved=True
        while car_lst[int(y/(width+space))+vertical_number].touching(x-width,y) or car_lst[int(y/(width+space))+vertical_number].touching(x,y):
          if y<=(width+space):
            break
          fill_rect(x+1,y+1,heidth-1,width-1,"white")
          y-=heidth+space
          draw_player(x)
          last_motion=monotonic()
          moved=True
#        while car_lst[int(y/(width+space))+vertical_number].touching(x-width,y) or car_lst[int(y/(width+space))+vertical_number].touching(x,y):
#          if y<=0:
#            break
#          fill_rect(x+1,y+1,heidth-1,width-1,"white")
#          y+=heidth+space
#          draw_player(x)
#          last_motion=monotonic()
#          moved=True
        if not moved:
          break
      iterations+=1
      if iterations%10==0:
        print("Timeout in autoplay")
        fill_rect(x+1,y+1,heidth-1,width-1,"white")
        x=80
        y=80
        draw_player(x)
        break