##pygameで○×ゲームのようなものを作成中なのですが
pygameでボタン作成の記事を読んでRectオブジェクトを利用して、ボタンもどきを作成して、このRectオブジェクトを押したらRectオブジェクトの色が変わって同じ色が3つ揃ったら勝ちみたいにしたいのですがわからないことが2つありました
###1.Rectオブジェクトを押したらRectオブジェクトの色を変更するやり方
###2.3つ揃ったら勝ちみたいにするやり方
この二つのやり方を教えてください!
python
1# -*- coding:utf-8 -*- 2import pygame 3from pygame.locals import * 4import sys 5 6def main(): 7 pygame.init() # Pygameの初期化 8 screen = pygame.display.set_mode((400, 400)) # 400*300の画面 9 # screen = pygame.display.set_mode((400, 300), FULLSCREEN) # フルスクリーン(解除時は大きさ400*300の画面) 10 pygame.display.set_caption("○×ゲーム") # タイトルバーに表示する文字 11 12 while (1): 13 screen.fill((0,0,0)) # 画面を黒色(#000000)に塗りつぶし 14 15 button1 = pygame.Rect(0,0,40,40) 16 button2 = pygame.Rect(0,45,40,40) 17 button3 = pygame.Rect(0,90,40,40) 18 button4 = pygame.Rect(45,0,40,40) 19 button5 = pygame.Rect(45,45,40,40) 20 button6 = pygame.Rect(45,90,40,40) 21 button7 = pygame.Rect(90,0,40,40) 22 button8 = pygame.Rect(90,45,40,40) 23 button9 = pygame.Rect(90,90,40,40) 24 25 pygame.draw.rect(screen,(100,100,100),button1) 26 pygame.draw.rect(screen,(100,100,100),button2) 27 pygame.draw.rect(screen,(100,100,100),button3) 28 pygame.draw.rect(screen,(100,100,100),button4) 29 pygame.draw.rect(screen,(100,100,100),button5) 30 pygame.draw.rect(screen,(100,100,100),button6) 31 pygame.draw.rect(screen,(100,100,100),button7) 32 pygame.draw.rect(screen,(100,100,100),button8) 33 pygame.draw.rect(screen,(100,100,100),button9) 34 35 pygame.display.update() # 画面を更新 36 37 # イベント処理 38 for event in pygame.event.get(): 39 if event.type == QUIT: # 閉じるボタンが押されたら終了 40 pygame.quit() # Pygameの終了(画面閉じられる) 41 sys.exit() 42 for event in pygame.event.get(): 43 if event.type == pygame.MOUSEBUTTONDOWN: 44 if button1.collidepoint(event.pos): 45 46 47if __name__ == "__main__": 48 main()
回答1件
あなたの回答
tips
プレビュー