top of page
Search

Assignment 2

  • ncampanelli0
  • Sep 10, 2019
  • 1 min read

I wrote a magic eight ball program in python using the micro-python library. The program works by shaking the device to display a yes, no or maybe answer which is denoted by the microbit displaying shapes on the led display and lighting up leds that were wired on the breadboard. Pressing either button will reset the program, in short a shutdown button. The random number to determine yes, no or maybe is achieved with use of importing the random library into my project (random.randint).


Problems I had with making this program mostly boiled down to trying to attach the shake check to a variable (did not work, in the end I replaced the variable name in one of the functions directly with the built-in function designed to check for shaking) and using else if instead of elif (force of habit).



code:

from microbit import *

from random import *


"""

pin 0 = no

pin 1 = yes

pin 0 + 1 = maybe

"""


#global vars

#shakeCheck = acceleromter.is_gesture(shake)

answer = -1

picYes = Image("00000:00009:00090:90900:09000:")

picNo = Image("90009:09090:00900:09090:90009:")

picMaybe = Image("00000:00000:99999:00000:00000:")


def getAnswer():

while accelromter.is_gesture(shake):

answer = random.randrange(0,2)


def eBall():

if answer == 0:

pin0.write_digital(1)

pin1.write_digital(0)

picNo

elif answer == 1:

pin0.write_digital(0)

pin1.write_digital(1)

picYes

elif answer == 2:

pin0.write_digital(1)

pin1.write_digital(1)

picMaybe

else:

pin0.write_digital(0)

pin1.write_digital(0)


def resetAnswer():

if button_a.is_pressed() or button_b.is_pressed():

answer = -1


def main():

while True:

getAnswer()

eBall()

resetAnswer()


if __name__ == "__main__":

main()

 
 
 

Recent Posts

See All
Final assignment (trivia project)

How I came to this idea for a project was simple. Back when I was in high school I made a trivia style game. This game was written for...

 
 
 
Assignment 9 (drawing program)

I made a drawing program with multiple colors that can be selected (with black being used as the eraser color as the background is...

 
 
 
Assignment 8 (bug fixes)

change log: 1. Revised collision for ball to bounce off paddles properly (instead of requiring perfect pixel alignment with a sweet spot,...

 
 
 

Comments


©2019 by creative coding art and games. Proudly created with Wix.com

bottom of page