import time
import random
import RPi.GPIO as GPIO
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
serial = i2c(port=1, address=0x3C)
device = ssd1306(serial, width=128, height=64)
zidx = [128 + (i + 1) * 32 for i in range(4)]
prazan = [random.randint(8, 32) for _ in range(4)]
razmak = 32
sirinaProlaza = 30
score = 0
stis = 0
fx = 30.00
fy = 22.00
smjer = 0
trenutno = 0
igra = 0
frame = 0
sviraj = 0
ton = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def setup():
print("\n\n")
device.show()
GPIO.output(2, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
for i in range(4):
zidx[i] = 128 + (i + 1) * razmak
prazan[i] = random.randint(8, 32)
device.contrast(0)
def loop():
global zidx, prazan, score, stis, fx, fy, smjer, trenutno, igra, frame, sviraj, ton
device.clear()
if igra == 0:
with canvas(device) as draw:
draw.text((0, 4), "Flappy ", fill="white")
draw.bitmap((0, 0), pozadina, fill="white")
draw.bitmap((20, 32), ptica, fill="white")
draw.text((0, 44), "press to start", fill="white")
if GPIO.input(4) == 0:
igra = 1
if igra == 1:
with canvas(device) as draw:
draw.text((3, 0), str(score), fill="white")
if GPIO.input(4) == 0:
if stis == 0:
trenutno = int(time.time() * 1000)
smjer = 1
sviraj = 1
stis = 1
ton = int(time.time() * 1000)
else:
stis = 0
for j in range(4):
device.rectangle((zidx[j], 0, 6, 64), outline="white", fill="white")
device.rectangle((zidx[j], prazan[j], 6, sirinaProlaza), outline="black", fill="black")
device.bitmap((fx, fy), ptica, fill="white")
for j in range(4):
zidx[j] = zidx[j] - 0.01
if zidx[j] < -7:
score = score + 1
GPIO.output(23, GPIO.HIGH)
prazan[j] = random.randint(8, 32)
zidx[j] = 128
if (trenutno + 185) < int(time.time() * 1000):
smjer = 0
if (ton + 40) < int(time.time() * 1000):
sviraj = 0
if smjer == 0:
fy = fy + 0.01
else:
fy = fy - 0.03
if sviraj == 1:
GPIO.output(23, GPIO.HIGH)
else:
GPIO.output(23, GPIO.LOW)
if fy > 63 or fy < 0:
igra = 0
fy = 22
score = 0
GPIO.output(23, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(23, GPIO.LOW)
for i in range(4):
zidx[i] = 128 + (i + 1) * razmak
prazan[i] = random.randint(8, 32)
for m in range(4):
if zidx[m] <= fx + 7 and fx + 7 <= zidx[m] + 6:
if fy < prazan[m] or fy + 8 > prazan[m] + sirinaProlaza:
igra = 0
fy = 22
score = 0
GPIO.output(23, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(23, GPIO.LOW)
for i in range(4):
zidx[i] = 128 + (i + 1) * razmak
prazan[i] = random.randint(8, 32)
device.rectangle((0, 0, 128, 64), outline="white")
device.show()
if __name__ == "__main__":
setup()
try:
while True:
loop()
except KeyboardInterrupt:
GPIO.cleanup()