SlideShare uma empresa Scribd logo
1 de 26
Kellyn Pot’Vin-Gorman, Consulting Member of
Technical Staff, Oracle
Programming Simple Games with a
Raspberry Pi and Using Python
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 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.
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!
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.
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.
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 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
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)
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()
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
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.
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.
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()
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
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.
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!
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 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!
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 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…
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….)
Connect With Me…

Mais conteúdo relacionado

Mais procurados

Mais procurados (10)

PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...
PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...
PyGame Tutorial | PyGame Python Tutorial For Beginners | Python Certification...
 
Introduction to Game programming with PyGame Part 1
Introduction to Game programming with PyGame Part 1Introduction to Game programming with PyGame Part 1
Introduction to Game programming with PyGame Part 1
 
Hug presentation for android tech talks #14
Hug presentation for android tech talks #14Hug presentation for android tech talks #14
Hug presentation for android tech talks #14
 
xyz
xyzxyz
xyz
 
PenO 3 2014 sessie 2
PenO 3 2014 sessie 2PenO 3 2014 sessie 2
PenO 3 2014 sessie 2
 
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
 
Flappy bird game in c#
Flappy bird game in c#Flappy bird game in c#
Flappy bird game in c#
 
Atari 2600 Programming for Fun
Atari 2600 Programming for FunAtari 2600 Programming for Fun
Atari 2600 Programming for Fun
 
Details on WannaCry malware virus operations
Details on WannaCry malware virus operationsDetails on WannaCry malware virus operations
Details on WannaCry malware virus operations
 
Project report 393_395
Project report 393_395Project report 393_395
Project report 393_395
 

Semelhante a Programming simple games with a raspberry pi and

Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
acteleshoppe
 
Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013
Tom Paulus
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
akaptur
 

Semelhante a Programming simple games with a raspberry pi and (20)

CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. Programming
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
 
Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1
 
Codemania101: The Present, Past and Future of Asynchronous Programming in Python
Codemania101: The Present, Past and Future of Asynchronous Programming in PythonCodemania101: The Present, Past and Future of Asynchronous Programming in Python
Codemania101: The Present, Past and Future of Asynchronous Programming in Python
 
Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013
 
Pygame presentation
Pygame presentationPygame presentation
Pygame presentation
 
Scratch pcduino
Scratch pcduinoScratch pcduino
Scratch pcduino
 
Introduction to Raspberry Pi and GPIO
Introduction to Raspberry Pi and GPIOIntroduction to Raspberry Pi and GPIO
Introduction to Raspberry Pi and GPIO
 
Software to the slaughter
Software to the slaughterSoftware to the slaughter
Software to the slaughter
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
Poker, packets, pipes and Python
Poker, packets, pipes and PythonPoker, packets, pipes and Python
Poker, packets, pipes and Python
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
 
Getting Started with Raspberry Pi - USC 2013
Getting Started with Raspberry Pi - USC 2013Getting Started with Raspberry Pi - USC 2013
Getting Started with Raspberry Pi - USC 2013
 
Cc code cards
Cc code cardsCc code cards
Cc code cards
 
Arduino for Beginners
Arduino for BeginnersArduino for Beginners
Arduino for Beginners
 
Bytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreterBytes in the Machine: Inside the CPython interpreter
Bytes in the Machine: Inside the CPython interpreter
 
Python in details
Python in detailsPython in details
Python in details
 

Mais de Kellyn Pot'Vin-Gorman

Mais de Kellyn Pot'Vin-Gorman (20)

Redgate_summit_atl_kgorman_intersection.pptx
Redgate_summit_atl_kgorman_intersection.pptxRedgate_summit_atl_kgorman_intersection.pptx
Redgate_summit_atl_kgorman_intersection.pptx
 
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptxSQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
 
Boston_sql_kegorman_highIO.pptx
Boston_sql_kegorman_highIO.pptxBoston_sql_kegorman_highIO.pptx
Boston_sql_kegorman_highIO.pptx
 
Oracle on Azure IaaS 2023 Update
Oracle on Azure IaaS 2023 UpdateOracle on Azure IaaS 2023 Update
Oracle on Azure IaaS 2023 Update
 
IaaS for DBAs in Azure
IaaS for DBAs in AzureIaaS for DBAs in Azure
IaaS for DBAs in Azure
 
Being Successful with ADHD
Being Successful with ADHDBeing Successful with ADHD
Being Successful with ADHD
 
Azure DBA with IaaS
Azure DBA with IaaSAzure DBA with IaaS
Azure DBA with IaaS
 
Turning ADHD into "Awesome Dynamic Highly Dependable"
Turning ADHD into "Awesome Dynamic Highly Dependable"Turning ADHD into "Awesome Dynamic Highly Dependable"
Turning ADHD into "Awesome Dynamic Highly Dependable"
 
PASS Summit 2020
PASS Summit 2020PASS Summit 2020
PASS Summit 2020
 
DevOps in Silos
DevOps in SilosDevOps in Silos
DevOps in Silos
 
Azure Databases with IaaS
Azure Databases with IaaSAzure Databases with IaaS
Azure Databases with IaaS
 
How to Win When Migrating to Azure
How to Win When Migrating to AzureHow to Win When Migrating to Azure
How to Win When Migrating to Azure
 
Securing Power BI Data
Securing Power BI DataSecuring Power BI Data
Securing Power BI Data
 
Cepta The Future of Data with Power BI
Cepta The Future of Data with Power BICepta The Future of Data with Power BI
Cepta The Future of Data with Power BI
 
Pass Summit Linux Scripting for the Microsoft Professional
Pass Summit Linux Scripting for the Microsoft ProfessionalPass Summit Linux Scripting for the Microsoft Professional
Pass Summit Linux Scripting for the Microsoft Professional
 
Taming the shrew Power BI
Taming the shrew Power BITaming the shrew Power BI
Taming the shrew Power BI
 
PASS 24HOP Linux Scripting Tips and Tricks
PASS 24HOP Linux Scripting Tips and TricksPASS 24HOP Linux Scripting Tips and Tricks
PASS 24HOP Linux Scripting Tips and Tricks
 
Power BI with Essbase in the Oracle Cloud
Power BI with Essbase in the Oracle CloudPower BI with Essbase in the Oracle Cloud
Power BI with Essbase in the Oracle Cloud
 
ODTUG Leadership Talk- WIT and Sponsorship
ODTUG Leadership Talk-  WIT and SponsorshipODTUG Leadership Talk-  WIT and Sponsorship
ODTUG Leadership Talk- WIT and Sponsorship
 
DevOps and Decoys How to Build a Successful Microsoft DevOps Including the Data
DevOps and Decoys  How to Build a Successful Microsoft DevOps Including the DataDevOps and Decoys  How to Build a Successful Microsoft DevOps Including the Data
DevOps and Decoys How to Build a Successful Microsoft DevOps Including the Data
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Programming simple games with a raspberry pi and

  • 1. Kellyn Pot’Vin-Gorman, Consulting Member of Technical Staff, Oracle Programming Simple Games with a Raspberry Pi and Using Python
  • 2. Today’s Goal Either on a Raspberry Pi or Oracle VirtualBox, (Vbox) Raspberry Pi image, code two simulated games of chance…
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Pass the Pigs Script Outline #Pass the Pigs import random def pick_pigs(): pass pick_pigs()
  • 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. 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. 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. 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. 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….)