tron_maze_2035.py

Created by wperez274

Created on August 16, 2023

6.45 KB


from math import *
from random import *
from random import randint as R
from kandinsky import *
from kandinsky import fill_rect as F
from ion import *
from time import *

SW=322
SH=222

DEAD=False
GAMEOVER=False
r_c=(0,0,0)


bg=(R(0,50),R(0,50),R(0,50))

direct=["left","right","up","down","still"]
edir=direct[-1]
pdir=direct[-1]
level=1
score=0
pixels_traveled=0
lives=3

px=10
py=111
pw=2
ph=2
pc=(R(0,255),R(0,255),R(0,255))





ex=311
ey=111
ew=2
eh=2
ec=(255,)*3
e_timer=0


shelter_1_x=R(40,280)
shelter_1_y=R(40,180)
shelter_1_w=R(10,20)
shelter_1_h=R(10,20)
shelter_1_c=(255)*3



mybgcolors= [
(1, 54, 85), (38, 13, 23), 
(74, 71, 15), (61, 134, 119), 
(25, 47, 56), (201, 20, 44), 
(65, 20, 11), (76, 88, 137), 
(51, 31, 66), (73, 139, 174), 
(137, 24, 92), (63, 71, 65), 
(195, 133, 251), (109, 100, 82),
(235, 151, 157), (145, 106, 86), 
(129, 216, 240), (149, 193, 58), 
(85, 220, 174),(R(0,255),R(0,255),R(0,255))


]



level_meter=R(1200,2000)+level*100

def generate_level():
  global r_c,lives,GAMEOVER,level

  global electric_fence_x,electric_fence_y
  global electric_fence_w,electric_fence_h,electric_fence_c
  global level_meter,level_meter_c

  global shelter_1_x,shelter_1_y
  global shelter_1_w,shelter_1_h,shelter_1_c

  global pc,ec
  pc=(R(0,255),R(0,255),R(0,255))

  ec=(R(0,255),R(0,255),R(0,255))

  level_meter=R(1200,2000)+level*100

  electric_fence_x=R(60,260)
  electric_fence_y=R(60,160)
  electric_fence_w=R(20,80)
  electric_fence_h=R(20,80)
  electric_fence_c=(24,10,14)

    
  y=100
  bg=choice(mybgcolors)
  level_meter_c=(R(100,255),R(100,255),R(100,255))
  

  F(0,0,322,222,bg)
  F(0,0,322,16,(0,0,0))

  
  F(0,10+level,322,2,(0,0,0))
  F(0,10+level-2,322,2,(255,0,0))

  F(0,212-level,322,2,(0,0,0))
  F(0,212-level+2,322,2,(255,0,0))
  
  F(electric_fence_x,electric_fence_y,electric_fence_w,electric_fence_h,electric_fence_c)

  F(electric_fence_x,electric_fence_y,2,electric_fence_h,(0,0,0))
  F(electric_fence_x+electric_fence_w-2,electric_fence_y,2,electric_fence_h,(0,0,0))
  F(electric_fence_x,electric_fence_y,electric_fence_w,2,(0,0,0))
  F(electric_fence_x,electric_fence_y+electric_fence_h-2,electric_fence_w,2,(0,0,0))
  
  
def grid():
  grill_gap=20
  grill_line_c=(0,0,150)


#verticle lines, top to bottom
  for i in range(0,SW,grill_gap):
    F(i,0,1,SH,grill_line_c)
#horizontal lines, left to right
  for j in range(0,SH,grill_gap):
    F(0,j,SW,1,grill_line_c)

  

def main():
  
  global level_meter,level
  
  shelter_1_x=R(10,310)
  shelter_1_y=R(20,200)

  lives=3
  pixels_traveled=0
  

  r_c=choice([(255,0,0),(0,255,0),(0,0,255)])
  GAMEOVER=False
  DEAD=False
  bg=(R(0,255),R(0,255),R(0,255))


  direct=["left","right","up","down","still"]
  pdir=direct[-1]
  edir=direct[-1]

  level=0
  score=0
  px=10
  py=111
  pw=2
  ph=2
  pc=(0,255,255)

  ex=311
  ey=111
  ew=3
  eh=3
  ec=(255,)*3

  

  
  generate_level()


  
  while not keydown(KEY_EXE) and lives>0:
    
  

    
    F(px,py,pw,ph,pc)
    F(px+1,py+1,pw-1,ph-1,(0,0,255))
    
    F(ex,ey,ew,eh,ec)
    F(ex+1,ey+1,ew-1,eh-1,(0,)*3)
    
    
  
    
    if keydown(KEY_OK):
      sleep(0.012)
      
      
    if pdir==direct[0]:
      sleep(0.01)
      px-=1
    if pdir==direct[1]:
      sleep(0.01)
      px+=1
    if pdir==direct[2]:
      sleep(0.01)
      py-=1

    if pdir==direct[3]:
      sleep(0.01)
      py+=1
      

    if edir==direct[0]:
      sleep(0.01)
      ex-=1
    if edir==direct[1]:
      sleep(0.01)
      ex+=1
    if edir==direct[2]:
      sleep(0.01)
      ey-=1

    if edir==direct[3]:
      sleep(0.01)
      ey+=1
        


    if keydown(KEY_LEFT):
      pdir=direct[0]
      score+=R(1,2)
      
    
    if keydown(KEY_RIGHT):
      pdir=direct[1]
      score+=R(1,2)

    
    if keydown(KEY_UP):
      pdir=direct[2]
      score+=R(1,2)

    
    if keydown(KEY_DOWN):
      pdir=direct[3]
      score+=R(1,2)

    if pdir!=direct[-1]:
      pixels_traveled+=1
      level_meter-=1
     
      

    if pdir==direct[0]:
      edir=direct[1]

    if pdir==direct[1]:
      edir=direct[0]

    if pdir==direct[2]:
      edir=direct[3]

    if pdir==direct[3]:
      edir=direct[2]

    if get_pixel(px+pw,py)==(0,255,255) and pdir==direct[1]: 
      DEAD=True
    if get_pixel(px,py+ph)==(0,255,255) and pdir==direct[3]: 
      DEAD=True
    if get_pixel(px,py-1)==(0,255,255) and pdir==direct[2]: 
      DEAD=True
    if get_pixel(px-1,py)==(0,255,255) and pdir==direct[0]: 
      DEAD=True

      
#    if px<300:
    if get_pixel(px+pw,py)==(0,0,0) and pdir==direct[1]: 
      DEAD=True
    if get_pixel(px,py+ph)==(0,0,0) and pdir==direct[3]: 
      DEAD=True
    if get_pixel(px,py-1)==(0,0,0) and pdir==direct[2]: 
      DEAD=True
    if get_pixel(px-1,py)==(0,0,0) and pdir==direct[0]: 
      DEAD=True


    if ey<1:
      ey=1
      edir=direct[R(1,2)]
      
    if ey>222:
      ey=222
      edir=direct[R(1,2)]

    if ex<0:
      ex=0
      edir=direct[R(2,3)]

    if ex>322:
      ex=322
      edir=direct[R(2,3)]




    if DEAD:
      F(0,0,322,222,(0,0,0))
      lives-=1
      DEAD=False
#      pixels_traveled=0
    
      sleep(0.5)
      draw_string("Lives: "+str(int(lives)),100,100,(R(10,255),R(10,255),R(0,255)),(0,0,0))  
      sleep(1)
      draw_string("Lives: "+str(lives),100,100,bg,bg)
      
      px=10
      py=111
      ex=311
      ey=111
      edir=direct[-1]
      pdir=direct[-1]

      generate_level()    

  

    F(electric_fence_x+4,electric_fence_y+4,electric_fence_w-8,electric_fence_h-8,(R(0,255),0,0))


#    F(shelter_1_x,shelter_1_y,40,20,(R(0,10),R(100,150),R(100,150)))

    F(shelter_1_x+2,shelter_1_y+2,shelter_1_w-4,shelter_1_h-4,(R(20,100),R(100,150),R(100,150)))




    F(100,2,level_meter//8,5,level_meter_c)
    F(100+level_meter//8+1,2,1,5,bg)

      
    if pixels_traveled>level_meter<1:
      pixels_traveled=0
      px=10
      py=111
      ex=311
      ey=111
      edir=direct[-1]
      level+=1
      lives+=0.33
      pdir=direct[-1]

      F(0,0,322,222,(100,255,255))
      sleep(0.5)
      draw_string("COMPLETE",100,100)
      sleep(1)
      draw_string("COMPLETE",100,100,(100,255,255),(100,255,255))
      score+=pixels_traveled//5
      generate_level()
      






  if lives<1:
    GAMEOVER=True
    
  if GAMEOVER:      

    F(0,0,322,222,(0,)*3)  
    draw_string("GAME OVER",100,80,(R(10,255),R(10,255),R(0,255)),(0,0,0))    
    sleep(1)
    draw_string("SCORE: "+str(score),100,120,(255,)*3,(0,0,0))
    sleep(1)
  

  
  sleep(2)
  draw_string("THANK FOR PLAYING!",100,200,"orange",(0,0,0))


    

main()

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.