from machine import Pin
from time import sleep
import lcd
import random
lcd_rs = Pin(12,Pin.OUT)
lcd_e = Pin(11,Pin.OUT)
lcd_d4 = Pin(10,Pin.OUT)
lcd_d5 = Pin(9,Pin.OUT)
lcd_d6 = Pin(8,Pin.OUT)
lcd_d7 = Pin(7,Pin.OUT)
button = Pin(15,Pin.IN)
lcd.init(lcd_rs,lcd_e,lcd_d4,lcd_d5,lcd_d6,lcd_d7) #initialize the lcd( prepare it for the commands)
lcd.clear() #clear screen
lcd.load_custom_chars() #load the character of obstacle and the player
#player coordinates
player_col = 1
player_row = 1
previous_button = 0
#obstacle coordinates
obstacle_col = 15
obstacle_row = random.randint(0,1)
while True:
#Check if the button is pressed or not
current_button = button.value()
#if button is pressed move player up or down
if current_button == 1 and previous_button == 0:
if player_row == 1:
player_row = 0
else:
player_row = 1
previous_button = current_button
lcd.clear()
#print th characters
lcd.setCursor(player_col,player_row)
lcd.print(chr(0))
lcd.setCursor(obstacle_col,obstacle_row)
lcd.print(chr(1))
#check for collision ( when obstacle hits the player)
if obstacle_col == player_col and obstacle_row == player_row:
lcd.setCursor(0,0)
lcd.print(" GAME OVER ")
lcd.setCursor(0,1)
lcd.print(" Restarting ")
sleep(2)
player_col = 1
player_row = 1
obstacle_col = 15
obstacle_row = random.randint(0,1)
lcd.clear()
continue
#mvoe the obstacle to left (15 --> 0)
obstacle_col = obstacle_col-1
if obstacle_col < 0:
obstacle_col = 15
obstacle_row = random.randint(0,1)
sleep(0.15)