reflex_sheep_game.pl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 02 October 2015
  5. # Website: https://github.com/trizen
  6. # A simple program which plays the Reflex Sheep game by itself.
  7. # See: https://youtu.be/FrYFE4m8jc0
  8. use strict;
  9. use warnings;
  10. use GD;
  11. use Time::HiRes qw(sleep);
  12. my $count = 0;
  13. ROOT: while (1) {
  14. my $gd = GD::Image->new(scalar `maim -x 640 -y 150 -w 1 -h 850 --format=jpg /dev/stdout`);
  15. #my $gd = GD::Image->new(scalar `maim -x 555 -y 100 -w 10 -h 650 --format=jpg /dev/stdout`); # faster, but buggy
  16. my ($width, $height) = $gd->getBounds;
  17. OUTER: foreach my $y (0 .. $height - 1) {
  18. my $pixel = $gd->getPixel(0, $y);
  19. my ($r, $g, $b) = $gd->rgb($pixel);
  20. my $avg = ($r + $g + $b) / 3;
  21. if ($avg < 50) {
  22. sleep(0.085); # let the ship run a little bit more
  23. system("xdotool", "click", "1");
  24. sleep(1); # sleep a little bit after the click
  25. ++$count == 5 ? last ROOT: last OUTER;
  26. }
  27. }
  28. }