turtle_run.py

Created by loic-azavant

Created on March 22, 2022

8.24 KB

un mario kart en moins bien. Le programme a besoin de race_generator.py pour fonctionner


from math import cos, sin, radians
from turtle import *
from kandinsky import fill_rect, draw_string
from ion import *
from time import monotonic
from random import choice, randint

pause=False
wait_to_pause=0

time_start=monotonic()
race_time=0

has_begun=False

background_color=(0,210,90)
road_color=(110,110,110)
road_border_color=(255,255,255)

road_size=75

road=[[0,0,4,1],[3,1,1,3],[4,3,2,1],[5,-2,1,5],[0,-2,5,1],[-1,-2,1,3]]

xMap=int(road[0][0]*30)-int(road_size/2)+160
yMap=int(road[0][1]*30)-int(road_size/2)+111

def draw_background():
  fill_rect(0,0,320,222,background_color)

def draw_road():
  for i in road:
    if not (i[0]*road_size+xMap>320 or i[1]*road_size+yMap>222 or i[0]*road_size+i[2]*road_size+xMap<0 or i[1]*road_size+i[3]*road_size+yMap<0):
      fill_rect(int(xMap+i[0]*road_size),int(yMap+i[1]*road_size),i[2]*road_size,i[3]*road_size,road_color)
  fill_rect(int(road[-1][0]*road_size+xMap),int(road[-1][1]*road_size+yMap),road[-1][2]*road_size,road[-1][3]*road_size,road_border_color)

def hide_road(x, y):
  for i in road:
    xBefore=int(xMap+i[0]*road_size+x)
    yBefore=int(yMap+i[1]*road_size-y)
    xAfter=int(xMap+i[0]*road_size)
    yAfter=int(yMap+i[1]*road_size)
    if not ((xBefore>320 and xAfter>320) or (yBefore>222 and yAfter>222) or (xBefore+i[2]*road_size<0 and xAfter+i[2]*road_size<0) or (yBefore+i[3]*road_size<0 and yBefore+i[3]*road_size<0)):
      if xBefore>=xAfter and yBefore>yAfter:
        fill_rect(xAfter+i[2]*road_size,yBefore,xBefore-xAfter,i[3]*road_size,background_color)
        fill_rect(xBefore,yAfter+i[3]*road_size,i[2]*road_size,yBefore-yAfter,background_color)
      elif xBefore>xAfter and yBefore<=yAfter:
        fill_rect(xAfter+i[2]*road_size,yBefore,xBefore-xAfter,i[3]*road_size,background_color)
        fill_rect(xBefore,yBefore,i[2]*road_size,yAfter-yBefore,background_color)
      elif xBefore<=xAfter and yBefore<yAfter:
        fill_rect(xBefore,yBefore,xAfter-xBefore,i[3]*road_size,background_color)
        fill_rect(xBefore,yBefore,i[2]*road_size,yAfter-yBefore,background_color)
      elif xBefore<xAfter and yBefore>=yAfter:
        fill_rect(xBefore,yBefore,xAfter-xBefore,i[3]*road_size,background_color)
        fill_rect(xBefore,yAfter+i[3]*road_size,i[2]*road_size,yBefore-yAfter,background_color)

def check_collision():
  for i in road:
    if position()[0]>i[0]*road_size+xMap-160 and position()[0]<i[0]*road_size+xMap+i[2]*road_size-160 and position()[1]>i[1]*road_size+yMap-111 and position()[1]<i[1]*road_size+yMap+i[3]*road_size-111:
      return True
  return False
      

def check_road_collision():
  a=0
  for i in road:
    if not (i[0]>road[-1][0]+road[-1][2] or i[1]>road[-1][1]+road[-1][3] or i[0]+i[2]<road[-1][0] or i[1]+i[3]<road[-1][1]):
      a+=1
  if a!=2:
    return True
  return False


def generate():
  global road
  road=[]
  x=0
  y=0
  stop_generation=False
  if randint(0,1)==1:
    c=choice([-2,-3,-4,-5,2,3,4,5])
    road.append([0,0,c,1])
    sens=0
  else:
    b=choice([-2,-3,-4,-5,5,4,3,2])
    road.append([0,0,1,b])
    sens=1
  for i in range(1,20):
    if sens==0:
      if randint(0,1)==1:
        b=choice([2,3,4,5])
        if c>0:
          road.append([road[i-1][0]+road[i-1][2],road[i-1][1],1,b])
        else:
          road.append([road[i-1][0]-1,road[i-1][1],1,b])
      else:
        b=choice([-5,-4,-3,-2])
        if c>0:
          road.append([road[i-1][0]+road[i-1][2],road[i-1][1]+1+b,1,abs(b)])
        else:
          road.append([road[i-1][0]-1,road[i-1][1]+1+b,1,abs(b)])
      if check_road_collision():
        for j in range(50):
          if check_road_collision():
            del road[len(road)-1]
            if randint(0,1)==1:
              b=choice([2,3,4,5])
              if c>0:
                road.append([road[i-1][0]+road[i-1][2],road[i-1][1],1,b])
              else:
                road.append([road[i-1][0]-1,road[i-1][1],1,b])
            else:
              b=choice([-5,-4,-3,-2])
              if c>0:
                road.append([road[i-1][0]+road[i-1][2],road[i-1][1]+1+b,1,abs(b)])
              else:
                road.append([road[i-1][0]-1,road[i-1][1]+1+b,1,abs(b)])
        if check_road_collision():
          stop_generation=True
          del road[-1]
      sens=1
    else:
      if randint(0,1)==0:
        c=choice([2,3,4,5])
        if b>0:
          road.append([road[i-1][0],road[i-1][1]+road[i-1][3],c,1])
        else:
          road.append([road[i-1][0],road[i-1][1]-1,c,1])
      else:
        c=choice([-5,-4,-3,-2])
        if b>0:
          road.append([road[i-1][0]+c+1,road[i-1][1]+road[i-1][3],abs(c),1])
        else:
          road.append([road[i-1][0]+c+1,road[i-1][1]-1,abs(c),1])
      if check_road_collision():
        for j in range(50):
          if check_road_collision():
            del road[len(road)-1]
            if randint(0,1)==1:
              c=choice([2,3,4,5])
              if b>0:
                road.append([road[i-1][0],road[i-1][1]+road[i-1][3],c,1])
              else:
                road.append([road[i-1][0],road[i-1][1]-1,c,1])
            else:
              c=choice([-5,-4,-3,-2])
              if b>0:
                road.append([road[i-1][0]+c+1,road[i-1][1]+road[i-1][3],abs(c),1])
              else:
                road.append([road[i-1][0]+c+1,road[i-1][1]-1,abs(c),1])
        if check_road_collision():
          stop_generation=True
          del road[-1]
      sens=0
    if stop_generation==True:
      break
  return road





road=generate()
while len(road)!=20:
  road=generate()

xMap=int(road[0][0]*30)-int(road_size/2)+160
yMap=int(road[0][1]*30)-int(road_size/2)+111

draw_background()
draw_road()
pencolor((150,0,0))
goto(0,0)

def draw_start_menu():
  fill_rect(0,0,320,50,(60,60,60))
  fill_rect(0,50,320,1,(255,255,255))

  fill_rect(0,59,151,164,(255,255,255))
  fill_rect(0,60,150,162,(60,60,60))

  draw_string("TURTLE RACE",100,10,(255,255,255),(60,60,60))
  draw_string("press OK to",15,70,(255,255,255),(60,60,60))
  draw_string("start",40,90,(255,255,255),(60,60,60))
  draw_string("press ONE to",10,120,(255,255,255),(60,60,60))
  draw_string("get a new race",3,140,(255,255,255),(60,60,60))
  draw_string("press CLEAR to",3,170,(255,255,255),(60,60,60))
  draw_string("pause",40,190,(255,255,255),(60,60,60))

draw_start_menu()

while True:
  
  if has_begun==False:
    if keydown(KEY_OK) and wait_to_pause>100:
      has_begun=True
      draw_background()
      draw_road()
      goto(0,0)
      time_start=monotonic()
  
  if keydown(KEY_ONE):
    road=generate()
    while len(road)!=20:
      road=generate()
    xMap=int(road[0][0]*30)-int(road_size/2)+160
    yMap=int(road[0][1]*30)-int(road_size/2)+111
    fill_rect(0,0,320,222,background_color)
    draw_road()
    goto(0,0)
    time_start=monotonic()
    race_time=0
    if has_begun==False:
      draw_start_menu()

  if has_begun==True:
    if keydown(KEY_TWO):
      xMap=int(road[0][0]*30)-int(road_size/2)+160
      yMap=int(road[0][1]*30)-int(road_size/2)+111
      draw_background()
      draw_road()
      goto(0,0)
      time_start=monotonic()
      race_time=0
  
    if keydown(0):
      left(2)
    if keydown(3):
      right(2)
    if keydown(KEY_OK) and check_collision():
      x=cos(radians(heading()))*int(road_size/25)
      y=sin(radians(heading()))*int(road_size/25)
      xMap-=x
      yMap+=y
      if not check_collision():
        xMap+=x
        yMap-=y
      else:
        hide_road(x,y)
        draw_road()
        goto(0,0)
    if keydown(KEY_BACKSPACE) and wait_to_pause>100:
      pause=True
      fill_rect(0,0,150,222,(60,60,60))
      fill_rect(150,0,1,222,(255,255,255))
      wait_to_pause=0
      race_time+=monotonic()-time_start
  
  wait_to_pause+=1
  
  while pause==True:
    draw_string("press CLEAR to",3,10,(255,255,255),(60,60,60))
    draw_string("continue",28,30,(255,255,255),(60,60,60))
    draw_string("press ONE to",13,70,(255,255,255),(60,60,60))
    draw_string("get a new race",3,90,(255,255,255),(60,60,60))
    draw_string("press TWO to",13,130,(255,255,255),(60,60,60))
    draw_string("start again",18,150,(255,255,255),(60,60,60))
    draw_string("your time:"+str(int(race_time))+"s",3,200,(255,255,255),(60,60,60))
    if keydown(KEY_BACKSPACE) and wait_to_pause>100:
      pause=False
      wait_to_pause=0
      draw_background()
      draw_road()
      goto(0,0)
      time_start=monotonic()
    wait_to_pause+=1

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.