snake.py

Created by schraf

Created on July 23, 2023

1.04 KB

Explication en vidéo ici


from time import *
from kandinsky import *
from random import *
from ion import *

def go(v = 1):
  score = 0
  dx,dy = 0,1
  vert,rouge = (0,255,0),(255,0,0)
  s = [[160,110]]
  food = True
  pt = monotonic()
  while True:
    ct = monotonic()
    dt = ct-pt
    if food:
      fx = 10 * randint(0,31)
      fy = 10 * randint(0,21)
      food = False
    fill_rect(fx,fy,10,10,rouge)
    if keydown(KEY_UP): dx,dy = 0,-1
    if keydown(KEY_DOWN) : dx,dy = 0,1
    if keydown(KEY_LEFT): dx,dy = -1,0
    if keydown(KEY_RIGHT): dx,dy = 1,0
    if dt>.2-.02*v:
      pt = monotonic()
      x = s[0][0] + 10*dx
      y = s[0][1] + 10*dy
      if x<0 or x>310 or y<0 or y>210 or get_pixel(x,y)==vert:
        draw_string("oups !!",5,10)
        fill_rect(x,y,10,10,rouge)
        return score
      s.insert(0,[x,y])
      if get_pixel(x,y)!=rouge:
        q = s.pop()
        fill_rect(q[0],q[1],10,10,(248,255,248))
      else:
        score += 1
        draw_string(str(score),5,10)
        food=True
      fill_rect(s[0][0],s[0][1],10,10,vert)

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.