投稿時間:2022-08-06 19:18:54 RSSフィード2022-08-06 19:00 分まとめ(21件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
js JavaScriptタグが付けられた新着投稿 - Qiita #twitter #twitterapi (バージョン1.1)で取得したデータの文字化け対処( #Javascript ) https://qiita.com/qiita21409102/items/ef3516ee7afd68bd5929 twitternodeclient 2022-08-06 18:02:19
AWS AWSタグが付けられた新着投稿 - Qiita AWS Systems Manager の Fleet Manager で EC2 インスタンスのパフォーマンスカウンターを見る https://qiita.com/emiki/items/dd3ea1991d3a618ed028 fleetmanage 2022-08-06 18:30:06
AWS AWSタグが付けられた新着投稿 - Qiita AWS: 簡単なLAMPサーバーでWordPressを動かすまで(Intel Mac) 〜EC2へSSH接続〜 https://qiita.com/holySh10/items/689f4c83cd18db128983 intelmacec 2022-08-06 18:04:31
Git Gitタグが付けられた新着投稿 - Qiita GitのコミットにGnuPGで署名する https://qiita.com/usamik26/items/6b816db27b7661611d59 author 2022-08-06 18:39:53
海外TECH DEV Community Creating flappy bird with pygame. Part - 1 https://dev.to/pulimoodan/creating-flappy-bird-with-pygame-part-1-11kd Creating flappy bird with pygame Part IntroductionWe had made a little of our flappy game in the previous post We could fly our bird and draw the ground bg etc Now we are continuing the development by creating pipes as obstacles and we need to create a game over event and everything to finish up the game So let s get started Read the first part if you haven t Part PipesLet s create the pipe which will generate randomly from the end of the screen and will scroll towards the bird We need to manage the bird not to hit the pipes So let s first create the Pipe class pipe classclass Pipe pygame sprite Sprite def init self x y position pass def update self passLike we created the bird class the pipe class has the init method which is the constructor and the update method We receive the x y and position to define whether the pipe is on the upside or at the ground We need to declare a pipe gap variable at the top of the code and set it to pipe gap Now let s complete the init method def init self x y position pygame sprite Sprite init self self image pygame image load img pipe png self image pygame transform scale self image self rect self image get rect position is from the top and is from bottom if position self image pygame transform flip self image False True self rect bottomleft x y int pipe gap elif position self rect topleft x y int pipe gap We load the image of the pipe first and then position it according to the position argument if position the pipe should be upside down like originating from the sky And if it s the pipe should be placed at the ground Make sure we consider the pipe gap when we set the y position of pipes Now let s jump into the update method def update self self rect x scroll speed if self rect right lt self kill Here we subtracted the scroll speed from the image rect s x position to move the pipe leftwards And if the pipe goes after the screen to the left side we would destroy it So we could save some space The pipe class is completed now we need to make a pipe group like we created the bird group one pipe group pygame sprite Group Jump into the loop and draw the pipe group to the screen make sure it s right above the code to draw the ground draw pipes pipe group draw screen Now we are drawing the pipe group to the screen but there is no pipe in the pipe group It s empty we need to add that To do that declare a variable at the top of the code named pipe frequency Set it to Also declare a last pipe variable pipe frequency last pipe pygame time get ticks pipe frequencyWe do this to generate the pipe after a defined frequency of frames each time Inside the loop go to the position where we set the code to update the ground scroll inside a condition checking game over False and flying True Right inside the condition add this time now pygame time get ticks if time now last pipe gt pipe frequency pipe height random randint btm pipe Pipe screen width int screen height pipe height top pipe Pipe screen width int screen height pipe height pipe group add btm pipe pipe group add top pipe last pipe time nowpipe group update We check if it s the time to generate new pipe according to the pipe frequency When we increase the pipe frequency the pipe would generate in more distance We generate a random height for the pipe and generate the bottom and top pipes Then add them to the pipe group Set the last pipe to time now to generate another pipe according to that In conclusion we need to generate two pipes one from the top and another one from the bottom after each pipe frequency times of frames So we have to check if time now last pipe s time is greater than the pipe frequency That s all At last we call the update function of pipe group outside the condition Try the game now and you can see the pipes generating from the right side as the bird is flying Collision and game overNow we don t have collision detection between the bird and the pipes We need to check this to determine if it s game over or not Here is the simple code to check the collision Paste it right inside the loop look for collision if pygame sprite groupcollide bird group pipe group False False or flappy rect top lt game over TruePygame already has a groupcollide method to check if two sprite groups collide We check the condition and set the game over to true We need to stop the game if the bird hits the ground too It s simple check if bird hit the groundif flappy rect bottom gt game over True flying FalseAdd this code to the loop where we check if the flappy sprite goes greater than where the ground is and set the game over true and flying false Play the game and you can see the bird falling down when we collide with the pipes This is because we had already coded the bird falling codes in it s class ScoreWe can make the score feature next Declare a variable at the top first Also a boolean variable named pass pipe score pass pipe FalseWe have to check if the bird passes each set of pipes and increase the variable by one Here s is the code to put inside the loop check the score if len pipe group gt if bird group sprites rect left gt pipe group sprites rect left and bird group sprites rect left lt pipe group sprites rect right and pass pipe False pass pipe True if pass pipe True if bird group sprites rect left gt pipe group sprites rect right score pass pipe FalseWe check the coordinates of pipe group and bird group and increase the score according to that Try printing the score in the console you can see it changes when the bird passes the pipe print score TextWe don t need the score to being printed on the console or terminal We need it inside the game inside the graphics Here we need text rendering Let s create a draw text function at the top of the code def draw text text font text col x y img font render text True text col screen blit img x y We get the text font text color and the x y postion into the function and render the font into an image Then draw it to the screen Now we need to call this function from inside the loop draw score draw text str score font white int screen width The score variable converted to sting is supplied as the text here The position need to be at the top centre So we divided the screen width by and set y to We call a font and white variable which needed to be declared before outside the loop define fontfont pygame font SysFont Bauhaus define colourswhite We use Bauhaus font to render text Run the game and you will see the score at the top RestartWe need to display a restart button if the game is over and the game is need to be restarted when we click that So declare a Button class with init and update method class Button def init self x y image self image image self rect self image get rect self rect topleft x y def draw self action False pos pygame mouse get pos if self rect collidepoint pos if pygame mouse get pressed action True screen blit self image self rect x self rect y return actionNeed to receive a image and x y as arguments and set the image according to the x y In the update method we return if the mouse is clicked on the button And also we draw the button to the screen Now initialise the button button Button screen width screen height button img Make sure to load the image into the button img variable at the top button img pygame image load img restart png Now draw the button inside the loop after checking if it s game over check for game over and resetif game over if button draw game over False score reset game Here we call the reset game function which return the initial score Declare this function at the top def reset game pipe group empty flappy rect x flappy rect y int screen height score return scoreTry the game intentionally hit the pipes and you will see a restart button at the centre of the screen Click it and the game will restart ConclusionHope you enjoyed developing the Flappy bird I will be back with another tutorial soon Drop your comments Here is the Github repo Flappy 2022-08-06 09:32:00
ニュース @日本経済新聞 電子版 一時帰国の記者がみた日本のコロナ対策のちぐはぐ https://t.co/h9gmtRwLCs https://twitter.com/nikkei/statuses/1555854642604519424 一時帰国 2022-08-06 09:53:47
ニュース @日本経済新聞 電子版 フジロック、「洋楽フェス」復活の熱い3日間 https://t.co/hMd8FrBw2o https://twitter.com/nikkei/statuses/1555846578208129025 熱い 2022-08-06 09:21:45
ニュース BBC News - Home Rishi Sunak: No hope of election win if inflation sticks https://www.bbc.co.uk/news/uk-politics-62440019?at_medium=RSS&at_campaign=KARANGA hustings 2022-08-06 09:31:26
北海道 北海道新聞 鈴木愛と上田が首位 女子ゴルフmeijiカップ第2日 https://www.hokkaido-np.co.jp/article/715037/ 北広島市 2022-08-06 18:39:00
北海道 北海道新聞 マルコス大統領、訪米検討 中国との引き合い加速 https://www.hokkaido-np.co.jp/article/715036/ 国務長官 2022-08-06 18:36:00
北海道 北海道新聞 <記録ファイル>軟式野球 第18回登別ロータリークラブ杯争奪登別少年大会 https://www.hokkaido-np.co.jp/article/715029/ 軟式野球 2022-08-06 18:15:00
北海道 北海道新聞 京都で「表現の不自由展」始まる 反対派抗議、会場周辺は厳重警戒 https://www.hokkaido-np.co.jp/article/715035/ 公共施設 2022-08-06 18:23:00
北海道 北海道新聞 千葉・船橋で大相撲巡業 26歳の貴景勝「一日を大切に」 https://www.hokkaido-np.co.jp/article/715012/ 千葉県船橋市 2022-08-06 18:02:15
北海道 北海道新聞 核の脅威、あらがい続ける 被爆者や若者、誓い新た https://www.hokkaido-np.co.jp/article/715034/ 核拡散防止条約 2022-08-06 18:22:00
北海道 北海道新聞 野外フェス、3年ぶり開催 「ロッキン」千葉移転し初 https://www.hokkaido-np.co.jp/article/715024/ 野外フェス 2022-08-06 18:22:15
北海道 北海道新聞 国連総長「先制不使用」に初言及 核戦争「断じて許容できない」 https://www.hokkaido-np.co.jp/article/715032/ 事務総長 2022-08-06 18:20:55
北海道 北海道新聞 花火玉落下し、高専出火か 筑後川大会、弓道場焼く https://www.hokkaido-np.co.jp/article/715033/ 久留米工業高等専門学校 2022-08-06 18:20:00
北海道 北海道新聞 サハリン1、年内の株式売却禁止 ロシア、日本の権益当面維持か https://www.hokkaido-np.co.jp/article/715031/ 天然ガス 2022-08-06 18:17:00
北海道 北海道新聞 ソ9―1楽(6日) 武田、今季初先発で白星 https://www.hokkaido-np.co.jp/article/715030/ 勝ち越し 2022-08-06 18:17:00
北海道 北海道新聞 7日の予告先発 日本ハムは根本 https://www.hokkaido-np.co.jp/article/715027/ 予告先発 2022-08-06 18:01:00
IT 週刊アスキー 発売前から期待大!井村屋「やわもちアイス 焦がしみたらし」に「食べたい」の声 https://weekly.ascii.jp/elem/000/004/100/4100995/ 限定 2022-08-06 18:45:00

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)