📜 ⬆️ ⬇️

Flappy Bot for Flappy Bird

image
Crazy game. What lad did not hear about Flappy Bird? About 50,000 dollars in revenue per day? The game is dedicated to financial reports, fun songs, yellow articles and research. The Chinese guys even invented a mechanical robot, chasing a bird .
Oh, and I installed the app on my favorite 2007 iPhone. At my age, you look at the download slider and think. But aren't you downloading the latest app in your life?
Yes. I chased the bird for about 20 minutes. I could not get past the 10th tower. Played another ten minutes. Oh, the 22 towers are my ceiling. Nerves to hell.
And we decided with a friend Cyril to create our own ro-bot. To score the Chinese tanks, as Ostap said.

Read how it was and see what happened.
Under the button three-kilobyte text and minute video.


Half franc coin


First of all, our robot is not mechanical. Pressing the screen can be simulated not by a mechanical stick, but by an electric charge. We stick a coin to the iPad screen. To the coin we put a blue tape with a wire. You feed a wire on a wire on the wire - the iPad is triggered by pressing. In this place you have to say the clever words of the capacitive screen and it is important to shake your head.
image
Of course, no photographer from me.
')
To control the current need an iPhone. He shoots a video game running on an iPad, recognizes the point in time when he has to tap the game screen and sends a signal to a coin. How? By audio wire. We generate a signal with a frequency of 20 kHz and a duration of 20 milliseconds. You can play a wav file, and it is better to simulate a signal programmatically, or more accurately. However, the delay from the beginning of the PI-and-and-and-and-and-and command until the simulated depression is 100-110 milliseconds. Remember this number.

Sample code? God, this is more boring than ORT news, today without a code, ok?

So the signal from the iPhone through the audio output runs to the capacitor, the capacitor is wired to our coin - voila, the circuit works.

image

Repeat our plan



According to the scheme, according to the specifications of semiconductors and other electro-technical knick-knacks, Kirill can answer. But he will not answer, modest. There are few knick-knacks on the board, sometimes they weakly beat them with an electric current if you want to touch them.
For those who fumble in electrical engineering
Of course, no this is not a capacitor.

image
When an audio signal with a level> = 1V pnp transistor opens and supplies a logical "1" to the input of the first inverter, causing a change in the polarity of the two-gate FET transistor.

Opening the FET connects a coin “in the air” to the ground, increasing the capacitance measured by the screen to the level it takes to be touched. The capacity of the gate-drain channel in the closed state is 5pF. The transistor is mounted directly on the coin to reduce the parasitic capacitance of the connecting wires.
The attempts to connect the coin to the transistor with shielded cable that preceded this decision were unsuccessful.

Not shown in the diagram: the board ground is connected to the tablet ground with an audio cable.


Xerox Box


The mini studio for the robot was made from used boxes. The box was split, piled on its side, and a game iPad with a pre-glued coin was carefully laid on the bottom. In the ceiling of the box, they made a hole for the iPhone video camera and put the phone on top, so that the camera could see the game screen of the underlying iPad. Uh-f, be patient, soon the end.

Under the iPad, I had to put the book Bakhvalov's Numerical Modeling so that the game screen completely filled the video frame.
Everything.

Recognition of the playing field, birds and pillars


Here it is simple.
To begin with, I made a screenshot of the game and in Photoshop I measured with a pipette the color intensity of the blue background, the red beak of the bird and the green, like a bottle of Absinthe, pillars.

Since the corners of the field are almost straight, catching the black frame is not difficult. Run the entire array of 1280 720 points of the video buffer is not necessary. I let the diagonals from the four corners to find the first point of the game screen. This is approximately 5,000 comparison operations for each angle. Penny.

Yes! I completely forgot.

What video mode to choose on our iPhone? requires a good frequency.

I tried Low-video mode. 192 by 144 pixels. Frequency - 20 frames per second. No good.
Standard video capture 480 by 360 points. Gives a frequency of 30 frames per second. Better, but not enough.
But the direct search of video modes finds 60 frames per second at a video resolution of 1280 by 720. The search method is taken from stackoverflow.com.

P-paradoxically it turns out - the higher the resolution, the more often the frames. What did you want from Steve Jobs? He also did not present such surprises.

For those who fumbles in iOS
@implementation ViewController static AVCaptureDevice *cam; static AVCaptureSession *sess; const float requiredFps = 60.0f; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. sess = [AVCaptureSession new]; cam = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *err = nil; AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:cam error:&err]; if (err) { NSLog(@"Failed to create AVCaptureDeviceInput: %@", err.description); } [sess addInput:input]; AVCaptureDeviceFormat *bestFmt = nil; int64_t bestDim = 0; CMTime tfps = CMTimeMake(10, 10 * requiredFps); for (AVCaptureDeviceFormat *fmt in cam.formats) { NSLog(@"Format: %@", fmt); for (AVFrameRateRange *fps in fmt.videoSupportedFrameRateRanges) { if (fps.maxFrameRate >= requiredFps) { if (CMFormatDescriptionGetMediaSubType(fmt.formatDescription) == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) { CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(fmt.formatDescription); int64_t sq = dimensions.height * dimensions.width; if (sq > bestDim) { bestDim = sq; bestFmt = fmt; } } } } } if (bestFmt) { if ( [cam lockForConfiguration:&err] ) { NSLog(@"Selected format: %@", bestFmt); cam.activeFormat = bestFmt; cam.activeVideoMaxFrameDuration = tfps; [cam unlockForConfiguration]; } else { NSLog(@"Failed to lock camera config: %@", err.description); } } else { NSLog(@"Failed to find a %f FPS format", requiredFps); } AVCaptureVideoDataOutput *output = [AVCaptureVideoDataOutput new]; [sess addOutput:output]; dispatch_queue_t queue; queue = dispatch_queue_create("cameraQueue", NULL); [output setSampleBufferDelegate:self queue:queue]; [output setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; [self.view bringSubviewToFront:cameraReal]; cameraReal.transform =CGAffineTransformMakeRotation(M_PI_2); // [sess startRunning]; turn on/off when view appear/disappear } - (void)viewWillDisappear:(BOOL)animated { [sess stopRunning]; [super viewWillDisappear:animated]; } - (void)viewWillAppear:(BOOL)animated { [sess startRunning]; [super viewWillAppear:animated]; } 



I finish. White crosses on the video marked the corners of the border of the playing field. I calculate every time, although it is not necessary. The bird is marked with a red cross, I am sure to calculate these parameters every measure. Knowing the time, knowing the coordinates of the bird is easy to calculate its speed. It is also easy to calculate the speed of the bird's jump after pressing and the acceleration of free fall. 2000 pixels per second per second, Vietnamese love round numbers))

Why know the speed? Due to the delay of the pressing command of 110 milliseconds. It is necessary to calculate the parabola of the movement of the bird to peep in advance.

The algorithm is not yet perfect, since it was created during today's Friday.
But our bird takes 100 pillars quite often. Twice in the evening I was able to, gold, and not a bird.

I think it is easy to bring this case to 1000 and calm down.

Enjoy all.



Attention advertising


Of course, I wrote my clone of this game. Nobody downloads the game, it is secret, but especially for you, dear readers, I give a link to the Winter Jumping app.
In honor of the birthday, it is free and without advertising.

thank


Thanks for attention.

Source: https://habr.com/ru/post/215913/


All Articles