SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
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…

Más contenido relacionado

La actualidad más candente

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...Edureka!
 
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 1Abhishek Mishra
 
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 #14Artur Staniec
 
PenO 3 2014 sessie 2
PenO 3 2014 sessie 2PenO 3 2014 sessie 2
PenO 3 2014 sessie 2Sven Charleer
 
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)Chris Adamson
 
Flappy bird game in c#
Flappy bird game in c#Flappy bird game in c#
Flappy bird game in c#Comstas
 
Atari 2600 Programming for Fun
Atari 2600 Programming for FunAtari 2600 Programming for Fun
Atari 2600 Programming for FunPaul Dixon
 
Details on WannaCry malware virus operations
Details on WannaCry malware virus operationsDetails on WannaCry malware virus operations
Details on WannaCry malware virus operationsDavid Sweigert
 
Project report 393_395
Project report 393_395Project report 393_395
Project report 393_395VishruthKhare
 

La actualidad más candente (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
 

Similar a Programming simple games with a raspberry pi and

CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingSlide_N
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - pythonSunjid Hasan
 
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.pdfacteleshoppe
 
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.1Tom Paulus
 
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 PythonYothin Muangsommuk
 
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 2013Tom Paulus
 
Introduction to Raspberry Pi and GPIO
Introduction to Raspberry Pi and GPIOIntroduction to Raspberry Pi and GPIO
Introduction to Raspberry Pi and GPIOKris Findlay
 
Software to the slaughter
Software to the slaughterSoftware to the slaughter
Software to the slaughterQuinn Wilton
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchainedEduard Tomàs
 
Poker, packets, pipes and Python
Poker, packets, pipes and PythonPoker, packets, pipes and Python
Poker, packets, pipes and PythonRoger Barnes
 
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 2017Romain Dorgueil
 
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 DorgueilPôle Systematic Paris-Region
 
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 2013Tom Paulus
 
Arduino for Beginners
Arduino for BeginnersArduino for Beginners
Arduino for BeginnersSarwan Singh
 
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 interpreterakaptur
 

Similar 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 - 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
 
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
 
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
 

Más de Kellyn Pot'Vin-Gorman

Redgate_summit_atl_kgorman_intersection.pptx
Redgate_summit_atl_kgorman_intersection.pptxRedgate_summit_atl_kgorman_intersection.pptx
Redgate_summit_atl_kgorman_intersection.pptxKellyn Pot'Vin-Gorman
 
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptxSQLSatOregon_kgorman_keynote_NIAIMLEC.pptx
SQLSatOregon_kgorman_keynote_NIAIMLEC.pptxKellyn Pot'Vin-Gorman
 
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"Kellyn Pot'Vin-Gorman
 
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 BIKellyn Pot'Vin-Gorman
 
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 ProfessionalKellyn Pot'Vin-Gorman
 
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 TricksKellyn Pot'Vin-Gorman
 
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 CloudKellyn Pot'Vin-Gorman
 
ODTUG Leadership Talk- WIT and Sponsorship
ODTUG Leadership Talk-  WIT and SponsorshipODTUG Leadership Talk-  WIT and Sponsorship
ODTUG Leadership Talk- WIT and SponsorshipKellyn Pot'Vin-Gorman
 
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 DataKellyn Pot'Vin-Gorman
 

Más 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

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 

Último (20)

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 

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….)