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

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

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

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

Q&A

解決済

2回答

815閲覧

コードの内容が理解できません。

technophobia07

総合スコア2

Python

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

0グッド

1クリップ

投稿2022/08/24 14:32

前提

python初心者です。
書籍を使ってゲームをつくっていますが、
以下のコードにおいて、flg=False, flg=True, return=flg の箇所がよく理解できません。
このコードでは最終的に「True」が戻り値となるのでしょうか。
そうであれば、 elif index == 2: から始まるコードにおいて、
なぜ if drop_neko() == False: なのでしょうか。ここは==Trueではないのでしょうか。
(実際に==Trueに書き換えるとゲームが動きませんでした。)
なぜ間違いなのかがわかりません。

python

1def drop_neko(): 2 flg = False 3 for y in range(8, -1, -1): 4 for x in range(8): 5 if neko[y][x] != 0 and neko[y+1][x] == 0: 6 neko[y+1][x] = neko[y][x] 7 neko[y][x] = 0 8 flg = True 9 return flg 10------------------------------------------ 11 elif index == 2: 12 if drop_neko() == False: 13 index = 3 14 draw_neko()

また、上記のコード(書籍に記載されていたコード)を以下のコードで書き換え
可能なのでは、と思いましたが、実際にはゲームは正しく動きませんでした。
いろいろ検索して調べたのですが、正しくない理由がわかりませんでした。
独学な故に基礎力不足かと思いますが、お教えください。

python

1def drop_neko(): 2 3 for y in range(8, -1, -1): 4 for x in range(8): 5 if neko[y][x] != 0 and neko[y+1][x] == 0: 6 neko[y+1][x] = neko[y][x] 7 neko[y][x] = 0 8 9------------------------------------------ 10 elif index == 2: 11 drop_neko() 12 index = 3 13 draw_neko()

ソースコード

import tkinter import random index = 0 timer = 0 score = 0 tsugi = 0 cursor_x = 0 cursor_y = 0 mouse_x = 0 mouse_y = 0 mouse_c = 0 def mouse_move(e): global mouse_x, mouse_y mouse_x = e.x mouse_y = e.y def mouse_press(e): global mouse_c mouse_c = 1 neko = [] check = [] for i in range(10): neko.append([0, 0, 0, 0, 0, 0, 0, 0]) check.append([0, 0, 0, 0, 0, 0, 0, 0]) def draw_neko(): cvs.delete("NEKO") for y in range(10): for x in range(8): if neko[y][x] > 0: cvs.create_image(x*72+60, y*72+60, image=img_neko[neko[y][x]], tag="NEKO") def check_neko(): for y in range(10): for x in range(8): check[y][x] = neko[y][x] for y in range(1, 9): for x in range(8): if check[y][x] > 0: if check[y-1][x] == check[y][x] and check[y+1][x] == check[y][x]: neko[y-1][x] = 7 neko[y][x] = 7 neko[y+1][x] = 7 for y in range(10): for x in range(1, 7): if check[y][x] > 0: if check[y][x-1] == check[y][x] and check[y][x+1] == check[y][x]: neko[y][x-1] = 7 neko[y][x] = 7 neko[y][x+1] = 7 for y in range(1, 9): for x in range(1, 7): if check[y][x] > 0: if check[y-1][x-1] == check[y][x] and check[y+1][x+1] == check[y][x]: neko[y-1][x-1] = 7 neko[y][x] = 7 neko[y+1][x+1] = 7 if check[y+1][x-1] == check[y][x] and check[y-1][x+1] == check[y][x]: neko[y+1][x-1] = 7 neko[y][x] = 7 neko[y-1][x+1] = 7 def sweep_neko(): num = 0 for y in range(10): for x in range(8): if neko[y][x] == 7: neko[y][x] = 0 num = num + 1 return num def drop_neko(): flg = False for y in range(8, -1, -1): for x in range(8): if neko[y][x] != 0 and neko[y+1][x] == 0: neko[y+1][x] = neko[y][x] neko[y][x] = 0 flg = True return flg def over_neko(): for x in range(8): if neko[0][x] > 0: return True return False def set_neko(): for x in range(8): neko[0][x] = random.randint(0, 6) def draw_txt(txt, x, y, siz, col, tg): fnt = ("Times New Roman", siz, "bold") cvs.create_text(x+2, y+2, text=txt, fill="black", font=fnt, tag=tg) cvs.create_text(x, y, text=txt, fill=col, font=fnt, tag=tg) def game_main(): global index, timer, score, tsugi global cursor_x, cursor_y, mouse_c if index == 0: # タイトルロゴ draw_txt("ねこねこ", 312, 240, 100, "violet", "TITLE") draw_txt("Click to start.", 312, 560, 50, "orange", "TITLE") index = 1 mouse_c = 0 elif index == 1: # タイトル画面 スタート待ち if mouse_c == 1: for y in range(10): for x in range(8): neko[y][x] = 0 mouse_c = 0 score = 0 tsugi = 0 cursor_x = 0 cursor_y = 0 set_neko() draw_neko() cvs.delete("TITLE") index = 2 elif index == 2: # 落下 if drop_neko() == False: index = 3 draw_neko() elif index == 3: # 揃ったか check_neko() draw_neko() index = 4 elif index == 4: # 揃ったネコがあれば消す sc = sweep_neko() score = score + sc*10 if sc > 0: index = 2 else: if over_neko() == False: tsugi = random.randint(1, 6) index = 5 else: index = 6 timer = 0 draw_neko() elif index == 5: # マウス入力を待つ if 24 <= mouse_x and mouse_x < 24+72*8 and 24 <= mouse_y and mouse_y < 24+72*10: cursor_x = int((mouse_x-24)/72) cursor_y = int((mouse_y-24)/72) if mouse_c == 1: mouse_c = 0 set_neko() neko[cursor_y][cursor_x] = tsugi tsugi = 0 index = 2 cvs.delete("CURSOR") cvs.create_image(cursor_x*72+60, cursor_y*72+60, image=cursor, tag="CURSOR") draw_neko() elif index == 6: # ゲームオーバー timer = timer + 1 if timer == 1: draw_txt("GAME OVER", 312, 348, 60, "red", "OVER") if timer == 50: cvs.delete("OVER") index = 0 cvs.delete("INFO") draw_txt("SCORE "+str(score), 160, 60, 32, "blue", "INFO") if tsugi > 0: cvs.create_image(752, 128, image=img_neko[tsugi], tag="INFO") root.after(100, game_main) root = tkinter.Tk() root.title("落ち物パズル「ねこねこ」") root.resizable(False, False) root.bind("<Motion>", mouse_move) root.bind("<ButtonPress>", mouse_press) cvs = tkinter.Canvas(root, width=912, height=768) cvs.pack() bg = tkinter.PhotoImage(file="neko_bg.png") cursor = tkinter.PhotoImage(file="neko_cursor.png") img_neko = [ None, tkinter.PhotoImage(file="neko1.png"), tkinter.PhotoImage(file="neko2.png"), tkinter.PhotoImage(file="neko3.png"), tkinter.PhotoImage(file="neko4.png"), tkinter.PhotoImage(file="neko5.png"), tkinter.PhotoImage(file="neko6.png"), tkinter.PhotoImage(file="neko_niku.png") ] cvs.create_image(456, 384, image=bg) game_main() root.mainloop() ここにより詳細な情報を記載してください。 def drop_neko(): flg = False for y in range(8, -1, -1): for x in range(8): if neko[y][x] != 0 and neko[y+1][x] == 0: neko[y+1][x] = neko[y][x] neko[y][x] = 0 flg = True return flg

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

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

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

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

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

jbpb0

2022/08/24 14:50 編集

> このコードでは最終的に「True」が戻り値となるのでしょうか。 if neko[y][x] != 0 and neko[y+1][x] == 0: が成立する場合としない場合のそれぞれで、 return flg の時の「flg」がどうなってるのか考えてみてください
javahack

2022/08/24 15:01

「理解できなくてもプログラムが動けば良い」のでなければ、基礎をしっかり勉強しましょう。 "="と"=="の違いやIf文による条件分岐も良く分かっていないように思えます。
TakaiY

2022/08/25 01:15

「このコードでは最終的に「True」が戻り値となる」と思ったのは何故ですか?
technophobia07

2022/08/25 01:48

ifの条件を常に満たす場合のことしか考えていませんでした。「条件を満たさない場合」もあることが抜けていました。
jbpb0

2022/08/25 22:11

解決したのなら、ベストアンサーを選んで「解決済」にしてください
guest

回答2

0

斜め読みですが。

このゲームは落ちもので、ゲームの最初でdrop_neko()が呼ばれます。この関数では、1. 下が空いていればnekoを落すようにデータを修正 2. nekoが1つでも落ちたらTrueを、1つも落ちなければFalseを返す という動作をします。
呼び出し側では、Trueが返った場合は、盤面のチェックをして揃っていれば消すなどの動作をします。 Falseの場合はそのままユーザの入力を待つ(のかな?)。

こんな感じでわかりますか?

投稿2022/08/25 01:57

TakaiY

総合スコア12747

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

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

0

ベストアンサー

プログラムの動きを確認していない状態で回答して申し訳ございません。

def drop_neko(): flg = False for y in range(8, -1, -1): for x in range(8): if neko[y][x] != 0 and neko[y+1][x] == 0: # ① neko[y+1][x] = neko[y][x] neko[y][x] = 0 flg = True return flg

drop_neko関数のうごきとしてはnekoという二次元配列をすべてチェックして①の条件を一度でも満たした場合Trueを返し、一度も①の条件を満たさない場合はFalseを返すという動きです。
よって常にTrueが戻ってくるわけではなく、nekoの内容によってはFalseが戻ってきます。

投稿2022/08/25 00:53

hide1198

総合スコア41

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

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

technophobia07

2022/08/25 02:13

回答ありがとうございます。「一度も①の条件を満たさない場合はFalseを返す」ことが頭から抜けておりました。 ご指摘いただいて気づき、もやもやが晴れました。
hide1198

2022/08/26 00:47

最初はよくあることだと思います。こらからコツコツと頑張っていってください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問