Discuss Scratch

9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

Hello, I'm making a basketball scoreboard app in Python, but I want it to respond to my Scratch remote control's broadcast blocks. DO you know what code to enter into the program to make it respond to the remote control? If the receiver is idle, the code should be ignored, but if it receives one, it should do the following WITHOUT affecting normal operation:
1.
broadcast[start/stop clock]
should execute the start_stop() function.
2.
broadcast[reset clock]
should execute the reset() function.
3.
broadcast[home +n]
should execute the increase_home_score_by_n() function. n is a number between 1 and 3.
4.
broadcast[guest +n]
should execute the increase_guest_score_by_n() function. n is a number between 1 and 3.
5.
broadcast[home foul +1]
should execute the increase_home_fouls() function.
6.
broadcast[guest foul +1]
should execute the increase_guest_fouls() function.
7.
broadcast[advance period]
should execute the increase_period() function.
8.
broadcast[set poss to home]
should execute the change_poss_to_home() function.
9.
broadcast[set poss to guest]
should execute the change_poss_to_guest() function.
10.
broadcast[toggle home bonus]
should execute the toggle_home_bonus() function.
11.
broadcast[toggle guest bonus]
should execute the toggle_guest_bonus() function.
12.
broadcast[run shot clock]
should execute the shot_run_stop(1) function.
13.
broadcast[stop shot clock]
should execute the shot_run_stop(0) function.
14.
broadcast[shot clock reset 1]
should execute the reset_1() function.
15.
broadcast[shot clock reset 2]
should execute the reset_2() function.
16.
broadcast[home tol -]
should execute the remove_home_tol function.
17.
broadcast[guest tol -]
should execute the remove_guest_tol function.
The scoreboard should be able to respond to all 17 commands.
My scoreboard (receiver): https://github.com/s8007/BasketballScoreboard
My remote control (transmitter): https://scratch.mit.edu/projects/491787761/

Last edited by 9876546789 (Feb. 25, 2021 18:06:10)

ninjaMAR
Scratcher
1000+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

In the project make it so when you broadcast you set a cloud variable to the broadcast id. In the python script listen till the log changes and run a function according to the result

9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

It is not responding when I do it. Also, I can't use a while loop because it will not respond while the clock is running The clock uses a while loop to run until the clockstate variable is False or the time expires. Finally, I have more than 10 commands to send, and I can only have up to 10 cloud variables. When a while loop runs, other loops are disrupted.

Last edited by 9876546789 (Feb. 26, 2021 02:23:09)

SimpleScratch
Scratcher
500+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

Hi Cymplecy from Snap! forum

So, in the old days of Scratch 1.4, you can use this approach in Python to listen to Scratch broadcasts

https://github.com/pilliq/scratchpy

SimpleScratch
Scratcher
500+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

ninjaMAR wrote:

In the project make it so when you broadcast you set a cloud variable to the broadcast id. In the python script listen till the log changes and run a function according to the result

I'd never thought of using that technique - I'll have to give it a try out

SimpleScratch
Scratcher
500+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

@9876546789

What is the reason behind having both the python and the Scratch program ?
9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

The Python program is the receiver app (the scoreboard) and the Scratch program is the transmitter (remote).
9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

I can't use while loops because the clock uses a while loop as well. If I use a while loop, the scoreboard will stop listening while the clock is running.
SimpleScratch
Scratcher
500+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

9876546789 wrote:

I can't use while loops because the clock uses a while loop as well. If I use a while loop, the scoreboard will stop listening while the clock is running.

In general, you need to do some form of multi-tasking when you want to do two or more things at the same time

Threading is the most popular method in Python but it can take some learning and getting used to, in order to use it


Is this just a fun project or some sort of learning assignment/project?
9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

I am doing this project to help people around the world, so they don't need a $1000+ scoreboard.
Is there a way to keep listening without a while loop or make the clock run without a while loop?
SimpleScratch
Scratcher
500+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

9876546789 wrote:

I am doing this project to help people around the world, so they don't need a $1000+ scoreboard.
Is there a way to keep listening without a while loop or make the clock run without a while loop?


Unfortunately, there isn't a simple way of doing what you want

unlike Scratch or Snap!, python doesn't do many things at the same time without being explicitly programmed to do so


Is the idea that one person is pressing buttons on one machine/pad and it updates another screen?

Could you maybe use a laptop and arrange screens so that your control python dialogs are on laptop screen but your scoreboard is displayed on a separate monitor/projector connected to the laptop HDMI/VGA output?

ninjaMAR
Scratcher
1000+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

9876546789 wrote:

It is not responding when I do it. Also, I can't use a while loop because it will not respond while the clock is running The clock uses a while loop to run until the clockstate variable is False or the time expires. Finally, I have more than 10 commands to send, and I can only have up to 10 cloud variables. When a while loop runs, other loops are disrupted.
Have one cloud var and set it too different values.

9876546789 wrote:

I am doing this project to help people around the world, so they don't need a $1000+ scoreboard.
Is there a way to keep listening without a while loop or make the clock run without a while loop?
import time
def start_time():
    global start
    start = time.time()
def get_time():
   return start_time-time.time()

Firesoldier999
Scratcher
28 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

Is this a Snap! or Scratch program?
If it's a Snap! program you can set up a python HTTP server and make HTTP requests from Snap!.
If this is a Scratch program you could use cloud vars and have the python program respond to changes.

li t0, 0x10000000
li t1, 88
sb t0, t1
https://forum.snap.berkeley.edu/u/programmer_user/
ninjaMAR
Scratcher
1000+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

Firesoldier999 wrote:

Is this a Snap! or Scratch program?
If it's a Snap! program you can set up a python HTTP server and make HTTP requests from Snap!.
If this is a Scratch program you could use cloud vars and have the python program respond to changes.
It is scratch. Also I told him he should use cloud vars

9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

Yes, but not only that, I want to make it also be controlled by other people with their phone or tablet.
9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

I have to send more than 10 commands, so how can I have more than 10 cloud variables.
9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

@ninjaMAR, This is not what I want. There are three different variables: minutes, seconds, and tenths, for the time. Every 10th of a second the tenths variable should be decremented. If the tenths variable<0, the seconds should be decremented and the tenths should be set to 9. If the seconds variable<0, the minutes should be decremented and the seconds should be set to 59, as in my GitHub code. This process should repeat as long as the clockstate variable is True. If it's false, it should stop. When the time expires, it should stop and sound the buzzer. Is there a way to do this without a while loop?
ninjaMAR
Scratcher
1000+ posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

9876546789 wrote:

@ninjaMAR, This is not what I want. There are three different variables: minutes, seconds, and tenths, for the time. Every 10th of a second the tenths variable should be decremented. If the tenths variable<0, the seconds should be decremented and the tenths should be set to 9. If the seconds variable<0, the minutes should be decremented and the seconds should be set to 59, as in my GitHub code. This process should repeat as long as the clockstate variable is True. If it's false, it should stop. When the time expires, it should stop and sound the buzzer. Is there a way to do this without a while loop?
Base it off my code. Also can you quote posts by clicking the button next to report?

9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

The code that I don't want is this:
import time
def start_time():
global start
start = time.time()
def get_time():
return start_time-time.time()
This is for a clock that counts up. I want a countdown clock with tenths, minutes, and seconds with each unit as a separate variable.
9876546789
Scratcher
81 posts

Get a python application to connect to my project and respond to Scratch's broadcast blocks

ninjaMAR wrote:

9876546789 wrote:

It is not responding when I do it. Also, I can't use a while loop because it will not respond while the clock is running The clock uses a while loop to run until the clockstate variable is False or the time expires. Finally, I have more than 10 commands to send, and I can only have up to 10 cloud variables. When a while loop runs, other loops are disrupted.
Have one cloud var and set it too different values.

9876546789 wrote:

I am doing this project to help people around the world, so they don't need a $1000+ scoreboard.
Is there a way to keep listening without a while loop or make the clock run without a while loop?
import time
def start_time():
    global start
    start = time.time()
def get_time():
   return start_time-time.time()
This is a count-up timer code, and I want a countdown timer with tenths, as I said in the previous post.

Powered by DjangoBB