draw_mountain.py

Created by wperez274

Created on February 01, 2023

2.32 KB


# drawing a line without Turtle() :-)
from math import *
from random import *
from random import randint as R
from kandinsky import *
from ion import *
from time import *

px=50
py=80
pw=2
ph=2
pc="black"

pen_is_down=False

def move_pixel():
  global px,py,pw,ph,pc
  
  if keydown(KEY_LEFT):
    px-=1
    fill_rect(0,40,160,20,bg)
    if pen_is_down==False:
      fill_rect(px+pw,py,1,ph,bg)
    
  if keydown(KEY_RIGHT):
    px+=1
    fill_rect(0,40,160,20,bg)
    if pen_is_down==False:
      fill_rect(px-1,py,1,ph,bg)
      
  if keydown(KEY_UP):
    py-=1
    fill_rect(0,40,160,20,bg)
    if pen_is_down==False:
      fill_rect(px,py+ph,pw,1,bg)
    
  if keydown(KEY_DOWN):
    py+=1
    fill_rect(0,40,160,20,bg)
    if pen_is_down==False:
      fill_rect(px,py-1,pw,1,bg)
      
    
    
    
  

def draw_pixel():
   global px,py,pw,ph,pc
   
   
   fill_rect(px,py,pw,ph,pc)

bg=(R(210,255),R(210,255),R(210,255))
mx=0
my=R(100,200)
mw=3
mh=2
mc=(0,0,0)

mountain_c=(R(0,155),R(0,155),0)

def print_data():
  global bg,mountain_c,px,py
  draw_string("Sky:"+str(bg),0,0,"black",bg)
  draw_string("Mountain:"+str(mountain_c),0,20,"black",bg)
  draw_string("px,py:"+str(px)+","+str(py),0,40,"blue",bg)

def pendown():
  global px,py,pw,ph,pc,pen_is_down
  pen_is_down=True  
  fill_rect(px,py,pw,ph,"black")


def draw_mountain():
  global mx,my,mw,mh,bg,mountain_c 
  mx+=1
  
  my+=randint(-2,2)
  fill_rect(mx,my,mw,mh,bg)
  fill_rect(mx,my,2,200,mountain_c)
  
  if mx>=322:
    mx=322
    
  

def redraw():
  global mx,my,mw,mh,bg,mountain_c
  bg=(R(200,255),R(200,255),R(200,255))
  mx=0
  my=R(100,200)
  mountain_c=(R(0,155),R(0,155),R(0,155))
  fill_rect(0,0,322,222,bg)
  mx+=1
  my+=randint(-2,2)
  fill_rect(mx,my,mw,mh,bg)
  fill_rect(mx,my,2,200,mountain_c)
  draw_string("Sky:"+str(bg),0,0,"black",bg)
  draw_string("Mountain:"+str(mountain_c),0,20,"black",bg)

  
  if mx>322:
    mx=322
  

  
fill_rect(0,0,322,222,bg)
while 1:
  fill_rect(mx,my,mw,mh,mc)
  
  draw_mountain()
  
  
  if keydown(KEY_OK):
    sleep(0.2)
    redraw()
  
  if keydown(KEY_BACKSPACE):
    pendown()

  
    
  draw_pixel()
  move_pixel()
  print_data()

  if keydown(KEY_BACKSPACE) and pen_is_down==False:
    pen_is_down=True
    sleep(0.2)
    

  if keydown(KEY_BACKSPACE) and pen_is_down==True:
    pen_is_down=False
    sleep(0.2)

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.