質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

1320閲覧

プログラミング初心者です。Pythonについて知りたいです。

manki_kong

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/11/05 00:22

プログラミング初心者です。Pythonについて知りたいです。

落ちものパズルゲームを作っているのですがBGMを入れたい場合はどうすればいいのでしょうか?

該当のソースコード

Python

1import tkinter 2import random 3 4index = 0 5timer = 0 6score = 0 7hisc = 1000 8difficulty = 0 9tsugi = 0 10 11cursor_x = 0 12cursor_y = 0 13mouse_x = 0 14mouse_y = 0 15mouse_c = 0 16 17def mouse_move(e): 18 global mouse_x, mouse_y 19 mouse_x = e.x 20 mouse_y = e.y 21 22def mouse_press(e): 23 global mouse_c 24 mouse_c = 1 25 26sakana = [] 27check = [] 28for i in range(10): 29 sakana.append([0, 0, 0, 0, 0, 0, 0, 0]) 30 check.append([0, 0, 0, 0, 0, 0, 0, 0]) 31 32def draw_sakana(): 33 cvs.delete("sakana") 34 for y in range(10): 35 for x in range(8): 36 if sakana[y][x] > 0: 37 cvs.create_image(x*72+60, y*72+60, image=img_sakana[sakana[y][x]], tag="sakana") 38 39def check_sakana(): 40 for y in range(10): 41 for x in range(8): 42 check[y][x] = sakana[y][x] 43 44 for y in range(1, 9): 45 for x in range(8): 46 if check[y][x] > 0: 47 if check[y-1][x] == check[y][x] and check[y+1][x] == check[y][x]: 48 sakana[y-1][x] = 7 49 sakana[y][x] = 7 50 sakana[y+1][x] = 7 51 52 for y in range(10): 53 for x in range(1, 7): 54 if check[y][x] > 0: 55 if check[y][x-1] == check[y][x] and check[y][x+1] == check[y][x]: 56 sakana[y][x-1] = 7 57 sakana[y][x] = 7 58 sakana[y][x+1] = 7 59 60 for y in range(1, 9): 61 for x in range(1, 7): 62 if check[y][x] > 0: 63 if check[y-1][x-1] == check[y][x] and check[y+1][x+1] == check[y][x]: 64 sakana[y-1][x-1] = 7 65 sakana[y][x] = 7 66 sakana[y+1][x+1] = 7 67 if check[y+1][x-1] == check[y][x] and check[y-1][x+1] == check[y][x]: 68 sakana[y+1][x-1] = 7 69 sakana[y][x] = 7 70 sakana[y-1][x+1] = 7 71 72def sweep_sakana(): 73 num = 0 74 for y in range(10): 75 for x in range(8): 76 if sakana[y][x] == 7: 77 sakana[y][x] = 0 78 num = num + 1 79 return num 80 81def drop_sakana(): 82 flg = False 83 for y in range(8, -1, -1): 84 for x in range(8): 85 if sakana[y][x] != 0 and sakana[y+1][x] == 0: 86 sakana[y+1][x] = sakana[y][x] 87 sakana[y][x] = 0 88 flg = True 89 return flg 90 91def over_sakana(): 92 for x in range(8): 93 if sakana[0][x] > 0: 94 return True 95 return False 96 97def set_sakana(): 98 for x in range(8): 99 sakana[0][x] = random.randint(0, difficulty) 100 101def draw_txt(txt, x, y, siz, col, tg): 102 fnt = ("Times New Roman", siz, "bold") 103 cvs.create_text(x+2, y+2, text=txt, fill="black", font=fnt, tag=tg) 104 cvs.create_text(x, y, text=txt, fill=col, font=fnt, tag=tg) 105 106def game_main(): 107 global index, timer, score, hisc, difficulty, tsugi 108 global cursor_x, cursor_y, mouse_c 109 if index == 0: # タイトルロゴ 110 draw_txt("さかな君", 312, 240, 100, "royalblue", "TITLE") 111 cvs.create_rectangle(168, 384, 456, 456, fill="skyblue", width=0, tag="TITLE") 112 draw_txt("Easy", 312, 420, 40, "white", "TITLE") 113 cvs.create_rectangle(168, 528, 456, 600, fill="lightgreen", width=0, tag="TITLE") 114 draw_txt("Normal", 312, 564, 40, "white", "TITLE") 115 cvs.create_rectangle(168, 672, 456, 744, fill="orange", width=0, tag="TITLE") 116 draw_txt("Hard", 312, 708, 40, "white", "TITLE") 117 index = 1 118 mouse_c = 0 119 elif index == 1: # タイトル画面 スタート待ち 120 difficulty = 0 121 if mouse_c == 1: 122 if 168 < mouse_x and mouse_x < 456 and 384 < mouse_y and mouse_y < 456: 123 difficulty = 4 124 if 168 < mouse_x and mouse_x < 456 and 528 < mouse_y and mouse_y < 600: 125 difficulty = 5 126 if 168 < mouse_x and mouse_x < 456 and 672 < mouse_y and mouse_y < 744: 127 difficulty = 6 128 if difficulty > 0: 129 for y in range(10): 130 for x in range(8): 131 sakana[y][x] = 0 132 mouse_c = 0 133 score = 0 134 tsugi = 0 135 cursor_x = 0 136 cursor_y = 0 137 set_sakana() 138 draw_sakana() 139 cvs.delete("TITLE") 140 index = 2 141 elif index == 2: # 落下 142 if drop_sakana() == False: 143 index = 3 144 draw_sakana() 145 elif index == 3: # 揃ったか 146 check_sakana() 147 draw_sakana() 148 index = 4 149 elif index == 4: # 揃ったネコがあれば消す 150 sc = sweep_sakana() 151 score = score + sc*difficulty*2 152 if score > hisc: 153 hisc = score 154 if sc > 0: 155 index = 2 156 else: 157 if over_sakana() == False: 158 tsugi = random.randint(1, difficulty) 159 index = 5 160 else: 161 index = 6 162 timer = 0 163 draw_sakana() 164 elif index == 5: # マウス入力を待つ 165 if 24 <= mouse_x and mouse_x < 24+72*8 and 24 <= mouse_y and mouse_y < 24+72*10: 166 cursor_x = int((mouse_x-24)/72) 167 cursor_y = int((mouse_y-24)/72) 168 if mouse_c == 1: 169 mouse_c = 0 170 set_sakana() 171 sakana[cursor_y][cursor_x] = tsugi 172 tsugi = 0 173 index = 2 174 cvs.delete("CURSOR") 175 cvs.create_image(cursor_x*72+60, cursor_y*72+60, image=cursor, tag="CURSOR") 176 draw_sakana() 177 elif index == 6: # ゲームオーバー 178 timer = timer + 1 179 if timer == 1: 180 draw_txt("GAME OVER", 312, 348, 60, "red", "OVER") 181 if timer == 50: 182 cvs.delete("OVER") 183 index = 0 184 cvs.delete("INFO") 185 draw_txt("SCORE "+str(score), 160, 60, 32, "blue", "INFO") 186 draw_txt("HISC "+str(hisc), 450, 60, 32, "yellow", "INFO") 187 if tsugi > 0: 188 cvs.create_image(752, 128, image=img_sakana[tsugi], tag="INFO") 189 root.after(100, game_main) 190 191root = tkinter.Tk() 192root.title("落ち物パズル「さかな君」") 193root.resizable(False, False) 194root.bind("<Motion>", mouse_move) 195root.bind("<ButtonPress>", mouse_press) 196cvs = tkinter.Canvas(root, width=912, height=768) 197cvs.pack() 198 199bg = tkinter.PhotoImage(file="sakana_bg.png") 200cursor = tkinter.PhotoImage(file="sakana_cursor.png") 201img_sakana = [ 202 None, 203 tkinter.PhotoImage(file="sakana1.png"), 204 tkinter.PhotoImage(file="sakana2.png"), 205 tkinter.PhotoImage(file="sakana3.png"), 206 tkinter.PhotoImage(file="sakana4.png"), 207 tkinter.PhotoImage(file="sakana5.png"), 208 tkinter.PhotoImage(file="sakana6.png"), 209 tkinter.PhotoImage(file="sakana_niku.png") 210] 211 212cvs.create_image(456, 384, image=bg) 213game_main() 214root.mainloop() 215

試したこと

ネットで調べてみましたがうまいこといきませんでした。

最後に

もっと改良点などがあれば教えてください!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

1T2R3M4

2021/11/05 01:10

調べたこと(検索ワードと確認したサイト等)と うまくいかなかったコードを質問に追記していただけませんか。
guest

回答1

0

pygame.mixer.Sound.playが楽な気がします。無限ループは引数に-1を指定するだけで行けます。

投稿2021/11/05 22:34

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問