Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Kellyn Pot’Vin-Gorman, Consulting Member of
Technical Staff, Oracle
Programming Simple Games with a
Raspberry Pi and Using...
Today’s Goal
Either 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 d...
Connect and Power Up Raspberry
Pi’s
If you are new to Raspberry Pi…
You should also have a micro or SD card for storage
...
Basics of Python
Writing a Python Script, (simple Python Coding) consists of
the following:
1. Import Modules- which are ...
Writing a Program/Script is Like
Outlining
What are your Requirements?
List them out, naming them as Functions and DEFIN...
A Python Script Outline
#Our modules will be put in as we go along at the top
def set_board():
pass
def start_motor():
pas...
Fill it in
import time
import gpio
def set_board():
pass
GPIO.setmode(GPIO.BOARD) #Use
Breadboard
Motor1A = 16 #Location o...
Coding Start and Forward of Motor
import time
import GPIO
def set_board():
GPIO.setmode(GPIO.BOARD)
Motor1A = 16
Motor1B =...
Final Steps Scripting
import time
import GPIO
def set_board():
GPIO.setmode(GPIO.BOARD)
Motor1A = 16
Motor1B = 18
def star...
Time to Code
Log into Your Raspberry Pi and open up a Terminal
Window.
Ensure that the screen background and text is vis...
To Create a Dice Game
Decide what your requirements are.
Create an outline of our simple script
Fill in the code for th...
The Dice Game Requirements
Import the RANDOM module.
Will “roll” the dice 2 times
Will use two dice, with random calls ...
Finished Script
import time
import GPIO
def set_board():
GPIO.setmode(GPIO.BOARD)
Motor1A = 16
Motor1B = 18
def start_moto...
Code Outline
#Dice Game
import random
def dice_role():
pass
dice_role()
Comments about what we are
coding
Import random mo...
Build out first steps of function
#Dice Game
import random
def dice_role():
for x in range(1, 2):
dice_1 = random.randint(...
Finished Script
#Dice Game
import random
def dice_roll():
for x in range(1, 2):
dice_1 = random.randint(1, 6)
dice_2 = ran...
Execute The Program/Script!
Save the script: <Esc> :wq! or save and exit, etc.
Execute the script to see the results of yo...
Pass the Pigs
From Dice to “Pigs”
We will want to show
the roll and display
the points that would
be gained by that roll...
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_...
Pick_Pigs Function, (Deep Breath!)
def pick_pigs():
for x in range(1, 3): #two dice to roll
dice_roll = random.randint(1, ...
Finished Program
#Pass the Pigs
import random
def pick_pigs():
for x in range(1, 3): #two dice to roll
dice_roll = random....
Execute our Script
Save our script/code: <Esc> :wq! Or Save and Quit.
Execute our script to see what we’ve coded!
>sudo py...
Next Steps
Consider how this code could be used for other games…
Consider enhancements or using this code in conjunction...
Connect With Me…
Upcoming SlideShare
Loading in …5
×

Programming simple games with a raspberry pi and

978 views

Published on

Devoxx4Kids and RMOUG Raspberry Pi Slides

Published in: Technology
  • Be the first to comment

Programming simple games with a raspberry pi and

  1. 1. Kellyn Pot’Vin-Gorman, Consulting Member of Technical Staff, Oracle Programming Simple Games with a Raspberry Pi and Using Python
  2. 2. Today’s Goal Either on a Raspberry Pi or Oracle VirtualBox, (Vbox) Raspberry Pi image, code two simulated games of chance…
  3. 3. 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=password Root password=password Open up a terminal once you’ve logged in.
  4. 4. Connect and Power Up Raspberry Pi’s If you are new to Raspberry Pi… You should also have a micro or SD card for storage A HDMI monitor A USB mouse and keyboard Already 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!
  5. 5. Basics of Python Writing 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.
  6. 6. Writing a Program/Script is Like Outlining What 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 execute Code LAST, filling in each section as you go, testing through each step.
  7. 7. 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()
  8. 8. Fill it in import time import gpio def set_board(): pass GPIO.setmode(GPIO.BOARD) #Use Breadboard Motor1A = 16 #Location of Motor pins Motor1B = 18 def start_motor(): pass
  9. 9. Coding Start and Forward of Motor import time import 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)
  10. 10. Final Steps Scripting import time import 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()
  11. 11. Time to Code Log 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
  12. 12. To Create a Dice Game Decide what your requirements are. Create an outline of our simple script Fill 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.
  13. 13. The Dice Game Requirements Import the RANDOM module. Will “roll” the dice 2 times Will use two dice, with random calls of 1-6. Will output the first dice ‘+’ the second dice.
  14. 14. Finished Script import time import 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()
  15. 15. Code Outline #Dice Game import random def dice_role(): pass dice_role() Comments about what we are coding Import 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
  16. 16. Build out first steps of function #Dice Game import 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.
  17. 17. Finished Script #Dice Game import 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!
  18. 18. 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
  19. 19. Pass the Pigs From 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!
  20. 20. Pass the Pigs Script Outline #Pass the Pigs import random def pick_pigs(): pass pick_pigs()
  21. 21. 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()
  22. 22. 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
  23. 23. 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()
  24. 24. Execute our Script Save our script/code: <Esc> :wq! Or Save and Quit. Execute our script to see what we’ve coded! >sudo python3 pass_pigs.py 2 Trotter, 5 pts… 1 Razorback, 5 pts… (for total of 10 pts!) But if you roll a six in either roll? 6 Oinker, Back to Zero, Next Players Turn…
  25. 25. Next Steps Consider 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….)
  26. 26. Connect With Me…

×