tron.py

Created by kbdechez-lidl

Created on February 02, 2023

2.8 KB


from math import *
from kandinsky import *
from ion import *
from time import *

def terrain():
  fill_rect(0,0,400,400,color(20,20,20))  
  l=0
  for i in range(20):
    l=l+20
    fill_rect(0,l,400,5,color(40,40,40))

def perso(x,y,x2,y2):
  if get_pixel(x,y)==(200,20,16) or get_pixel(x,y)==(16,20,200):
    return -1,-1
  fill_rect(x,y,5,5,color(160,10,10)) 
  fill_rect(x2,y2,5,5,color(200,20,20)) 
  x2=x
  y2=y
  return x2,y2
  
def perso2(x11,y11,x22,y22):
  if get_pixel(x11,y11)==(200,20,16) or get_pixel(x11,y11)==(16,20,200):
    return -1,-1
  fill_rect(x11,y11,5,5,color(10,10,160)) 
  fill_rect(x22,y22,5,5,color(20,20,200)) 
  x22=x11
  y22=y11
  return x22,y22
  
def jeu(score1,score2):
  x=30
  y=100
  x2=1000
  y2=1000
  x11=280
  y11=100
  x22=1000
  y22=1000
  sens2=2
  sens=0
  speed=0.05  
  print(terrain())
  while True:
    if x<0:
      x=320
    if x>320:
      x=0
    if y<0:
      y=220
    if y>220:
      y=0
    if x11<0:
      x11=320
    if x11>320:
      x11=0
    if y11<0:
      y11=220
    if y11>220:
      y11=0
    if sens==0:
      x=x+5
    if sens==1:
      y=y+5
    if sens==2:
      x=x-5
    if sens==3:
      y=y-5
    if sens2==0:
      x11=x11+5
    if sens2==1:
      y11=y11+5
    if sens2==2:
      x11=x11-5
    if sens2==3:
      y11=y11-5
    sleep(speed)
    x2,y2=perso(x,y,x2,y2)
    x22,y22=perso2(x11,y11,x22,y22)
    if x2==-1:
      draw_string("Blue win",115,100)
      draw_string("[OK] to rematch",80,130)
      return score1,score2+1    
    if x22==-1:
      draw_string("Red win",115,100)
      draw_string("[OK] to rematch",80,130)
      return score1+1,score2
    if keydown(KEY_RIGHT)==True:
      sens=0
      sleep(0.05)
    if keydown(KEY_UP)==True:
      sens=3
      sleep(0.05)
    if keydown(KEY_LEFT)==True:
      sens=2
      sleep(0.05)
    if keydown(KEY_DOWN)==True:
      sens=1
      sleep(0.05)
    if keydown(KEY_DIVISION)==True:
      sens=0
      sleep(0.05) 
    if keydown(KEY_LEFTPARENTHESIS)==True:
      sens2=3
      sleep(0.05)
    if keydown(KEY_SIX)==True:
      sens2=2
      sleep(0.05)
    if keydown(KEY_PLUS)==True:
      sens2=1
      sleep(0.05)

while True:
  draw_string("TRON",125,60)
  fill_rect(125,75,45,1,color(255,0,0))
  draw_string("[OK] pour commencer",65,90)
  draw_string("M.R.",280,200)
  if keydown(KEY_OK) == True:
    fill_rect(0,0,400,300,color(255,255,255))
    break
score1=0
score2=0      
print(terrain())
draw_string("[OK] to start",85,100)  
draw_string("Red -> Arrow    Blue -> (,/,+,6",5,140)
while True:
  draw_string("Red-"+str(score1),30,0)
  draw_string("Blue-"+str(score2),230,0)
  if keydown(KEY_OK)==True:
    draw_string("3",150,50)
    sleep(1)
    draw_string("2",150,50)
    sleep(1)
    draw_string("1",150,50)
    sleep(1)
    draw_string("GO",150,50)
    sleep(1)
    score1,score2=jeu(score1,score2)