I'm here and I'm starting using MicroPython and really want to enter in this community. Now I'm testing some code in the simulator called Unicorn at the official site and is answer me a very strange error, here is the error message
Traceback (most recent call last):
File "<stdin>", line 96, in <module>
AttributeError: 'Switch' object has no attribute 'callback'
I don't know which version is using but it seems to me really strange. Here is the code if some one can say me if it's wrong.
Code: Select all
# LET'S SINK THE FLOAT
import machine
import pyb
import framebuf
import time
import math
#Literals representing screen size
SCREEN_WIDTH = 64
SCREEN_HEIGHT = 32
torpedo_shooted = False
cannon_angle = 0
slope = 0.0
#Same function as map() in arduino but in Python
def arduino_map(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
#Declare classes for diferent elements in the game
class Entity:
def __init__(self, x, y, w, h, vx, vy):
self.x = x;
self.y = y;
self.w = w;
self.h = h;
self.vx = vx;
self.vy = vy;
def draw(self, fbuf):
fbuf.fill_rect(int(self.x), int(self.y), self.w, self.h, 1)
class Player(Entity):
pass
class Torpedo(Entity):
def update(self):
print(torpedo_shooted)
torpedo_shooted = False
class Boat(Entity):
def update(self):
self.x += self.vx;
if (self.x <= 0):
self.x = 0
self.vx = -self.vx
if (self.x >= SCREEN_WIDTH - self.w):
self.x = SCREEN_WIDTH - self.w
self.vx = -self.vx
class Portaviones(Boat):
pass
class Fragata(Boat):
pass
class Corbeta(Boat):
pass
#Declare boats types, cannon and torpedo
torpedo = Torpedo(31, 31, 1, 1, 0, 0)
player = Player(31, 31, 1, 1, 0, 0)
portaaviones = Portaviones(30, 1, 10, 1, 1, 0)
fragata = Fragata(30, 1, 5, 1, -2, 0)
corbeta = Corbeta(30, 1, 2, 1, 3, 0)
#Declare servo motor for cannon accuracy
servo = pyb.Servo(1)
#Declare torpedo hit LED
y12 = machine.Pin('Y12')
#Declare ADC slider input
y4 = machine.Pin('Y4')
adc = pyb.ADC(y4)
#Declare I2C screen
scl = machine.Pin('X9')
sda = machine.Pin('X10')
i2c = machine.I2C(scl=scl, sda=sda)
#Declare screen buffer
fbuf = framebuf.FrameBuffer(bytearray(SCREEN_WIDTH * SCREEN_HEIGHT // 8), SCREEN_WIDTH, SCREEN_HEIGHT, framebuf.MONO_HLSB)
def update_cannon():
adc_read = adc.read()
cannon_angle = arduino_map(adc_read, 0, 255, -90, 90)
servo.angle(int(cannon_angle), 500)
time.sleep(1)
def update_torpedo():
torpedo_shooted = True
#Set the callback when shoot a torpedo
trigger.callback(update_torpedo)
#Main loop
while True:
update_cannon()
fbuf.fill(0)
if torpedo_shooted:
#slope = math.tan(cannon_angle)
torpedo.update()
#torpedo.draw(fbuf)
player.draw(fbuf)
i2c.writeto(8, fbuf)
time.sleep_ms(50)
fbuf.fill(0)
fbuf.text('GAME', 15, 8)
fbuf.text('OVER', 15, 18)
i2c.writeto(8, fbuf)