{"id":268626,"date":"2015-11-20T11:51:02","date_gmt":"2015-11-20T08:51:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=268626"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=268626","title":{"rendered":"\u0418\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b"},"content":{"rendered":"<p>       \u041e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e, \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0438\u0433\u0440\u044b \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435. \u0415\u0441\u043b\u0438 \u0438\u0433\u0440\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0451 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0435. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0451, \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 \u043d\u043e\u0432\u044b\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d. \u041d\u0430\u0448\u0435 \u0437\u0440\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0442\u0430\u043a, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0437\u0430 0,04 \u0441, \u0442\u043e \u043d\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u043c. \u041d\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0438, \u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u044b \u0438\u0433\u0440\u0430\u0435\u0442\u0435 \u2013 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u043e \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044e.<\/p>\n<p>  \u041c\u043e\u0436\u0435\u0442 \u0442\u0430\u043a \u0441\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u043e\u0434\u043d\u0438\u0445 \u043f\u043b\u0430\u043d\u0448\u0435\u0442\u0430\u0445 \u0438\u043b\u0438 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u0438\u043a\u0430\u0445 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u00ab\u043b\u0435\u0442\u0430\u0442\u044c\u00bb, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u0430\u0436\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u0430\u0442\u044c, \u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u2013 \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0441\u043a\u043e\u0440\u0435\u0439 \u0432\u0441\u0435\u0433\u043e, \u0443\u0434\u0430\u043b\u0438\u0442 \u0435\u0451 \u0441\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430. \u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043c\u044b\u0441\u043b\u044c, \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b \u0437\u0430 0,04 \u0441 (25 \u043a\u0430\u0434\u0440\u043e\u0432 (\u0446\u0438\u043a\u043b\u043e\u0432) \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443) \u043d\u0430 \u0432\u0441\u0435\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445. \u0412\u0441\u0451 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043c\u043e\u0433\u043b\u0438 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 10 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0438\u0433\u0440\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u044f \u043d\u043e\u0432\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0437\u0440\u044b\u0432 \u043f\u0440\u0438 \u0441\u0442\u043e\u043b\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u043d\u0430\u0434\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0437\u0432\u0443\u043a\u0438 \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0438\u0433\u0440\u0443. \u042f \u0443\u0436\u0435 \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u044e \u043e \u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u043e\u043a\u0440\u0443\u0436\u0430\u044e\u0449\u0435\u0433\u043e \u043c\u0438\u0440\u0430.<\/p>\n<p>  \u0427\u0442\u043e \u0436\u0435 \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0430\u0448\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442 \u0432 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u0446\u0435\u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b? \u0420\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u043e\u0434\u043d\u043e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b<\/h4>\n<p><a name=\"habracut\"><\/a><br \/>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u043a\u0430\u0434\u0440\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0440\u0430\u0441\u0447\u0435\u0442\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u0430\u0434\u0440\u043e\u0432 (\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442), \u043c\u044b \u0436\u0435\u0440\u0442\u0432\u0443\u0435\u043c \u044d\u0442\u0438\u043c \u043a\u0430\u0434\u0440\u043e\u043c \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u0430\u0434\u0440\u0430. \u0415\u0441\u043b\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0441\u0440\u0430\u0437\u0443 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0434\u0440, \u0442\u043e \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e, \u043d\u043e \u043f\u043e\u0442\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u043e\u043f\u0435\u0440\u0435\u0436\u0435\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u043e\u0432, \u0430 \u0442\u043e\u0447\u043d\u0435\u0435 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u0431\u044b\u043b\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c, \u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0435\u043b\u0430 \u043e\u0442 \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<p>  \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043d\u0438\u0437\u0438\u0442\u044c \u044d\u043d\u0435\u0440\u0433\u043e\u0437\u0430\u0442\u0440\u0430\u0442\u044b \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0438\u0433\u0440\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445. \u041d\u0430 \u0447\u0430\u0441\u0442\u043e\u0442\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f 25 \u043a\u0430\u0434\u0440\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u044f \u0437\u0430\u043c\u0435\u0447\u0430\u043b \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0435 \u043f\u043e\u0434\u0435\u0440\u0433\u0438\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0435 \u0441 \u0430\u043d\u0434\u0440\u043e\u0438\u0434 2.2.1 (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u043e), \u0430 \u0432\u043e\u0442 \u043f\u0440\u0438 30 \u043a\u0430\u0434\u0440\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u0432\u0441\u0451 \u0431\u044b\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e. \u041f\u0440\u0438 50 \u043a\u0430\u0434\u0440\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0430 \u043f\u043b\u0430\u043d\u0448\u0435\u0442\u0435 \u0441 \u0430\u043d\u0434\u0440\u043e\u0438\u0434 4.2.2 \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e, \u0430 \u0432\u043e\u0442 \u043d\u0430 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0435 \u0431\u044b\u043b\u0438 \u0438\u043d\u043e\u0433\u0434\u0430 \u0431\u044b\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u043d\u044b \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0438 \u043a\u0430\u0434\u0440\u043e\u0432, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043d\u0430\u0448 \u043a\u043e\u0434.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 MainThread, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 Thread. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0438\u0433\u0440\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 GameView.<\/p>\n<pre><code class=\"java\">public class MainThread extends Thread {  <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"java\">public class MainThread extends Thread { <\/code><\/pre>\n<p>  \u0417\u0430\u0434\u0430\u0435\u043c\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043a\u0430\u0434\u0440\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 (MAX_FPS) \u0440\u0430\u0432\u043d\u044b\u043c 30.<\/p>\n<pre><code class=\"java\"> private final static int MAX_FPS = 30; \/\/ desired fps <\/code><\/pre>\n<p>  \u041f\u0443\u0441\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u044b \u0433\u043e\u0442\u043e\u0432\u044b \u043f\u043e\u0436\u0435\u0440\u0442\u0432\u043e\u0432\u0430\u0442\u044c (\u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c) \u043f\u0440\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0440\u0430\u0432\u043d\u043e 4. \u042d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043c\u043d\u043e\u044e \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u043e, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438.<\/p>\n<pre><code class=\"java\">  \/\/ maximum number of frames to be skipped <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"java\"> private final static int MAX_FRAME_SKIPS = 4; <\/code><\/pre>\n<p>  \u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u0430\u0434\u0440\u0430.<\/p>\n<pre><code class=\"java\"> private final static int FRAME_PERIOD = 1000 \/ MAX_FPS;  \/\/ the frame period <\/code><\/pre>\n<p>  \u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u044c, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b.<\/p>\n<pre><code class=\"java\"> private SurfaceHolder surfaceHolder; \/\/ Surface holder that can access the physical surface <\/code><\/pre>\n<p>  \u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b.<\/p>\n<pre><code class=\"java\"> private GameView gameView;\/\/ The actual view that handles inputs and draws to the surface <\/code><\/pre>\n<p>  \u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438\u0433\u0440\u044b, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0440\u0430\u0432\u043d\u0430 true, \u0442\u043e \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"java\"> private boolean running;   \/\/ flag to hold game state    public void setRunning(boolean running) { this.running = running;   } <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<pre><code class=\"java\">public MainThread(SurfaceHolder surfaceHolder, GameView gameView) { super();  this.surfaceHolder = surfaceHolder; this.gameView = gameView;  }   @ Override  public void run() {  Canvas canvas;&lt;\/code&gt; long beginTime;  \/\/ the time when the cycle begun long timeDiff;  \/\/ the time it took for the cycle to execute int sleepTime;\/\/ ms to sleep (&lt;0 if we're behind) int framesSkipped;&lt;\/font&gt;\/\/ number of frames being skipped  sleepTime = 0; while (running) { canvas = null; \/\/ try locking the canvas for exclusive pixel editing in the surface  try { canvas = this.surfaceHolder.lockCanvas(); synchronized (surfaceHolder) {  beginTime = System.currentTimeMillis(); \/\/Returns the current time in milliseconds since January 1, 1970 00:00:00.0 UTC. framesSkipped = 0; \/\/ resetting the frames skipped  \/\/ update game state this.gameView.update(); \/\/ render state to the screen draws the canvas on the panel this.gameView.render(canvas); timeDiff = System.currentTimeMillis() - beginTime;&lt;\/font&gt; \/\/ calculate how long did the cycle take  \/\/ calculate sleep time sleepTime = (int)(FRAME_PERIOD - timeDiff);  if (sleepTime &gt; 0) {&lt;\/font&gt; \/\/ if sleepTime &gt; 0 we're OK try {&lt;\/font&gt;  \/\/ send the thread to sleep for a short period \/\/ very useful for battery saving  Thread.sleep(sleepTime); } catch (InterruptedException e) {} }   while (sleepTime &lt; 0 && framesSkipped &lt; MAX_FRAME_SKIPS) {  \/\/ we need to catch up this.gameView.update();&lt;\/font&gt;  \/\/ update without rendering sleepTime += FRAME_PERIOD; \/\/ add frame period to check if in next frame framesSkipped++;  }  }  } finally {  \/\/ in case of an exception the surface is not left in  \/\/ an inconsistent state if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } }  \/\/ end finally } } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 GameView.<\/p>\n<pre><code class=\"java\">public class GameView extends SurfaceView implements SurfaceHolder.Callback {  private final Drawable mAsteroid; private int widthAsteroid; private int heightAsteroid; private int leftAsteroid; private int xAsteroid1 = 30; private int rightAsteroid; private int topAsteroid; private int yAsteroid = -30; private int bottomAsteroid; private int centerAsteroid; private int height; private int width; private int speedAsteroid = 5; private int xAsteroid;  private MainThread thread; public GameView(Context context) { super(context);    \/\/ adding the callback (this) to the surface holder to intercept events getHolder().addCallback(this);  \/\/ create mAsteroid where adress of picture  asteroid is located mAsteroid = context.getResources().getDrawable(R.drawable.asteroid);  \/\/ create the game loop thread thread = new MainThread(getHolder(), this);  } <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u044d\u043a\u0440\u0430\u043d.<\/p>\n<pre><code class=\"java\">@Override public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {  } <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<pre><code class=\"java\">@Override public void surfaceCreated(SurfaceHolder holder) { thread.setRunning(true); thread.start();  } <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<pre><code class=\"java\">@Override public void surfaceDestroyed(SurfaceHolder holder) { thread.setRunning(false); boolean retry = true; while (retry) { try { thread.join(); retry = false;  } catch (InterruptedException e) { \/\/ try again shutting down the thread } } } <\/code><\/pre>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043c\u044b \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u0438 \u044d\u043a\u0440\u0430\u043d\u0430.<\/p>\n<pre><code class=\"java\">public void render(Canvas canvas) { <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0435\u043c\u043d\u043e-\u0433\u043e\u043b\u0443\u0431\u043e\u0439 \u0444\u043e\u043d.<\/p>\n<pre><code class=\"java\">canvas.drawColor(Color.argb(255, 2, 19, 151)); <\/code><\/pre>\n<p>  \u0423\u0437\u043d\u0430\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0443 \u0438 \u0448\u0438\u0440\u0438\u043d\u0443 \u044d\u043a\u0440\u0430\u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u044d\u043a\u0440\u0430\u043d\u0430.<\/p>\n<pre><code class=\"java\">height = canvas.getHeight(); width = canvas.getWidth(); <\/code><\/pre>\n<p>  \u0417\u0430\u0434\u0430\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434\u0430 (\u0448\u0438\u0440\u0438\u043d\u0443 \u0438 \u0432\u044b\u0441\u043e\u0442\u0443 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438). \u041b\u0435\u0432\u044b\u0439 \u043a\u0440\u0430\u0439 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u043a \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0435 \u043f\u043e \u0425, \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u043a \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0435 \u043f\u043e \u0423. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b, \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u043d\u0430\u0447\u043d\u0435\u0442 \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f. \u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043f\u0430\u043f\u043a\u0443 drawable \u0440\u0438\u0441\u0443\u043d\u043e\u043a \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 png.<\/p>\n<pre><code class=\"java\">\/\/Work with asteroid widthAsteroid = 2 * width \/ 13;\/\/set width asteroid heightAsteroid = widthAsteroid; leftAsteroid = xAsteroid; \/\/the left edge of asteroid rightAsteroid = leftAsteroid + widthAsteroid;\/\/set right edge of asteroid topAsteroid = yAsteroid; bottomAsteroid = topAsteroid + heightAsteroid; centerAsteroid = leftAsteroid + widthAsteroid \/ 2;  mAsteroid.setBounds(leftAsteroid, topAsteroid, rightAsteroid, bottomAsteroid); mAsteroid.draw(canvas);  } <\/code><\/pre>\n<p>  \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438, \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u0435\u0451 \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437. \u0415\u0441\u043b\u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430 \u043f\u043e \u0423 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0441\u0442\u0430\u043b\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0432\u044b\u0441\u043e\u0442\u0430 \u044d\u043a\u0440\u0430\u043d\u0430, \u0442\u043e \u043e\u0431\u043d\u0443\u043b\u044f\u0435\u043c \u0423 \u0438 \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434 \u0441\u043a\u0430\u0447\u043a\u043e\u043c \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0432\u0435\u0440\u0445\u0443 \u044d\u043a\u0440\u0430\u043d\u0430. \u0427\u0442\u043e\u0431\u044b \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434 \u043b\u0435\u0442\u0435\u043b \u0432\u043d\u0438\u0437 \u043c\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0435 \u0447\u0438\u0441\u043b\u043e speedAsteroid, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c\u0443 \u0437\u0430\u043a\u043e\u043d\u0443 \u043e\u0442 5 \u0434\u043e 15 (speedAsteroid = 5+ rnd.nextInt(10);).<\/p>\n<pre><code class=\"java\">public void update() { if (yAsteroid &gt; height) { yAsteroid = 0; \/\/ find by random function Asteroid & speed Asteroid Random rnd = new Random();  xAsteroid = rnd.nextInt(width - widthAsteroid); speedAsteroid = 5+ rnd.nextInt(10);  } else {  yAsteroid +=speedAsteroid; } } } <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434 \u043d\u0435 \u0432\u044b\u043b\u0435\u0442\u0430\u043b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u043c\u0435\u0441\u0442\u0430, \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0443 \u043f\u043e \u0425 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c\u0443 \u0437\u0430\u043a\u043e\u043d\u0443 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0448\u0438\u0440\u0438\u043d\u044b \u044d\u043a\u0440\u0430\u043d\u0430 \u043c\u0438\u043d\u0443\u0441 \u0448\u0438\u0440\u0438\u043d\u0430 \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434\u0430. \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434 \u0432\u044b\u043b\u0435\u0442\u0430\u0435\u0442 \u0441 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0437\u0430\u0434\u0430\u043b\u0438 \u043f\u0440\u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 private int xAsteroid = 30; private int yAsteroid = -30;. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0435\u0442\u0430 \u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430 \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c\u0443 \u0437\u0430\u043a\u043e\u043d\u0443.<\/p>\n<h4>\u0424\u0430\u0439\u043b\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442<\/h4>\n<p>  <i>AhdroidManifest.xml<\/i><\/p>\n<pre><code class=\"java\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; package=&quot;com.adc2017gmail.moonbase&quot; &gt;  &lt;application android:allowBackup=&quot;true&quot; android:icon=&quot;@mipmap\/ic_launcher&quot; android:label=&quot;@string\/app_name&quot; android:theme=&quot;@style\/AppTheme&quot; &gt; &lt;activity android:name=&quot;.MainActivity&quot;  android:label=&quot;@string\/app_name&quot; android:screenOrientation=&quot;portrait&quot; &gt; &lt;intent-filter&gt; &lt;action android:name=&quot;android.intent.action.MAIN&quot; \/&gt;  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; \/&gt; &lt;\/intent-filter&gt; &lt;\/activity&gt; &lt;activity android:name=&quot;.SecondActivity&quot; android:label=&quot;@string\/title_activity_second&quot; android:screenOrientation=&quot;portrait&quot; &gt; &lt;\/activity&gt; &lt;\/application&gt; &lt;\/manifest&gt; <\/code><\/pre>\n<p>  <i>GameView.java<\/i><\/p>\n<pre><code class=\"java\">package com.adc2017gmail.moonbase; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.util.Random;  public class GameView extends SurfaceView implements SurfaceHolder.Callback {  private final Drawable mAsteroid; private int widthAsteroid; private int heightAsteroid; private int leftAsteroid; private int rightAsteroid; private int topAsteroid; private int yAsteroid = -30; private int bottomAsteroid; private int centerAsteroid; private int height; private int width;  private int speedAsteroid = 5; private int xAsteroid = 30;  private MainThread thread;  public GameView(Context context) { super(context);  \/\/ adding the callback (this) to the surface holder to intercept events getHolder().addCallback(this);  \/\/ create mAsteroid where adress picture  asteroid mAsteroid = context.getResources().getDrawable(R.drawable.asteroid);  \/\/ create the game loop thread thread = new MainThread(getHolder(), this); }  @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { } @Override public void surfaceCreated(SurfaceHolder holder) {  thread.setRunning(true); thread.start(); }  @Override public void surfaceDestroyed(SurfaceHolder holder) {  thread.setRunning(false); boolean retry = true; while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { \/\/ try again shutting down the thread } } }  public void render(Canvas canvas) { canvas.drawColor(Color.argb(255, 2, 19, 151)); height = canvas.getHeight(); width = canvas.getWidth();  \/\/Work with asteroid widthAsteroid = 2 * width \/ 13;\/\/set width asteroid heightAsteroid = widthAsteroid; leftAsteroid = xAsteroid;\/\/the left edge of asteroid rightAsteroid = leftAsteroid + widthAsteroid;\/\/set right edge of asteroid  topAsteroid = yAsteroid; bottomAsteroid = topAsteroid + heightAsteroid; centerAsteroid = leftAsteroid + widthAsteroid \/ 2;  mAsteroid.setBounds(leftAsteroid, topAsteroid, rightAsteroid, bottomAsteroid); mAsteroid.draw(canvas); }  public void update() { if (yAsteroid &gt; height) { yAsteroid = 0;  \/\/ find by random function Asteroid & speed Asteroid Random rnd = new Random(); xAsteroid = rnd.nextInt(width - widthAsteroid); speedAsteroid = 5+ rnd.nextInt(10); } else { yAsteroid +=speedAsteroid; } } } <\/code><\/pre>\n<p>  <i>MainThread.java<\/i><\/p>\n<pre><code class=\"java\">package com.adc2017gmail.moonbase;  import android.graphics.Canvas; import android.view.SurfaceHolder;  public class MainThread extends Thread {  private final static int MAX_FPS = 30;\/\/ desired fps private final static int MAX_FRAME_SKIPS = 4;\/\/ maximum number of frames to be skipped private final static int FRAME_PERIOD = 1000 \/ MAX_FPS; \/\/ the frame period  \/\/ Surface holder that can access the physical surface  private SurfaceHolder surfaceHolder;  \/\/ The actual view that handles inputs \/\/ and draws to the surface private GameView gameView;  \/\/ flag to hold game state private boolean running;  public void setRunning(boolean running) { this.running = running; }  public MainThread(SurfaceHolder surfaceHolder, GameView gameView) { super(); this.surfaceHolder = surfaceHolder; this.gameView = gameView; }  @Override public void run() { Canvas canvas; long beginTime;\/\/ the time when the cycle begun long timeDiff; \/\/ the time it took for the cycle to execute  int sleepTime;\/\/ ms to sleep (&lt;0 if we're behind) int framesSkipped;\/\/ number of frames being skipped sleepTime = 0; while (running) { canvas = null; \/\/ try locking the canvas for exclusive pixel editing in the surface  try { canvas = this.surfaceHolder.lockCanvas(); synchronized (surfaceHolder) {  beginTime = System.currentTimeMillis();\/\/Returns the current time in milliseconds since January 1, 1970 00:00:00.0 UTC. framesSkipped = 0; \/\/ resetting the frames skipped  \/\/ update game state this.gameView.update(); \/\/ render state to the screen draws the canvas on the panel this.gameView.render(canvas); \/\/ calculate how long did the cycle take timeDiff = System.currentTimeMillis() - beginTime;  \/\/ calculate sleep time sleepTime = (int)(FRAME_PERIOD - timeDiff); if (sleepTime &gt; 0) { if sleepTime &gt; 0 \/\/we're OK try { \/\/ send the thread to sleep for a short period \/\/ very useful for battery saving Thread.sleep(sleepTime); } catch (InterruptedException e) {} }  while (sleepTime &lt; 0 && framesSkipped &lt; MAX_FRAME_SKIPS) { \/\/ we need to catch up this.gameView.update(); \/\/ update without rendering sleepTime += FRAME_PERIOD; \/\/ add frame period to check if in next frame framesSkipped++; } } } finally { \/\/ in case of an exception the surface is not left in \/\/ an inconsistent state if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas);  }  }  \/\/ end finally  }  } } <\/code><\/pre>\n<p>  <i>MainActivity.java<\/i><\/p>\n<pre><code class=\"java\">package com.adc2017gmail.moonbase;  import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.ImageButton;  public class MainActivity extends Activity {  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);  final ImageButton imgbtn8 = (ImageButton)findViewById(R.id.image_button8);  imgbtn8.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { imgbtn8.setImageResource(R.drawable.btn2); Intent intent8 = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent8); } }); }  @Override public boolean onOptionsItemSelected(MenuItem item) { \/\/ Handle action bar item clicks here. The action bar will \/\/ automatically handle clicks on the Home\/Up button, so long \/\/ as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); \/\/noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }  return super.onOptionsItemSelected(item);  }  } <\/code><\/pre>\n<p>  <i>SecondActivity.java<\/i><\/p>\n<pre><code class=\"java\">package com.adc2017gmail.moonbase;  import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;  public class SecondActivity extends Activity {  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GameView(this));  }  @Override public boolean onCreateOptionsMenu(Menu menu) { \/\/ Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_second, menu);  return true;  }  @Override public boolean onOptionsItemSelected(MenuItem item) { \/\/ Handle action bar item clicks here. The action bar will \/\/ automatically handle clicks on the Home\/Up button, so long \/\/ as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();   \/\/noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true;  }  return super.onOptionsItemSelected(item); } } <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/271315\/\"> http:\/\/habrahabr.ru\/post\/271315\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u041e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e, \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0438\u0433\u0440\u044b \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435. \u0415\u0441\u043b\u0438 \u0438\u0433\u0440\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0451 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0435. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0451, \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 \u043d\u043e\u0432\u044b\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d. \u041d\u0430\u0448\u0435 \u0437\u0440\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0442\u0430\u043a, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0437\u0430 0,04 \u0441, \u0442\u043e \u043d\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u043c. \u041d\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0440\u0438\u0441\u043e\u0432\u043a\u0438, \u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u044b \u0438\u0433\u0440\u0430\u0435\u0442\u0435 \u2013 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u043e \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044e.<\/p>\n<p>  \u041c\u043e\u0436\u0435\u0442 \u0442\u0430\u043a \u0441\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u043e\u0434\u043d\u0438\u0445 \u043f\u043b\u0430\u043d\u0448\u0435\u0442\u0430\u0445 \u0438\u043b\u0438 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u0438\u043a\u0430\u0445 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u00ab\u043b\u0435\u0442\u0430\u0442\u044c\u00bb, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u0430\u0436\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0442\u044c \u0438\u0433\u0440\u0430\u0442\u044c, \u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u2013 \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0441\u043a\u043e\u0440\u0435\u0439 \u0432\u0441\u0435\u0433\u043e, \u0443\u0434\u0430\u043b\u0438\u0442 \u0435\u0451 \u0441\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430. \u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043c\u044b\u0441\u043b\u044c, \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b \u0437\u0430 0,04 \u0441 (25 \u043a\u0430\u0434\u0440\u043e\u0432 (\u0446\u0438\u043a\u043b\u043e\u0432) \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443) \u043d\u0430 \u0432\u0441\u0435\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445. \u0412\u0441\u0451 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043c\u043e\u0433\u043b\u0438 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 10 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0438\u0433\u0440\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u044f \u043d\u043e\u0432\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0437\u0440\u044b\u0432 \u043f\u0440\u0438 \u0441\u0442\u043e\u043b\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u043d\u0430\u0434\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0437\u0432\u0443\u043a\u0438 \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0438\u0433\u0440\u0443. \u042f \u0443\u0436\u0435 \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u044e \u043e \u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u043e\u043a\u0440\u0443\u0436\u0430\u044e\u0449\u0435\u0433\u043e \u043c\u0438\u0440\u0430.<\/p>\n<p>  \u0427\u0442\u043e \u0436\u0435 \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0430\u0448\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442 \u0432 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u0446\u0435\u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b? \u0420\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u043e\u0434\u043d\u043e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438\u0433\u0440\u043e\u0432\u043e\u0439 \u0446\u0438\u043a\u043b<\/h4>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-268626","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/268626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=268626"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/268626\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=268626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=268626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=268626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}