snake5.py

Created by wperez274

Created on March 26, 2023

6.21 KB


#simple calc window
from math import *
from random import *
from random import randint as R
from kandinsky import *
from ion import *
from ion import *
from time import *







bg=(R(0,255),R(0,255),R(0,255))
    
playerMoving=False

time=9
points=0
level=1

text_x=0
text_y=0

level=1
travel=0
guard_default=10

guard_s=1

guard_x=R(160,310)
guard_y=R(22,60)
guard_w=guard_h=guard_default

guard_c=(0,0,0)
guard_outline_c=(R(0,255),R(0,255),R(0,255))


food_x=R(120,300)
food_y=R(20,190)
food_w=food_h=R(5,10)
food_c=(R(0,255),R(0,255),R(0,255))

food_out='yellow'


lives=3
energy=30

b=0
x=10
y=160
w=10
h=15
c=(R(155,255),R(155,255),R(155,255))




spike_s="#"
spike_x=R(40,300)
spike_y=R(50,250)
spike_w=9
spike_h=16
spike_c=(R(50,250),R(50,250),R(50,250))



def drawEyes():
  global x,y
  fill_rect(x+1,y+2,w-2,4,'white')
  
  fill_rect(x+3,y+3,2,3,'black')
  fill_rect(x+6,y+3,2,3,'black')
  
  





def drawFood():
  global food_x,food_y,food_w,food_h,food_c,food_out
  
  fill_rect(food_x,food_y,food_w,food_h,food_c)
  
  fill_rect(food_x,food_y,1,food_h,food_out)
  fill_rect(food_x+food_w-1,food_y,1,food_h,food_out)
  fill_rect(food_x,food_y,food_w,1,food_out)
  fill_rect(food_x,food_y+food_h-1,food_w,1,food_out)
  
#eraser
  fill_rect(food_x-2,food_y,2,food_h,bg)
  fill_rect(food_x+food_w+1,food_y,2,food_h,bg)
  fill_rect(food_x,food_y-2,food_w,2,bg)
  fill_rect(food_x,food_y+food_h+1,food_w,2,bg)


  
  
def moveguard():
  global guard_x,guard_y
  guard_x-=R(1,4)
  
  
def drawGuard():
  global guard_x,guard_y,guard_w,guard_h,guard_c,guard_outline_c,bg

  fill_rect(guard_x,guard_y,guard_w,guard_h,guard_c)  
  fill_rect(guard_x+guard_w+1,guard_y,2,guard_h,bg)  

  fill_rect(guard_x,guard_y,1,guard_h,guard_outline_c)  
  fill_rect(guard_x+guard_w-1,guard_y,1,guard_h,guard_outline_c)  
  fill_rect(guard_x,guard_y,guard_w,1,guard_outline_c)  
  fill_rect(guard_x,guard_y+guard_h-1,guard_w,1,guard_outline_c)  

  
def drawPlayer():

  global x,y,w,h,c,outline_c,drawEyes

  outline_c=(0,0,0)
  fill_rect(x,y,w,h,c)
  #left  
  fill_rect(x,y,2,h,outline_c)
#right
  fill_rect(x+w-1,y,1,h,outline_c)
#top
  fill_rect(x,y,w,1,outline_c)
#bottom
  fill_rect(x,y+h-1,w,1,outline_c)


  drawEyes()




def erasePlayer():
  global x,y,w,h,bg
  
  fill_rect(x-2,y,2,h,bg)
  fill_rect(x+w+2,y,2,h,bg)
  fill_rect(x,y-2,w,2,bg)
  fill_rect(x,y+h+2,w,2,bg)




fill_rect(0,0,322,222,bg)

drawPlayer()
drawGuard()

while lives>0:
  drawPlayer()
  drawFood()
  drawGuard()


  
    
  if keydown(KEY_LEFT):
    fill_rect(x+w+1,y,1,h,bg)
    fill_rect(x-1,y,1,h,bg)
    fill_rect(x,y+h+1,w,1,bg)
    fill_rect(x,y-1,w,1,bg)

    x-=2
    erasePlayer()
#    drawPlayer()


  if keydown(KEY_RIGHT):
    fill_rect(x+w+1,y,1,h,bg)
    fill_rect(x-1,y,1,h,bg)
    fill_rect(x,y+h+1,w,1,bg)
    fill_rect(x,y-1,w,1,bg)
    x+=2
    drawPlayer()

  if keydown(KEY_UP):
    fill_rect(x,y+h+3,w,1,bg)
    y-=3
    drawPlayer()
  



  if keydown(KEY_DOWN):
    fill_rect(x,y-3,w,1,bg)
    y+=3
    drawPlayer()


  if x<-20:
    x=-20
    
  if x+w>=350:
    x=350-w

  if y<=-10:
    y=-10
  if y+h>=232:
    y=232-h
  
          
  if x+w>=food_x and x<=food_x+food_w and y+h>=food_y and y<=food_y+food_h:
    sleep(0.2)
    fill_rect(0,0,322,222,"cyan")
    fill_rect(0,0,322,50,(0,0,0))
    fill_rect(0,47,322,3,"blue")
    fill_rect(0,222-53,322,3,"blue")
    
    fill_rect(0,222-50,322,50,(0,0,0))
    
    draw_string("Level cleared!!",70,70)
    points+=round(time)*R(20,100)
    sleep(0.05)
    lives+=time/6

    draw_string("Life: "+str(int(lives)),80,140,"blue")
  
    sleep(2)  
    bg=(R(0,255),R(0,255),R(0,255))  
    fill_rect(0,0,322,222,bg)
    guard_x=R(160,300)
    guard_y=R(10,200)


    
    guard_w+=1
    guard_h+=1
          
    food_x=R(120,300)
    food_y=R(20,190)

    food_w=food_h=R(6,12)

    x=10
    y=150
    
    
    fill_rect(x,y,w,h,"cyan")
    
    level+=1
    time=9

  food_x-=R(-2,2)
  food_y+=R(-2,2)
  
  if food_x<0:
    food_x=0

  if food_x>317:
    food_x=317
  if food_y<20:
    food_y=20
  if food_y>200:
    food_y=200
  

  
  
  
  if keydown(KEY_RIGHT) or keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_DOWN):
    if guard_x>x:
      guard_x-=R(0,3)
  
  if keydown(KEY_RIGHT) or keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_DOWN):
    if guard_x<x:
      guard_x+=R(0,3)
  
  if keydown(KEY_RIGHT) or keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_DOWN):
    if guard_y>y:
      guard_y-=R(0,3)
  
  if keydown(KEY_RIGHT) or keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_DOWN):
    if guard_y<y:
      guard_y+=R(0,3)
  
  
#player hit/energy loss high
  
  if x+w>=guard_x and x<=guard_x+guard_w and y+h>=guard_y and y<=guard_y+guard_h:
    fill_rect(x,y,w,h,"red")
    
    sleep(0.08)
    fill_rect(0,0,322,222,"red")
    sleep(0.02)
    fill_rect(0,0,322,222,bg)
    
    energy-=3
    guard_w=guard_h=guard_default

    
    
  if energy<0 or time<0:
    sleep(0.2)
    lives-=1
    energy=20
    fill_rect(0,0,322,222,(0,0,0))
    draw_string("lives: "+str(int(lives)),80,100,'white','black')
    sleep(0.12)
    draw_string("Travel: "+str(int(travel/10))+" feet.",80,140,'orange','black')
    
    sleep(1.5)
    fill_rect(0,0,322,222,bg)
    guard_x=R(150,300)
    guard_y=R(15,200)
    food_x=R(150,300)
    food_y=R(15,200)
    time=9
    
  
  fill_rect(guard_x,guard_y+guard_h+1,1,1,(0,0,0))
  
  
  if playerMoving:
    fill_rect(guard_x+int(guard_w/4),guard_y+int(guard_h/3),int(guard_w/2),int(guard_w/5),"red")
  
  if playerMoving==False:
    fill_rect(guard_x+int(guard_w/4),guard_y+int(guard_h/3),int(guard_w/2),int(guard_w/5),"white")
  
  
  draw_string("Time: "+str(round(time)),240,1,"cyan","black")
  draw_string("SCORE: "+str(points),2,1,"white","black")

  
  time-=0.05
  

  if keydown(KEY_LEFT) or keydown(KEY_RIGHT) or keydown(KEY_UP) or keydown(KEY_DOWN):
    playerMoving=True

  
  else:
    playerMoving=False




  if time<1:
    time=1
    guard_w+=R(0,1)
    guard_h+=R(0,1)
    
  if guard_w>35:
    guard_w=35
    
  if guard_h>35:
    guard_h=35
    

  if playerMoving:
    travel+=1
    
  
    


sleep(0.2)
fill_rect(0,0,322,222,(0,0,0))
draw_string("GAME OVER",50,100,"red",(0,0,0))
draw_string("SCORE: "+str(points),50,140,"white",(0,0,0))
draw_string("Questions?  email:  wperez274@gmail.com",50,170)

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.