Discuss Scratch

Juhis90
Scratcher
76 posts

How to tell if cpu car won?

https://scratch.mit.edu/projects/510872115/

There may be a close situation where two cars cross the finish line at about the same time…
HolleBolleKnol
Scratcher
2 posts

How to tell if cpu car won?

Wow, this is some creative work!

I would checkout the possibility to keep the score (laps done) in an list for all player including the cpu player. And using collision detection on the CPU car and the checkpoints to increase these values.

then check regularly if any of the values in the list has reached 3 laps or so.
ZippingBobcaat49
Scratcher
38 posts

How to tell if cpu car won?

You could use a script like so:

Player:
when I receive [whatever starts the race]
wait until <not <touching [Finish Line] ?>>
wait until <touching [Finish Line] ?>
broadcast [Win]
when I receive [Lose]
stop [other scripts in sprite]
when I receive [Win]
stop [other scripts in sprite]
And for the CPU:
when I receive [whatever starts the race]
wait until <not <touching [Finish Line] ?>>
wait until <touching [Finish Line] ?>
broadcast [Lose]
when I receive [Lose]
stop [other scripts in sprite]
when I receive [Win]
stop [other scripts in sprite]
Or for laps:
when I receive [whatever starts the race]
repeat (Number of laps)
wait until <not <touching [Finish Line] ?>>
wait until <touching [Finish Line] ?>
end
broadcast [Win/Lose]
Be sure to have another sprite to broadcast the message!
And it can NOT be a when green flag clicked, unless you want the only way to retry is the green flag.

Last edited by ZippingBobcaat49 (Oct. 3, 2021 12:56:01)


Generation 5: the first time you see this copy and paste it on top of your sig in the scratch forums and increase generation by 1. Social experiment.

I use Chrome 101 on Windows 11 (Or Ubuntu 21.10, I'll get around to installing 22.04 LTS later).
Juhis90
Scratcher
76 posts

How to tell if cpu car won?

Okay, thanks!
orangetheory
Scratcher
500+ posts

How to tell if cpu car won?

Instead of using broadcasts, we can create two global variables to check if something is touching the finish line, and use the backdrop or an empty sprite to check if the var is higher than 0.

// in backdrop:
when green flag clicked
set [player 1 win v] to [0]
set [player 2 win v] to [0]

// in player 1 sprite:
forever
if <touching [finish line v]> then
set [player 1 win v] to [1]
end
end

// in player 2 sprite:
forever
if <touching [finish line v] ?> then
set [player 2 win v] to [1]
end
end

// in backdrop:
when green flag clicked
forever
if <(player 1 win) > [0]> then
broadcast [player 1 win v]
else
if <(player 2 win) > [0]> then
broadcast [player 2 win v]
end
end
This will detect the win easily, even if the cars are miliseconds apart from touching the finish. Using broadcasts is not the best way as broadcasts can be slow to detect.

Last edited by orangetheory (Oct. 3, 2021 16:43:00)


-Frisbee- -Cricket--Basketball--Forums--Mapping--Coding--Trigonometry master--Mathematics King--Future Prime Minister--Intro-Maker-
I know HTML/CSS, and JavaScript, and with these coding skills I have made some decent projects! Test them out below!
~Archery~Cops + Robbers~
Want an intro, outro, music, effects, or more? Check out the intro shop!

The forum helpers, a place where we help people in the forums

Powered by DjangoBB