from machine import Pin, I2C
import ssd1306
import time
################################################################################
#ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_hight = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_hight, i2c)
#-----------------------------------------------------------------------------#
string = "00E"
string_pics = len(string) * 6 + len(string)-1
gameover = "Game Over"
gameover_pics = len(gameover) * 6 + len(gameover)-1
#---------------------------------strings-------------------------------------#
bottem_wall = 57
top_wall = -1
left_wall = -1
right_wall = 128 - string_pics
#----------------------------------walls-------------------------------------#
x_cord = 0
y_cord = 28
#start cords
x = 0
y = 1
gen_dirction = 0
#used for determening dirction
speed = 2
y_direction = speed
x_direction = speed
#x and y vectors
#---------------------------------movement-----------------------------------#
switch = Pin(4, Pin.IN, Pin.PULL_UP)
switch2 = Pin(2, Pin.IN, Pin.PULL_UP)
switch_memory = 0
switch2_memory = 0
#----------------------------------switch------------------------------------#
################################################################################
while True:
if switch.value() == switch_memory: ###
if switch_memory == 0:
switch_memory = 1
elif switch_memory == 1:
switch_memory = 0
#change in switch
gen_dirction += 1
x_y_direction = gen_dirction % 2
if x_y_direction == x:
if y_direction == speed*(-1):
x_direction = speed*(-1)
elif y_direction == speed:
x_direction = speed
elif x_y_direction == y:
if x_direction == speed:
y_direction = speed*(-1)
elif x_direction == speed*(-1):
y_direction = speed
#turns left if switch from pin 4 has changed position
#-----------------------------------------------------------------------------#
if switch2.value() == switch2_memory:
if switch2_memory == 0:
switch2_memory = 1
elif switch2_memory == 1:
switch2_memory = 0
#change in switch
gen_dirction += 1
x_y_direction = gen_dirction % 2
if x_y_direction == x:
if y_direction == speed:
x_direction = speed*(-1)
elif y_direction == speed*(-1):
x_direction = speed
elif x_y_direction == y:
if x_direction == speed:
y_direction = speed
elif x_direction == speed*(-1):
y_direction = speed*(-1)
#turns right if switch from pin 2 has changed position
################################################################################
x_y_direction = gen_dirction % 2
if x_y_direction == x:
x_cord += x_direction
elif x_y_direction == y:
y_cord += y_direction
#determens next cords and if it is moving on the x or y axis
#-----------------------------------------------------------------------------#
oled.text(string,x_cord ,y_cord)
oled.show()
#shows text
################################################################################
gameover_x_cord = int((128-gameover_pics)/2)
if x_cord >= right_wall or x_cord <= left_wall: ###
oled.fill(0)
oled.text(gameover, gameover_x_cord ,28)
oled.show()
break
if y_cord <= top_wall or y_cord >= bottem_wall: ###
oled.fill(0)
oled.text(gameover, gameover_x_cord ,28)
oled.show()
break
#if you touch a wall you lose
#-----------------------------------------------------------------------------#
time.sleep(0.01)
#waits a moment
oled.fill(0)
#clears board
################################################################################