weird_snake.py

Created by martin-garel-528

Created on February 23, 2025

3.47 KB

A sort of snake game using kandinsky


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

"""
0.1
16, 11
20
"""
DELAY = 0.1
FIELD_SIZE = 16, 11
TS = 20
INIT_OFFSET = 0, 0
SCREEN_SIZE = FIELD_SIZE[0] * TS, FIELD_SIZE[1] * TS
DIRECTIONS = {"left": (-1, 0),
              "right": (1, 0),
              "up": (0, -1),
              "down": (0, 1),
              "stop": (0, 0)}


class Dot:
  def __init__(s):
    s.rect = TS, TS
    s.x = randint(0, FIELD_SIZE[0]-1) * TS + INIT_OFFSET[0]
    s.y = randint(0, FIELD_SIZE[1]-1) * TS + INIT_OFFSET[1]
    s.color = 50, 50, 50
    s.draw()
    
  def draw(s):
    fill_rect(s.x,s.y,s.rect[0],s.rect[1],s.color)


class Player:
  def __init__(s, game):
    s.game = game
    s.rect = TS, TS
    s.x = FIELD_SIZE[0]//2 * TS + INIT_OFFSET[0]
    s.y = FIELD_SIZE[1]//2 * TS + INIT_OFFSET[1]
    s.time_now = monotonic()
    s.direction = "stop"

  def delta_time(s):
    if monotonic()-s.time_now > DELAY:
      s.time_now = monotonic()
      return True
    return False 

  def check_dot_collision(s):
    for dot in s.game.dots:
      if s.x == dot.x and s.y == dot.y:
        s.game.dots[s.game.dots.index(dot)] = Dot()
        s.game.points += 1
        s.check_dot_collision()
        break
      
  def check_wall_collision(s):
    if s.x < 0 + INIT_OFFSET[0] or s.x >= SCREEN_SIZE[0] + s.game.app.offset[0] or s.y < 0 + s.game.app.offset[1] or s.y >= SCREEN_SIZE[1] + INIT_OFFSET[1]:
      fill_rect(0,0,320,222,(255,255,255))
      s.game.new_game()
                 
  def move(s, direction):
    move_direction = DIRECTIONS[direction]
    if move_direction != (0, 0):
      fill_rect(s.x,s.y,s.rect[0],s.rect[1],(255,255,255))
    if move_direction[0]:
      s.x += move_direction[0] * TS
    else:
      s.y += move_direction[1] * TS
    s.draw()
    
  def update(s):
    if s.delta_time():
      s.move(s.direction)
      s.check_dot_collision()
      s.check_wall_collision()
    
  def draw(s):
    fill_rect(s.x,s.y,s.rect[0],s.rect[1],(255,20,20))


class Game:
  def __init__(s, app):
    s.app = app
    s.new_game()
    
  def new_game(s):
    sleep(0.2)
    s.player = Player(s)
    s.points  = 0
    fill_rect(0,0,320,222,(255,255,255))
    for x in range(FIELD_SIZE[0]+1):
      fill_rect(x*TS+INIT_OFFSET[0],0+INIT_OFFSET[1],1,SCREEN_SIZE[1],(0,0,0))
      sleep(0.05)
    for y in range(FIELD_SIZE[1]+1):
      fill_rect(0+INIT_OFFSET[0],y*TS+INIT_OFFSET[1],SCREEN_SIZE[0],1,(0,0,0))
      sleep(0.05)
    s.dots = []
    for _ in range(1):
      s.dots.append(Dot())
    sleep(0.5)
    
  def draw_grid(s):
    for x in range(FIELD_SIZE[0]+1):
      fill_rect(x*TS+INIT_OFFSET[0],0+INIT_OFFSET[1],1,SCREEN_SIZE[1],(0,0,0))
    for y in range(FIELD_SIZE[1]+1):
      fill_rect(0+INIT_OFFSET[0],y*TS+INIT_OFFSET[1],SCREEN_SIZE[0],1,(0,0,0))

  def draw(s):
    s.draw_grid()
    draw_string(str(s.points),2+INIT_OFFSET[0],2+INIT_OFFSET[1])
    
  def update(s):
    s.player.update()


class App:
  def __init__(s):
    s.offset = INIT_OFFSET
    s.game = Game(s)
  def check_input(s):    
    if keydown(KEY_LEFT):
        s.game.player.direction = "left"
    elif keydown(KEY_RIGHT):
        s.game.player.direction = "right"
    if keydown(KEY_UP):
        s.game.player.direction = "up"
    elif keydown(KEY_DOWN):
        s.game.player.direction = "down"
      
  def draw(s):
    s.game.draw()
    
  def update(s):
    s.game.update()
    
  def run(s):
    while 1:
      s.check_input()
      s.update()
      s.draw()
      
def start():
  app = App()
  app.run()

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.