programming simple games with a raspberry pi and

26
Kellyn Pot’Vin-Gorman, Consulting Member of Technical Staff, Oracle Programming Simple Games with a Raspberry Pi and Using Python

Upload: kellyn-potvin-gorman

Post on 09-Jan-2017

724 views

Category:

Technology


0 download

TRANSCRIPT

Kellyn Pot’Vin-Gorman, Consulting Member of Technical Staff, Oracle

Programming Simple Games with a Raspberry Pi and Using

Python

Today’s GoalEither on a Raspberry Pi or Oracle

VirtualBox, (Vbox) Raspberry Pi image, code two simulated games of chance…

Who Will Use Vbox?Let Kellyn know if you need a copy of the image

to import onto your own PC. She will offer you a USB drive with the 500G OVA file.

The image requires 1G of free memory to use the image.

You will need to “Import” the appliance onto your pc.

The user name=rpi and the password=passwordRoot password=password

Open up a terminal once you’ve logged in.

Connect and Power Up Raspberry Pi’sIf you are new to Raspberry Pi…

You should also have a micro or SD card for storageA HDMI monitor A USB mouse and keyboardAlready performed the setup. If you haven’t,

please talk to Kellyn, she has the OS setup and all appropriate libraries updated on a secondary card, (both SD or Micro SD format)

Once everything is connected, attach the micro USB power and plug it in to power up the OS.

Open up a terminal window!

Basics of PythonWriting a Python Script, (simple Python

Coding) consists of the following:1. Import Modules- which are programs

already stored in the system that can be used, saving the programmer time and allows them to re-use code already available.

2. Define Functions-3. Build Code into each Function.4. Execute Functions in order they are to be

performed in.

Writing a Program/Script is Like OutliningWhat are your Requirements?List them out, naming them as Functions

and DEFINING each. Entering the command PASS after each one to “bypass” the function until you are ready to code.

Place them in the order they will executeCode LAST, filling in each section as you

go, testing through each step.

A Python Script Outline#Our modules will be put in as we go along at the top def set_board():  pass def start_motor():  pass def forward_motor(): pass def backward_motor(): pass def motor_cleanup():  pass#This set of commands executes our functions in the correct order.set_board()start_motor()backward_motor()motor_cleanup()

Fill it inimport timeimport gpio

def set_board():  pass GPIO.setmode(GPIO.BOARD) #Use Breadboard

Motor1A = 16 #Location of Motor pins Motor1B = 18 def start_motor():  pass

Coding Start and Forward of Motorimport timeimport GPIO def set_board():  GPIO.setmode(GPIO.BOARD)  Motor1A = 16 Motor1B = 18 def start_motor(): pass  GPIO.setup(Motor1A,GPIO.OUT) GPIO.setup(Motor1B,GPIO.OUT) def forward_motor(): pass print "Going forwards" GPIO.output(Motor1A,GPIO.HIGH) GPIO.output(Motor1B,GPIO.LOW) time.sleep(2)

Final Steps Scriptingimport timeimport GPIO def set_board():  GPIO.setmode(GPIO.BOARD)  Motor1A = 16 Motor1B = 18 def start_motor():  GPIO.setup(Motor1A,GPIO.OUT) GPIO.setup(Motor1B,GPIO.OUT) def forward_motor(): print "Going forwards" GPIO.output(Motor1A,GPIO.HIGH) GPIO.output(Motor1B,GPIO.LOW) time.sleep(2) def backward_motor(): pass print "Going backwards" GPIO.output(Motor1A,GPIO.LOW) GPIO.output(Motor1B,GPIO.HIGH) time.sleep(2)  print "Now stop" GPIO.output(Motor1B,GPIO.LOW) def motor_cleanup():  pass GPIO.cleanup()

set_board()start_motor()backward_motor()motor_cleanup()

Time to CodeLog into Your Raspberry Pi and open up a

Terminal Window.Ensure that the screen background and

text is visible and easy to read, if not, ask Kellyn how to update the preferences or update it to your liking.

Use an text editor, (vi, nano, etc.) to start a script.> vi dice_gm.py

To Create a Dice GameDecide what your requirements are.Create an outline of our simple scriptFill in the code for the function and add any

modules that will need to be imported at the top of the script.

Test and verify that the code works as expected.

The Dice Game RequirementsImport the RANDOM module.Will “roll” the dice 2 timesWill use two dice, with random calls of 1-6.Will output the first dice ‘+’ the second

dice.

Finished Scriptimport timeimport GPIO def set_board():  GPIO.setmode(GPIO.BOARD)  Motor1A = 16 Motor1B = 18 def start_motor():  GPIO.setup(Motor1A,GPIO.OUT) GPIO.setup(Motor1B,GPIO.OUT) def forward_motor(): print "Going forwards" GPIO.output(Motor1A,GPIO.HIGH) GPIO.output(Motor1B,GPIO.LOW) time.sleep(2) def backward_motor(): print "Going backwards" GPIO.output(Motor1A,GPIO.LOW) GPIO.output(Motor1B,GPIO.HIGH) time.sleep(2)  print "Now stop" GPIO.output(Motor1B,GPIO.LOW) def motor_cleanup():  GPIO.cleanup()

set_board()start_motor()backward_motor()motor_cleanup()

Code Outline#Dice Game import random def dice_role(): pass

dice_role()

Comments about what we are codingImport random module we’ll be using with this script.

Define your function name and put in pass as a “placeholder”

End with Function we defined to Execute

Build out first steps of function#Dice Gameimport random

def dice_role(): for x in range(1, 2): dice_1 = random.randint(1, 6) dice_2 = random.randint(1, 6)

dice_role()

We let the program know we are coding the function by indenting, using four spaces, then indenting the subfunction step four more.

Finished Script#Dice Gameimport random

def dice_roll(): for x in range(1, 2): dice_1 = random.randint(1, 6) dice_2 = random.randint(1, 6) total = dice_1 + dice_2 if total == 7: print(‘Seven Thrown!’) if total == 11: print(‘Eleven Thrown!’) if dice_1 == dice_2: print(‘Doubles!!’)

dice_roll()

Now take the random integers produced in our “dice roll”, total them and provide feedback and the rolls!

Execute The Program/Script!Save the script: <Esc> :wq! or save and

exit, etc.

Execute the script to see the results of your work:

> sudo python3 dice_gm.py

Pass the PigsFrom Dice to

“Pigs”We will want to

show the roll and display the points that would be gained by that roll. We’ll immediately break from the turn if the player hits a Pig out or an Oinker!

Pass the Pigs Script Outline#Pass the Pigs import random

def pick_pigs():         pass

pick_pigs()

Pass the Pigs, Define our Dice#Pass the Pigs import random

def pick_pigs():   for x in range(1, 3): #two dice to roll dice_roll = random.randint(1, 6) #standard dice print(dice_rol1) #show us the roll break      

pick_pigs()

Pick_Pigs Function, (Deep Breath!)def pick_pigs():   for x in range(1, 3): #two dice to roll dice_roll = random.randint(1, 6) #standard dice print(dice_roll) #show us the roll if dice_roll == 1: print(‘Razorback, 5 pts! If you get doubles, 20 pts!’) elif dice_roll == 2: print(‘Trotter, 5 pts! If you get doubles, 20 pts!’) elif dice_roll == 3: print(‘Snouter, 10 pts! If you get doubles, 40 pts!’)   elif dice_roll == 4: print(‘Leaning Jowl, 15 pts! If you get doubles, 60 pts!’) elif dice_roll == 5: print(‘Pig Out, Back to Zero, Next Players turn unless you get doubles,

then only 1pt!’) break elif dice_roll == 6: print(‘Oinker, Back to ZERO, Next Players Turn!’) break

Finished Program#Pass the Pigs

import random

def pick_pigs():  

for x in range(1, 3): #two dice to roll

dice_roll = random.randint(1, 6) #standard dice

print(dice_roll)

if dice_roll == 1:

print(‘Razorback, 5 pts! If you get doubles, 20 pts!’)

elif dice_roll == 2:

print(‘Trotter, 5 pts! If you get doubles, 20 pts!’)

elif dice_roll == 3:

print(‘Snouter, 10 pts! If you get doubles, 40 pts!’)  

elif dice_roll == 4:

print(‘Leaning Jowl, 15 pts! If you get doubles, 60 pts!’)

elif dice_roll == 5:

print(‘Pig Out, Back to Zero, Next Players turn unless you get doubles, then only 1pt!

break

elif dice_roll == 6:

print(‘Oinker, Back to ZERO, Next Players Turn!’)

break

pick_pigs()

Execute our ScriptSave our script/code: <Esc> :wq! Or Save and Quit.Execute our script to see what we’ve coded!>sudo python3 pass_pigs.py2Trotter, 5 pts…1Razorback, 5 pts… (for total of 10 pts!)But if you roll a six in either roll?6Oinker, Back to Zero, Next Players Turn…

Next StepsConsider how this code could be used for

other games…Consider enhancements or using this code

in conjunction with Pygame, (there’s your Google research term… )

Think about how many ways the RANDOM module could be used for different functional scripts and smarthome modules, (turn on lights while on vacation at random times throughout day and evening, etc….)

Connect With Me…