afmcdnl
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 4
Joined: Sat Jan 21, 2017 1:57 am
Location: Ottawa, Ontario
Contact: Twitter

Launch Scratch game from OS

Thu Nov 16, 2017 4:59 am

Hello,

I'm helping some students create a Pi arcade loaded with games that they created.
I am basing our code off of something I got at #picademy but the did the Scratch programming on Scratch 1 on the Pi.
My students are programming on their computers on the Scratch 2.0 website and we will download their games and put them on the Pi.

I was using this code as my model:

def start_invaders():
os.system("scratch presentation /home/pi/Desktop/RES/Games/Invaders.sb")

I know that the extension for Scratch 2.0 is .sb2 so changed that... and also found out that it should b:

def start_invaders():
os.system("scratch2 presentation /home/pi/Desktop/RES/Games/Invaders.sb2")

To launch Scratch 2.0 but the game doesn't get launched - has anyone else tried anything like this or know of any resources out there for something like this?

Many thanks in advance!!

ghp
Posts: 1155
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Launch Scratch game from OS

Sun Nov 19, 2017 3:59 pm

Hello,
did not find a simple way to automate scratch2.
What could help is an automated way to move the mouse pointer and click on the screen. This script should have some sort of 'record' capability to get the coordinates from the screen, and a 'play' mode to execute the actions. To keep the complexity low, just copy and paste the output of 'record' into the script.
Start the script with command line arguments 'record' or 'play'.

Code: Select all

# sudo pip3 install python3-Xlib
# sudo pip3 install pyautogui

import pyautogui
import time
import sys


if sys.argv[1] == 'record':

    print("move mouse pointer to [[maximize]]")
    time.sleep(5)
    maximize_x, maximize_y = pyautogui.position()

    print(maximize_x, maximize_y)

    print("click maximize")
    time.sleep(5)
    print("move mouse pointer to [[green flag]]")
    time.sleep(5)
    green_flag_x, green_flag_y = pyautogui.position()

    print(green_flag_x, green_flag_y)

    print("    ## copy following lines into the script")
    print("    maximize_x = {v:d}".format ( v=maximize_x) )
    print("    maximize_y = {v:d}".format ( v=maximize_y) )
    print("    green_flag_x = {v:d}".format ( v=green_flag_x) )
    print("    green_flag_y = {v:d}".format ( v=green_flag_y) )

if sys.argv[1] == 'play':
    ## copy the output of 'record' here
    maximize_x = 492
    maximize_y = 241
    green_flag_x = 1404
    green_flag_y = 217

    pyautogui.moveTo(maximize_x, maximize_y)
    pyautogui.click()
    time.sleep(3)

    pyautogui.moveTo(green_flag_x, green_flag_y)
    pyautogui.click()
Hope this helps.
Gerhard

pablito
Posts: 2
Joined: Wed Apr 27, 2016 9:59 am

Re: Launch Scratch game from OS

Sun Dec 03, 2017 9:14 pm

Is this to boot straight into a Scratch 2 project? If so, I am trying to do the exact same thing. unfortunately, I have not yet found anything that works as I would like.

I have found a way to boot straight into a Scratch 2 project but not an easy way out of it.

If you open terminal and:

sudo nano /etc/profile.d/boottoscratch.sh

and add the app and file name (I made sure mine was in the home folder /home/pi)

e.g. scratch2 game1.sb2

ctrl-o and ENTER to save and then ctrl-x to exit

sudo reboot

then it should boot in Scratch 2 (worked for me in Raspbian Stretch) but there does not seem to be any way to close Scratch or open anything else.

The only way out I have found is to SSH onto the Pi and edit the boottoscratch.sh in the SSH command window (comment out the boot file line) followed by a reboot.

Hope that helps, and if you find a better way, please let me know,
Pablo

ghp
Posts: 1155
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Launch Scratch game from OS

Mon Dec 04, 2017 6:26 pm

Hello,
the script I proposed extends the idea from afmcdnl which looks as if there is already some python script to start scratch2. The added functionality is to have some sort of 'mouse action robot' which presses some buttons in a started scratch2.
If you automatically start this combined python script when desktop has started and a user has logged in automatically, then you can manually stop scratch2 and you have full access to the machine.
Regards,
Gerhard

Return to “Scratch”

Who is online

Users browsing this forum: No registered users and 2 guests